'How can I use flatbuffers parse json in C++?

I have some protocol file, but they don't have root type. So I can't use flatc to parse json. When I look up for the docs, I find a few methods.For example: flatbuffes::Parse flatbuffers::Registry.I don't know how to use them correctly,because I'm not very proficient in C++.Can anyone give me an example or detailed documentation. A protocol file

namespace serial.proto.api.login;

table LoginReq {
    account:string; 
    passwd:string; 
    device:string;  
    token:string;
}

table LoginRsp {
    account:string; 
    passwd:string;  
    device:string; 
    token:string;
}

table LogoutReq {
    account:string;
}

table LogoutRsp {
    account:string;
}

My code:

auto json_file = R"({"account":"test","passwd":"test01","device":"test","token":"asdfasdfasdf"})";
    flatbuffers::Parser parser;
    parser.Parse(json_file);

Thanks!!!



Solution 1:[1]

Welcome to C++, you will love it here once you get over pointers and STL templates.

Before I answer your question you should look at this discussion on the library repo .

You need to make sure the parser contains a schema, otherwise it wouldn't know where or how to generate the result.

You could do something like so:

parser.Parse(schema.c_str())

where schema is the loaded schema

Hope this helps

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Odhiambo Dormnic