webbhuset/elm-json-decode/1.0.0

NOTE: the latest version is 1.2.0

Rerouted (regenerated, verified). Same repository and tag, but GitHub now builds the zip with different bytes, so it no longer matches the SHA-1 package.elm-lang.org recorded. The Lamdera package server serves a copy recovered from https://github.com/webbhuset/elm-json-decode.

What we checked: Tag 1.0.0 is still on github.com/webbhuset/elm-json-decode; its commit b8aec69576cf is from 2018-11-07 09:00 UTC, before the version was published (2018-11-07 09:00 UTC); elm.json is byte-identical to package.elm-lang.org's; the exposed modules match docs.json; the zip's SHA-1 is not the published one (GitHub's zip no longer comes out byte for byte the same), so the contents were checked instead.

The Lamdera compiler uses this copy and prints a one-line notice saying so. elm itself fails to download this version.

Details · package.elm-lang.org's zip: https://github.com/webbhuset/elm-json-decode/zipball/1.0.0/

Json.Decode.Field

Decode JSON objects

Since JSON values are not known until runtime there is no way of checking them at compile time. This means that there is a possibility a decoding operation can not be successfully completed.

In that case there are two possible solutions:

  1. Fail the whole decoding operation.
  2. Deal with the missing value situation, either by defaulting to some value or by using a Maybe value

In this module these two options are represented by require and attempt.

  • require can fail if the decoding can not be completed.
  • attempt will never fail. It always decodes to a Maybe value.
require : String -> Decoder a -> (a -> Decoder b) -> Decoder b

Decode required fields.

Example:

user : Decoder User
user =
    require "id" Decode.int <| \id ->
    require "name" Decode.string <| \name ->


    Decode.succeed
        { id = id
        , name = name
        }

In this example the decoder will fail if:

  • The JSON value is not an object.
  • Any of the fields "id" or "name" are missing. If the object contains other fields they are ignored and will not cause the decoder to fail.
  • The value of field "id" is not an Int.
  • The value of field "name" is not a String.
requireAt : List String -> Decoder a -> (a -> Decoder b) -> Decoder b

Decode nested fields. Works the same as require but on nested fieds.

blogPost : Decoder BlogPost
blogPost =
    require "id" Decode.int <| \id ->
    require "title" Decode.string <| \title ->
    requireAt ["author", "name"] Decode.string <| \authorName ->


    Decode.succeed
        { id = id
        , title = title
        , author = authorName
        }
attempt : String -> Decoder a -> (Maybe a -> Decoder b) -> Decoder b

Decode optional fields.

Always decodes to a Maybe value and never fails.

Example:

person : Decoder Person
person =
    require "name" Decode.string <| \name ->
    attempt "weight" Decode.int <| \maybeWeight ->


    Decode.succeed
        { name = name
        , weight = maybeWeight
        }

In this example the maybeWeight value will be Nothing if:

  • The JSON value was not an object
  • The weight field is missing.
  • The weight field is not an Int.

In this case there is no difference between a field being null or missing. If a field must exist but can be null, use require and Decode.maybe instead:

require "field" (Decode.maybe Decode.string) <| \field ->
attemptAt : List String -> Decoder a -> (Maybe a -> Decoder b) -> Decoder b

Decode optional nested fields. Works the same way as attempt but on nested fields.