Class CborSerializer

All Implemented Interfaces:
Function<Object,byte[]>, ThrowingFunction<Object,byte[]>, CborMetaProvider, ArrayRecordWritable, RecordWritable, TokenWritable
Direct Known Subclasses:
CborSerializer.Base64, CborSerializer.SpacedHex

Serializes POJO models to CBOR (RFC 8949).
Media types:
Handles Accept types: application/cbor

Produces Content-Type types: application/cbor

Description

Java-to-CBOR type mapping:

  • Maps and beans → CBOR map (major type 5)
  • Collections and arrays → CBOR array (major type 4)
  • String / Enum → CBOR text string (major type 3, UTF-8)
  • Boolean → CBOR simple values 20/21 (0xF4/0xF5)
  • Number (int, long, float, double) → CBOR compact integers (major types 0/1) or IEEE 754 floats (major type 7)
  • Date / Calendar / Instant → ISO 8601 string
  • Duration → ISO 8601 duration string
  • byte[] → CBOR byte string (major type 2)
  • null → CBOR simple value 22 (0xF6)
  • All other types → fallback to toString() as string
Behavior-specific subclasses:
Example:

// Serialize a bean to CBOR bytes using the default serializer byte[] cbor = CborSerializer.DEFAULT.write(myBean); // Custom serializer with bean type properties enabled CborSerializer s = CborSerializer.create().addBeanTypes().build(); byte[] cbor = s.write(myBean);

Round-trip notes:
  • BigInteger — emitted losslessly as a native CBOR integer when it fits CBOR's integer range (-2^64 .. 2^64-1), otherwise as a lossless decimal string.
  • BigDecimal — emitted as a lossless decimal string (native CBOR has no decimal number head without semantic tags).
  • The high-level databind path emits definite-length containers; the streaming CborTokenWriter emits indefinite-length containers (both are valid RFC 8949).
  • Half-precision float (0xF9) not produced; always uses float32 (0xFA) or float64 (0xFB).
  • Semantic tags (major type 6) not emitted unless useTags(true).
Notes:
  • This class is thread safe and reusable.
See Also: