Created
December 5, 2016 00:26
-
-
Save focusaurus/1085181366a6399414f0b0049ece3750 to your computer and use it in GitHub Desktop.
Decode JSON string enum into elm union type
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
module A exposing (..) | |
import Json.Decode as JD | |
import Json.Encode as JE | |
type alias User = | |
{ id : Int | |
, theme : Theme | |
} | |
type Theme | |
= Light | |
| Dark | |
userDecoder = | |
(JD.map2 User | |
( JD.field "id" JD.int ) | |
( JD.field "theme" decodeTheme ) | |
) | |
-- Can't figure out how to make a custom Json.Decoder.Decoder a | |
decodeTheme toDecode = | |
let | |
valueDe = | |
JD.decodeString JD.string toDecode | |
in | |
case valueDe of | |
Ok name -> | |
if (String.toLower name) == "light" then | |
Light | |
else | |
Dark | |
Err msg -> | |
Dark |
How to do this if custom type contains values that should also be decoded from JSON? If, for example, Dark contains Int-value telling how dark it is.
Can anyone answer LimmaPaulus' question? How would themeDecoder look if Dark had a payload?
type Theme
= Light
| Dark Int
I haven't tried this myself, but this is what I imagine would work
So your JSON might look like
{
"theme": "Light"
}
or
{
"theme": ["Dark", 2]
}
So your decoder will need to look like this
type Theme
= Light
| Dark Int
type Intermediate =
Intermediate String (Maybe Int)
decoder : Decoder Theme
decoder =
Decode.map2 Intermediate (index 0 Decode.string) (index 1 Decode.maybe <| Decode.int)
|> Decode.andThen fromIntermediate
fromIntermediate : Intermediate -> Decoder Theme
fromIntermediate (Intermediate string maybeInt) =
case (string, maybeInt) of
("light", Nothing) ->
Decode.succeed Light
("dark", Just int) ->
Decode.succeed <| Dark int
other ->
Decode.fail "Invalid json"
Great example!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Maybe call it
unknownTheme