CborEncode()
Encodes a CFML value into CBOR (Concise Binary Object Representation) binary format. CBOR is like JSON but binary — smaller, faster to parse, and supports types JSON can't (like raw binary data).
Accepts structs, arrays, strings, numbers, binary data, and booleans.
Requires Extension: Crypto Extension
CborEncode( data=any );
Returns: binary
| Argument | Description |
|---|---|
|
data
any,
required
|
edit
CFML value to encode (struct, array, string, number, binary, boolean) |
Usage Notes
editStruct key handling: CFML struct keys are always strings, but CBOR distinguishes between string keys and integer keys. CborEncode automatically converts numeric-looking string keys (like "1", "-7") to CBOR integer keys. This means COSE key structs roundtrip correctly without any extra work.
The tradeoff: if you genuinely need a CBOR text key "1" (the string, not the integer), it will be encoded as integer 1 instead. This is fine for COSE/WebAuthn use cases but worth knowing if you're working with general-purpose CBOR maps that mix text and integer keys.
Binary data: CFML binary values are encoded as CBOR byte strings. This is more efficient than JSON's approach of base64-encoding binary data inside a string.
Examples
edit// Encode a struct
cbor = CborEncode( { name: "Zac", age: 42 } );
// cbor is binary data
// Encode nested data
cbor = CborEncode( {
users: [
{ name: "Alice", scores: [ 10, 20, 30 ] },
{ name: "Bob", scores: [ 40, 50 ] }
],
count: 2
} );
// Encode raw binary data (JSON can't do this — it would need base64)
payload = charsetDecode( "binary payload", "UTF-8" );
cbor = CborEncode( payload );
// Roundtrip: encode then decode gets you back the original data
original = { key: "value", numbers: [ 1, 2, 3 ] };
decoded = CborDecode( CborEncode( original ) );
// decoded.key == "value"
// Structs with numeric-looking keys are encoded as CBOR integer keys
// This makes COSE key roundtrips work automatically
coseKey = {};
coseKey[ "1" ] = 2; // kty = EC
coseKey[ "-1" ] = 1; // crv = P-256
cbor = CborEncode( coseKey );
// CBOR map has integer keys 1 and -1 (not string keys)
See also
- Cryptography
- CborDecode()
- JsonToCbor()
- KeyToCose()
- Search Issue Tracker open_in_new
- Search Lucee Test Cases open_in_new (good for further, detailed examples)