我在artyom.me/aeson找到了我的问题的以下解决方案。但是,为toJSON
结果添加单个键的任务看起来有点臃肿。难道不存在更容易的解决方案吗?
instance ToJSON RussianName where
toJSON RussianName{..} = Object $
toObject russianName <>
fromList ["patronymic" .= russianPatronymic]
toObject :: ToJSON a => a -> Object
toObject a = case toJSON a of
Object o -> o
_ -> error "toObject: value isn't an Object"
至少还需要
import Data.Aeson
import Data.Monoid ((<>))
import GHC.Exts (fromList)