I have a json source that is not well behaved. It too often provides unexpected JSON that contains array elements that are malformed. I would like to parse this JSON and ignore any malformed array elements.
Here is my attempt:
{-# 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)
When teststring
is malformed, the whole decoding of Foo
returns Nothing
. I would like Foo
to parse, but any malformed array elements of b
to be Nothing
.