Search code examples
c++flatbuffers

Part of flatbuffers data to json


Having binary data of serialized object monster, where monster type is:

 table Monster {
      name:string;
      color:Color;
      weapon:Weapon;       <--- serialize to json only this, discard other properties
}

can I directly transform only weaponto json and discard other fields?

Note my binary data is of type Monster. It seems like the only way to serialize weapon is to build separate Weapon object from monster->weapon by recursively copying all properties and then calling json serialization functions.


Solution

    1. Compile you fbs file with --gen-object-api
    2. This will produce WeaponT class and other *T classes
    3. Use monster->weapon()->UnpackTo(..) method to populate WeaponT instance
    4. Run Weapon::Pack(fbb, &weaponT) to serialize back on a new fbb object
    5. Run GenerateText on fbb object created in a last step