Created
December 6, 2014 15:50
-
-
Save Warry/274addc9862c436b0605 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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