Add a field to a record decoder.
import Json.Decode
import Json.Decode.Safe exposing (record, field)
userDecoder =
record userConstructor userConstructor
|> field "firstName" .firstName Json.Decode.string
|> field "lastName" .lastName Json.Decode.string
|> field "pets" .pets Json.Decode.int
|> ...Finalise the definition of a record decoder.
import Json.Decode
import Json.Decode.Safe exposing (endRecord, field, record)
userDecoder =
record userConstructor userConstructor
|> field "firstName" .firstName Json.Decode.string
|> field "lastName" .lastName Json.Decode.string
|> field "pets" .pets Json.Decode.int
|> endRecord
Start constructing a record decoder.
You will need to supply a polymorphic record constructor function as both arguments to this function.
For example, if you want to decode a record like this:
You will need to write a constructor function as follows:
Note: it is important that the type annotation does not constrain the types of the arguments passed into this constructor function. That is part of the secret of how this package works its magic!
Now, pass the polymorphic constructor function into
recordtwice, like this: