Search code examples
c++flatbuffers

Can flatbuffers parse json given a generated type?


After using flatc to generate a type, can I parse a string of JSON into this type?

In documentation, we can see

This works similarly to how the command-line compiler works: a sequence of files parsed by the same Parser object allow later files to reference definitions in earlier files. Typically this means you first load a schema file (which populates Parser with definitions), followed by one or more JSON files.

And there is sample code in sample_text.cpp

ok = parser.Parse(schemafile.c_str(), include_directories) &&
     parser.Parse(jsonfile.c_str(), include_directories);

However, this means I must distribute the original .fbs schema file together with my application. Since I have already generate the C++ type using flatc during build time, can I parse a JSON string into this type without having to parse the schema one more time during runtime?


Solution

  • No, you currently can't. There's no JSON parsing code in the generated code.

    Parsing the schema is really quick though, and you can reuse the Parser object that has parsed the schema for multiple JSON files.