voile.cbor

CBOR data module
This module provides functionality for working with CBOR (Concise Binary Object Representation) data. It includes definitions for various CBOR data types, a builder for constructing CBOR values, and methods for converting between CBOR and native D types.
See Also: RFC 7049
enum auto isBinary(T);
Determines if the type is binary
auto kind(string name, string value);
auto kind(string value);
auto kind(string value)();
Kind attribute
Attribute used for serialization of SumType. By adding this attribute to all aggregate types used in SumType, they become serializable.
auto converter(T1, T2)(void function(in T2, ref T1) from, void function(in T1, ref T2) to);
auto converter(T1, T2)(T1 function(T2) from, T2 function(in T1) to);
auto converterString(T)(T function(string) from, string function(in T) to);
alias convStr = converterString(T)(T function(string) from, string function(in T) to);
auto converterBinary(T)(T function(immutable(ubyte)[]) from, immutable(ubyte)[] function(in T) to);
alias convBin = converterBinary(T)(T function(immutable(ubyte)[]) from, immutable(ubyte)[] function(in T) to);
Attribute converting method
auto converterSysTime();
auto converterDateTime();
auto converterDate();
auto converterTimeOfDay();
auto converterUUID();
auto converterDuration();
Special conveter attributes
enum auto isSerializableTuple(T);
Determines if the Tuple can be serialized to CBOR format
This template returns true if the type T meets the following conditions:
  • T is a Tuple
  • All members of the Tuple are serializable
Parameters:
T The type to check
Returns: true if T is a serializable sum type, false otherwise
enum auto isSerializableSumType(T);
Determines if the SumType can be serialized to CBOR format
This template returns true if the type T meets the following conditions:
  • T is a SumType
  • All members of the SumType are serializable
  • All members of the SumType have the @kind attribute if they are aggregate types
  • There is at most one member of each of the following types: integral, floating point, boolean, string, binary, array, associative array
Parameters:
T The type to check
Returns: true if T is a serializable sum type, false otherwise
Examples: ditto
@kind("test1") struct Test1 { int value; }
@kind("test2") struct Test2 { int value; }
alias ST1 = SumType!(Test1, Test2);
static assert(allSatisfy!(hasKind, ST1.Types));
static assert(allSatisfy!(isSerializable, ST1.Types));

struct Test3 { int value; }
alias ST2 = SumType!(Test1, Test2, Test3);
static assert(!allSatisfy!(hasKind, ST2.Types));
static assert(!isSerializableSumType!ST2);
static assert(!isSerializableData!ST2);
static assert(!isSerializable!ST2);

alias ST3 = SumType!(int, ulong, string, immutable(ubyte)[]);
static assert(!isSerializableSumType!ST3);
static assert(!isSerializable!ST3);
template isSerializable(T)
Checks if a given type is serializable to CBOR format.
This function determines whether a type can be serialized into the CBOR (Concise Binary Object Representation) format. CBOR is a binary data serialization format which aims to provide a more compact representation compared to JSON.
Parameters:
T The type to check for serializability.
Returns: bool - true if the type is serializable to CBOR, false otherwise.
struct Builder;
The Builder struct is used to generate and manipulate CBOR (Concise Binary Object Representation) data.
  • Use the make method to create a CborValue.
  • Use the parse method to convert binary data to a CborValue.
  • Use the build method to convert a CborValue to binary format.
  • The serialize and deserialize methods support ORM (Object-Relational Mapping).
