使用 Aeson 将嵌套 JSON 解析为元组列表
Parsing nested JSON into a list of Tuples with Aeson
假设我有以下结构:
data AddressDto = AddressDto
{ addressDtoId :: UUID
, addressDtoCode :: Text
, addressDtoCity :: Maybe Text
, addressDtoStreet :: Text
, addressDtoPostCode :: Text
} deriving (Eq, Show, Generic)
instance FromJSON AddressDto where
parseJSON = genericParseJSON $ apiOptions "addressDto"
instance ToJSON AddressDto where
toJSON = genericToJSON $ apiOptions "addressDto"
toEncoding = genericToEncoding $ apiOptions "addressDto"
正如您所期望的那样工作。
现在假设我要解析一个 JSON 格式的结构:
{ UUID: AddressDto, UUID: AddressDto, UUID: AddressDto }
一个合理的Haskell表示似乎是:
data AddressListDto = AddressListDto [(UUID, AddressDto)]
像这样创建辅助函数:
keyAndValueToList :: Either ServiceError AddressListDto -> Text -> DiscountDto -> Either ServiceError AddressListDto
keyAndValueToList (Left err) _ _ = Left err
keyAndValueToList (Right (AddressListDto ald)) k v = do
let maybeUUID = fromString $ toS k
case maybeUUID of
Nothing -> Left $ ParseError $ InvalidDiscountId k
Just validUUID -> Right $ AddressListDto $ (validUUID, v) : ald
最后是 FromJSON:
的实例
instance FromJSON AddressListDto where
parseJSON = withObject "tuple" $ \o -> do
let result = foldlWithKey' keyAndValueToList (Right $ AddressListDto []) o
case result of
Right res -> pure res
Left err -> throwError err
编译失败:
Couldn't match type ‘aeson-1.4.5.0:Data.Aeson.Types.Internal.Value’
with ‘AddressDto’
Expected type: unordered-containers-0.2.10.0:Data.HashMap.Base.HashMap
Text AddressDto
两个问题:
1) 如何确保哈希图中的嵌套值被正确解析为 AddressDto。
2)我如何避免将初始值强制为任意一个?有没有我可以使用而不是 foldlWithKey'
的函数,它不会让我像这样包装初始值?
有趣的回答。要实现 parseJSON
,您可以使用
parseJSON :: Value -> Parser (HashMap Text AddressDto)
...但更好的是,为什么不使用类型别名而不是新的数据类型,所以:
type AddressListDto = HashMap UUID AddressDto
这里已经有一个合适的FromJSON
实例,所以你甚至不需要自己写任何代码。
假设我有以下结构:
data AddressDto = AddressDto
{ addressDtoId :: UUID
, addressDtoCode :: Text
, addressDtoCity :: Maybe Text
, addressDtoStreet :: Text
, addressDtoPostCode :: Text
} deriving (Eq, Show, Generic)
instance FromJSON AddressDto where
parseJSON = genericParseJSON $ apiOptions "addressDto"
instance ToJSON AddressDto where
toJSON = genericToJSON $ apiOptions "addressDto"
toEncoding = genericToEncoding $ apiOptions "addressDto"
正如您所期望的那样工作。
现在假设我要解析一个 JSON 格式的结构:
{ UUID: AddressDto, UUID: AddressDto, UUID: AddressDto }
一个合理的Haskell表示似乎是:
data AddressListDto = AddressListDto [(UUID, AddressDto)]
像这样创建辅助函数:
keyAndValueToList :: Either ServiceError AddressListDto -> Text -> DiscountDto -> Either ServiceError AddressListDto
keyAndValueToList (Left err) _ _ = Left err
keyAndValueToList (Right (AddressListDto ald)) k v = do
let maybeUUID = fromString $ toS k
case maybeUUID of
Nothing -> Left $ ParseError $ InvalidDiscountId k
Just validUUID -> Right $ AddressListDto $ (validUUID, v) : ald
最后是 FromJSON:
的实例instance FromJSON AddressListDto where
parseJSON = withObject "tuple" $ \o -> do
let result = foldlWithKey' keyAndValueToList (Right $ AddressListDto []) o
case result of
Right res -> pure res
Left err -> throwError err
编译失败:
Couldn't match type ‘aeson-1.4.5.0:Data.Aeson.Types.Internal.Value’
with ‘AddressDto’
Expected type: unordered-containers-0.2.10.0:Data.HashMap.Base.HashMap
Text AddressDto
两个问题:
1) 如何确保哈希图中的嵌套值被正确解析为 AddressDto。
2)我如何避免将初始值强制为任意一个?有没有我可以使用而不是 foldlWithKey'
的函数,它不会让我像这样包装初始值?
有趣的回答。要实现 parseJSON
,您可以使用
parseJSON :: Value -> Parser (HashMap Text AddressDto)
...但更好的是,为什么不使用类型别名而不是新的数据类型,所以:
type AddressListDto = HashMap UUID AddressDto
这里已经有一个合适的FromJSON
实例,所以你甚至不需要自己写任何代码。