JSON Decode package for decoding objects


#1

I just published a package for building JSON decoders using continuation style (chains of functions). I hope someone finds it useful.

webbhuset/elm-json-decode

This is the result of some converstions in this thread aswell as feedback from our Elm meetups.

Thank to @rtfeldman for helping out with documentation and ideas.


#2

Looks great! I like the way you get access to the data before building the object. Even though the syntax is longer than elm pipeline decoder, I think it’s more flexible. Thanks!


#3

I’m trying to install this with elm install webbhuset/elm-json-decode but get error about corrupt ZIP:

I got an unexpected zip file from:

    <https://github.com/webbhuset/elm-json-decode/zipball/1.0.0/>

I check the hash the zip, and it seems off:

  Expected: bdb9648731f8824f8d7d2df3378449d133dc0b2f
    Actual: d4a81c47a40c53d597b340284ab7bb0c7e22810a

This usually means that the package author moved the version tag, so report it
to them and see if that is the issue. Folks on Elm slack can probably help as
well.

#4

Hmm, that is strange. Thank you for reporting. I will look into this.


#5

I could reproduce this when I cleared ~/.elm. I didn’t think about that when I tested after publishing yesterday so it worked fine.

I created the tag 1.0.0 and then ran elm publish. It failed due to a missing LICENSE file.
I added the license file, commited and moved the tag to the new commit. I then ran elm publish again and it succeeded.

Probably it was wrong of me to assume that it was OK to move a tag when elm publish failed?

@malaire I bumped to 1.0.1 and it works now, thanks for reporting.


#6

What’s the likelihood Elm would ever see a do syntax? It would make the API a lot nicer – especially with the recent removal of user-defined infix functions

person : Decoder Person
person = do
    name <- Field.require "name" Decode.string
    id <- Field.require "id" Decode.int
    maybeWeight <- Field.attempt "weight" Decode.int
    maybeLikes <- Field.attempt "likes" Decode.int

    Decode.succeed
        { name = name
        , id = id
        , maybeWeight = maybeWeight
        , likes = Maybe.withDefault 0 maybeLikes
        , hardcoded = "Hardcoded Value"
        }

#7

I love this! It made immediately more sense to me than any other way to decode json. Thank you very much!