Created
December 2, 2016 10:45
-
-
Save alg/9b47b40ecc7f7e06cac252bf764db4d2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
decodeModel : Json.Decode.Value -> Result String Model | |
decodeModel modelJson = | |
Json.Decode.decodeValue modelDecoder modelJson | |
modelDecoder : Json.Decode.Decoder Model | |
modelDecoder = | |
Json.Decode.map4 Model | |
(field "todos" (Json.Decode.list todoDecoder)) | |
(field "todo" todoDecoder) | |
(field "filter" filterStateDecoder) | |
(field "nextIdentifier" int) | |
todoDecoder : Json.Decode.Decoder Todo | |
todoDecoder = | |
Json.Decode.map4 Todo | |
(field "title" Json.Decode.string) | |
(field "completed" Json.Decode.bool) | |
(field "editing" Json.Decode.bool) | |
(field "identifier" Json.Decode.int) | |
filterStateDecoder : Json.Decode.Decoder FilterState | |
filterStateDecoder = | |
let | |
decodeFilterState string = | |
case string of | |
"All" -> Result.Ok All | |
"Active" -> Result.Ok Active | |
"Completed" -> Result.Ok Completed | |
_ -> Result.Err ("Not a valid filterState: " ++ string) | |
in | |
customDecoder Json.Decode.string decodeFilterState | |
customDecoder decoder toResult = | |
Json.Decode.andThen | |
(\a -> | |
case toResult a of | |
Ok b -> Json.Decode.succeed b | |
Err err -> Json.Decode.fail err | |
) | |
decoder | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can rewrite the last part with