CBORious is a fast, standards-compliant CBOR (Concise Binary Object Representation) library for Nim 2.x. It offers streaming, deterministic and canonical encodings, and compile-time derivation of serializers inspired by msgpack4nim.
- Streaming reader and writer
- Deterministic and canonical encoding modes following RFC 8949
- Compile-time encode/decode derivation for Nim types
- Liberal decode mode for interoperability
Requires Nim >= 2.0.14.
atlas use https://github.com/elcritch/cborious
nimble install https://github.com/elcritch/cborious Encode and decode values using the high-level helpers:
import cborious
type Msg = object
greeting: string
value: int
let enc = toCbor(Msg(greeting: "hi", value: 42))
let dec = fromCbor(enc, Msg)
assert dec.value == 42Use CborStream for incremental reads and writes with pack and unpack:
import cborious
var s = CborStream.init()
let data = [1, 2, 3]
pack(s, data) # encode into the stream's buffer
s.setPosition(0) # rewind for reading
let out = unpack(s, seq[int])
assert out == dataCBOR tags attach semantic meaning to the next value. Types can define a
cborTag proc to associate a numeric tag with the type; the tag is emitted on
encode and verified or skipped on decode. Unknown tags are ignored.
import cborious
type Foo = object
bar: int
proc cborTag(_: typedesc[Foo]): CborTag = 3.CborTag
var cs = CborStream.init()
pack(cs, Foo(bar: 7))
cs.setPosition(0)
let foo = unpack(cs, Foo)
assert foo.bar == 7EncodingMode flags control how values are written or read. The default is
{CborObjToArray, CborCheckHoleyEnums}:
CborObjToArray: encode Nim objects as positional arraysCborObjToMap: encode Nim objects as maps with string keysCborObjToStream: emit object fields sequentially without a container for streamingCborCanonical: enforce RFC 8949 canonical form (minimal encodings, sorted map keys)CborEnumAsString: encode enums by name instead of ordinalCborCheckHoleyEnums: reject undefined enum values during decodeCborSelfDescribe: prefix the self-describe CBOR tag (55799)
Run the full test suite:
nim testGenerate API docs into docs/api:
nim docsLicensed under the Apache-2.0 license. See cborious.nimble for details.