Closed dontlaugh closed 1 year ago
Is there any way to have the parser ignore fields that aren't defined in the flatbuffer schema?
Quick answer: yes there is, see JSON parser header file comments and possibly JSON test code. There are runtime flags to ignore unknown fields.
Ask again if you want more detail or guidance.
I have been reading through the headers and I see these flags. I will try to make this work.
I have another question. The API response I'm interested in returns a top-level array. So I would need to parse an array of the following, as the root.
table Build {
web_url:string;
number:short;
state:string;
blocked:bool;
message:string;
}
Is this possible in flatbuffers? I couldn't find a syntax for it.
So I would need to parse an array of the following, as the root.
No, this is also something I would sometimes want. Notably if a CSV parser were to be implemented. Flatbuffers cannot have top-level arrays. There are two options:
In either case you cannot parse directly. I could suggest many options, but if you go with stacked buffers, the simplest solution is to write your own array parser and immediately call the Flatbuffer parser on each element start with '{'. You need to extract the end marker of each parse so you can continue skipping the separating comma, but this should be easy to access.
Each table also comes with a JSON array parser of said table (because the parser is made up of many small parsers), but it is not usually called directly so you would have to first manually start a buffer with a parent table it can parse into, then setup the parsing context to match. This is definitely possible, but not a beginner topic.
You could also copy the input array into a buffer with the parent table json added, then parse everthing as the parent table in one go. This is definitely the easiest option.
Oh, there is also the following option: You create your own array parser, call each parser individually, but instead of stacking, you manually build the parent table and add each parsed flatbuffer into the new buffer as an array element. There is a way to efficiently copy elements from one buffer into another so it appears as a local data type and not just a binary blob. But again, not entirely beginner level. A sort of in-between is to create an index of the stack buffers in a separate buffer.
Thank you for your responses. I'm comfortable with using json-c to parse the top-level array cURL returns, then creating flatbuffers in a loop. I'm not concerned with maximum efficiency right now, rather simply using flatbuffers as a consistent serialization method.
We can close this issue, if you like.
Performance should (almost) not be an issue using this method, it's just about how you want the final data represented. If your parser needs to skip over all the content of array elements, you can improve by hand coding a simple parser to skip a comma and detect end of array.
I'm new to flatbuffers, and I'd like to confirm my understanding.
I'm asking about this based on trying to parse a tiny subset of a large JSON object returned from an HTTP api, and the generated parser returns the "unknown symbol" error.
I got past number 1 by using
json-c
to do the initial parsing then manually build a flatbuffer.Is there any way to have the parser ignore fields that aren't defined in the flatbuffer schema?