CDDL
This section provides an overview of the cbor_serialization CDDL tools.
Parsing
Concise Data Definition Language (CDDL) is a notation to describe CBOR and JSON. It is defined in RFC8610.
The fromCddl API will parse a CDDL and generate Nim types out of it. It can be imported from the cbor_serialization/cddl/type_generator module:
import cbor_serialization, cbor_serialization/cddl/type_generator
It accepts the CDDL as a string, which can be hardcoded or read from a file using staticRead:
fromCddl """
Request = {
cborrpc: text ; CBOR RPC version
method: text ; API method name
params: [* int] ; API parameters
id: int
}
"""
The Request object defined in the CDDL is generated and exported so it can be used from other modules:
let encoded =
Cbor.encode(Request(cborrpc: "2.0", `method`: "subtract", params: @[42, 3], id: 1))
Supported constructs
| CDDL construct | Nim output |
|---|---|
Map types { key: type, ... } | object |
Type choices a / b / c | enum |
Simple type references foo = tstr | type alias |
Array types [* T] | seq[T] |
Table types { * tstr => T } | Table[string, T] |
Optional fields ? key: type | Opt[T] |
Supported types
| CDDL type | Nim output |
|---|---|
any | CborBytes |
uint | uint64 |
int | int64 |
nint (negative int) | int64 |
float32 | float32 |
float64 | float64 |
float | float |
bool | bool |
float16, float16-32 | float32 |
float32-64 | float |
bstr, bytes | seq[byte] |
tstr, text | string |