Replies: 1 comment 11 replies
-
Thanks for the positive feedback. From memory without testing, you can create a new syntax using your suggested schema:
You can add a "key" attribute to the keys field in the schema as you have already done, which would let flatbuffers in general allow for sorting and binary searching. FlatCC specifically also supports the sorted attribute (as you also use), which will cause it to generate a sort function that will traverse the entire buffer and sort all fields marked as sorted. You can also call the sort operation on specific fields individually without the sorted attribute. That has to do with allowing a table with a key attribute to be used both in sorted and unsorted tables. Note that FlatCC does not automatically sort during construction for technical and performance reasons, but does it inline on a newly created buffer if asked to do so. FlatCC also supports unsorted search through linear scan methods. A more FlatBuffer native version, but less JSON native structure would separate key and value arrays. Here you would search the key, and use the resulting index to find the value. This approach is much more efficient since in does not need to construct an object for each item, but either way should work:
I have not shown the corresponding JSON, but I guess you have the idea by now, or please ask again. Note that in this case there are neither sorted or key attributes, but you should still be able to sort. I guess you could add sorted attribtute to the childkeys field, but it has been a while since I worked with this. Incidentally, your approach, or my suggested alternative, is what I recommend when you dealing arbitrary key value data. This is one reason FlatCC does not support FlexBuffers that are available in Googles C++ project. I feel that JSON works just as well as any other format when you have odd data, but then FlatCC has a comparatively faster JSON to FlatBuffers parser. That said, you might also want to look into FlexBuffers, but it probably won't help you with parsing JSON directly, and it will be slower. Side note: |
Beta Was this translation helpful? Give feedback.
-
I've been very impressed with flatcc so far, but seem to have run into a challenge I can't seem to work my way out of. I have an application that makes heavy use of json, where we would really like to switch to a more structured schema-oriented format incrementally, and have the option to go binary later. It's mainly C with some C++, so flatcc looks like a fantastic fit. The trick is, we have a lot of existing code and files with pre-generated json we need to keep supporting. I've been doing some tests with one of the more horrendous examples, a 400+MB json document, a very small chunk of which looks like this:
The "children" key of each entry under "R_lite" is a generic map of string to string where the key is a resource type and the value is an IDset (range-encoded set of ints separated by commas). There are a few things like this in our json documents, and I can't seem to work out how to make a schema that can parse these without hitting "expected array" or similar. IIRC they could be handled as "unknown fields" but that wouldn't get me the data I need. Here's what I've been trying:
Hence the "expected array" error. Is there a way to mark in the schema that a vector of tables should be parsed/generated as an object rather than a json array, or some other way to parse this json with jsoncc?
Beta Was this translation helpful? Give feedback.
All reactions