This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
A pipeline-friendly library for building decoders.
version 1.1.0
license BSD-3-Clause
native-modules False
elm-version 0.16.0 <= v < 0.17.0
Tag 1.1.0
Committed At 2016-04-22 18:46:31 UTC
evancz/elm-effects 2.0.1 <= v < 3.0.0 2.0.1
elm-lang/core 3.0.0 <= v < 4.0.0 3.0.0



A library for building decoders using the pipeline (|>) operator and ordinary function calls.


It's common to decode into a record that has a type alias. Here's an example of this from the object3 docs:

type alias Job = { name : String, id : Int, completed : Bool }

point : Decoder Job
point =
  object3 Job
    ("name" := string)
    ("id" := int)
    ("completed" := bool)

This works because a record type alias can be called as a normal function. In that case it accepts one argument for each field (in whatever order the fields are declared in the type alias) and then returns an appropriate record built with those arguments.

The objectN decoders are straightforward, but require manually changing N whenever the field count changes. This library provides functions designed to be used with the |> operator, with the goal of having decoders that are both easy to read and easy to modify.


Here is a decoder built with this library.

import Json.Decode exposing (int, string, float, Decoder)
import Json.Decode.Pipeline exposing (decode, required, optional, hardcoded)

type alias User =
  { id : Int
  , email : String
  , name : String
  , percentExcited : Float

userDecoder : Decoder User
userDecoder =
  decode User
    |> required "id" int
    |> required "email" string
    |> optional "name" string "(fallback if name not provided)"
    |> hardcoded 1.0

In this example:

  • decode is a synonym for succeed (it just reads better here)
  • required "id" int is similar to ("id" := int)
  • optional is like required, but if the field is not present, decoding does not fail; instead it succeeds with the provided fallback value.
  • hardcoded does not look at the provided JSON, and instead always decodes to the same value.

You could use this decoder as follows:

    {"id": 123, "email": "", "name": "Sam Sample"}

The result would be:

{ id = 123
, email = ""
, name = "Sam Sample"
, percentExcited = 1.0

Alternatively, you could use it like so:

    {"id": 123, "email": "", "percentExcited": "(hardcoded)"}

In this case, the result would be:

{ id = 123
, email = ""
, name = "(fallback if name not present)"
, percentExcited = 1.0

[NoRedInk][team] [team]: