3#include "CesiumUtility/Assert.h"
4#include "DoubleJsonHandler.h"
5#include "IntegerJsonHandler.h"
6#include "JsonHandler.h"
8#include "StringJsonHandler.h"
21template <
typename T,
typename THandler>
33 template <
typename... Ts>
37 std::bind(handlerFactory<Ts...>, std::forward<Ts>(args)...)),
45 JsonHandler::reset(pParent);
46 this->_pArray = pArray;
47 this->_arrayIsOpen =
false;
48 this->_objectHandler.reset(this->_handlerFactory());
52 return this->invalid(
"A null")->
readNull();
56 return this->invalid(
"A boolean")->
readBool(b);
60 return this->invalid(
"An integer")->
readInt32(i);
64 return this->invalid(
"An integer")->
readUint32(i);
68 return this->invalid(
"An integer")->
readInt64(i);
72 return this->invalid(
"An integer")->
readUint64(i);
76 return this->invalid(
"A double (floating-point)")->
readDouble(d);
80 return this->invalid(
"A string")->
readString(str);
84 if (!this->_arrayIsOpen) {
85 return this->invalid(
"An object")->readObjectStart();
88 CESIUM_ASSERT(this->_pArray);
89 T& o = this->_pArray->emplace_back();
90 this->_objectHandler->reset(
this, &o);
91 return this->_objectHandler->readObjectStart();
102 if (this->_arrayIsOpen) {
103 return this->invalid(
"An array")->readArrayStart();
106 this->_arrayIsOpen =
true;
107 this->_pArray->clear();
114 const std::string& warning,
115 std::vector<std::string>&& context =
116 std::vector<std::string>())
override {
118 std::string(
"[") + std::to_string(this->_pArray->size()) +
"]");
119 this->parent()->reportWarning(warning, std::move(context));
124 if (this->_arrayIsOpen) {
126 type +
" value is not allowed in the object array and has been "
127 "replaced with a default value.");
128 this->_pArray->emplace_back();
129 return this->ignoreAndContinue();
131 this->reportWarning(type +
" is not allowed and has been ignored.");
132 return this->ignoreAndReturnToParent();
136 template <
typename... Ts>
static THandler* handlerFactory(Ts&&... args) {
137 return new THandler(std::forward<Ts>(args)...);
140 std::vector<T>* _pArray =
nullptr;
141 bool _arrayIsOpen =
false;
143 std::function<THandler*()> _handlerFactory;
144 std::unique_ptr<THandler> _objectHandler;
167 JsonHandler::reset(pParent);
168 this->_pArray = pArray;
169 this->_arrayIsOpen =
false;
173 return this->invalid(
"A null")->
readNull();
177 return this->invalid(
"A bool")->
readBool(b);
181 if (!this->_arrayIsOpen) {
182 return this->invalid(
"An integer")->readInt32(i);
185 CESIUM_ASSERT(this->_pArray);
186 this->_pArray->emplace_back(
static_cast<double>(i));
191 if (!this->_arrayIsOpen) {
192 return this->invalid(
"An integer")->readUint32(i);
195 CESIUM_ASSERT(this->_pArray);
196 this->_pArray->emplace_back(
static_cast<double>(i));
201 if (!this->_arrayIsOpen) {
202 return this->invalid(
"An integer")->readInt64(i);
205 CESIUM_ASSERT(this->_pArray);
206 this->_pArray->emplace_back(
static_cast<double>(i));
211 if (!this->_arrayIsOpen) {
212 return this->invalid(
"An integer")->readUint64(i);
215 CESIUM_ASSERT(this->_pArray);
216 this->_pArray->emplace_back(
static_cast<double>(i));
221 if (!this->_arrayIsOpen) {
222 return this->invalid(
"An integer")->readDouble(d);
225 CESIUM_ASSERT(this->_pArray);
226 this->_pArray->emplace_back(d);
231 return this->invalid(
"A string")->
readString(str);
239 if (this->_arrayIsOpen) {
240 return this->invalid(
"An array")->readArrayStart();
243 this->_arrayIsOpen =
true;
244 this->_pArray->clear();
251 const std::string& warning,
252 std::vector<std::string>&& context =
253 std::vector<std::string>())
override {
255 std::string(
"[") + std::to_string(this->_pArray->size()) +
"]");
256 this->parent()->reportWarning(warning, std::move(context));
261 if (this->_arrayIsOpen) {
263 type +
" value is not allowed in the double array and has been "
264 "replaced with a default value.");
265 this->_pArray->emplace_back();
266 return this->ignoreAndContinue();
268 this->reportWarning(type +
" is not allowed and has been ignored.");
269 return this->ignoreAndReturnToParent();
273 std::vector<double>* _pArray =
nullptr;
274 bool _arrayIsOpen =
false;
295 JsonHandler::reset(pParent);
296 this->_pArray = pArray;
297 this->_arrayIsOpen =
false;
301 return this->invalid(
"A null")->
readNull();
305 return this->invalid(
"A bool")->
readBool(b);
309 if (!this->_arrayIsOpen) {
310 return this->invalid(
"An integer")->readInt32(i);
313 CESIUM_ASSERT(this->_pArray);
314 this->_pArray->emplace_back(
static_cast<T
>(i));
319 if (!this->_arrayIsOpen) {
320 return this->invalid(
"An integer")->readUint32(i);
323 CESIUM_ASSERT(this->_pArray);
324 this->_pArray->emplace_back(
static_cast<T
>(i));
329 if (!this->_arrayIsOpen) {
330 return this->invalid(
"An integer")->readInt64(i);
333 CESIUM_ASSERT(this->_pArray);
334 this->_pArray->emplace_back(
static_cast<T
>(i));
339 if (!this->_arrayIsOpen) {
340 return this->invalid(
"An integer")->readUint64(i);
343 CESIUM_ASSERT(this->_pArray);
344 this->_pArray->emplace_back(
static_cast<T
>(i));
349 return this->invalid(
"A double (floating-point)")->
readDouble(d);
353 return this->invalid(
"A string")->
readString(str);
361 if (this->_arrayIsOpen) {
362 return this->invalid(
"An array")->readArrayStart();
365 this->_arrayIsOpen =
true;
366 this->_pArray->clear();
373 const std::string& warning,
374 std::vector<std::string>&& context =
375 std::vector<std::string>())
override {
377 std::string(
"[") + std::to_string(this->_pArray->size()) +
"]");
378 this->parent()->reportWarning(warning, std::move(context));
383 if (this->_arrayIsOpen) {
385 type +
" value is not allowed in the integer array and has been "
386 "replaced with a default value.");
387 this->_pArray->emplace_back();
388 return this->ignoreAndContinue();
390 this->reportWarning(type +
" is not allowed and has been ignored.");
391 return this->ignoreAndReturnToParent();
395 std::vector<T>* _pArray =
nullptr;
396 bool _arrayIsOpen =
false;
417 JsonHandler::reset(pParent);
418 this->_pArray = pArray;
419 this->_arrayIsOpen =
false;
423 return this->invalid(
"A null")->
readNull();
427 return this->invalid(
"A bool")->
readBool(b);
431 return this->invalid(
"An integer")->
readInt32(i);
435 return this->invalid(
"An integer")->
readUint32(i);
439 return this->invalid(
"An integer")->
readInt64(i);
443 return this->invalid(
"An integer")->
readUint64(i);
447 return this->invalid(
"A double (floating-point)")->
readDouble(d);
455 if (this->_arrayIsOpen) {
456 return this->invalid(
"An array")->readArrayStart();
459 this->_arrayIsOpen =
true;
460 this->_pArray->clear();
467 if (!this->_arrayIsOpen) {
468 return this->invalid(
"A string")->readString(str);
471 CESIUM_ASSERT(this->_pArray);
472 this->_pArray->emplace_back(str);
477 const std::string& warning,
478 std::vector<std::string>&& context =
479 std::vector<std::string>())
override {
481 std::string(
"[") + std::to_string(this->_pArray->size()) +
"]");
482 this->parent()->reportWarning(warning, std::move(context));
487 if (this->_arrayIsOpen) {
489 type +
" value is not allowed in the string array and has been "
490 "replaced with a default value.");
491 this->_pArray->emplace_back();
492 return this->ignoreAndContinue();
494 this->reportWarning(type +
" is not allowed and has been ignored.");
495 return this->ignoreAndReturnToParent();
499 std::vector<std::string>* _pArray =
nullptr;
500 bool _arrayIsOpen =
false;
510template <
typename T,
typename THandler>
511class CESIUMJSONREADER_API
524 template <
typename... Ts>
528 std::bind(handlerFactory<Ts...>, std::forward<Ts>(args)...)),
536 JsonHandler::reset(pParent);
537 this->_pArray = pArray;
538 this->_arrayIsOpen =
false;
539 this->_elementHandler.reset(this->_handlerFactory());
543 return this->invalid(
"A null")->
readNull();
547 return this->invalid(
"A bool")->
readBool(b);
551 return this->invalid(
"An integer")->
readInt32(i);
555 return this->invalid(
"An integer")->
readUint32(i);
559 return this->invalid(
"An integer")->
readInt64(i);
563 return this->invalid(
"An integer")->
readUint64(i);
567 return this->invalid(
"A double (floating-point)")->
readDouble(d);
571 return this->invalid(
"A string")->
readString(str);
579 if (this->_arrayIsOpen) {
580 CESIUM_ASSERT(this->_pArray);
581 std::vector<T>& o = this->_pArray->emplace_back();
582 this->_elementHandler->reset(
this, &o);
583 return this->_elementHandler->readArrayStart();
585 this->_arrayIsOpen =
true;
586 this->_pArray->clear();
594 const std::string& warning,
595 std::vector<std::string>&& context =
596 std::vector<std::string>())
override {
598 std::string(
"[") + std::to_string(this->_pArray->size()) +
"]");
599 this->parent()->reportWarning(warning, std::move(context));
604 if (this->_arrayIsOpen) {
606 type +
" value is not allowed in the array of arrays and has been "
607 "replaced with a default value.");
608 this->_pArray->emplace_back();
609 return this->ignoreAndContinue();
611 this->reportWarning(type +
" is not allowed and has been ignored.");
612 return this->ignoreAndReturnToParent();
616 template <
typename... Ts>
617 static ArrayJsonHandler<T, THandler>* handlerFactory(Ts&&... args) {
618 return new ArrayJsonHandler<T, THandler>(std::forward<Ts>(args)...);
621 std::vector<std::vector<T>>* _pArray =
nullptr;
622 bool _arrayIsOpen =
false;
624 std::function<ArrayJsonHandler<T, THandler>*()> _handlerFactory;
625 std::unique_ptr<ArrayJsonHandler<T, THandler>> _elementHandler;
virtual IJsonHandler * readUint32(uint32_t i) override
Called when the JSON parser encounters a uint32 value.
virtual IJsonHandler * readString(const std::string_view &str) override
Called when the JSON parser encounters a string value.
virtual IJsonHandler * readObjectStart() override
Called when the JSON parser encounters the beginning of an object.
virtual IJsonHandler * readArrayStart() override
Called when the JSON parser encounters the start of an array.
virtual void reportWarning(const std::string &warning, std::vector< std::string > &&context=std::vector< std::string >()) override
Report a warning while reading JSON.
virtual IJsonHandler * readArrayEnd() override
Called when the JSON parser encounters the end of an array.
virtual IJsonHandler * readUint64(uint64_t i) override
Called when the JSON parser encounters a uint64 value.
std::vector< T > ValueType
The destination type.
virtual IJsonHandler * readDouble(double d) override
Called when the JSON parser encounters a double value.
virtual IJsonHandler * readNull() override
Called when the JSON parser encounters a null.
void reset(IJsonHandler *pParent, std::vector< T > *pArray)
Resets the parent and destination array of this ArrayJsonHandler.
virtual IJsonHandler * readInt64(int64_t i) override
Called when the JSON parser encounters an int64 value.
virtual IJsonHandler * readInt32(int32_t i) override
Called when the JSON parser encounters an int32 value.
virtual IJsonHandler * readBool(bool b) override
Called when the JSON parser encounters a boolean value.
virtual IJsonHandler * readInt64(int64_t i) override
Called when the JSON parser encounters an int64 value.
virtual IJsonHandler * readUint64(uint64_t i) override
Called when the JSON parser encounters a uint64 value.
virtual IJsonHandler * readNull() override
Called when the JSON parser encounters a null.
virtual IJsonHandler * readDouble(double d) override
Called when the JSON parser encounters a double value.
virtual IJsonHandler * readObjectStart() override
Called when the JSON parser encounters the beginning of an object.
std::vector< double > ValueType
The destination type.
virtual IJsonHandler * readString(const std::string_view &str) override
Called when the JSON parser encounters a string value.
virtual IJsonHandler * readUint32(uint32_t i) override
Called when the JSON parser encounters a uint32 value.
virtual IJsonHandler * readBool(bool b) override
Called when the JSON parser encounters a boolean value.
virtual IJsonHandler * readArrayStart() override
Called when the JSON parser encounters the start of an array.
virtual IJsonHandler * readArrayEnd() override
Called when the JSON parser encounters the end of an array.
void reset(IJsonHandler *pParent, std::vector< double > *pArray)
Resets the parent and destination array of this ArrayJsonHandler.
virtual void reportWarning(const std::string &warning, std::vector< std::string > &&context=std::vector< std::string >()) override
Report a warning while reading JSON.
virtual IJsonHandler * readInt32(int32_t i) override
Called when the JSON parser encounters an int32 value.
virtual IJsonHandler * readArrayEnd() override
Called when the JSON parser encounters the end of an array.
virtual IJsonHandler * readInt32(int32_t i) override
Called when the JSON parser encounters an int32 value.
virtual IJsonHandler * readArrayStart() override
Called when the JSON parser encounters the start of an array.
virtual IJsonHandler * readUint32(uint32_t i) override
Called when the JSON parser encounters a uint32 value.
virtual IJsonHandler * readObjectStart() override
Called when the JSON parser encounters the beginning of an object.
virtual IJsonHandler * readDouble(double d) override
Called when the JSON parser encounters a double value.
void reset(IJsonHandler *pParent, std::vector< std::string > *pArray)
Resets the parent and destination array of this ArrayJsonHandler.
virtual IJsonHandler * readNull() override
Called when the JSON parser encounters a null.
virtual IJsonHandler * readBool(bool b) override
Called when the JSON parser encounters a boolean value.
virtual IJsonHandler * readInt64(int64_t i) override
Called when the JSON parser encounters an int64 value.
virtual IJsonHandler * readString(const std::string_view &str) override
Called when the JSON parser encounters a string value.
std::vector< std::string > ValueType
The destination type.
virtual void reportWarning(const std::string &warning, std::vector< std::string > &&context=std::vector< std::string >()) override
Report a warning while reading JSON.
virtual IJsonHandler * readUint64(uint64_t i) override
Called when the JSON parser encounters a uint64 value.
virtual IJsonHandler * readNull() override
Called when the JSON parser encounters a null.
virtual void reportWarning(const std::string &warning, std::vector< std::string > &&context=std::vector< std::string >()) override
Report a warning while reading JSON.
virtual IJsonHandler * readUint32(uint32_t i) override
Called when the JSON parser encounters a uint32 value.
virtual IJsonHandler * readArrayStart() override
Called when the JSON parser encounters the start of an array.
virtual IJsonHandler * readArrayEnd() override
Called when the JSON parser encounters the end of an array.
virtual IJsonHandler * readUint64(uint64_t i) override
Called when the JSON parser encounters a uint64 value.
void reset(IJsonHandler *pParent, std::vector< std::vector< T > > *pArray)
Resets the parent and destination array of this ArrayJsonHandler.
virtual IJsonHandler * readDouble(double d) override
Called when the JSON parser encounters a double value.
virtual IJsonHandler * readString(const std::string_view &str) override
Called when the JSON parser encounters a string value.
virtual IJsonHandler * readObjectStart() override
Called when the JSON parser encounters the beginning of an object.
virtual IJsonHandler * readBool(bool b) override
Called when the JSON parser encounters a boolean value.
std::vector< T > ValueType
The destination type.
ArrayJsonHandler(Ts &&... args) noexcept
Creates a new ArrayJsonHandler.
virtual IJsonHandler * readInt64(int64_t i) override
Called when the JSON parser encounters an int64 value.
virtual IJsonHandler * readInt32(int32_t i) override
Called when the JSON parser encounters an int32 value.
IJsonHandler for reading a JSON array into an std::vector.
virtual IJsonHandler * readObjectStart() override
Called when the JSON parser encounters the beginning of an object.
virtual IJsonHandler * readArrayStart() override
Called when the JSON parser encounters the start of an array.
std::vector< T > ValueType
The destination type.
void reset(IJsonHandler *pParent, std::vector< T > *pArray)
Resets the parent and destination array of this ArrayJsonHandler.
virtual IJsonHandler * readObjectKey(const std::string_view &) noexcept override
Called when the JSON parser encounters a key while reading an object.
virtual IJsonHandler * readInt64(int64_t i) override
Called when the JSON parser encounters an int64 value.
virtual IJsonHandler * readUint32(uint32_t i) override
Called when the JSON parser encounters a uint32 value.
virtual IJsonHandler * readString(const std::string_view &str) override
Called when the JSON parser encounters a string value.
virtual IJsonHandler * readArrayEnd() override
Called when the JSON parser encounters the end of an array.
ArrayJsonHandler(Ts &&... args) noexcept
Creates a new ArrayJsonHandler.
virtual IJsonHandler * readUint64(uint64_t i) override
Called when the JSON parser encounters a uint64 value.
virtual IJsonHandler * readDouble(double d) override
Called when the JSON parser encounters a double value.
virtual IJsonHandler * readBool(bool b) override
Called when the JSON parser encounters a boolean value.
virtual IJsonHandler * readNull() override
Called when the JSON parser encounters a null.
virtual IJsonHandler * readInt32(int32_t i) override
Called when the JSON parser encounters an int32 value.
virtual void reportWarning(const std::string &warning, std::vector< std::string > &&context=std::vector< std::string >()) override
Report a warning while reading JSON.
virtual IJsonHandler * readObjectEnd() noexcept override
Called when the JSON parser encounters the end of an object.
IJsonHandler for reading double values.
Base interface for all JSON handlers. Types that need to be deserialized from JSON should implement I...
virtual IJsonHandler * readUint32(uint32_t i)=0
Called when the JSON parser encounters a uint32 value.
virtual IJsonHandler * readUint64(uint64_t i)=0
Called when the JSON parser encounters a uint64 value.
virtual IJsonHandler * readNull()=0
Called when the JSON parser encounters a null.
virtual IJsonHandler * readDouble(double d)=0
Called when the JSON parser encounters a double value.
virtual IJsonHandler * readBool(bool b)=0
Called when the JSON parser encounters a boolean value.
virtual IJsonHandler * readObjectStart()=0
Called when the JSON parser encounters the beginning of an object.
virtual IJsonHandler * readString(const std::string_view &str)=0
Called when the JSON parser encounters a string value.
virtual IJsonHandler * readInt64(int64_t i)=0
Called when the JSON parser encounters an int64 value.
virtual IJsonHandler * readInt32(int32_t i)=0
Called when the JSON parser encounters an int32 value.
IJsonHandler for reading integer values.
A dummy implementation of IJsonHandler that will report a warning and return its parent when any of i...
IJsonHandler for reading string values.
Classes for reading JSON.