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

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -