How to decode recursive JSON arrays in Haskell? -
for such piece of json:
[ [ "a", "b", [ "c" ] ] ]
how decode in haskell?
and recursive array in json:
data cvalue = clist [cvalue] | cstring string
i've read demos on parsing records json.text
, aeson
, code not work on recursive data types.
you can little this
{-# language overloadedstrings #-} import data.aeson import data.traversable (traverse) import data.foldable (tolist) import control.applicative data cvalue = clist [cvalue] | cstring text deriving show instance fromjson cvalue parsejson v = withtext "cstring" (pure . cstring) v <|> witharray "clist" (\a -> clist . tolist <$> traverse parsejson a) v
with instance set can use decode :: fromjson => data.bytestring.lazy.internal.bytestring -> maybe a
> decode "[[\"a\",\"b\",[\"c\"]]]" :: maybe cvalue (clist [clist [cstring "a",cstring "b",clist [cstring "c"]]])
Comments
Post a Comment