CborSimpleCodec class

A simple codec for CBOR, using CborSimpleEncoder and CborSimpleDecoder.

To see how CBOR values are transformed from and into objects, verify the documentation of both CborValue.CborValue and CborValue.toObject.

JavaScript (dart2js) Limitations

On JavaScript (dart2js), there is no distinction between integer and floating-point numbers at the type level. All numbers are IEEE 754 doubles. This means:

  • 0.0 is int returns true on JS but false on native/WASM platforms
  • Integer-valued doubles like 1.0, -4.0, 65504.0 are encoded as CBOR integers instead of CBOR floats
  • Very large doubles (e.g., 1.0e+300) may lose precision when treated as integers

Note: This limitation does not apply to dart2wasm, which correctly distinguishes between integers and floats.

Workaround: To ensure consistent float encoding across all platforms, use the advanced API with explicit types:

// Instead of:
cbor.encode(1.0);  // Encoded as int on JS, float on native/WASM

// Use:
cborEncode(CborFloat(1.0));  // Always encoded as float
Inheritance

Constructors

CborSimpleCodec({bool encodeDateTimeEpoch = false, bool parseDateTime = true, bool decodeBase64 = true, bool parseUri = true, Object? toEncodable(dynamic object)?})
Create a CBOR simple codec.
const

Properties

decoder → CborSimpleDecoder
Returns the decoder of this, converting from T to S.
no setteroverride
encoder → CborSimpleEncoder
Returns the encoder from S to T.
no setteroverride
hashCode → int
The hash code for this object.
no setterinherited
inverted → Codec<List<int>, Object?>
Inverts this.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

decode(List<int> encoded, {bool? parseDateTime, bool? decodeBase64, bool? parseUri}) → Object?
Decodes encoded data.
override
encode(Object? input, {bool? dateTimeEpoch, Object? toEncodable(dynamic object)?}) → List<int>
Encodes input.
override
fuse<R>(Codec<List<int>, R> other) → Codec<Object?, R>
Fuses this with other.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited