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 creation 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
|> endRecordAn intermediate data structure used to check that the decoders we are
creating have their fields in the correct order. A Builder is created by the
record function, updated by the field function, and finally converted into a
Json.Decode.Decoder by the endRecord function.
Used for type safety.
Used for type safety.
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 must not use the
Userconstructor. Instead, 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: