【发布时间】:2019-10-17 23:46:11
【问题描述】:
我正在用 Haskell 编写一个游戏,我想将抽象数据类型的列表保存到 JSON 格式的文本文件中,然后重新加载该保存的文件。即将该文件读回抽象数据类型列表,然后在我的游戏中正常使用该重新加载的列表。我对 Haskell 和一般编程很陌生,所以我有点不确定这是否可能首先。
我相信我已经正确设置了我的数据类型(房间)。但我不确定 FromJson 和 toJSON (语法正确吗?)。
另外,我目前保存的方式并没有以我想要的格式保存 JSON 文件:
[{"reward":"treasure","enemy":"enemyOne","description":"这是一个房间"}]
而描述、敌人和奖励应该是顺序。
(我也相信数据类型 Room 应该在 JSON 文件中的所有内容之前正确吗?)。
如果您需要更多说明,请告诉我。谢谢!
{-# LANGUAGE
OverloadedStrings
, DeriveGeneric
#-}
module Game where
--import System.IO
import Text.Read
import Data.Char
import Prelude hiding (readFile, writeFile)
import Data.Aeson
import Data.Text as T hiding (length, tail)
import Data.ByteString.Lazy as B hiding (putStrLn, length, tail, writeFile, readFile)
import Data.ByteString.Lazy.Char8 as BC hiding (putStrLn, length, tail)
import GHC.Generics
data Room = Room
{ description :: String
, enemy :: String
, reward :: String
} deriving (Show, Generic)
-- is this syntax correct for parseJSON?
instance FromJSON Room where
parseJSON (Object v) = Room <$> v .: "description" <*> v .: "enemy" <*> v .: "reward"
-- is this syntax correct for toJSON?
instance ToJSON Room where
toJSON (Room desc enem reward) = object ["description" .= desc, "enemy" .= enem, "reward" .= reward]
save lst =
do
writeFile "savegame.txt" (encode lst)
return()
load =
do
lst <- readFile "savegame.txt"
let new = decode lst
start new
start :: [Room] -> IO()
start lst =
putStrLn("Starting the game, need to use the lst as a list of rooms")
这些是我收到的错误消息:
forStackOverflow.hs:46:14:
No instance for (FromJSON a0) arising from a use of ‘decode’
The type variable ‘a0’ is ambiguous
Relevant bindings include
new :: Maybe a0 (bound at forStackOverflow.hs:46:8)
Note: there are several potential instances:
instance FromJSON DotNetTime
-- Defined in ‘aeson-1.4.5.0:Data.Aeson.Types.FromJSON’
instance FromJSON Value
-- Defined in ‘aeson-1.4.5.0:Data.Aeson.Types.FromJSON’
instance FromJSON a => FromJSON (Control.Applicative.Const a b)
-- Defined in ‘aeson-1.4.5.0:Data.Aeson.Types.FromJSON’
...plus 89 others
In the expression: decode lst
In an equation for ‘new’: new = decode lst
In the expression:
do { lst <- readFile "savegame.txt";
let new = decode lst;
start new }
forStackOverflow.hs:47:10:
Couldn't match expected type ‘[Room]’ with actual type ‘Maybe a0’
Relevant bindings include
new :: Maybe a0 (bound at forStackOverflow.hs:46:8)
In the first argument of ‘start’, namely ‘new’
In a stmt of a 'do' block: start new
Failed, modules loaded: none.
Prelude>
【问题讨论】:
标签: file haskell save load aeson