When converting flatbuffers object to JSON, how to deal with binary data?

794 Views Asked by At

Let's say I have an fbs scheme like this one:

table SomeItem
{
    module_version:string;
    message_id:[uint8];
    some_other_id:[uint8];
    event_time_us:uint64;
    message:string;
}

It's an example, the real structure is much, much bigger. So I need to acquire this data from another module in fbs format, convert it to JSON and then send it as an HTTP Response to the client.

The problem concerns those binary data fields. As far as I know you just can't send JSON with raw binary data, you have to encode it (e.g. to Base64). And I would love to do that but only if the data structure were of adequate complexity. It's so big that it's a no brainer to use built-in flatbuffers Obj-To-JSON converter, but flatbuffers library doesn't support Base64 encoding/decoding for binary data fields.

So that leaves me with one option:

Manually create JSON representation of flatbuffers object and process all the data as I please (convert binary data fields to base64 format and put it in JSON), but it's going to take a very long time.

Is there a way to simply use flatbuffers built-in converter and somehow transfer the result JSON to the HTTP Client?

1

There are 1 best solutions below

0
On

The [uint8] when converted to JSON will show up as a list, e.g. [1, 2, 3], where each of those numbers represents a single byte.

Try it out with flatc --json myschema.fbs -- myflatbuffer.bin

Generally, FlatBuffers can convert any data to JSON.