You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importJson.Decodeexposing (int, string, float, Decoder)
importJson.Decode.Pipelineexposing (decode, required, optional, hardcoded)
-- basically a record in Elmtype alias User={ id :Int, email :MaybeString, name :String, percentExcited :Float}userDecoder:DecoderUseruserDecoder =
decode User|> required "id" int
|> required "email"(nullable string)-- `null` decodes to `Nothing`|> optional "name" string "(fallback if name is `null` or not present)"|> hardcoded 1.0
The text was updated successfully, but these errors were encountered:
At the moment, you have to convert between tuples and records, so there's a bit of boilerplate involved. The above example would look something like this:
I'd definitely like to look at improving this, and translating the example has already highlighted another opportunity to improve handling of default values :) Elm has kind of a head start since User is a constructor function for the record type, but maybe there's a way around that.
Just some nice examples in the
README.md
would go a long way.Love the idea of this library by the way ❤️.
One thing I was hoping to find: How do you convert a some JSON into a record? In Elm you would use something like: http://package.elm-lang.org/packages/elm-lang/core/5.1.1/Json-Decode#map or perhaps http://package.elm-lang.org/packages/NoRedInk/elm-decode-pipeline/latest
Example in Elm using
elm-decode-pipeline
The text was updated successfully, but these errors were encountered: