Skip to content

Instantly share code, notes, and snippets.

@Warry
Created December 6, 2014 15:50
Show Gist options
  • Save Warry/274addc9862c436b0605 to your computer and use it in GitHub Desktop.
Save Warry/274addc9862c436b0605 to your computer and use it in GitHub Desktop.
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