All Projects → MrSmith33 → cbor-d

MrSmith33 / cbor-d

Licence: BSL-1.0 license
Concise Binary Object Representation (CBOR) binary data format for D language

Programming Languages

d
599 projects

Projects that are alternatives of or similar to cbor-d

Dahomey.Cbor
High-performance CBOR (RFC 7049) serialization framework for .Net (C#)
Stars: ✭ 47 (+135%)
Mutual labels:  serialization, cbor
Libcbor
CBOR protocol implementation for C
Stars: ✭ 215 (+975%)
Mutual labels:  serialization, cbor
Kotlinx.serialization
Kotlin multiplatform / multi-format serialization
Stars: ✭ 3,550 (+17650%)
Mutual labels:  serialization, cbor
Cbor
CBOR RFC 7049 (Go/Golang) - safe & fast with standard API + toarray & keyasint, CBOR tags, float64/32/16, fuzz tested.
Stars: ✭ 250 (+1150%)
Mutual labels:  serialization, cbor
Borer
Efficient CBOR and JSON (de)serialization in Scala
Stars: ✭ 131 (+555%)
Mutual labels:  serialization, cbor
SwiftCBOR
A CBOR implementation for Swift
Stars: ✭ 95 (+375%)
Mutual labels:  serialization, cbor
laravel5-hal-json
Laravel 5 HAL+JSON API Transformer Package
Stars: ✭ 15 (-25%)
Mutual labels:  serialization
cddl
Concise data definition language (RFC 8610) implementation and JSON and CBOR validator in Rust
Stars: ✭ 55 (+175%)
Mutual labels:  cbor
jsonrec
JSON parser/encoder "type spec"-based code-generator
Stars: ✭ 13 (-35%)
Mutual labels:  serialization
NanoCBOR
CBOR library aimed at heavily constrained devices
Stars: ✭ 32 (+60%)
Mutual labels:  cbor
Maat
Validation and transformation library powered by deductive ascending parser. Made to be extended for any kind of project.
Stars: ✭ 27 (+35%)
Mutual labels:  serialization
hprose-as3
Hprose for ActionScript 3.0
Stars: ✭ 18 (-10%)
Mutual labels:  serialization
vallang
Generic immutable recursive data representation API targeted at source code models and more.
Stars: ✭ 28 (+40%)
Mutual labels:  serialization
BinaryLove3
Simple C++ 20 Serialization Library that works out of the box with aggregate types!
Stars: ✭ 13 (-35%)
Mutual labels:  serialization
jzon
A correct and safe JSON parser.
Stars: ✭ 78 (+290%)
Mutual labels:  serialization
protoc-plugin
A protoc compiler plugin for Clojure applications
Stars: ✭ 28 (+40%)
Mutual labels:  serialization
hs-packer
Fast serialization in haskell
Stars: ✭ 13 (-35%)
Mutual labels:  serialization
serialize-json
A serialize algorithm for JSON
Stars: ✭ 22 (+10%)
Mutual labels:  serialization
kdl4j
KDL Parser for the JVM
Stars: ✭ 16 (-20%)
Mutual labels:  serialization
bookshelf-advanced-serialization
Plugin for Bookshelf.js supporting advanced serialization behavior
Stars: ✭ 17 (-15%)
Mutual labels:  serialization

Concise Binary Object Representation (CBOR) for D language Build Status

Implementation of CBOR for D programming language.

Supported features

  1. Encoding
  • integers - ubyte ushort uint ulong byte short int long;
  • floating numbers - float, double;
  • boolean - bool;
  • fixed length byte strings - ubyte[];
  • fixed length UTF-8 strings - string;
  • fixed length arrays aka tuples. Items can be of any type;
  • fixed length maps. Pairs of key/value of any type - K[V];
  • Aggregate encoding. Structs, tuples and classes - class, Tuple, struct. Can be encoded as both arrays and maps;
  • null values - null;
  • Tags;
  • Simple values
  1. Decoding
  • Integers;
  • Floats (half, float, double);
  • Arrays, maps, string, byte strings;
  • Aggregates can only be decoded from arrays now;
  • null values can be decoded as classes, arrays, maps, string;
  • All unused simple values will cause CborException to be thrown;
  • Streaming support (i.e. Indefinite-Length Arrays, Maps, Byte Strings and Text Strings)
  • Tags produce corresponding token.

Restrictions

  • While aggregates can be encoded as maps, they can not be decoded as such, only from arrays.

API

CBOR encoding

size_t encodeCborInt (sink, value)
size_t encodeCborFloat (sink, value)
size_t encodeCborBool (sink, value)
size_t encodeCborNull (sink, value)
size_t encodeCborUndefined (sink, value)
size_t encodeCborBreak (sink, value)
size_t encodeCborSimple (sink, value)

// version without length encodes indefinite-length header.
size_t encodeCborBytesHeader(sink [, length])
size_t encodeCborStringHeader(sink [, length])
size_t encodeCborArrayHeader(sink [, length])
size_t encodeCborMapHeader(sink [, length])

size_t encodeCborBytesItems (sink, bytes)

size_t encodeCborTag (sink, value)

Serialization

size_t encodeCbor <flatten> (sink, value)
size_t encodeCborBytes <flatten> (sink, value)
size_t encodeCborString <flatten> (sink, value)
size_t encodeCborArray <flatten> (sink, value)
size_t encodeCborMap <flatten> (sink, value)
size_t encodeCborAggregate <withFieldName, flatten> (sink, value)

@ignore attribute

CBOR Decoding

enum CborTokenType {
	arrayHeader
	arrayIndefiniteHeader
	mapHeader
	mapIndefiniteHeader
	bytesHeader
	bytesIndefiniteHeader
	textHeader
	textIndefiniteHeader
	undefined
	nil
	boolean
	tag
	simple
	breakCode
	posinteger
	neginteger
	floating
}
struct CborToken {
	CborTokenType type;
	union {
		bool boolean;
		long integer;
		double floating;
		ulong uinteger;
	}
}

CborToken decodeCborToken (input)

Deserialization

void decodeCbor <dup, flatten> (input, ref outValue)
T decodeCborSingle <T, flatten> (input)
T decodeCborSingleDup <T, flatten> (input)

ubyte[] readBytes (input, length)
void printCborStream <indent="  "> (input, sink = stdout, numItems = ulong.max, indent = "")

Usage example

	import cbor;

	static struct Inner
	{
		int[] array;
		string someText;
	}

	static struct Test
	{
		ubyte b;
		short s;
		uint i;
		long l;
		float f;
		double d;
		ubyte[] arr;
		string str;
		Inner inner;
		@ignore long ignored; // not encoded

		void fun(){} // not encoded
		void* pointer; // not encoded
		int* numPointer; // not encoded
	}

	import std.array : appender;
	auto buffer = appender!(ubyte[])();

	Test test = Test(42, -120, 111111, -123456789, 0.1234, -0.987654,
		cast(ubyte[])[1,2,3,4,5,6,7,8], "It is a test string",
		Inner([1,2,3,4,5], "Test of inner struct"), 88);

	encodeCbor(buffer, test);

	// ubyte[] and string types are slices of input ubyte[].
	Test result = decodeCborSingle!Test(buffer.data);
	// or
	// Test result;
	// decodeCbor(buffer.data, result);

	// decodeCborSingleDup can be used to auto-dup those types.

	assert(result.ignored == 0);
	result.ignored = test.ignored;
	assert(test == result);


	struct Vector4f
	{
		float x, y, z, w;
	}

	// encoded as array of 4 floats
	encodeCbor(buffer, Vector4f(1,2,3,4));
	buffer.clear();

	// Flat encoding
	// encoded as 4 floats
	encodeCbor!(Yes.Flatten)(buffer, Vector4f(1,2,3,4));

	// You need to use Yes.Flatten on both sides.
	decodeCborSingle!(Vector4f, Yes.Flatten)(buffer.data);

	// Printing cbor data (by default into stdout)
	printCborStream(buffer.data);
	// prints:
	// (floating, 1)
	// (floating, 2)
	// (floating, 3)
	// (floating, 4)

	// you can also use other sink
	auto textbuffer = appender!(char[])();
	printCborStream(buffer.data, textbuffer);

For more examples see unittests.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].