Last active
September 9, 2017 00:33
-
-
Save jszmajda/13097b1e5240e43df960090b22be6659 to your computer and use it in GitHub Desktop.
Elm JSON Decoding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- So I'm decoding JSON with this pattern, but there has to be a better way | |
import Json.Decode as JD | |
type alias User = | |
{ id : Int | |
, email : String | |
, firstName : String | |
, lastName : String | |
} | |
-- specifically here: this map4 seems dumb. This function depends on the proper | |
-- ordering of the fields to match the record constructor above. There must be | |
-- a better way, right? | |
jsonDecoder : JD.Decoder User | |
jsonDecoder = JD.map4 User | |
(JD.field "id" int) | |
(JD.field "email" string) | |
(JD.field "firstName" string) | |
(JD.field "lastName", string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey! I built this tool to output Elm decoders for JSON: https://elm.quicktype.io/
Here's an order-independent decoder that it built given your
User
type:It uses NoRedInk/elm-decode-pipeline.