How to decode a static value in Elm

Viewed 75

Using Json.decode is it possible to decode a static value, say The Json doesn't contain a value, but I want the local type alias to have more data that has a default. How can I do that? Thanks!

1 Answers

There are two ways to do this,

using Decode.succeeded

import Json.Decode as Decode

-- snip

Decode.map2 Item
    (Decode.field "title" Decode.string)
    (Decode.succeed 0)

(where Item is an arbitrary type alias)


Or using a closure

Decode.map (\name -> {name = name, age = 42} ) (Decode.field "name" Decode.string)
Related