Decode

DecodeError : [TooShort]

Error types when decoding a List U8 of utf-8 bytes using a Decoder

DecodeResult val

Return type of a Decoder.

This is can be useful when creating a custom decoder or when using fromBytesPartial. For example writing unit tests, such as;

expect
    input = "\"hello\", " |> Str.toUtf8
    actual = Decode.fromBytesPartial input Json.json
    expected = Ok "hello"

    actual.result == expected

Decoder val fmt

Decodes a List U8 of utf-8 bytes where val is the type of the decoded value, and fmt is a Decoder which implements the DecoderFormatting ability

Decoding :

Definition of the Decoding ability

DecoderFormatting :

Definition of the DecoderFormatting ability

custom

Build a custom Decoder function. For example the implementation of decodeBool could be defined as follows;

decodeBool = Decode.custom \bytes, @Json {} ->
    when bytes is
        ['f', 'a', 'l', 's', 'e', ..] -> { result: Ok Bool.false, rest: List.drop bytes 5 }
        ['t', 'r', 'u', 'e', ..] -> { result: Ok Bool.true, rest: List.drop bytes 4 }
        _ -> { result: Err TooShort, rest: bytes }

decodeWith

Decode a List U8 utf-8 bytes using a specific Decoder function

fromBytesPartial

Decode a List U8 utf-8 bytes and return a DecodeResult

expect
    input = "\"hello\", " |> Str.toUtf8
    actual = Decode.fromBytesPartial input Json.json
    expected = Ok "hello"

    actual.result == expected

fromBytes

Decode a List U8 utf-8 bytes and return a Result with no leftover bytes expected. If successful returns Ok val, however, if there are bytes remaining returns Err Leftover (List U8).

expect
    input = "\"hello\", " |> Str.toUtf8
    actual = Decode.fromBytes input Json.json
    expected = Ok "hello"

    actual == expected

mapResult : DecodeResult a, (a -> b) -> DecodeResult b

Transform the val of a DecodeResult