elm/json
package docsHere's an Ellie example that shows the error when you have an implicit type that your flags decode to and it's incorrect.
Sorry Dillon... Jeroen won the trivia challenge this time 😉 It turns out that ports will throw exceptions when they are given a bad value from JavaScript, but it doesn't bring down your Elm app. Elm continues to run with an unhandled exception. Here's an Ellie example.
Flags and ports will never throw a runtime exception in your Elm app if you always use Json.Decode.Value
s and Json.Encode.Value
s for them and handle unexpected cases. Flags and ports are the one place that Elm lets you make unsafe assumptions about JSON data.
elm-iso8601 package
elm/time package
Json.Decode.succeed
is helpful for stubbing out dataJson.Decode.fail
lets you validate data and fail your whole decoder if there’s a problemelm-cli-options-parser
packageJson.Decode.maybe
docsNote about Decode.maybe
. It can be unsafe to use this function because it can cover up failures.
Json.Decode.maybe
will cover up some cases that you may not have intended to. For example, if an API returns a float we would suddenly get Nothing
back, but we probably want a decoding failure here:
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
Json.Decode.Extra.optionalNullableField
might have more intuitive and desirable behavior for these cases.
Thank you to lydell for the tip! See the discussion in this discourse thread.
elm-graphql
packageelm-pages
elm-pages
StaticHttp
API docsintellij-elm
JSON decoder generatorJson.Decode.map
type alias Album = { ... }
Submit your question to Elm Radio!