Convenience functions for working with Json
Can be helpful when decoding large objects incrementally.
See the andMap docs
for an explanation of how andMap works and how to use it.
Infix version of andMap that makes for a nice DSL when decoding objects.
See the (|:) docs
for an explanation of how (|:) works and how to use it.
This function turns a list of decoders into a decoder that returns a list.
The returned decoder will zip the list of decoders with a list of values, matching each decoder with exactly one value at the same position. This is most often useful in cases when you find yourself needing to dynamically generate a list of decoders based on some data, and decode some other data with this list of decoders.
Note that this function, unlike List.map2's behaviour, expects the list of
decoders to have the same length as the list of values in the JSON.
>>> sequence
... [ map Just string
... , succeed Nothing
... , map Just string
... ]
... |> flip decodeString """ [ "pick me", "ignore me", "and pick me" ] """
Ok [ Just "pick me", Nothing, Just "and pick me" ]Extract a set.
>>> import Set
>>> "[ 1, 1, 5, 2 ]"
... |> decodeString (set int)
Ok <| Set.fromList [ 1, 2, 5 ]Extract a dict using separate decoders for keys and values.
>>> import Dict
>>> let
... input = """
... { "1": "foo", "2": "bar" }
... """
... in
... decodeString (dict2 int string) input
Ok <| Dict.fromList [ ( 1, "foo" ), ( 2, "bar" ) ]Try running the given decoder; if that fails, then succeed with the given fallback value.
>>> """ { "children": "oops" } """
... |> decodeString (field "children" (list string) |> withDefault [])
Ok []
>>> """ null """
... |> decodeString (field "children" (list string) |> withDefault [])
Ok []
>>> """ 30 """
... |> decodeString (int |> withDefault 42)
Ok 30If a field is missing, succeed with Nothing. If it is present, decode it
as normal and wrap successes in a Just.
When decoding with
maybe,
if a field is present but malformed, you get a success and Nothing.
optionalField gives you a failed decoding in that case, so you know
you received malformed data.
Examples:
If the "stuff" field is missing, decode to Nothing.
>>> """ { } """
... |> decodeString (optionalField "stuff" string)
Ok NothingIf the "stuff" field is present but not a String, fail decoding.
>>> """ { "stuff": [] } """
... |> decodeString (optionalField "stuff" string)
Err "Expecting a String at _.stuff but instead got: []"If the "stuff" field is present and valid, decode to Just String.
>>> """ { "stuff": "yay!" } """
... |> decodeString (optionalField "stuff" string)
Ok <| Just "yay!"Transform a result into a decoder
Sometimes it can be useful to use functions that primarily operate on
Result in decoders. An example of this is Json.Decode.Extra.date. It
uses the built-in Date.fromString to parse a String as a Date, and
then converts the Result from that conversion into a decoder which has
either already succeeded or failed based on the outcome.
date : Decoder Date
date =
string |> andThen (Date.fromString >> fromResult)Extract an int using String.toInt
>>> """ { "field": "123" } """
... |> decodeString (field "field" parseInt)
Ok 123Extract a float using String.toFloat
>>> """ { "field": "50.5" } """
... |> decodeString (field "field" parseFloat)
Ok 50.5Extract a JSON-encoded string field
"Yo dawg, I heard you like JSON..."
If someone has put JSON in your JSON (perhaps a JSON log entry, encoded as a string) this is the function you're looking for. Give it a decoder and it will return a new decoder that applies your decoder to a string field and yields the result (or fails if your decoder fails).
>>> """ { "logs": "[\\"log1\\", \\"log2\\"]"} """
... |> decodeString (field "logs" <| doubleEncoded (list string))
Ok [ "log1", "log2" ]
Extract a date using
Date.fromString