The writer module contains utilities for implementing custom CBOR output, both when implementing writeValue to provide custom serialization of a type and when streaming CBOR directly without first creating Nim objects.
CBOR values are generally written using writeValue. It is also possible to stream the fields and elements of objects/arrays using the writeArray/writeObject templates - alternatively, the low-level begin{Array,Object} and end{Array,Object} helpers provide fine-grained writing access.
Finally, streamElement can be used when direct access to the stream is needed, for example to efficiently encode a value without intermediate allocations.
Types
CborWriter[Flavor] = object
- Source Edit
Procs
proc beginArray(w: var CborWriter; length = -1) {....raises: [IOError], raises: [], gcsafe.}
- Start writing a Cbor array. Must be closed with a matching endArray. Source Edit
proc beginBytes(w: var CborWriter; length = -1) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc beginObject(w: var CborWriter; length = -1) {....raises: [IOError], raises: [], gcsafe.}
-
Start writing an object, to be followed by fields.
Must be closed with a matching endObject.
See also writeObject.
Use writeField to add fields to the object.
Source Edit proc beginText(w: var CborWriter; length = -1) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc endArray(w: var CborWriter; stopCode = true) {....raises: [IOError], raises: [], gcsafe.}
- Finish writing a Cbor array started with beginArray. Source Edit
proc endBytes(w: var CborWriter; stopCode = true) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc endObject(w: var CborWriter; stopCode = true) {....raises: [IOError], raises: [], gcsafe.}
- Finish writing an object started with beginObject. Source Edit
proc endText(w: var CborWriter; stopCode = true) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc write(w: var CborWriter; val: bool) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc write(w: var CborWriter; val: CborBytes) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc write(w: var CborWriter; val: CborSimpleValue) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc write(w: var CborWriter; val: CborTag) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc write(w: var CborWriter; val: CborVoid) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc write(w: var CborWriter; val: cstring) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc write(w: var CborWriter; val: openArray[char]) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc write(w: var CborWriter; val: seq[byte]) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc write(w: var CborWriter; value: CborNumber) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc write(w: var CborWriter; value: CborObjectType) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc write(w: var CborWriter; value: CborValue) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc write[T: distinct](w: var CborWriter; val: T) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc write[T: object](w: var CborWriter; value: T) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc write[T: range](w: var CborWriter; val: T) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc write[T: SomeFloat](w: var CborWriter; val: T) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc write[T: SomeInteger](w: var CborWriter; val: T) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc write[T: tuple](w: var CborWriter; value: T) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc write[T](w: var CborWriter; val: ref T) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc write[T](w: var CborWriter; values: openArray[T]) {....raises: [IOError], raises: [], gcsafe.}
- Write a collection as a Cbor array. Source Edit
proc writeByte(w: var CborWriter; x: byte) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc writeChar(w: var CborWriter; x: char) {....raises: [IOError], raises: [], gcsafe.}
- Source Edit
proc writeField[V: not void](w: var CborWriter; name: string; value: V) {. ...raises: [IOError], raises: [], gcsafe.}
-
Write a field of an object, i.e., the name followed by the value.
Optional fields may get omitted depending on the Flavor.
Source Edit proc writeIterable(w: var CborWriter; collection: auto) {....raises: [IOError], raises: [], gcsafe.}
- Write each element of a collection as a Cbor array. Source Edit
proc writeName(w: var CborWriter; name: string) {....raises: [IOError], raises: [], gcsafe.}
- Write the name part of the field of an object, to be followed by the value. Source Edit
Templates
template configureCborSerialization(Flavor: type; T: type[enum]; enumRep: static[EnumRepresentation]) {..}
- Configure Cbor serialization for an enum type and flavor with a specific representation. Source Edit
template configureCborSerialization(T: type[enum]; enumRep: static[EnumRepresentation]) {..}
- Configure Cbor serialization for an enum type with a specific representation. Source Edit
template PreferredOutputType(T`gensym0: type Cbor): type {..}
- Source Edit
template shouldWriteObjectField[FieldType](F: type Cbor; field: FieldType): bool {..}
- Template to determine if an object field should be written. Called when omitsOptionalField is enabled - the field is omitted if the template returns false. Source Edit
template streamElement(w: var CborWriter; streamVar: untyped; body: untyped) {..}
-
Write an element giving direct access to the underlying stream - each separate Cbor value needs to be written in its own streamElement block.
Within the streamElement block, do not use writeValue and other high-level helpers as these already perform the element tracking done in streamElement.
Source Edit template write(w: var CborWriter; value: enum) {..}
- Write an enum value as Cbor according to the flavor's enum representation. Source Edit
template writeArray[T: void](w: var CborWriter; body: T) {..}
- Write a Cbor array using a code block for its elements. Source Edit
template writeBytes(w: var CborWriter; body: untyped) {..}
- Source Edit
template writeBytes(w: var CborWriter; length: int; body: untyped) {..}
- Write a Cbor bytes; use writeByte to write the bytes. Source Edit
template writeField[T: void](w: var CborWriter; name: string; body: T) {..}
-
Write a field of an object, i.e., the name followed by the value.
Optional field handling is not performed and must be done manually.
Source Edit template writeObject[T: void](w: var CborWriter; body: T) {..}
- Write a Cbor object using a code block for its fields. Source Edit
template writeObjectField[FieldType, ObjectType](w: var CborWriter; obj: ObjectType; fieldName: static string; field: FieldType) {..}
- Write a field of an object. Source Edit
template writeRecordValue(w: var CborWriter; value: object) {..}
- This exists for nim-serialization integration Source Edit
template WriterType(T`gensym0: type Cbor; F`gensym0: distinct type = Flavor`gensym0): type {..}
- Source Edit
template writeText(w: var CborWriter; body: untyped) {..}
- Source Edit
template writeText(w: var CborWriter; length: int; body: untyped) {..}
- Write a Cbor text; use writeChar to write the characters. Source Edit
template writeValue[U: Writer(Cbor); W: array](w`gensym47: var U; value`gensym47: W) {..}
- Source Edit
template writeValue[U: Writer(Cbor); W: bool](w`gensym12: var U; value`gensym12: W) {..}
- Source Edit
template writeValue[U: Writer(Cbor); W: CborBytes](w`gensym101: var U; value`gensym101: W) {..}
- Source Edit
template writeValue[U: Writer(Cbor); W: CborNumber](w`gensym73: var U; value`gensym73: W) {..}
- Source Edit
template writeValue[U: Writer(Cbor); W: CborSimpleValue](w`gensym96: var U; value`gensym96: W) {..}
- Source Edit
template writeValue[U: Writer(Cbor); W: CborTag](w`gensym78: var U; value`gensym78: W) {..}
- Source Edit
template writeValue[U: Writer(Cbor); W: CborValueRef](w`gensym88: var U; value`gensym88: W) {..}
- Source Edit
template writeValue[U: Writer(Cbor); W: CborVoid](w`gensym83: var U; value`gensym83: W) {..}
- Source Edit
template writeValue[U: Writer(Cbor); W: cstring](w`gensym52: var U; value`gensym52: W) {..}
- Source Edit
template writeValue[U: Writer(Cbor); W: distinct](w`gensym67: var U; value`gensym67: W) {..}
- Source Edit
template writeValue[U: Writer(Cbor); W: enum](w`gensym27: var U; value`gensym27: W) {..}
- Source Edit
template writeValue[U: Writer(Cbor); W: object](w`gensym107: var U; value`gensym107: W) {..}
- Source Edit
template writeValue[U: Writer(Cbor); W: openArray](w`gensym57: var U; value`gensym57: W) {..}
- Source Edit
template writeValue[U: Writer(Cbor); W: ptr](w`gensym22: var U; value`gensym22: W) {..}
- Source Edit
template writeValue[U: Writer(Cbor); W: range](w`gensym62: var U; value`gensym62: W) {..}
- Source Edit
template writeValue[U: Writer(Cbor); W: ref](w`gensym17: var U; value`gensym17: W) {..}
- Source Edit
template writeValue[U: Writer(Cbor); W: seq](w`gensym42: var U; value`gensym42: W) {..}
- Source Edit
template writeValue[U: Writer(Cbor); W: SomeFloat](w`gensym37: var U; value`gensym37: W) {..}
- Source Edit
template writeValue[U: Writer(Cbor); W: SomeInteger](w`gensym32: var U; value`gensym32: W) {..}
- Source Edit
template writeValue[U: Writer(Cbor); W: string](w`gensym7: var U; value`gensym7: W) {..}
- Source Edit
template writeValue[U: Writer(Cbor); W: TT`gensym87](w`gensym93: var U; value`gensym93: W) {..}
- Source Edit
template writeValue[U: Writer(Cbor); W: tuple](w`gensym112: var U; value`gensym112: W) {..}
- Source Edit
Exports
-
defaultBuiltinWriter, allowsUnknownFields, createCborFlavor, defaultReader, defaultPrimitiveWriter, defaultPrimitiveSerialization, defaultObjectWriter, requiresAllFields, EnumRepresentation, mimeType, Cbor, omitsOptionalFields, flavorEnumRep, supports, defaultWriters, versionInt, defaultObjectReader, skipsNullFields, defaultReaders, defaultPrimitiveReader, defaultSerialization, enumRep, enumRep, enumRep, defaultObjectSerialization, flavorEnumRep, defaultBuiltinSerialization, enumRep, defaultBuiltinReader, defaultSerialization, defaultWriter, isUndefined, isTrue, CborNumber, CborMajor, cborFalse, toInt, ==, $, CborError, CborSign, ==, cborMinorLen8, cborMinorLen0, defaultCborReaderConf, cborMinorLen1, contains, cborNull, cborTrue, cborMinorLens, CborValue, cborMinorIndef, CborTag, cborMinorLen2, CborSimpleValue, ==, toInt, toBytes, add, cborMinorLen4, CborValueKind, cborUndefined, ==, isFalsy, ==, isNullish, isNull, CborReaderConf, isFalse, CborValueRef, toInt, CborBytes, CborVoid, cborBreakStopCode, CborObjectType