eeue56/elm-json-field-value

Json decoder helpers for dealign with complex values


License
BSD-3-Clause
Install
elm-package install eeue56/elm-json-field-value 1.0.0

Documentation

elm-json-field-value

This package is intended to make it easier to work with complicated JSON decoders suitable for parsing ASTs from JSON in Elm.

Install


elm-package install --yes eeue56/elm-json-field-value

Usage

import Json.Decode
import Json.Decode.FieldValue exposing (fieldValue)

type Animal = Dog | Cat

decodeAnimal : Json.Decode.Decoder Animal
decodeAnimal = 
    Json.Decode.oneOf
        [ decodeDog
        , decodeCat
        ]

decodeDog : Json.Decode.Decoder Animal
decodeDog =
    Json.Decode.map (\_ -> Dog)
        (Json.Decode.field "type" <| fieldValue "dog")

decodeCat : Json.Decode.Decoder Animal
decodeCat =
    Json.Decode.map (\_ -> Cat)
        (Json.Decode.field "type" <| fieldValue "cat")