Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to decode union types in Elm
import Html exposing (text)
import Json.Decode exposing (..)
type Status
= Done
| Open
| Failed
type alias Stuff =
{ f: Status }
statusDecoder =
let
statusHelper s =
case s of
"Done" -> Done
"Open" -> Open
_ -> Failed
in
map statusHelper string
stuffDecoder =
map Stuff
(field "f" statusDecoder)
main =
"{\"f\": \"Done\"}"
|> decodeString stuffDecoder
|> toString
|> text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment