MackeyRMS / json-decode-attempt / Json.Decode.Attempt

Module for Decoders that always return a valid value.

Type aliases


type Decoder status value


type WithDefaults

Phantom type to indicate that a Decoder has defaults provided for all of it's fields.


type MissingDefaults

Phantom type to indicate that a Decoder has a risk in it's procedure thus rendering the entire decoder susceptible to failure.


type alias Validated value =
{ errors : List Json.Decode.Error
, value : value 
}

A record that carries both: A. a properly decoded value & [] like { value = "Stuff", errors = [] } or B. a default value & List Error like { value = "", [Decode.Failure "unexpected null value"] }

Runners

attempt : Decoder MissingDefaults value -> Json.Decode.Value -> Result Json.Decode.Error { errors : List Json.Decode.Error, value : value }

Attempt to decode with a decoder that could potentially fail

decode : Decoder WithDefaults value -> Json.Decode.Value -> Validated value

Provided with defaults using the primitives defined in this module decode will return a value composed of decoded values & defaults paired with whatever associated errors may have happened along the way.

toTuple : { errors : List Json.Decode.Error, value : value } -> ( value, List Json.Decode.Error )

In case you need a tuple instead of record accessors :man_shrugging:

toDecoder : Decoder status value -> Json.Decode.Decoder (Validated value)

Convert a Validated decoder into a regular elm/json Decoder

Constructors

withDefault : a -> Json.Decode.Decoder a -> Decoder WithDefaults a

Provide a default value, turning a Decoder that has the potential to fail due to missing fields or bad data or, into a Decoder that will always succeed.

succeed : a -> Decoder WithDefaults a

wrap a value in a Decoder

maybe : Json.Decode.Decoder a -> Decoder WithDefaults (Maybe a)

turn a regular decoder into a Validated decoder using Maybe & Nothing as the default.

Primitives

unit : Decoder WithDefaults ()

Decoder for a unit value, may be useful when responding to DELETE requests that return HTTP 201 No Content

value : Decoder WithDefaults Json.Encode.Value

boolOr : Basics.Bool -> Decoder WithDefaults Basics.Bool

Decoder between a JSON boolean and an Elm Bool

intOr : Basics.Int -> Decoder WithDefaults Basics.Int

Decoder between a JSON number and an Elm Int

floatOr : Basics.Float -> Decoder WithDefaults Basics.Float

Decoder between a JSON number and an Elm Float

charOr : Char -> Decoder WithDefaults Char

Decoder between a JSON string of length 1 and an Elm Char

stringOr : String -> Decoder WithDefaults String

Decoder between a JSON string and an Elm String

Core Data Structures

list : Decoder WithDefaults a -> Decoder WithDefaults (List a)

Decoder between a JSON array and an Elm List.

Use when a default for the elements is available to fill if decoding fails. Will still collect errors for the failed decoders

filteredList : Json.Decode.Decoder a -> Decoder WithDefaults (List a)

Swallows errors for elements but probably what you want for most use cases...

array : Decoder WithDefaults a -> Decoder WithDefaults (Array a)

Decoder between a JSON array and an Elm Array.

dict : Decoder WithDefaults a -> Decoder WithDefaults (Dict String a)

Decoder between a JSON object and an Elm Dict.

tuple : Decoder WithDefaults a -> Decoder WithDefaults b -> Decoder WithDefaults ( a, b )

Datum between a JSON array of length 2 and an Elm Tuple.

triple : Decoder WithDefaults a -> Decoder WithDefaults b -> Decoder WithDefaults c -> Decoder WithDefaults ( a, b, c )

Datum between a JSON array of length 3 and an Elm triple.

Records Primitives

required : String -> List String -> Decoder WithDefaults f -> Decoder status (f -> b) -> Decoder status b

Specify the name, and a Decoder for a field.

required propagates errors when the name is missing in the resulting json.

optional : String -> List String -> Decoder WithDefaults f -> Decoder status (f -> b) -> Decoder status b

optional only propagates errors where the decoder doesn't match the expected type.

Functor, Applicative, Monad, Alternative

map : (a -> b) -> Decoder WithDefaults a -> Decoder WithDefaults b

apply : Decoder WithDefaults a -> Decoder status (a -> b) -> Decoder status b

andMap for a Decoder WithDefaults

try : Decoder MissingDefaults a -> Decoder status (a -> b) -> Decoder MissingDefaults b

andMap for adding a call to risk

risk : Json.Decode.Decoder a -> Decoder status (a -> b) -> Decoder MissingDefaults b

By introducing a raw elm/json decoder you're exposing the rest of your decoder to the possibility of failing

andThen : (a -> Decoder WithDefaults b) -> Decoder WithDefaults a -> Decoder WithDefaults b

oneOf : List (Json.Decode.Decoder a) -> a -> Decoder WithDefaults a

Provide a list of regular elm/json decoders and if all of them fail, provide a default.

Helpers

nullable : Decoder WithDefaults c -> Decoder WithDefaults c

Specify that a decoder should use it's defaults when encountering a null

doubleEncoded : { hideSensitiveInfo : Basics.Bool } -> Decoder WithDefaults a -> Decoder WithDefaults a

For dealing with a json string inside a json payload.

Tests

tests : Test

Tests