Jump to content

User:Kroggen/sandbox

From Wikipedia, the free encyclopedia
Binn
Operating systemAny
PlatformCross-platform
TypeData interchange
LicenseApache 2.0
Websitegithub.com/liteserver/binn

Binn is a computer data serialization format used mainly for application data transfer. It stores primitive data types and data structures in a binary form.[1]

Performance[edit]

The Binn format is designed to be compact and fast on readings. The elements are stored with theirs sizes to increase the read performance. The strings are null terminated so when read the library returns a pointer to them inside the buffer, avoiding memory allocation and data copying, an operation knwon as zero-copy.

Data types[edit]

Primitive data types:

  • null
  • boolean (true and false)
  • integer (up to 64 bits signed or unsigned)
  • floating point numbers (IEEE single/double precision)
  • string
  • blob (binary data)
  • User defined

Containers:

Format[edit]

Binn structures consist of a list of elements. Each element has a type that can be followed by the size, count of internal items, and the data itself:

boolean, null:
[type]

int, float (storage: byte, word, dword or qword):
[type][data]

string, blob:
[type][size][data]

list, object, map:
[type][size][count][data]

Example Encoding[edit]

A json data such as {"hello":"world"} is serialized in binn with the same size:

  \xE2                               // type = object (container)
  \x11                               // container total size
  \x01                               // items in the container (key/value pairs in this case)
  \x05hello                          // field name
  \xA0                               // type = string
  \x05world\x00                      // field value (null terminated)

Example Code[edit]

Writing to a list in C:

  binn *list;

  // create a new list
  list = binn_list();

  // add values to it
  binn_list_add_int32(list, 123);
  binn_list_add_double(list, 2.55);
  binn_list_add_str(list, "testing");

  // send over the network or save to a file...
  send(sock, binn_ptr(list), binn_size(list));

  // release the buffer
  binn_free(list);

Reading from that list:

  int_value = binn_list_int32(ptr, 1);
  dbl_value = binn_list_double(ptr, 2);
  str_value = binn_list_str(ptr, 3);

See also[edit]

References[edit]

External links[edit]

Category:Data serialization formats