Elm Radio Episode 4 - JSON Decoders

Great point @lydell!

You’re right that if you use Json.Decode.maybe it will cover up some cases that you may not have intended to. I didn’t really consider this, but you’re absolutely right!

import Json.Decode as Decode

""" {"temperatureInF": 86} """ |> Decode.decodeString (Decode.maybe (Decode.field "temperatureInF" Decode.int))
--> Ok (Just 86)

""" {"temperatureInF": 86.14} """ |> Decode.decodeString (Decode.maybe (Decode.field "temperatureInF" Decode.int))
--> Ok Nothing

In many cases, if you’re allowing the field to be null or absent, you would still want to know if it’s present but the decoder fails. I bet that’s tripped some people up. I wish I was aware of Json.Decode.Extra.optionalNullableField when we recorded that episode! I’ll update the show notes with a reference to this, though Update: I updated the show notes! :smile: