A Codec a contains a Bytes.Decoder a and the corresponding a -> Bytes.Encoder.
The direction bytes are ordered in memory. Refer to the elm/bytes docs for more information.
Describes how to generate a sequence of bytes.
A sequence of bytes. Refer to the elm/bytes docs for more information.
Describes how to turn a sequence of bytes into a nice Elm value.
Run a Codec to turn a sequence of bytes into an Elm value.
Convert an Elm value into a sequence of bytes.
Codec between a signed 32-bit integer and an Elm Int.
Use this if the byte ordering and number of bytes used isn't a concern.
Codec between an unsigned 32-bit integer and an Elm Int.
Use this if the byte ordering and number of bytes used isn't a concern.
Codec between a 32-bit float and an Elm Float.
Due to Elm Floats being 64-bit, encoding and decoding it as a 32-bit float won't exactly equal the original value.
Codec between a signed 32-bit integer and an Elm Int
Codec between an unsigned 32-bit integer and an Elm Int
Codec between a signed 16-bit integer and an Elm Int
Codec between an unsigned 16-bit integer and an Elm Int
Codec between a signed 8-bit integer and an Elm Int
Codec between an unsigned 8-bit integer and an Elm Int
Codec for Bytes. This is useful if you wanted to include binary data that you're going to decode elsewhere such as a PNG file.
Codec between a sequence of bytes and an Elm Dict.
Codec between a sequence of bytes and an Elm triple.
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.
type alias Point =
{ x : Int
, y : Int
}
pointCodec : Codec Point
pointCodec =
Codec.object Point
|> Codec.field .x Codec.signedInt
|> Codec.field .y Codec.signedInt
|> Codec.buildObjectSpecify how to get a value from the object we want to encode and then give a Codec for that value.
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, see the FAQ for details.
type Semaphore
= Red Int String Bool
| Yellow Float
| Green
semaphoreCodec : Codec Semaphore
semaphoreCodec =
Codec.custom
(\redEncoder yellowEncoder greenEncoder value ->
case value of
Red i s b ->
redEncoder i s b
Yellow f ->
yellowEncoder f
Green ->
greenEncoder
)
|> Codec.variant3 0 Red Codec.signedInt Codec.string Codec.bool
|> Codec.variant1 1 Yellow Codec.float64
|> Codec.variant0 2 Green
|> Codec.buildCustomDefine a variant with 0 parameters for a custom type.
Define a variant with 1 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 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.
Build a Codec for a fully specified custom type.
This is useful for recursive structures that are not easily modeled with recursive.
type Peano
= Peano (Maybe Peano)
{-| This is the same example used in Codec.recursive but adapted for lazy.
-}
peanoCodec : Codec Peano
peanoCodec =
Codec.maybe (Codec.lazy (\() -> peanoCodec)) |> Codec.map Peano (\(Peano a) -> a)Create a Codec for a recursive data structure.
The argument to the function you need to pass is the fully formed Codec, see the FAQ for details.
type Peano
= Peano (Maybe Peano)
peanoCodec : Codec Peano
peanoCodec =
Codec.recursive
(\finishedCodec ->
Codec.maybe finishedCodec
|> Codec.map Peano (\(Peano p) -> p)
)Same as custom but here we can choose what codec to use for the integer id we tell apart variants with.
This is useful if, for example, you know you won't have ids outside of the range 0 - 255 and can use unsignedInt8 instead of the default signedInt32 to save some space.
A value that knows how to encode and decode a sequence of bytes.