hildjj / node-cbor

Encode and decode CBOR documents, with both easy mode, streaming mode, and SAX-style evented mode.
MIT License
357 stars 73 forks source link

Encode an indefinite map inside a map #120

Closed janpoltan closed 3 years ago

janpoltan commented 3 years ago

How do I set to encode the map inside a map to be an indefinite map? It seem it wont encode as an indefinite map even i set up the encoder to cbor.Encoder.encodeIndefinite. It only encodes the parent Map as the indefinite length.

const cbor = require('cbor');

const parentMap = new Map();
parentMap.set(0, 1);

const childMap = new Map();
childMap.set(0, 1)

parentMap.set(1,childMap);

parentMap.encodeCBOR = cbor.Encoder.encodeIndefinite;
const hex = cbor.encode(parentMap).toString('hex');

console.log(hex);

// Outputs
// BF       # map(*)
//    00    # unsigned(0)
//    01    # unsigned(1)
//    01    # unsigned(1)
//    A1    # map(1)
//       00 # unsigned(0)
//       01 # unsigned(1)
//    FF    # primitive(*)

// Should be

// BF       # map(*)
//    00    # unsigned(0)
//    01    # unsigned(1)
//    01    # unsigned(1)
//    BF    # map(*)  <------ Indefinite Map
//       00 # unsigned(0)
//       01 # unsigned(1)
//       FF # primitive(*)
//    FF    # primitive(*)
janpoltan commented 3 years ago

Never mind, i need to add the indefinite encoder to the child map.

childMap.encodeCBOR = cbor.Encoder.encodeIndefinite;