vsg: include/vsg/io/JSONParser.h Source File

vsg 1.1.13 VulkanSceneGraph library

Loading...
Searching...
No Matches
JSONParser.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2025 Robert Osfield
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of
8this software and associated documentation files (the "Software"), to deal in
9the Software without restriction, including without limitation the rights to
10use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11the Software, and to permit persons to whom the Software is furnished to do so,
12subject to the following conditions:
13
14The above copyright notice and this permission notice shimages be included in images
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24</editor-fold> */
25
26#include <vsg/core/Objects.h>
27#include <vsg/io/ReaderWriter.h>
28#include <vsg/io/mem_stream.h>
29#include <vsg/io/stream.h>
30
31#include <list>
32
33namespace vsg
34{
35
37 struct VSG_DECLSPEC JSONParser : public Inherit<Object, JSONParser>
38 {
39 ref_ptr<const Options> options;
40 std::string buffer;
41 std::size_t pos = 0;
42 mem_stream mstr;
43
44 JSONParser();
45
48 struct VSG_DECLSPEC Schema : public Inherit<Object, Schema>
49 {
50 // array elements [ value, value.. ]
51 virtual void read_array(JSONParser& parser);
52 virtual void read_object(JSONParser& parser);
53 virtual void read_string(JSONParser& parser);
54 virtual void read_number(JSONParser& parser, std::istream& input);
55 virtual void read_bool(JSONParser& parser, bool value);
56 virtual void read_null(JSONParser& parser);
57
58 // object properties { name, value; ... }
59 virtual void read_array(JSONParser& parser, const std::string_view& name);
60 virtual void read_object(JSONParser& parser, const std::string_view& name);
61 virtual void read_string(JSONParser& parser, const std::string_view& name);
62 virtual void read_number(JSONParser& parser, const std::string_view& name, std::istream& input);
63 virtual void read_bool(JSONParser& parser, const std::string_view& name, bool value);
64 virtual void read_null(JSONParser& parser, const std::string_view& name);
65 };
66
67 bool read_uri(std::string& value, ref_ptr<Object>& object);
68 bool read_string_view(std::string_view& value);
69 bool read_string(std::string& value);
70 void read_object(Schema& schema);
71 void read_array(Schema& schema);
72
73 std::pair<std::size_t, std::size_t> lineAndColumnAtPosition(std::size_t position) const;
74 std::string_view lineEnclosingPosition(std::size_t position) const;
75
76 std::list<std::string> warnings;
77
78 template<typename... Args>
79 void warning(Args&&... args)
80 {
81 auto [line, column] = lineAndColumnAtPosition(pos);
82 warnings.push_back(vsg::make_string("Parsing error at [", line, ":", column, "], pos = ", pos, " [ ", lineEnclosingPosition(pos), " ]. ", std::forward<Args>(args)...));
83 }
84
85 inline bool white_space(char c) const
86 {
87 return (c == ' ' || c == '\t' || c == '\r' || c == '\n');
88 }
89 };
90 VSG_type_name(vsg::JSONParser);
91
96 struct VSG_DECLSPEC JSONtoMetaDataSchema : public Inherit<JSONParser::Schema, JSONtoMetaDataSchema>
97 {
98 // object created when parsing JSON file
99 ref_ptr<Object> object;
100 ref_ptr<Objects> objects;
101
102 void addToArray(ref_ptr<Object> in_object);
103 void addToObject(const std::string_view& name, ref_ptr<Object> in_object);
104
105 // array elements [ value, value.. ]
106 void read_array(JSONParser& parser) override;
107 void read_object(JSONParser& parser) override;
108 void read_string(JSONParser& parser) override;
109 void read_number(JSONParser& parser, std::istream& input) override;
110 void read_bool(JSONParser& parser, bool value) override;
111 void read_null(JSONParser& parser) override;
112
113 // object properties { name, value; ... }
114 void read_array(JSONParser& parser, const std::string_view& name) override;
115 void read_object(JSONParser& parser, const std::string_view& name) override;
116 void read_string(JSONParser& parser, const std::string_view& name) override;
117 void read_number(JSONParser& parser, const std::string_view& name, std::istream& input) override;
118 void read_bool(JSONParser& parser, const std::string_view& name, bool value) override;
119 void read_null(JSONParser& parser, const std::string_view& name) override;
120 };
121 VSG_type_name(vsg::JSONtoMetaDataSchema);
122
124 struct StringsSchema : public Inherit<JSONParser::Schema, StringsSchema>
125 {
126 std::vector<std::string> values;
127 void read_string(JSONParser& parser) override
128 {
129 std::string value;
130 parser.read_string(value);
131 values.push_back(value);
132 }
133 };
134
136 template<typename T>
137 struct ValuesSchema : public Inherit<JSONParser::Schema, ValuesSchema<T>>
138 {
139 std::vector<T> values;
140 void read_number(vsg::JSONParser&, std::istream& input) override
141 {
142 T value;
143 input >> value;
144 values.push_back(value);
145 }
146 };
147
149 template<typename T>
150 struct ObjectsSchema : public Inherit<JSONParser::Schema, ObjectsSchema<T>>
151 {
152 std::vector<ref_ptr<T>> values;
153
154 void read_object(vsg::JSONParser& parser) override
155 {
156 values.push_back(T::create());
157 parser.read_object(*values.back());
158 }
159
160 void report(vsg::LogOutput& output)
161 {
162 for (auto& value : values) value->report(output);
163 }
164 };
165
166} // namespace vsg
vsg::mem_stream
Definition mem_stream.h:26
vsg::ref_ptr
Definition ref_ptr.h:22
vsg::JSONParser::Schema
Definition JSONParser.h:49
vsg::JSONParser
JSON parser based on spec: https://www.json.org/json-en.html.
Definition JSONParser.h:38
vsg::JSONtoMetaDataSchema
Definition JSONParser.h:97
vsg::LogOutput
Helper class for recording a set of indented log output.
Definition Logger.h:407
vsg::ObjectsSchema
Template class for reading an array of objects.
Definition JSONParser.h:151
vsg::StringsSchema
class for reading array of strings.
Definition JSONParser.h:125
vsg::ValuesSchema
Template class for reading an array of numeric values.
Definition JSONParser.h:138