Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
module Test where
import Text (asText)
import Array (Array, get)
import Json.Decode (..)
import Maybe (..)
type alias Foo =
{ foo : String
, bar : Bar
}
type alias Bar =
{ titi : String
, toto : Maybe String
, tutu : Int
}
fooTest : String
fooTest =
"""
[
{
"foo": "abcd",
"bar": {
"titi": "efgh",
"tutu": 42
}
},
{
"foo": "ijkl",
"bar": {
"titi": "mnop",
"toto": "qrst",
"tutu": 123
}
}
]
"""
decodeFoo : Decoder Foo
decodeFoo = object2 Foo
("foo" := string)
("bar" := decodeBar)
decodeBar : Decoder Bar
decodeBar = object3 Bar
("titi" := string)
(maybe ("toto" := string))
("tutu" := int)
foos : Decoder (Array Foo)
foos = array decodeFoo
main =
let
r : Result String (Array Foo)
r = decodeString foos fooTest
in
case r of
Ok v -> asText (get 0 v)
Err e -> asText e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.