Home

Awesome

FlexBuffers-CSharp

FlexBuffers is JSON comparable binary format with random value access capabilities. The binary format and data layout was designed at Google as part of FlatBuffers project. This projects brings FlexBuffers as a standalone solution to C# with the focus to convert large JSON files to randomly accessable FlexBuffers.

The main focus of this project is to be used with Unity3D. However current solution has no Unity3D dependencies and should be usable in other C# environments. Please feel free to contribute tests and patches for other environments.

Supported types

As mentioned in the project description, FlexBuffers is a JSON comparable data format. It supports all the types JSON does, but also stores more detailed informations about the values.

Here is the list of supported types, as of November 2019:

Creation of FlexBuffer

There are multple ways how we can create a FlexBuffer

Single value

It is posible to store just one value in a FlexBuffer. It is not that probable to do so in day to day business, but was helpful for unit testing the format.

From JSON

FlexBuffers-CSharp has a special type called JsonToFlexBufferConverter which allows user to convert a JSON string to FlexBuffer byte array.

var buffer = JsonToFlexBufferConverter.Convert("{\"a\":1, \"b\":2}");

JsonToFlexBufferConverter Contains an actualt JSON parser (based on LightJson) and there for converts one byte array (JSON string) directly to another byte aray (FlexBuffer) efficient and with a minimal amount of temporary objects.

FlexBuffer Builder

The FlexBufferBuilder enable users to create FlexBuffers directly form values, without a need for temporary representations. The Root elelemnt can be defined as a Map:

var bytes = FlexBufferBuilder.Map(root =>
{
    root.Add("name", "Maxim");
    root.Add("age", 38);
    root.Add("weight", 72.5);
    root.Map("address", address =>
    {
        address.Add("city", "Bla");
        address.Add("zip", "12345");
        address.Add("countryCode", "XX");
    });
    root.Vector("flags", tags =>
    {
        tags.Add(true);
        tags.Add(false);
        tags.Add(true);
        tags.Add(true);
    });
});

Or as a Vector:

var bytes = FlexBufferBuilder.Vector(root =>
{
    root.AddNull();
    root.Add(new byte[]{1,2,3});
    root.Map(map =>
    {
        map.AddNull("a");
        map.Add("b", new byte[]{3,4,5});
    });
});

FlexBuffers to JSON

As Flexbuffer has JSON compatible types it is very easy to conver a FlexBuffer to JSON.

The FlexBuffer from previous paragraph can be converted to following JSON string: [null,"AQID",{"a":null,"b":"AwQF"}] "AQID" and "AwQF" are Base64 representations of new byte[]{1,2,3} and new byte[]{3,4,5}

Byte array to FlexBuffer value

With FlexBuffers we can extract values directly from the buffer, without any parsing or complex upfront conversions. With var flx = FlxValue.FromBytes(bytes); users can create an instance of a FlexBuffer value which allows conversion to types and access of sub elements. FlxValue struct has following getters:


Outlook

As next steps we are cosnidering:

Contribution is welcome.