如果您还没有使用泛型,这里有一种使用 TEXT.JSON 实例的方法
import Text.JSON
data SO = SO {
name :: String,
mytype :: Int,
code :: String,
options :: [Option]
} deriving (Show)
data Option =Option {
atb :: KV
}
data KV = KV {
desc :: String,
v:: Bool
}
instance JSON SO where
showJSON ge = makeObj
[ ("name", showJSON $ name ge),
("type", showJSON $ mytype ge),
("options", showJSON $ options ge)
]
readJSON = error "readJSON not implemented for SO"
instance JSON Option where
showJSON ge = makeObj
[ ("atb", showJSON $ atb ge)
]
readJSON = error "readJSON not implemented for Option"
instance JSON KV where
showJSON ge = makeObj
[ ("description", showJSON $ desc ge),
[ ("value", showJSON $ v ge)
]
readJSON = error "readJSON not implemented for kv"
--encode $ SO ......