The Concise Binary Object Representation (CBOR) is a data format whose design goals include the possibility of extremely small code size, fairly small message size, and extensibility without the need for version negotiation. These design goals make it different from earlier binary serializations such as ASN.1 and MessagePack.
Decode a boolean.
D.decode D.bool Bytes<0xF4> == Just False
D.decode D.bool Bytes<0xF5> == Just TrueDecode an integer. Note that there's no granular decoders (nor encoders) because, the CBOR encoding of an integers varies depending on the integer value. Therefore, at the CBOR-level, as in Elm, there's no notion of smaller integers.
D.decode D.int Bytes<0x00> == Just 0
D.decode D.int Bytes<0x00> == Just 0
D.decode D.int Bytes<0x1A, 0x00, 0x02, 0x33, 0x56> == Just 1337
D.decode D.int Bytes<0x2D> == Just -14Decode a floating point number, regardless of its precision. Because in Elm, there exists only one 'Float' type, which is encoded in double-precision, we do decode all floats into this double-precision number in Elm which may lead to some precision gain and therefore, different representation than one would expect.
D.decode D.float <| E.encode (E.float16 1.1) == Just 1.099609375
D.decode D.float <| E.encode (E.float64 1.1) == Just 1.1Decode a bunch UTF-8 bytes into a String.
In case of streaming, all string chunks are decoded at once and concatenated as if they were
one single string. See also Cbor.Encode.beginString.
Decode a bunch bytes into Bytes.
In case of streaming, all byte chunks are decoded at once and concatenated as if they were
one single byte string. See also Cbor.Encode.beginBytes.
Decode a List of items a. The list can be definite or indefinite.
D.decode (D.list D.int) Bytes<0x82, 0x0E, 0x18, 0x2A> == Just [ 14, 42 ]
D.decode (D.list D.int) Bytes<0x9F, 0x01, 0x02, 0xFF> == Just [ 1, 1 ]
D.decode (D.list (D.list D.bool)) Bytes<0x81, 0x9F, 0xF4, 0xFF> == Just [ [ False ] ]Decode an CBOR map of pairs of data-items into an Elm Dict.
The map can be either of definite length or indefinite length.
D.decode (D.dict D.int D.int) Bytes<0xA1, 0x0E, 0x18, 0x2A>
== Dict.fromList [ ( 14, 42 ) ]
D.decode (D.dict D.string D.int) Bytes<0xBF, 0x61, 0x61, 0x0E, 0x61, 0x62, 0x18, 0x2A, 0xFF>
== Dict.fromList [ ( "a", 14 ), ( "b", 42 ) ]Decode a (definite) CBOR map into a record. Keys in the map can be arbitrary CBOR but are expected to be homogeneous across the record.
type alias Album =
{ artist : String
, title : String
, label : Maybe String
}
-- In this example, we use compact integer as keys.
decodeAlbumCompact : D.Decoder Album
decodeAlbumCompact =
D.record D.int Album <|
D.fields
>> D.field 0 D.string
>> D.field 1 D.string
>> D.optionalField 2 D.string
-- In this example, we use more verbose string keys.
decodeAlbumVerbose : D.Decoder Album
decodeAlbumVerbose =
D.record D.string Album <|
D.fields
>> D.field "artist" D.string
>> D.field "title" D.string
>> D.optionalField "label" D.stringA helper that makes writing record decoders nicer. It is equivalent to
succeed, but let us align decoders to fight compulsory OCDs.
Decode a field of record and step through the decoder. This ensures that the decoded key matches the expected key. If it doesn't, the entire decoder is failed.
See record for detail about usage.
Decode an optional field of record and step through the decoder. This
ensures that the decoded key matches the expected key. If it doesn't, the entire
decoder is failed. When the key is missing, returns Nothing as a value.
See record for detail about usage.
Decode a (definite) CBOR array into a record / tuple.
type alias Track =
{ title : String
, duration : Int
}
decodeTrack : D.Decoder Track
decodeTrack =
D.tuple Track <|
D.elems
>> D.elem D.string
>> D.elem D.intA helper that makes writing record decoders nicer. It is equivalent to
succeed, but let us align decoders to fight compulsory OCDs.
Decode an element of a record or tuple and step through the decoder.
See tuple for detail about usage.
Decode an optional element of a record or tuple and step through the decoder. Note that optional elements only make sense at the end of a structure.
In particular, optional elements must be contiguous and can only be optional (resp. missing) if all their successors are also optional (resp. missing).
For example, consider:
type alias Foo =
Foo
{ a : Int
, b : Maybe Int
, c : Maybe Int
}
decodeFoo =
tuple Foo <|
elems
>> elem int
>> optionalElem int
>> optionalElem intc to be missing.b and c to be missing.b to be missing as this is undistinguishable from c missing.Always succeed to produce a certain Elm value.
D.decode (D.succeed 14) Bytes<0x42, 0x28> == Just 14
D.decode (D.list (D.int |> D.andThen (\_ -> D.succeed 1))) Bytes<0x83, 0x00, 0x00, 0x00>
== Just [ 1, 1, 1 ]This particularly handy when used in combination with andThen.
A decoder that always fail.
D.decode D.fail Bytes<0x00> == NothingThis is particularly handy when used in combination with andThen
Decode something and then use that information to decode something else. This is useful when a 'Decoder' depends on a value held by another decoder:
tagged : Tag -> Decoder a -> Decoder a
tagged expectedTag decodeA =
tag
|> andThen
(\decodedTag ->
if decodedTag == expectedTag then
decodeA
else
fail
)Decode something and then another thing, but only continue with the second result.
ignoreThen a ignored =
ignored |> andThen (always a)Decode something and then another thing, but only continue with the first result.
thenIgnore ignored a =
a |> andThen (\result -> map (always result) ignored)Transform a decoder. For example, maybe you just want to know the length of a string:
import String
stringLength : Decoder Int
stringLength =
D.map String.length D.stringTry two decoders and then combine the result. Can be used to decode objects with many fields:
type alias Point =
{ x : Float, y : Float }
point : Decoder Point
point =
D.map2 Point D.float D.floatIt tries each individual decoder and puts the result together with the Point constructor.
Try three decoders and then combine the result. Can be used to decode objects with many fields:
type alias Person =
{ name : String, age : Int, height : Float }
person : Decoder Person
person =
D.map3 Person D.string D.int D.floatLike map2 it tries each decoder in order and then give the results to the
Person constructor. That can be any function though!
Try four decoders and then combine the result. See also 'map3' for some examples.
Try five decoders and then combine the result. See also 'map3' for some examples.
Apply a decoder to all elements of a List, in order, and yield the
resulting resulting List.
Decode the beginning of an indefinite String sequence.
This is useful in combination with ignoreThen and
andThen to manually decode sequences of text strings.
Decode the beginning of an indefinite Bytes sequence.
This is useful in combination with ignoreThen and
andThen to manually decode sequences of byte strings.
Decode the beginning of an indefinite List.
This is useful in combination with ignoreThen and
andThen to manually decode indefinite List.
Decode the beginning of an indefinite Dict.
This is useful in combination with ignoreThen and
andThen to manually decode indefinite Dict.
Decode a termination of an indefinite structure. See
beginString, beginBytes,
beginList, beginDict for detail about usage.
This implementation does little interpretation of the tags and is limited to only decoding the tag's value. The tag's payload has to be specified as any other decoder. So, using the previous example, one could decode a bignum as:
D.decode (D.tag |> D.andThen (\tag -> D.bytes)) inputYou may also use tagged if you as a helper to decode a tagged value, while
verifying that the tag matches what you expect.
D.decode (D.tagged PositiveBigNum D.bytes) inputDecode remaining bytes as any CborItem. This is useful
for debugging or to inspect some unknown Cbor data.
D.decode D.any <| E.encode (E.int 14) == Just (CborInt 14)
Describes how to turn a binary CBOR sequence of bytes into any Elm value.