Skip to content

Commit 901cc7d

Browse files
Merge pull request #1448 from vsg-dev/extensible_json
Implemented a extensible JSONParser::Schema base class for customizing how json files are converted to objects.
2 parents 2423e53 + 02a8491 commit 901cc7d

File tree

2 files changed

+355
-147
lines changed

2 files changed

+355
-147
lines changed

include/vsg/io/json.h

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,47 +23,105 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2323
2424
</editor-fold> */
2525

26+
#include <vsg/core/Objects.h>
2627
#include <vsg/io/ReaderWriter.h>
2728
#include <vsg/io/mem_stream.h>
2829

2930
namespace vsg
3031
{
3132

3233
/// JSON parser based on spec: https://www.json.org/json-en.html
33-
struct JSONParser
34+
struct VSG_DECLSPEC JSONParser
3435
{
3536
std::string buffer;
3637
std::size_t pos = 0;
37-
vsg::mem_stream mstr;
38-
vsg::indentation indent;
38+
mem_stream mstr;
3939

4040
JSONParser();
4141

42+
/// Schema base class to provides a mechanism for customizing the json parsing to handle
43+
/// mapping between json schema's and user data/scene graph objects
44+
struct Schema
45+
{
46+
// array elements [ value, value.. ]
47+
virtual void read_array(JSONParser& parser);
48+
virtual void read_object(JSONParser& parser);
49+
virtual void read_string(JSONParser& parser);
50+
virtual void read_number(JSONParser& parser, std::istream& input);
51+
virtual void read_bool(JSONParser& parser, bool value);
52+
virtual void read_null(JSONParser& parser);
53+
54+
// object properties { name, value; ... }
55+
virtual void read_array(JSONParser& parser, const std::string_view& name);
56+
virtual void read_object(JSONParser& parser, const std::string_view& name);
57+
virtual void read_string(JSONParser& parser, const std::string_view& name);
58+
virtual void read_number(JSONParser& parser, const std::string_view& name, std::istream& input);
59+
virtual void read_bool(JSONParser& parser, const std::string_view& name, bool value);
60+
virtual void read_null(JSONParser& parser, const std::string_view& name);
61+
};
62+
63+
bool read_string(std::string& value);
64+
void read_object(Schema& schema);
65+
void read_array(Schema& schema);
66+
67+
template<typename... Args>
68+
void warning(Args&&... args)
69+
{
70+
warn("Parsing error at pos = ", pos, ". ", std::forward<Args>(args)...);
71+
}
72+
4273
inline bool white_space(char c) const
4374
{
4475
return (c == ' ' || c == '\t' || c == '\r' || c == '\n');
4576
}
4677

47-
bool read_string(std::string& value);
48-
49-
vsg::ref_ptr<vsg::Object> read_array();
50-
vsg::ref_ptr<vsg::Object> read_object();
5178
};
5279
VSG_type_name(vsg::JSONParser);
5380

81+
/// Default support for mapping standard JSON types directly to VSG.
82+
/// JSON objects are mapped to Object metadata.
83+
/// JSON arrays to Objects.
84+
/// string, number and bool are mapped to stringValue, doubleValue and boolValue.
85+
struct VSG_DECLSPEC JSONtoMetaDataSchema : public JSONParser::Schema
86+
{
87+
// object created when parsing JSON file
88+
ref_ptr<Object> object;
89+
ref_ptr<Objects> objects;
90+
91+
void addToArray(ref_ptr<Object> in_object);
92+
void addToObject(const std::string_view& name, ref_ptr<Object> in_object);
93+
94+
// array elements [ value, value.. ]
95+
void read_array(JSONParser& parser) override;
96+
void read_object(JSONParser& parser) override;
97+
void read_string(JSONParser& parser) override;
98+
void read_number(JSONParser& parser, std::istream& input) override;
99+
void read_bool(JSONParser& parser, bool value) override;
100+
void read_null(JSONParser& parser) override;
101+
102+
// object properties { name, value; ... }
103+
void read_array(JSONParser& parser, const std::string_view& name) override;
104+
void read_object(JSONParser& parser, const std::string_view& name) override;
105+
void read_string(JSONParser& parser, const std::string_view& name) override;
106+
void read_number(JSONParser& parser, const std::string_view& name, std::istream& input) override;
107+
void read_bool(JSONParser& parser, const std::string_view& name, bool value) override;
108+
void read_null(JSONParser& parser, const std::string_view& name) override;
109+
};
110+
VSG_type_name(vsg::JSONtoMetaDataSchema);
111+
54112
/// json ReaderWriter
55-
class json : public vsg::Inherit<vsg::ReaderWriter, json>
113+
class json : public Inherit<ReaderWriter, json>
56114
{
57115
public:
58116
json();
59117

60-
vsg::ref_ptr<vsg::Object> read(const vsg::Path&, vsg::ref_ptr<const vsg::Options>) const override;
61-
vsg::ref_ptr<vsg::Object> read(std::istream&, vsg::ref_ptr<const vsg::Options>) const override;
62-
vsg::ref_ptr<vsg::Object> read(const uint8_t* ptr, size_t size, vsg::ref_ptr<const vsg::Options> options = {}) const override;
118+
ref_ptr<Object> read(const Path&, ref_ptr<const Options>) const override;
119+
ref_ptr<Object> read(std::istream&, ref_ptr<const Options>) const override;
120+
ref_ptr<Object> read(const uint8_t* ptr, size_t size, ref_ptr<const Options> options = {}) const override;
63121

64-
vsg::ref_ptr<vsg::Object> _read(std::istream&, vsg::ref_ptr<const vsg::Options>) const;
122+
ref_ptr<Object> _read(std::istream&, ref_ptr<const Options>) const;
65123

66-
bool supportedExtension(const vsg::Path& ext) const;
124+
bool supportedExtension(const Path& ext) const;
67125

68126
bool getFeatures(Features& features) const override;
69127
};

0 commit comments

Comments
 (0)