Skip to content

Commit 02a8491

Browse files
committed
Warning fixes and code cleanup.
1 parent 9aabb94 commit 02a8491

File tree

2 files changed

+68
-75
lines changed

2 files changed

+68
-75
lines changed

include/vsg/io/json.h

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,12 @@ namespace vsg
3535
{
3636
std::string buffer;
3737
std::size_t pos = 0;
38-
vsg::mem_stream mstr;
39-
vsg::indentation indent;
38+
mem_stream mstr;
4039

4140
JSONParser();
4241

43-
inline bool white_space(char c) const
44-
{
45-
return (c == ' ' || c == '\t' || c == '\r' || c == '\n');
46-
}
47-
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
4844
struct Schema
4945
{
5046
// array elements [ value, value.. ]
@@ -71,23 +67,29 @@ namespace vsg
7167
template<typename... Args>
7268
void warning(Args&&... args)
7369
{
74-
vsg::warn("Parsing error at pos = ", pos, ". ", std::forward<Args>(args)...);
70+
warn("Parsing error at pos = ", pos, ". ", std::forward<Args>(args)...);
71+
}
72+
73+
inline bool white_space(char c) const
74+
{
75+
return (c == ' ' || c == '\t' || c == '\r' || c == '\n');
7576
}
77+
7678
};
7779
VSG_type_name(vsg::JSONParser);
7880

7981
/// Default support for mapping standard JSON types directly to VSG.
80-
/// JSON objects are mapped to vsg::Object metadata.
81-
/// JSON arrays to vsg::Objects.
82+
/// JSON objects are mapped to Object metadata.
83+
/// JSON arrays to Objects.
8284
/// string, number and bool are mapped to stringValue, doubleValue and boolValue.
8385
struct VSG_DECLSPEC JSONtoMetaDataSchema : public JSONParser::Schema
8486
{
8587
// object created when parsing JSON file
86-
vsg::ref_ptr<vsg::Object> object;
87-
vsg::ref_ptr<vsg::Objects> objects;
88+
ref_ptr<Object> object;
89+
ref_ptr<Objects> objects;
8890

89-
void addToArray(vsg::ref_ptr<vsg::Object> in_object);
90-
void addToObject(const std::string_view& name, vsg::ref_ptr<vsg::Object> in_object);
91+
void addToArray(ref_ptr<Object> in_object);
92+
void addToObject(const std::string_view& name, ref_ptr<Object> in_object);
9193

9294
// array elements [ value, value.. ]
9395
void read_array(JSONParser& parser) override;
@@ -107,20 +109,19 @@ namespace vsg
107109
};
108110
VSG_type_name(vsg::JSONtoMetaDataSchema);
109111

110-
111112
/// json ReaderWriter
112-
class json : public vsg::Inherit<vsg::ReaderWriter, json>
113+
class json : public Inherit<ReaderWriter, json>
113114
{
114115
public:
115116
json();
116117

117-
vsg::ref_ptr<vsg::Object> read(const vsg::Path&, vsg::ref_ptr<const vsg::Options>) const override;
118-
vsg::ref_ptr<vsg::Object> read(std::istream&, vsg::ref_ptr<const vsg::Options>) const override;
119-
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;
120121

121-
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;
122123

123-
bool supportedExtension(const vsg::Path& ext) const;
124+
bool supportedExtension(const Path& ext) const;
124125

125126
bool getFeatures(Features& features) const override;
126127
};

src/vsg/io/json.cpp

Lines changed: 46 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -24,51 +24,51 @@ using namespace vsg;
2424
//
2525
// JSONParser::Schema
2626
//
27-
void JSONParser::Schema::read_array(JSONParser& parser)
27+
void JSONParser::Schema::read_array(JSONParser& )
2828
{
2929
}
3030

31-
void JSONParser::Schema::read_object(JSONParser& parser)
31+
void JSONParser::Schema::read_object(JSONParser& )
3232
{
3333
}
3434

35-
void JSONParser::Schema::read_string(JSONParser& parser)
35+
void JSONParser::Schema::read_string(JSONParser& )
3636
{
3737
}
3838

39-
void JSONParser::Schema::read_number(JSONParser& parser, std::istream& input)
39+
void JSONParser::Schema::read_number(JSONParser&, std::istream&)
4040
{
4141
}
4242

43-
void JSONParser::Schema::read_bool(JSONParser& parser, bool value)
43+
void JSONParser::Schema::read_bool(JSONParser&, bool)
4444
{
4545
}
4646

47-
void JSONParser::Schema::read_null(JSONParser& parser)
47+
void JSONParser::Schema::read_null(JSONParser&)
4848
{
4949
}
5050

51-
void JSONParser::Schema::read_array(JSONParser& parser, const std::string_view& name)
51+
void JSONParser::Schema::read_array(JSONParser&, const std::string_view&)
5252
{
5353
}
5454

55-
void JSONParser::Schema::read_object(JSONParser& parser, const std::string_view& name)
55+
void JSONParser::Schema::read_object(JSONParser&, const std::string_view&)
5656
{
5757
}
5858

59-
void JSONParser::Schema::read_string(JSONParser& parser, const std::string_view& name)
59+
void JSONParser::Schema::read_string(JSONParser&, const std::string_view&)
6060
{
6161
}
6262

63-
void JSONParser::Schema::read_number(JSONParser& parser, const std::string_view& name, std::istream& input)
63+
void JSONParser::Schema::read_number(JSONParser&, const std::string_view&, std::istream&)
6464
{
6565
}
6666

67-
void JSONParser::Schema::read_bool(JSONParser& parser, const std::string_view& name, bool value)
67+
void JSONParser::Schema::read_bool(JSONParser&, const std::string_view&, bool)
6868
{
6969
}
7070

71-
void JSONParser::Schema::read_null(JSONParser& parser, const std::string_view& name)
71+
void JSONParser::Schema::read_null(JSONParser&, const std::string_view&)
7272
{
7373
}
7474

@@ -77,19 +77,19 @@ void JSONParser::Schema::read_null(JSONParser& parser, const std::string_view& n
7777
// JSONtoMetaDataSchema
7878
//
7979

80-
void JSONtoMetaDataSchema::addToArray(vsg::ref_ptr<vsg::Object> in_object)
80+
void JSONtoMetaDataSchema::addToArray(ref_ptr<Object> in_object)
8181
{
8282
if (!in_object) return;
8383

84-
if (!objects) objects = vsg::Objects::create();
84+
if (!objects) objects = Objects::create();
8585
objects->addChild(in_object);
8686
}
8787

88-
void JSONtoMetaDataSchema::addToObject(const std::string_view& name, vsg::ref_ptr<vsg::Object> in_object)
88+
void JSONtoMetaDataSchema::addToObject(const std::string_view& name, ref_ptr<Object> in_object)
8989
{
9090
if (!in_object) return;
9191

92-
if (!object) object = vsg::Object::create();
92+
if (!object) object = Object::create();
9393
object->setObject(std::string(name), in_object);
9494
}
9595

@@ -114,23 +114,23 @@ void JSONtoMetaDataSchema::read_string(JSONParser& parser)
114114
std::string value;
115115
parser.read_string(value);
116116

117-
addToArray(vsg::stringValue::create(value));
117+
addToArray(stringValue::create(value));
118118
}
119119

120-
void JSONtoMetaDataSchema::read_number(JSONParser& parser, std::istream& input)
120+
void JSONtoMetaDataSchema::read_number(JSONParser&, std::istream& input)
121121
{
122122
double value;
123123
input >> value;
124124

125-
addToArray(vsg::doubleValue::create(value));
125+
addToArray(doubleValue::create(value));
126126
}
127127

128-
void JSONtoMetaDataSchema::read_bool(JSONParser& parser, bool value)
128+
void JSONtoMetaDataSchema::read_bool(JSONParser&, bool value)
129129
{
130-
addToArray(vsg::boolValue::create(value));
130+
addToArray(boolValue::create(value));
131131
}
132132

133-
void JSONtoMetaDataSchema::read_null(JSONParser& parser)
133+
void JSONtoMetaDataSchema::read_null(JSONParser&)
134134
{
135135
}
136136

@@ -155,23 +155,23 @@ void JSONtoMetaDataSchema::read_string(JSONParser& parser, const std::string_vie
155155
std::string value;
156156
parser.read_string(value);
157157

158-
addToObject(name, vsg::stringValue::create(value));
158+
addToObject(name, stringValue::create(value));
159159
}
160160

161-
void JSONtoMetaDataSchema::read_number(JSONParser& parser, const std::string_view& name, std::istream& input)
161+
void JSONtoMetaDataSchema::read_number(JSONParser&, const std::string_view& name, std::istream& input)
162162
{
163163
double value;
164164
input >> value;
165165

166-
addToObject(name, vsg::doubleValue::create(value));
166+
addToObject(name, doubleValue::create(value));
167167
}
168168

169-
void JSONtoMetaDataSchema::read_bool(JSONParser& parser, const std::string_view& name, bool value)
169+
void JSONtoMetaDataSchema::read_bool(JSONParser&, const std::string_view& name, bool value)
170170
{
171-
addToObject(name, vsg::boolValue::create(value));
171+
addToObject(name, boolValue::create(value));
172172
}
173173

174-
void JSONtoMetaDataSchema::read_null(JSONParser& parser, const std::string_view& name)
174+
void JSONtoMetaDataSchema::read_null(JSONParser&, const std::string_view&)
175175
{
176176
}
177177

@@ -212,8 +212,6 @@ void JSONParser::read_object(JSONParser::Schema& schema)
212212
pos = buffer.find_first_not_of(" \t\r\n", pos + 1);
213213
if (pos == std::string::npos) return;
214214

215-
indent += 4;
216-
217215
while (pos != std::string::npos && pos < buffer.size() && buffer[pos] != '}')
218216
{
219217
auto previous_position = pos;
@@ -229,14 +227,14 @@ void JSONParser::read_object(JSONParser::Schema& schema)
229227
pos = buffer.find_first_not_of(" \t\r\n", end_of_string + 1);
230228
if (pos == std::string::npos)
231229
{
232-
vsg::info(indent, "read_object() deliminator error end of buffer.");
230+
warning("read_object() deliminator error end of buffer.");
233231
break;
234232
}
235233

236234
// make sure next charater is the {name : value} deliminator
237235
if (buffer[pos] != ':')
238236
{
239-
vsg::info(indent, "read_object() deliminator error buffer[", pos, "] = ", buffer[pos]);
237+
warning("read_object() deliminator error buffer[", pos, "] = ", buffer[pos]);
240238
break;
241239
}
242240

@@ -296,7 +294,7 @@ void JSONParser::read_object(JSONParser::Schema& schema)
296294
}
297295
else
298296
{
299-
vsg::info(indent, "read_object() buffer[", pos, "] = ", buffer[pos]);
297+
warning("read_object() buffer[", pos, "] = ", buffer[pos]);
300298
}
301299

302300
pos = buffer.find_first_not_of(" \t\r\n", pos);
@@ -312,8 +310,6 @@ void JSONParser::read_object(JSONParser::Schema& schema)
312310
{
313311
++pos;
314312
}
315-
316-
indent -= 4;
317313
}
318314

