Introducing vsg::JSONParser for parsing JSON files #1508
vsg-dev
announced in
Announcements
Replies: 1 comment
-
only 2 days,very impressive. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The first step for writing native glTF and 3D Tiles support for the VulkanSceneGraph was to write a JSON parser. I chose to write my own to avoid adding another external dependency to the core VSG, and to be able to directly utilize the VSG's rich data types and meta object support, rather than have to adapt 3rd party data types to VSG equivalents. Writing the parser myself also enabled me to optimize it's design and implementation to our needs rather than more general purpose parsing.
The vsg::JSONParser header and source file can be found in the include/vsg/io and src/vsg/io directories:
include/vsg/io/JSONParser.h
src/vsg/io/JSONParser/cpp
These contain the three main classes:
To make it easy to read JSON files there is a new vsg::json ReaderWriter to opens and file/stream and use the vsg::JSONParser/vsg::JSONtoMetaDataSchema. It's as simple to use as:
Then you can programmatically step through the user data on the object returned to get all the values. Potentially you could load complex JSON files this way and then convert the meta objects into something more useful, in the early weeks of this work this is what I used in testing prior to having the vsgXchange::gltf and vsgXchange::Tiles3D written.
However, loading data into hierarchy of generic meta data objects is far less useful than parsing a JSON file directly into data structures written for a specific purpose. Both the vsgXchange::gltf and vsgXchange::Tiles3D loaders subclass from vsg::JSONParser::Schema in a fine grained way, with features mapped to a single Schema at the same granularity as the glTF and Tiles3D schema are specified. If you are curious about how to write your own Schema implementations these two implementations will be a place to start right now, but expect it to be a bit full on as the glTF and 3D Tiles specs are quite extensive.
The JSONParser header and source files are tiny, just 662 lines of code between then. This will grow a bit as we extend the range of JSON files that it can handle, but it shouldn't need to grow too much more. It would have taken me longer to learn how to use a 3rd party JSON parser than the 2 days it took me to write it.
At this point in time the aim is to just parsing JSON files, not writing them. If there comes a time when this is required we can figure out how best to write this.
The parsing is already pretty efficient, with std::string_view used when parsing data into subsets of the overall json text, this avoids allocating/deallocating data on the heap and copying data. I haven't yet done the work to have the JSONParser take a string_view as input as it presently requires loader code to read a file into a std::string and then copy this to the JSONParser. This type of optimization will come after I complete more of the critical features in glTF and 3DTiles.
glTF is strictly UTF8 so I haven't attempted to support anything else with the JSONParser yet. If later this is a limitation we can look at what, if anything additional needs to be done.
Beta Was this translation helpful? Give feedback.
All reactions