struct CborValue;
CborValue
enum Type: ubyte;
@trusted this(T)(T value, ref Builder builder);
Constructor
pure nothrow @safe Type type() const;
Type getter
pure ref @trusted CborValue opAssign(T)(T value) return;
Assign operator
pure nothrow @safe bool isNull() const;
Null check
pure nothrow @safe bool isUndefined() const;
Undefined check
pure nothrow @trusted bool isOverflowedInteger() const;
Overflow number check
CBOR supports signed integers, but the range it can handle differs from D's long type. This function determines whether a CBOR signed integer exceeds the range of D's long type. Specifically, the range of CBOR integers is -ulong.max(-1-2^64) to ulong.max(2^64). In contrast, the range of D's long type is long.min(-1-2^63) to long.max(2^63), and the range of the ulong type is 0 to ulong.max(2^64).
nothrow @safe T get(T)(lazy T defaultValue = T.init) const;
Retrieve the value as a D language type
Since the types handled by CBOR may not match the types in the D language, conversion is performed. Specify the type with T or a default value, and return the converted type. For example, if the long type is specified, CBOR's positive and negative integers, as well as floating-point numbers, are cast and converted to the long type. Strings are also parsed and converted to integers.
nothrow ref T require(T, K)(K key);
nothrow T getValue(T, K)(K key, lazy T defaultValue = T.init) const;
pure nothrow ref @safe Builder builder();
Get the builder instance.
nothrow T getValueAt(T)(size_t idx, lazy T defaultValue = T.init) const;
@safe CborValue make(T)(T value);
Create a CborValue from a given value.
pure nothrow @nogc @safe void dispose(ref CborValue v);
Dispose the given CborValue.
pure nothrow @safe CborValue emptyArray();
Create a CborValue of empty array.
pure nothrow @safe CborValue emptyMap();
Create a CborValue of empty map.
pure nothrow @safe CborValue nullValue();
Create a CborValue of null.
pure nothrow @safe CborValue undefinedValue();
Create a CborValue of undefined.
pure @safe CborValue deepCopy(CborValue src);
Deep copy a CborValue.
Examples:
Builder b1;
Builder b2;
immutable str = "123";
auto v1 = b1.make(str);
assert(v1.get!string is str);
auto v2 = b1.deepCopy(v1);
assert(v2.get!string is str);
// Check if the copied value is a new instance
auto v3 = b2.deepCopy(v1);
assert(v3.get!string !is v1.get!string);
@safe size_t parse(ref CborValue dst, immutable(ubyte)[] src);
@safe bool parse(OutputRange)(ref OutputRange dst, immutable(ubyte)[] src);
@safe CborValue parse(ref immutable(ubyte)[] src);
@trusted CborValue parse(ref ubyte[] src);
Parse a binary CBOR data to a CborValue.
Examples:
Builder b;
immutable(ubyte)[] data = [0x63, 'f', 'o', 'o', 0x43, 0x01, 0x02, 0x03];
auto v1 = b.parse(data);
assert(v1.get!string == "foo");
assert(data == [0x43, 0x01, 0x02, 0x03]);
auto v2 = b.parse(data);
assert(v2.get!(immutable(ubyte)[]) == [1, 2, 3]);
assert(data.length == 0);
@safe void build(OutputRange)(ref OutputRange dst, CborValue src)
if(isOutputRange!(OutputRange, ubyte));
@safe immutable(ubyte)[] build(CborValue src);
Build a CborValue to a binary CBOR data.
Examples:
Builder b;
auto v1 = b.make("foo");
auto data = b.build(v1);
assert(data == [0x63, 'f', 'o', 'o']);
@safe CborValue serialize(T)(T value);
Serialize a various data to CborValue.
The serialize function generates a CborValue instance from the given data. The data type must be one of the following.
  • CborValue
  • Integral type (int, uint, long, ulong, short, ushort, byte, ubyte)
  • Floating point type (float, double)
  • bool
  • string
  • binary (immutable(ubyte)[])
  • null
  • Array
    • Recursively serialized
  • AssociativeArray
    • Recursively serialized
  • SumType
    • Converted to a map type CborValue
      • All types have the @kind attribute
    • Recursively serialized
  • Aggregate type (struct, class, union): meets one of the following conditions
    • Composed of simple public member variables
      • Converted to a map type CborValue
      • Recursively serialized
      • If the @ignore attribute is present, do not serialize
      • If the @ignoreIf attribute is present, do not serialize if the condition is met
      • If the @name attribute is present, use that name
      • If the @value attribute is present, use that value
      • If the @converter attribute is present, use that conversion proxy
    • toCbor/fromCbor methods, where fromCtor is a static method
    • toBinary/fromBinary methods, where fromBinary is a static method
    • toRepresentation/fromRepresentation methods, where fromRepresentation is a static method
Examples:
Builder b;
struct Foo
{
	int x;
	string y;
}
static assert(isSerializable!Foo);
auto foo = Foo(42, "foo");
auto v = b.serialize(foo);
assert(v.getValue!int("x") == 42);
assert(v.getValue!string("y") == "foo");
@safe bool deserialize(T)(in CborValue src, ref T dst);
@safe T deserialize(T)(in CborValue src);
Deserialize a CborValue to a various data.
The deserialize function generates value from CborValue instance. The data type must be one of the following.
  • CborValue
  • Integral type (int, uint, long, ulong, short, ushort, byte, ubyte)
  • Floating point type (float, double)
  • bool
  • string
  • binary (immutable(ubyte)[])
  • null
  • Array
    • Recursively deserialized
  • AssociativeArray
    • Recursively deserialized
  • SumType
    • Converted from a map type CborValue
      • All types have the @kind attribute
    • Recursively deserialized
  • Aggregate type (struct, class, union): meets one of the following conditions
    • Composed of simple public member variables
      • Converted to a map type CborValue
      • Recursively deserialized
      • If the @ignore attribute is present, do not deserialize
      • If the @ignoreIf attribute is present, do not deserialize if the condition is met
      • If the @name attribute is present, use that name
      • If the @converter attribute is present, use that conversion proxy
      • If the @essential attribute is present, throw an exception if deserialization fails
    • toCbor/fromCbor methods, where fromCtor is a static method
    • toBinary/fromBinary methods, where fromBinary is a static method
    • toRepresentation/fromRepresentation methods, where fromRepresentation is a static method
Examples:
Builder b;
struct Foo
{
	int x;
	string y;
}
static assert(isSerializable!Foo);
auto v = b.deserialize!Foo(b.make(["x": b.make(42), "y": b.make("foo")]));
assert(v.x == 42);
assert(v.y == "foo");
alias CborValue = Builder.CborValue;
alias CborType = Builder.CborValue.Type;
CBOR data type
alias CBOR = Builder;
alias CborBuilder = Builder;
CBOR builder
@trusted CborValue parseCBOR(immutable(ubyte)[] binary);
Perse CBOR data
@trusted immutable(ubyte)[] toCBOR(CborValue cv);
Perse CBOR data