319315
void JSONParser::read_array(JSONParser::Schema& schema)
@@ -322,7 +318,7 @@ void JSONParser::read_array(JSONParser::Schema& schema)
322318
if (pos == std::string::npos) return;
323319
if (buffer[pos] != '[')
324320
{
325-
vsg::info(indent, "read_array() could not match opening [");
321+
warning("read_array() could not match opening [");
326322
return;
327323
}
328324

@@ -331,12 +327,10 @@ void JSONParser::read_array(JSONParser::Schema& schema)
331327
pos = buffer.find_first_not_of(" \t\r\n", pos + 1);
332328
if (pos == std::string::npos)
333329
{
334-
vsg::info(indent, "read_array() contents after [");
330+
warning("read_array() contents after [");
335331
return;
336332
}
337333

338-
indent += 4;
339-
340334
while (pos != std::string::npos && pos < buffer.size() && buffer[pos] != ']')
341335
{
342336
auto previous_position = pos;
@@ -405,8 +399,6 @@ void JSONParser::read_array(JSONParser::Schema& schema)
405399
{
406400
++pos;
407401
}
408-
409-
indent -= 4;
410402
}
411403

412404

@@ -418,12 +410,12 @@ json::json()
418410
{
419411
}
420412

421-
bool json::supportedExtension(const vsg::Path& ext) const
413+
bool json::supportedExtension(const Path& ext) const
422414
{
423415
return ext == ".json";
424416
}
425417

