A Codec a contain a JSON Decoder a and the corresponding a -> Value encoder.
This module is a port of miniBill/elm-codec. Thank you for the great API design miniBill!
Extracts the Decoder contained inside the Codec.
Extracts the encoding function contained inside the Codec.
Codec between a JSON string and an Elm String
Codec between a JSON boolean and an Elm Bool
Codec between a JSON number and an Elm Int
Codec between a JSON number and an Elm Float
Represents an optional value.
Codec between a JSON array and an Elm List.
Codec between a JSON array and an Elm Array.
Codec between a JSON object and an Elm Dict.
Codec between a JSON array and an Elm Set.
Codec between a JSON array of length 2 and an Elm Tuple.
Codec between a JSON array of length 3 and an Elm triple.
Codec for Result values.
A partially built Codec for an object.
Start creating a Codec for an object. You should pass the main constructor as argument.
If you don't have one (for example it's a simple type with no name), you should pass a function that given the field values builds an object.
Example with constructor:
type alias Point =
{ x : Float
, y : Float
}
pointCodec : Codec Point
pointCodec =
Codec.object Point
|> Codec.field "x" .x Codec.float
|> Codec.field "y" .y Codec.float
|> Codec.buildObjectExample without constructor:
pointCodec : Codec { x : Int, y : Bool }
pointCodec =
Codec.object (\x y -> { x = x, y = y })
|> Codec.field "x" .x Codec.int
|> Codec.field "y" .y Codec.bool
|> Codec.buildObjectSpecify the name, getter and Codec for a field.
The name is only used as the field name in the resulting JSON, and has no impact on the Elm side.
Specify the name getter and Codec for an optional field.
This is particularly useful for evolving your Codecs.
If the field is not present in the input then it gets decoded to Nothing.
If the optional field's value is Nothing then the resulting object will not contain that field.
Specify the name getter and Codec for a required field, whose value can be null.
If the field is not present in the input then the decoding fails.
If the field's value is Nothing then the resulting object will contain the field with a null value.
This is a shorthand for a field having a codec built using Codec.maybe.
Create a Codec from a fully specified ObjectCodec.
A partially built Codec for a custom type.
Starts building a Codec for a custom type.
You need to pass a pattern matching function, built like this:
import TsJson.Codec exposing (Codec)
import TsJson.Codec.Advanced as Codec
type Shape
= Rectangle Int Int
| Square Int
| Circle Int
shapeCodec : Codec Shape
shapeCodec =
Codec.custom (Just "shape")
(\vRectangle vSquare vCircle shape ->
case shape of
Rectangle width height ->
vRectangle width height
Square width ->
vSquare width
Circle radius ->
vCircle radius
)
|> Codec.namedVariant2 "rectangle" Rectangle Codec.int Codec.int
|> Codec.positionalVariant1 "square" Square Codec.int
|> Codec.namedVariant1 "circle" Circle Codec.int
|> Codec.buildCustomObject
The `TsType` for `shapeCodec` is the following discriminated union:
```typescript
| { shape: "rectangle"; width: number; height: number }
| { shape: "square"; args: [ number ] }
| { shape: "circle"; radius: number }
```Build a Codec for a fully specified custom type.
Define a variant with 0 parameters for a custom type.
Define a variant with 1 parameter for a custom type.
Define a variant with 2 parameters for a custom type.
Define a variant with 3 parameters for a custom type.
Define a variant with 4 parameters for a custom type.
Define a variant with 5 parameters for a custom type.
Define a variant with 6 parameters for a custom type.
Define a variant with 7 parameters for a custom type.
Define a variant with 8 parameters for a custom type.
Define a variant with 0 parameters for a custom type.
Define a variant with 2 parameters for a custom type.
Define a variant with 3 parameters for a custom type.
Define a variant with 3 parameters for a custom type.
Define a variant with 3 parameters for a custom type.
Define a variant with 3 parameters for a custom type.
Define a variant with 3 parameters for a custom type.
Define a variant with 3 parameters for a custom type.
Try a set of decoders (in order). The first argument is used for encoding and decoding, the list of other codecs is used as a fallback while decoding.
This is particularly useful for backwards compatibility. You would pass the current codec as the first argument,
and the old ones (eventually mapped) as a fallback list to use while decoding.
Transform a Codec.
Create a Codec that produces null as JSON and always decodes as the same value.
Create a Codec for a recursive data structure.
The argument to the function you need to pass is the fully formed Codec.
Ignore the JSON and make the decoder fail. This is handy when used with
oneOf where you want to give a custom error message in some
case. The encoder will produce null.
This is useful for recursive structures that are not easily modeled with recursive.
Have a look at the Json.Decode docs for examples.
Create a Codec that doesn't transform the JSON value, just brings it to and from Elm as a Value.
Build your own custom Codec.
Useful if you have pre-existing Decoders you need to use.
A value that knows how to encode and decode JSON values.