Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 constructNim output
Map types { key: type, ... }object
Type choices a / b / cenum
Simple type references foo = tstrtype alias
Array types [* T]seq[T]
Table types { * tstr => T }Table[string, T]
Optional fields ? key: typeOpt[T]

Supported types

CDDL typeNim output
anyCborBytes
uintuint64
intint64
nint (negative int)int64
float32float32
float64float64
floatfloat
boolbool
float16, float16-32float32
float32-64float
bstr, bytesseq[byte]
tstr, textstring