A continuation-based decoder, used for decoding values that were previously encoded using the Codec.CBOR.Encoding module. As Decoder has a Monad instance, you can easily write Decoders monadically for building your deserialisation logic.
Modulecborg-0.2.10.0Haskell2010
Codec.CBOR.Decoding
High level API for decoding values that were encoded with the Codec.CBOR.Encoding module, using a Monad based interface.
- 4 types
- 70 values
- Packagecborg-0.2.10.0
- Exports74
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceDecoding.hs
Decode primitive operations
4 declarationsAn action, representing a step for a decoder to taken and a continuation to invoke with the expected value.
Constructors
ConsumeWord (Word# -> ST s (DecodeAction s a))ConsumeWord8 (Word# -> ST s (DecodeAction s a))ConsumeWord16 (Word# -> ST s (DecodeAction s a))ConsumeWord32 (Word# -> ST s (DecodeAction s a))ConsumeNegWord (Word# -> ST s (DecodeAction s a))ConsumeInt (Int# -> ST s (DecodeAction s a))ConsumeInt8 (Int# -> ST s (DecodeAction s a))ConsumeInt16 (Int# -> ST s (DecodeAction s a))ConsumeInt32 (Int# -> ST s (DecodeAction s a))ConsumeListLen (Int# -> ST s (DecodeAction s a))ConsumeMapLen (Int# -> ST s (DecodeAction s a))ConsumeTag (Word# -> ST s (DecodeAction s a))ConsumeInteger (Integer -> ST s (DecodeAction s a))ConsumeFloat (Float# -> ST s (DecodeAction s a))ConsumeDouble (Double# -> ST s (DecodeAction s a))ConsumeBytes (ByteString -> ST s (DecodeAction s a))ConsumeByteArray (ByteArray -> ST s (DecodeAction s a))ConsumeString (Text -> ST s (DecodeAction s a))ConsumeUtf8ByteArray (ByteArray -> ST s (DecodeAction s a))ConsumeBool (Bool -> ST s (DecodeAction s a))ConsumeSimple (Word# -> ST s (DecodeAction s a))ConsumeBytesIndef (ST s (DecodeAction s a))ConsumeStringIndef (ST s (DecodeAction s a))ConsumeListLenIndef (ST s (DecodeAction s a))ConsumeMapLenIndef (ST s (DecodeAction s a))ConsumeNull (ST s (DecodeAction s a))ConsumeListLenOrIndef (Int# -> ST s (DecodeAction s a))ConsumeMapLenOrIndef (Int# -> ST s (DecodeAction s a))ConsumeBreakOr (Bool -> ST s (DecodeAction s a))PeekTokenType (TokenType -> ST s (DecodeAction s a))PeekAvailable (Int# -> ST s (DecodeAction s a))PeekByteOffset (Int# -> ST s (DecodeAction s a))ConsumeWordCanonical (Word# -> ST s (DecodeAction s a))ConsumeWord8Canonical (Word# -> ST s (DecodeAction s a))ConsumeWord16Canonical (Word# -> ST s (DecodeAction s a))ConsumeWord32Canonical (Word# -> ST s (DecodeAction s a))ConsumeNegWordCanonical (Word# -> ST s (DecodeAction s a))ConsumeIntCanonical (Int# -> ST s (DecodeAction s a))ConsumeInt8Canonical (Int# -> ST s (DecodeAction s a))ConsumeInt16Canonical (Int# -> ST s (DecodeAction s a))ConsumeInt32Canonical (Int# -> ST s (DecodeAction s a))ConsumeListLenCanonical (Int# -> ST s (DecodeAction s a))ConsumeMapLenCanonical (Int# -> ST s (DecodeAction s a))ConsumeTagCanonical (Word# -> ST s (DecodeAction s a))ConsumeIntegerCanonical (Integer -> ST s (DecodeAction s a))ConsumeFloat16Canonical (Float# -> ST s (DecodeAction s a))ConsumeFloatCanonical (Float# -> ST s (DecodeAction s a))ConsumeDoubleCanonical (Double# -> ST s (DecodeAction s a))ConsumeBytesCanonical (ByteString -> ST s (DecodeAction s a))ConsumeByteArrayCanonical (ByteArray -> ST s (DecodeAction s a))ConsumeStringCanonical (Text -> ST s (DecodeAction s a))ConsumeUtf8ByteArrayCanonical (ByteArray -> ST s (DecodeAction s a))ConsumeSimpleCanonical (Word# -> ST s (DecodeAction s a))Fail StringDone a
Lift an ST action into a Decoder. Useful for, e.g., leveraging
in-place mutation to efficiently build a deserialised value.
Given a Decoder, give us the DecodeAction
Read input tokens
Decode a Word.
Decode a Word8.
Decode a Word16.
Decode a Word32.
Decode a Word64.
Decode a negative Word.
Decode a negative Word64.
Decode an Int.
Decode an Int8.
Decode an Int16.
Decode an Int32.
Decode an Int64.
Decode an Integer.
Decode a Float.
Decode a Double.
Decode a string of bytes as a ByteString.
Decode a token marking the beginning of an indefinite length set of bytes.
Decode a textual string as a piece of Text.
Decode a token marking the beginning of an indefinite length string.
Decode the length of a list.
Decode a token marking the beginning of a list of indefinite length.
Decode the length of a map.
Decode a token marking the beginning of a map of indefinite length.
Decode an arbitrary tag and return it as a Word.
Decode an arbitrary 64-bit tag and return it as a Word64.
Decode a bool.
Decode a nullary value, and return a unit value.
Decode a simple CBOR value and give back a Word8. You
probably don't ever need to use this.
Specialised Read input token operations
Attempt to decode a word with decodeWord, and ensure the word is exactly as expected, or fail.
Attempt to decode a list length using decodeListLen, and ensure it is exactly the specified length, or fail.
Branching operations
Inspecting the token type
Peek at the current token we're about to decode, and return a TokenType specifying what it is.
The type of a token, which a decoder can ask for at an arbitrary time.
Instances5Bounded, Enum, Eq, Ord, Show
Bounded TokenTypeDefined in cborg-0.2.10.0 · Codec.CBOR.DecodingEnum TokenTypeDefined in cborg-0.2.10.0 · Codec.CBOR.DecodingEq TokenTypeDefined in cborg-0.2.10.0 · Codec.CBOR.DecodingOrd TokenTypeDefined in cborg-0.2.10.0 · Codec.CBOR.DecodingShow TokenTypeDefined in cborg-0.2.10.0 · Codec.CBOR.Decoding
Special operations
Peek and return the length of the current buffer that we're running our decoder on.
Get the current ByteOffset in the input byte sequence of the Decoder.
The Decoder does not provide any facility to get at the input data directly (since that is tricky with an incremental decoder). The next best is this primitive which can be used to keep track of the offset within the input bytes that makes up the encoded form of a term.
By keeping track of the byte offsets before and after decoding a subterm (a pattern captured by decodeWithByteSpan) and if the overall input data is retained then this is enables later retrieving the span of bytes for the subterm.
This captures the pattern of getting the byte offsets before and after decoding a subterm.
!before <- peekByteOffset
x <- decode
!after <- peekByteOffsetCanonical CBOR
https://tools.ietf.org/html/rfc7049#section-3.9
In general in CBOR there can be multiple representations for the same value,
for example the integer 0 can be represented in 8, 16, 32 or 64 bits. This
library always encoded values in the shortest representation but on
decoding allows any valid encoding. For some applications it is useful or
important to only decode the canonical encoding. The decoder primitives here
are to allow applications to implement canonical decoding.
It is important to note that achieving a canonical representation is not
simply about using these primitives. For example consider a typical CBOR
encoding of a Haskell Set data type. This will be encoded as a CBOR list
of the set elements. A typical implementation might be:
encodeSet = encodeList . Set.toList
decodeSet = fmap Set.fromList . decodeListThis does not enforce a canonical encoding. The decoder above will allow
set elements in any order. The use of Set.fromList forgets the order.
To enforce that the decoder only accepts the canonical encoding it will
have to check that the elements in the list are strictly increasing.
Similar issues arise in many other data types, wherever there is redundancy
in the external representation.
The decoder primitives in this section are not much more expensive than their normal counterparts. If checking the canonical encoding property is critical then a technique that is more expensive but easier to implement and test is to decode normally, re-encode and check the serialised bytes are the same.
Decode canonical representation of a Word.
Decode canonical representation of a Word8.
Decode canonical representation of a Word16.
Decode canonical representation of a Word32.
Decode canonical representation of a Word64.
Decode canonical representation of a negative Word.
Decode canonical representation of a negative Word64.
Decode canonical representation of an Int.
Decode canonical representation of an Int8.
Decode canonical representation of an Int16.
Decode canonical representation of an Int32.
Decode canonical representation of an Int64.
Decode canonical representation of a string of bytes as a ByteString.
Decode canonical representation of a textual string as a piece of Text.
Decode canonical representation of a textual string as UTF-8 encoded ByteArray. Note that the result is not validated to be well-formed UTF-8.
Also note that this will eagerly copy the content out of the input to ensure that the input does not leak in the event that the ByteArray is live but not forced.
Decode canonical representation of the length of a list.
Decode canonical representation of the length of a map.
Decode canonical representation of an arbitrary tag and return it as a Word.
Decode canonical representation of an arbitrary 64-bit tag and return it as a Word64.
Decode canonical representation of an Integer.
Decode canonical representation of a half-precision Float.
Decode canonical representation of a Float.
Decode canonical representation of a Double.
Decode canonical representation of a simple CBOR value and give back a
Word8. You probably don't ever need to use this.
Attempt to decode canonical representation of a word with decodeWordCanonical, and ensure the word is exactly as expected, or fail.
Attempt to decode canonical representation of a list length using decodeListLenCanonical, and ensure it is exactly the specified length, or fail.
Sequence operations
2 declarationsDecode an indefinite sequence length.
Decode a sequence length.