426-
vsg::ref_ptr<vsg::Object> json::_read(std::istream& fin, vsg::ref_ptr<const vsg::Options>) const
418+
ref_ptr<Object> json::_read(std::istream& fin, ref_ptr<const Options>) const
427419
{
428420
fin.seekg(0, fin.end);
429421
size_t fileSize = fin.tellg();
@@ -437,7 +429,7 @@ vsg::ref_ptr<vsg::Object> json::_read(std::istream& fin, vsg::ref_ptr<const vsg:
437429
fin.seekg(0);
438430
fin.read(reinterpret_cast<char*>(parser.buffer.data()), fileSize);
439431

440-
vsg::ref_ptr<vsg::Object> result;
432+
ref_ptr<Object> result;
441433

442434
// skip white space
443435
parser.pos = parser.buffer.find_first_not_of(" \t\r\n", 0);
@@ -449,56 +441,56 @@ vsg::ref_ptr<vsg::Object> json::_read(std::istream& fin, vsg::ref_ptr<const vsg:
449441
parser.read_object(schema);
450442
result = schema.object;
451443

452-
vsg::info("Read JSON object, result = ", result);
444+
info("Read JSON object, result = ", result);
453445
}
454446
else if (parser.buffer[parser.pos] == '[')
455447
{
456448
JSONtoMetaDataSchema schema;
457449
parser.read_array(schema);
458450
result = schema.objects;
459451

460-
vsg::info("Read JSON array, result = ", result);
452+
info("Read JSON array, result = ", result);
461453
}
462454
else
463455
{
464-
vsg::info("Parsing error, could not find opening { or [.");
456+
warn("Parsing error, could not find opening { or [.");
465457
}
466458

467459
return result;
468460
}
469461

470-
vsg::ref_ptr<vsg::Object> json::read(const vsg::Path& filename, vsg::ref_ptr<const vsg::Options> options) const
462+
ref_ptr<Object> json::read(const Path& filename, ref_ptr<const Options> options) const
471463
{
472-
vsg::Path ext = (options && options->extensionHint) ? options->extensionHint : vsg::lowerCaseFileExtension(filename);
464+
Path ext = (options && options->extensionHint) ? options->extensionHint : lowerCaseFileExtension(filename);
473465
if (!supportedExtension(ext)) return {};
474466

475-
vsg::Path filenameToUse = vsg::findFile(filename, options);
467+
Path filenameToUse = findFile(filename, options);
476468
if (!filenameToUse) return {};
477469

478470
std::ifstream fin(filenameToUse, std::ios::ate | std::ios::binary);
479471
return _read(fin, options);
480472
}
481473

482-
vsg::ref_ptr<vsg::Object> json::read(std::istream& fin, vsg::ref_ptr<const vsg::Options> options) const
474+
ref_ptr<Object> json::read(std::istream& fin, ref_ptr<const Options> options) const
483475
{
484476
if (!options || !options->extensionHint) return {};
485477
if (!supportedExtension(options->extensionHint)) return {};
486478

487479
return _read(fin, options);
488480
}
489481

490-
vsg::ref_ptr<vsg::Object> json::read(const uint8_t* ptr, size_t size, vsg::ref_ptr<const vsg::Options> options) const
482+
ref_ptr<Object> json::read(const uint8_t* ptr, size_t size, ref_ptr<const Options> options) const
491483
{
492484
if (!options || !options->extensionHint) return {};
493485
if (!supportedExtension(options->extensionHint)) return {};
494486

495-
vsg::mem_stream fin(ptr, size);
487+
mem_stream fin(ptr, size);
496488
return _read(fin, options);
497489
}
498490

499491
bool json::getFeatures(Features& features) const
500492
{
501-
vsg::ReaderWriter::FeatureMask supported_features = static_cast<vsg::ReaderWriter::FeatureMask>(vsg::ReaderWriter::READ_FILENAME | vsg::ReaderWriter::READ_ISTREAM | vsg::ReaderWriter::READ_MEMORY);
493+
ReaderWriter::FeatureMask supported_features = static_cast<ReaderWriter::FeatureMask>(ReaderWriter::READ_FILENAME | ReaderWriter::READ_ISTREAM | ReaderWriter::READ_MEMORY);
502494
features.extensionFeatureMap[".json"] = supported_features;
503495

504496
return true;

0 commit comments

Comments
 (0)