aeson:为toJSON结果添加一个键

时间:2017-01-26 17:32:04

标签: haskell aeson

我在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)

0 个答案:

没有答案