Prototyping a simple JSON API: best practices and gotchas

elm/json is really weird: It’s missing a function for decoding optional fields. (Fields that are there only sometimes.) Instead, it has the Decode.maybe kludge, which does this: “If my decoder fails for whatever reason, return Nothing instead”. It’s meant to catch “this field does not exist” errors, but it also catches all other reasons a decoder can fail, masking mistakes you make in your decoder, or that the JSON producer makes.

It’s better to use Json.Decode.Extra.optionalField.

2 Likes