我有一个表现不佳的 json 源。它经常提供意外的 JSON,其中包含格式错误的数组元素。我想解析这个 JSON 并忽略任何格式错误的数组元素。
这是我的尝试:
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Applicative ((<$>), (<*>))
import Control.Monad (mzero)
import Data.Aeson
import Data.ByteString.Lazy.Char8 (ByteString, pack)
data Foo = Foo Integer [Maybe Bar] deriving (Show)
data Bar = Bar Integer Integer deriving (Show)
instance FromJSON Foo where
parseJSON (Object v) = Foo <$> (v .: "a") <*> (v .: "b")
parseJSON _ = mzero
instance FromJSON Bar where
parseJSON (Object v) = Bar <$> (v .: "c") <*> (v .: "d")
parseJSON _ = mzero
-- this string is malformed - second array element has an unexpected key
testString = pack "{\"a\":1, \"b\":[{\"c\":11, \"d\":12}, {\"C\":21, \"d\":22}, {\"c\":31, \"d\":32}]}"
-- want this to be:
--
-- Just (Foo 1 [Just (Bar 11 12),Nothing,Just (Bar 31 32)])
--
-- or
--
-- Just (Foo 1 [Just (Bar 11 12),Just (Bar 31 32)])
main = print $ (decode testString :: Maybe Foo)
Run Code Online (Sandbox Code Playgroud)
当teststring格式错误时,整个解码Foo返回Nothing。我想Foo解析bto be的任何格式错误的数组元素Nothing。
您不能依赖FromJSONfor 列表的默认实现,您必须自己做更多的工作:
instance FromJSON Foo where
parseJSON (Object v) = do
a <- v .: "a"
bList <- v .: "b"
return $ Foo a $ map (parseMaybe parseJSON) bList
Run Code Online (Sandbox Code Playgroud)
函数parseMaybe parseJSON有类型FromJSON a => Value -> Maybe a,所以bList :: [Value]有含义map (parseMaybe parseJSON) bList :: [Maybe Bar]。考虑到文档(请参阅“解码混合类型对象”)使用类似的方法来解析表现不佳的 JSON,这可能是最好的方法。