3 #include "CesiumUtility/Assert.h"
4 #include "DoubleJsonHandler.h"
5 #include "IntegerJsonHandler.h"
6 #include "JsonHandler.h"
8 #include "StringJsonHandler.h"
15 template <
typename T,
typename THandler>
18 using ValueType = std::vector<T>;
20 template <
typename... Ts>
24 std::bind(handlerFactory<Ts...>, std::forward<Ts>(args)...)),
27 void reset(
IJsonHandler* pParent, std::vector<T>* pArray) {
28 JsonHandler::reset(pParent);
29 this->_pArray = pArray;
30 this->_arrayIsOpen =
false;
31 this->_objectHandler.reset(this->_handlerFactory());
35 return this->invalid(
"A null")->readNull();
39 return this->invalid(
"A boolean")->readBool(b);
43 return this->invalid(
"An integer")->readInt32(i);
47 return this->invalid(
"An integer")->readUint32(i);
51 return this->invalid(
"An integer")->readInt64(i);
55 return this->invalid(
"An integer")->readUint64(i);
59 return this->invalid(
"A double (floating-point)")->readDouble(d);
62 virtual IJsonHandler* readString(
const std::string_view& str)
override {
63 return this->invalid(
"A string")->readString(str);
67 if (!this->_arrayIsOpen) {
68 return this->invalid(
"An object")->readObjectStart();
71 CESIUM_ASSERT(this->_pArray);
72 T& o = this->_pArray->emplace_back();
73 this->_objectHandler->reset(
this, &o);
74 return this->_objectHandler->readObjectStart();
78 readObjectKey(
const std::string_view& ) noexcept
override {
82 virtual IJsonHandler* readObjectEnd() noexcept
override {
return nullptr; }
85 if (this->_arrayIsOpen) {
86 return this->invalid(
"An array")->readArrayStart();
89 this->_arrayIsOpen =
true;
90 this->_pArray->clear();
94 virtual IJsonHandler* readArrayEnd()
override {
return this->parent(); }
96 virtual void reportWarning(
97 const std::string& warning,
98 std::vector<std::string>&& context =
99 std::vector<std::string>())
override {
101 std::string(
"[") + std::to_string(this->_pArray->size()) +
"]");
102 this->parent()->reportWarning(warning, std::move(context));
107 if (this->_arrayIsOpen) {
109 type +
" value is not allowed in the object array and has been "
110 "replaced with a default value.");
111 this->_pArray->emplace_back();
112 return this->ignoreAndContinue();
114 this->reportWarning(type +
" is not allowed and has been ignored.");
115 return this->ignoreAndReturnToParent();
119 template <
typename... Ts>
static THandler* handlerFactory(Ts&&... args) {
120 return new THandler(std::forward<Ts>(args)...);
123 std::vector<T>* _pArray =
nullptr;
124 bool _arrayIsOpen =
false;
126 std::function<THandler*()> _handlerFactory;
127 std::unique_ptr<THandler> _objectHandler;
134 using ValueType = std::vector<double>;
138 void reset(
IJsonHandler* pParent, std::vector<double>* pArray) {
139 JsonHandler::reset(pParent);
140 this->_pArray = pArray;
141 this->_arrayIsOpen =
false;
145 return this->invalid(
"A null")->readNull();
149 return this->invalid(
"A bool")->readBool(b);
153 if (!this->_arrayIsOpen) {
154 return this->invalid(
"An integer")->readInt32(i);
157 CESIUM_ASSERT(this->_pArray);
158 this->_pArray->emplace_back(
static_cast<double>(i));
163 if (!this->_arrayIsOpen) {
164 return this->invalid(
"An integer")->readUint32(i);
167 CESIUM_ASSERT(this->_pArray);
168 this->_pArray->emplace_back(
static_cast<double>(i));
173 if (!this->_arrayIsOpen) {
174 return this->invalid(
"An integer")->readInt64(i);
177 CESIUM_ASSERT(this->_pArray);
178 this->_pArray->emplace_back(
static_cast<double>(i));
183 if (!this->_arrayIsOpen) {
184 return this->invalid(
"An integer")->readUint64(i);
187 CESIUM_ASSERT(this->_pArray);
188 this->_pArray->emplace_back(
static_cast<double>(i));
193 if (!this->_arrayIsOpen) {
194 return this->invalid(
"An integer")->readDouble(d);
197 CESIUM_ASSERT(this->_pArray);
198 this->_pArray->emplace_back(d);
202 virtual IJsonHandler* readString(
const std::string_view& str)
override {
203 return this->invalid(
"A string")->readString(str);
207 return this->invalid(
"An object")->readObjectStart();
211 if (this->_arrayIsOpen) {
212 return this->invalid(
"An array")->readArrayStart();
215 this->_arrayIsOpen =
true;
216 this->_pArray->clear();
220 virtual IJsonHandler* readArrayEnd()
override {
return this->parent(); }
222 virtual void reportWarning(
223 const std::string& warning,
224 std::vector<std::string>&& context =
225 std::vector<std::string>())
override {
227 std::string(
"[") + std::to_string(this->_pArray->size()) +
"]");
228 this->parent()->reportWarning(warning, std::move(context));
233 if (this->_arrayIsOpen) {
235 type +
" value is not allowed in the double array and has been "
236 "replaced with a default value.");
237 this->_pArray->emplace_back();
238 return this->ignoreAndContinue();
240 this->reportWarning(type +
" is not allowed and has been ignored.");
241 return this->ignoreAndReturnToParent();
245 std::vector<double>* _pArray =
nullptr;
246 bool _arrayIsOpen =
false;
249 template <
typename T>
253 using ValueType = std::vector<T>;
257 void reset(
IJsonHandler* pParent, std::vector<T>* pArray) {
258 JsonHandler::reset(pParent);
259 this->_pArray = pArray;
260 this->_arrayIsOpen =
false;
264 return this->invalid(
"A null")->readNull();
268 return this->invalid(
"A bool")->readBool(b);
272 if (!this->_arrayIsOpen) {
273 return this->invalid(
"An integer")->readInt32(i);
276 CESIUM_ASSERT(this->_pArray);
277 this->_pArray->emplace_back(
static_cast<T
>(i));
282 if (!this->_arrayIsOpen) {
283 return this->invalid(
"An integer")->readUint32(i);
286 CESIUM_ASSERT(this->_pArray);
287 this->_pArray->emplace_back(
static_cast<T
>(i));
292 if (!this->_arrayIsOpen) {
293 return this->invalid(
"An integer")->readInt64(i);
296 CESIUM_ASSERT(this->_pArray);
297 this->_pArray->emplace_back(
static_cast<T
>(i));
302 if (!this->_arrayIsOpen) {
303 return this->invalid(
"An integer")->readUint64(i);
306 CESIUM_ASSERT(this->_pArray);
307 this->_pArray->emplace_back(
static_cast<T
>(i));
312 return this->invalid(
"A double (floating-point)")->readDouble(d);
315 virtual IJsonHandler* readString(
const std::string_view& str)
override {
316 return this->invalid(
"A string")->readString(str);
320 return this->invalid(
"An object")->readObjectStart();
324 if (this->_arrayIsOpen) {
325 return this->invalid(
"An array")->readArrayStart();
328 this->_arrayIsOpen =
true;
329 this->_pArray->clear();
333 virtual IJsonHandler* readArrayEnd()
override {
return this->parent(); }
335 virtual void reportWarning(
336 const std::string& warning,
337 std::vector<std::string>&& context =
338 std::vector<std::string>())
override {
340 std::string(
"[") + std::to_string(this->_pArray->size()) +
"]");
341 this->parent()->reportWarning(warning, std::move(context));
346 if (this->_arrayIsOpen) {
348 type +
" value is not allowed in the integer array and has been "
349 "replaced with a default value.");
350 this->_pArray->emplace_back();
351 return this->ignoreAndContinue();
353 this->reportWarning(type +
" is not allowed and has been ignored.");
354 return this->ignoreAndReturnToParent();
358 std::vector<T>* _pArray =
nullptr;
359 bool _arrayIsOpen =
false;
366 using ValueType = std::vector<std::string>;
370 void reset(
IJsonHandler* pParent, std::vector<std::string>* pArray) {
371 JsonHandler::reset(pParent);
372 this->_pArray = pArray;
373 this->_arrayIsOpen =
false;
377 return this->invalid(
"A null")->readNull();
381 return this->invalid(
"A bool")->readBool(b);
385 return this->invalid(
"An integer")->readInt32(i);
389 return this->invalid(
"An integer")->readUint32(i);
393 return this->invalid(
"An integer")->readInt64(i);
397 return this->invalid(
"An integer")->readUint64(i);
401 return this->invalid(
"A double (floating-point)")->readDouble(d);
405 return this->invalid(
"An object")->readObjectStart();
409 if (this->_arrayIsOpen) {
410 return this->invalid(
"An array")->readArrayStart();
413 this->_arrayIsOpen =
true;
414 this->_pArray->clear();
418 virtual IJsonHandler* readArrayEnd()
override {
return this->parent(); }
420 virtual IJsonHandler* readString(
const std::string_view& str)
override {
421 if (!this->_arrayIsOpen) {
422 return this->invalid(
"A string")->readString(str);
425 CESIUM_ASSERT(this->_pArray);
426 this->_pArray->emplace_back(str);
430 virtual void reportWarning(
431 const std::string& warning,
432 std::vector<std::string>&& context =
433 std::vector<std::string>())
override {
435 std::string(
"[") + std::to_string(this->_pArray->size()) +
"]");
436 this->parent()->reportWarning(warning, std::move(context));
441 if (this->_arrayIsOpen) {
443 type +
" value is not allowed in the string array and has been "
444 "replaced with a default value.");
445 this->_pArray->emplace_back();
446 return this->ignoreAndContinue();
448 this->reportWarning(type +
" is not allowed and has been ignored.");
449 return this->ignoreAndReturnToParent();
453 std::vector<std::string>* _pArray =
nullptr;
454 bool _arrayIsOpen =
false;
457 template <
typename T,
typename THandler>
458 class CESIUMJSONREADER_API
462 using ValueType = std::vector<T>;
464 template <
typename... Ts>
468 std::bind(handlerFactory<Ts...>, std::forward<Ts>(args)...)),
471 void reset(
IJsonHandler* pParent, std::vector<std::vector<T>>* pArray) {
472 JsonHandler::reset(pParent);
473 this->_pArray = pArray;
474 this->_arrayIsOpen =
false;
475 this->_elementHandler.reset(this->_handlerFactory());
479 return this->invalid(
"A null")->readNull();
483 return this->invalid(
"A bool")->readBool(b);
487 return this->invalid(
"An integer")->readInt32(i);
491 return this->invalid(
"An integer")->readUint32(i);
495 return this->invalid(
"An integer")->readInt64(i);
499 return this->invalid(
"An integer")->readUint64(i);
503 return this->invalid(
"A double (floating-point)")->readDouble(d);
506 virtual IJsonHandler* readString(
const std::string_view& str)
override {
507 return this->invalid(
"A string")->readString(str);
511 return this->invalid(
"An object")->readObjectStart();
515 if (this->_arrayIsOpen) {
516 CESIUM_ASSERT(this->_pArray);
517 std::vector<T>& o = this->_pArray->emplace_back();
518 this->_elementHandler->reset(
this, &o);
519 return this->_elementHandler->readArrayStart();
521 this->_arrayIsOpen =
true;
522 this->_pArray->clear();
527 virtual IJsonHandler* readArrayEnd()
override {
return this->parent(); }
529 virtual void reportWarning(
530 const std::string& warning,
531 std::vector<std::string>&& context =
532 std::vector<std::string>())
override {
534 std::string(
"[") + std::to_string(this->_pArray->size()) +
"]");
535 this->parent()->reportWarning(warning, std::move(context));
540 if (this->_arrayIsOpen) {
542 type +
" value is not allowed in the array of arrays and has been "
543 "replaced with a default value.");
544 this->_pArray->emplace_back();
545 return this->ignoreAndContinue();
547 this->reportWarning(type +
" is not allowed and has been ignored.");
548 return this->ignoreAndReturnToParent();
552 template <
typename... Ts>
557 std::vector<std::vector<T>>* _pArray =
nullptr;
558 bool _arrayIsOpen =
false;
560 std::function<ArrayJsonHandler<T, THandler>*()> _handlerFactory;
561 std::unique_ptr<ArrayJsonHandler<T, THandler>> _elementHandler;
Classes for reading JSON.