mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 08:19:52 +03:00
remove TODO
This commit is contained in:
@@ -16,24 +16,23 @@ namespace ARDUINOJSON_NAMESPACE {
|
|||||||
template <typename TReader, typename TStringStorage>
|
template <typename TReader, typename TStringStorage>
|
||||||
class MsgPackDeserializer {
|
class MsgPackDeserializer {
|
||||||
public:
|
public:
|
||||||
MsgPackDeserializer(MemoryPool &pool, TReader reader,
|
MsgPackDeserializer(MemoryPool & pool, TReader reader, TStringStorage stringStorage)
|
||||||
TStringStorage stringStorage)
|
: _pool(&pool)
|
||||||
: _pool(&pool),
|
, _reader(reader)
|
||||||
_reader(reader),
|
, _stringStorage(stringStorage)
|
||||||
_stringStorage(stringStorage),
|
, _error(DeserializationError::Ok)
|
||||||
_error(DeserializationError::Ok),
|
, _foundSomething(false) {
|
||||||
_foundSomething(false) {}
|
}
|
||||||
|
|
||||||
template <typename TFilter>
|
template <typename TFilter>
|
||||||
DeserializationError parse(VariantData &variant, TFilter filter,
|
DeserializationError parse(VariantData & variant, TFilter filter, NestingLimit nestingLimit) {
|
||||||
NestingLimit nestingLimit) {
|
|
||||||
parseVariant(&variant, filter, nestingLimit);
|
parseVariant(&variant, filter, nestingLimit);
|
||||||
return _foundSomething ? _error : DeserializationError::EmptyInput;
|
return _foundSomething ? _error : DeserializationError::EmptyInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Prevent VS warning "assignment operator could not be generated"
|
// Prevent VS warning "assignment operator could not be generated"
|
||||||
MsgPackDeserializer &operator=(const MsgPackDeserializer &);
|
MsgPackDeserializer & operator=(const MsgPackDeserializer &);
|
||||||
|
|
||||||
bool invalidInput() {
|
bool invalidInput() {
|
||||||
_error = DeserializationError::InvalidInput;
|
_error = DeserializationError::InvalidInput;
|
||||||
@@ -41,9 +40,8 @@ class MsgPackDeserializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename TFilter>
|
template <typename TFilter>
|
||||||
bool parseVariant(VariantData *variant, TFilter filter,
|
bool parseVariant(VariantData * variant, TFilter filter, NestingLimit nestingLimit) {
|
||||||
NestingLimit nestingLimit) {
|
uint8_t code = 0;
|
||||||
uint8_t code = 0; // TODO: why do we need to initialize this variable?
|
|
||||||
if (!readByte(code))
|
if (!readByte(code))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@@ -222,7 +220,7 @@ class MsgPackDeserializer {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool readByte(uint8_t &value) {
|
bool readByte(uint8_t & value) {
|
||||||
int c = _reader.read();
|
int c = _reader.read();
|
||||||
if (c < 0) {
|
if (c < 0) {
|
||||||
_error = DeserializationError::IncompleteInput;
|
_error = DeserializationError::IncompleteInput;
|
||||||
@@ -232,7 +230,7 @@ class MsgPackDeserializer {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool readBytes(uint8_t *p, size_t n) {
|
bool readBytes(uint8_t * p, size_t n) {
|
||||||
if (_reader.readBytes(reinterpret_cast<char *>(p), n) == n)
|
if (_reader.readBytes(reinterpret_cast<char *>(p), n) == n)
|
||||||
return true;
|
return true;
|
||||||
_error = DeserializationError::IncompleteInput;
|
_error = DeserializationError::IncompleteInput;
|
||||||
@@ -240,7 +238,7 @@ class MsgPackDeserializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool readBytes(T &value) {
|
bool readBytes(T & value) {
|
||||||
return readBytes(reinterpret_cast<uint8_t *>(&value), sizeof(value));
|
return readBytes(reinterpret_cast<uint8_t *>(&value), sizeof(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -255,7 +253,7 @@ class MsgPackDeserializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool readInteger(T &value) {
|
bool readInteger(T & value) {
|
||||||
if (!readBytes(value))
|
if (!readBytes(value))
|
||||||
return false;
|
return false;
|
||||||
fixEndianess(value);
|
fixEndianess(value);
|
||||||
@@ -263,7 +261,7 @@ class MsgPackDeserializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool readInteger(VariantData *variant) {
|
bool readInteger(VariantData * variant) {
|
||||||
T value;
|
T value;
|
||||||
if (!readInteger(value))
|
if (!readInteger(value))
|
||||||
return false;
|
return false;
|
||||||
@@ -272,8 +270,7 @@ class MsgPackDeserializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
typename enable_if<sizeof(T) == 4, bool>::type readFloat(
|
typename enable_if<sizeof(T) == 4, bool>::type readFloat(VariantData * variant) {
|
||||||
VariantData *variant) {
|
|
||||||
T value;
|
T value;
|
||||||
if (!readBytes(value))
|
if (!readBytes(value))
|
||||||
return false;
|
return false;
|
||||||
@@ -283,8 +280,7 @@ class MsgPackDeserializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
typename enable_if<sizeof(T) == 8, bool>::type readDouble(
|
typename enable_if<sizeof(T) == 8, bool>::type readDouble(VariantData * variant) {
|
||||||
VariantData *variant) {
|
|
||||||
T value;
|
T value;
|
||||||
if (!readBytes(value))
|
if (!readBytes(value))
|
||||||
return false;
|
return false;
|
||||||
@@ -294,11 +290,10 @@ class MsgPackDeserializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
typename enable_if<sizeof(T) == 4, bool>::type readDouble(
|
typename enable_if<sizeof(T) == 4, bool>::type readDouble(VariantData * variant) {
|
||||||
VariantData *variant) {
|
|
||||||
uint8_t i[8]; // input is 8 bytes
|
uint8_t i[8]; // input is 8 bytes
|
||||||
T value; // output is 4 bytes
|
T value; // output is 4 bytes
|
||||||
uint8_t *o = reinterpret_cast<uint8_t *>(&value);
|
uint8_t * o = reinterpret_cast<uint8_t *>(&value);
|
||||||
if (!readBytes(i, 8))
|
if (!readBytes(i, 8))
|
||||||
return false;
|
return false;
|
||||||
doubleToFloat(i, o);
|
doubleToFloat(i, o);
|
||||||
@@ -308,7 +303,7 @@ class MsgPackDeserializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool readString(VariantData *variant) {
|
bool readString(VariantData * variant) {
|
||||||
T size;
|
T size;
|
||||||
if (!readInteger(size))
|
if (!readInteger(size))
|
||||||
return false;
|
return false;
|
||||||
@@ -331,11 +326,10 @@ class MsgPackDeserializer {
|
|||||||
return skipBytes(size);
|
return skipBytes(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool readString(VariantData *variant, size_t n) {
|
bool readString(VariantData * variant, size_t n) {
|
||||||
if (!readString(n))
|
if (!readString(n))
|
||||||
return false;
|
return false;
|
||||||
variant->setStringPointer(_stringStorage.save(),
|
variant->setStringPointer(_stringStorage.save(), typename TStringStorage::storage_policy());
|
||||||
typename TStringStorage::storage_policy());
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -357,8 +351,7 @@ class MsgPackDeserializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename TSize, typename TFilter>
|
template <typename TSize, typename TFilter>
|
||||||
bool readArray(VariantData *variant, TFilter filter,
|
bool readArray(VariantData * variant, TFilter filter, NestingLimit nestingLimit) {
|
||||||
NestingLimit nestingLimit) {
|
|
||||||
TSize size;
|
TSize size;
|
||||||
if (!readInteger(size))
|
if (!readInteger(size))
|
||||||
return false;
|
return false;
|
||||||
@@ -366,8 +359,7 @@ class MsgPackDeserializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename TFilter>
|
template <typename TFilter>
|
||||||
bool readArray(VariantData *variant, size_t n, TFilter filter,
|
bool readArray(VariantData * variant, size_t n, TFilter filter, NestingLimit nestingLimit) {
|
||||||
NestingLimit nestingLimit) {
|
|
||||||
if (nestingLimit.reached()) {
|
if (nestingLimit.reached()) {
|
||||||
_error = DeserializationError::TooDeep;
|
_error = DeserializationError::TooDeep;
|
||||||
return false;
|
return false;
|
||||||
@@ -375,12 +367,12 @@ class MsgPackDeserializer {
|
|||||||
|
|
||||||
bool allowArray = filter.allowArray();
|
bool allowArray = filter.allowArray();
|
||||||
|
|
||||||
CollectionData *array = allowArray ? &variant->toArray() : 0;
|
CollectionData * array = allowArray ? &variant->toArray() : 0;
|
||||||
|
|
||||||
TFilter memberFilter = filter[0U];
|
TFilter memberFilter = filter[0U];
|
||||||
|
|
||||||
for (; n; --n) {
|
for (; n; --n) {
|
||||||
VariantData *value;
|
VariantData * value;
|
||||||
|
|
||||||
if (memberFilter.allow()) {
|
if (memberFilter.allow()) {
|
||||||
value = array->addElement(_pool);
|
value = array->addElement(_pool);
|
||||||
@@ -400,8 +392,7 @@ class MsgPackDeserializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename TSize, typename TFilter>
|
template <typename TSize, typename TFilter>
|
||||||
bool readObject(VariantData *variant, TFilter filter,
|
bool readObject(VariantData * variant, TFilter filter, NestingLimit nestingLimit) {
|
||||||
NestingLimit nestingLimit) {
|
|
||||||
TSize size;
|
TSize size;
|
||||||
if (!readInteger(size))
|
if (!readInteger(size))
|
||||||
return false;
|
return false;
|
||||||
@@ -409,29 +400,28 @@ class MsgPackDeserializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename TFilter>
|
template <typename TFilter>
|
||||||
bool readObject(VariantData *variant, size_t n, TFilter filter,
|
bool readObject(VariantData * variant, size_t n, TFilter filter, NestingLimit nestingLimit) {
|
||||||
NestingLimit nestingLimit) {
|
|
||||||
if (nestingLimit.reached()) {
|
if (nestingLimit.reached()) {
|
||||||
_error = DeserializationError::TooDeep;
|
_error = DeserializationError::TooDeep;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
CollectionData *object = filter.allowObject() ? &variant->toObject() : 0;
|
CollectionData * object = filter.allowObject() ? &variant->toObject() : 0;
|
||||||
|
|
||||||
for (; n; --n) {
|
for (; n; --n) {
|
||||||
if (!readKey())
|
if (!readKey())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
const char *key = _stringStorage.c_str();
|
const char * key = _stringStorage.c_str();
|
||||||
TFilter memberFilter = filter[key];
|
TFilter memberFilter = filter[key];
|
||||||
VariantData *member;
|
VariantData * member;
|
||||||
|
|
||||||
if (memberFilter.allow()) {
|
if (memberFilter.allow()) {
|
||||||
// Save key in memory pool.
|
// Save key in memory pool.
|
||||||
// This MUST be done before adding the slot.
|
// This MUST be done before adding the slot.
|
||||||
key = _stringStorage.save();
|
key = _stringStorage.save();
|
||||||
|
|
||||||
VariantSlot *slot = object->addSlot(_pool);
|
VariantSlot * slot = object->addSlot(_pool);
|
||||||
if (!slot) {
|
if (!slot) {
|
||||||
_error = DeserializationError::NoMemory;
|
_error = DeserializationError::NoMemory;
|
||||||
return false;
|
return false;
|
||||||
@@ -482,7 +472,7 @@ class MsgPackDeserializer {
|
|||||||
return skipBytes(size + 1);
|
return skipBytes(size + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
MemoryPool *_pool;
|
MemoryPool * _pool;
|
||||||
TReader _reader;
|
TReader _reader;
|
||||||
TStringStorage _stringStorage;
|
TStringStorage _stringStorage;
|
||||||
DeserializationError _error;
|
DeserializationError _error;
|
||||||
@@ -494,24 +484,17 @@ class MsgPackDeserializer {
|
|||||||
//
|
//
|
||||||
// ... = NestingLimit
|
// ... = NestingLimit
|
||||||
template <typename TString>
|
template <typename TString>
|
||||||
DeserializationError deserializeMsgPack(
|
DeserializationError deserializeMsgPack(JsonDocument & doc, const TString & input, NestingLimit nestingLimit = NestingLimit()) {
|
||||||
JsonDocument &doc, const TString &input,
|
return deserialize<MsgPackDeserializer>(doc, input, nestingLimit, AllowAllFilter());
|
||||||
NestingLimit nestingLimit = NestingLimit()) {
|
|
||||||
return deserialize<MsgPackDeserializer>(doc, input, nestingLimit,
|
|
||||||
AllowAllFilter());
|
|
||||||
}
|
}
|
||||||
// ... = Filter, NestingLimit
|
// ... = Filter, NestingLimit
|
||||||
template <typename TString>
|
template <typename TString>
|
||||||
DeserializationError deserializeMsgPack(
|
DeserializationError deserializeMsgPack(JsonDocument & doc, const TString & input, Filter filter, NestingLimit nestingLimit = NestingLimit()) {
|
||||||
JsonDocument &doc, const TString &input, Filter filter,
|
|
||||||
NestingLimit nestingLimit = NestingLimit()) {
|
|
||||||
return deserialize<MsgPackDeserializer>(doc, input, nestingLimit, filter);
|
return deserialize<MsgPackDeserializer>(doc, input, nestingLimit, filter);
|
||||||
}
|
}
|
||||||
// ... = NestingLimit, Filter
|
// ... = NestingLimit, Filter
|
||||||
template <typename TString>
|
template <typename TString>
|
||||||
DeserializationError deserializeMsgPack(JsonDocument &doc, const TString &input,
|
DeserializationError deserializeMsgPack(JsonDocument & doc, const TString & input, NestingLimit nestingLimit, Filter filter) {
|
||||||
NestingLimit nestingLimit,
|
|
||||||
Filter filter) {
|
|
||||||
return deserialize<MsgPackDeserializer>(doc, input, nestingLimit, filter);
|
return deserialize<MsgPackDeserializer>(doc, input, nestingLimit, filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -520,24 +503,17 @@ DeserializationError deserializeMsgPack(JsonDocument &doc, const TString &input,
|
|||||||
//
|
//
|
||||||
// ... = NestingLimit
|
// ... = NestingLimit
|
||||||
template <typename TStream>
|
template <typename TStream>
|
||||||
DeserializationError deserializeMsgPack(
|
DeserializationError deserializeMsgPack(JsonDocument & doc, TStream & input, NestingLimit nestingLimit = NestingLimit()) {
|
||||||
JsonDocument &doc, TStream &input,
|
return deserialize<MsgPackDeserializer>(doc, input, nestingLimit, AllowAllFilter());
|
||||||
NestingLimit nestingLimit = NestingLimit()) {
|
|
||||||
return deserialize<MsgPackDeserializer>(doc, input, nestingLimit,
|
|
||||||
AllowAllFilter());
|
|
||||||
}
|
}
|
||||||
// ... = Filter, NestingLimit
|
// ... = Filter, NestingLimit
|
||||||
template <typename TStream>
|
template <typename TStream>
|
||||||
DeserializationError deserializeMsgPack(
|
DeserializationError deserializeMsgPack(JsonDocument & doc, TStream & input, Filter filter, NestingLimit nestingLimit = NestingLimit()) {
|
||||||
JsonDocument &doc, TStream &input, Filter filter,
|
|
||||||
NestingLimit nestingLimit = NestingLimit()) {
|
|
||||||
return deserialize<MsgPackDeserializer>(doc, input, nestingLimit, filter);
|
return deserialize<MsgPackDeserializer>(doc, input, nestingLimit, filter);
|
||||||
}
|
}
|
||||||
// ... = NestingLimit, Filter
|
// ... = NestingLimit, Filter
|
||||||
template <typename TStream>
|
template <typename TStream>
|
||||||
DeserializationError deserializeMsgPack(JsonDocument &doc, TStream &input,
|
DeserializationError deserializeMsgPack(JsonDocument & doc, TStream & input, NestingLimit nestingLimit, Filter filter) {
|
||||||
NestingLimit nestingLimit,
|
|
||||||
Filter filter) {
|
|
||||||
return deserialize<MsgPackDeserializer>(doc, input, nestingLimit, filter);
|
return deserialize<MsgPackDeserializer>(doc, input, nestingLimit, filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -546,24 +522,17 @@ DeserializationError deserializeMsgPack(JsonDocument &doc, TStream &input,
|
|||||||
//
|
//
|
||||||
// ... = NestingLimit
|
// ... = NestingLimit
|
||||||
template <typename TChar>
|
template <typename TChar>
|
||||||
DeserializationError deserializeMsgPack(
|
DeserializationError deserializeMsgPack(JsonDocument & doc, TChar * input, NestingLimit nestingLimit = NestingLimit()) {
|
||||||
JsonDocument &doc, TChar *input,
|
return deserialize<MsgPackDeserializer>(doc, input, nestingLimit, AllowAllFilter());
|
||||||
NestingLimit nestingLimit = NestingLimit()) {
|
|
||||||
return deserialize<MsgPackDeserializer>(doc, input, nestingLimit,
|
|
||||||
AllowAllFilter());
|
|
||||||
}
|
}
|
||||||
// ... = Filter, NestingLimit
|
// ... = Filter, NestingLimit
|
||||||
template <typename TChar>
|
template <typename TChar>
|
||||||
DeserializationError deserializeMsgPack(
|
DeserializationError deserializeMsgPack(JsonDocument & doc, TChar * input, Filter filter, NestingLimit nestingLimit = NestingLimit()) {
|
||||||
JsonDocument &doc, TChar *input, Filter filter,
|
|
||||||
NestingLimit nestingLimit = NestingLimit()) {
|
|
||||||
return deserialize<MsgPackDeserializer>(doc, input, nestingLimit, filter);
|
return deserialize<MsgPackDeserializer>(doc, input, nestingLimit, filter);
|
||||||
}
|
}
|
||||||
// ... = NestingLimit, Filter
|
// ... = NestingLimit, Filter
|
||||||
template <typename TChar>
|
template <typename TChar>
|
||||||
DeserializationError deserializeMsgPack(JsonDocument &doc, TChar *input,
|
DeserializationError deserializeMsgPack(JsonDocument & doc, TChar * input, NestingLimit nestingLimit, Filter filter) {
|
||||||
NestingLimit nestingLimit,
|
|
||||||
Filter filter) {
|
|
||||||
return deserialize<MsgPackDeserializer>(doc, input, nestingLimit, filter);
|
return deserialize<MsgPackDeserializer>(doc, input, nestingLimit, filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -572,28 +541,18 @@ DeserializationError deserializeMsgPack(JsonDocument &doc, TChar *input,
|
|||||||
//
|
//
|
||||||
// ... = NestingLimit
|
// ... = NestingLimit
|
||||||
template <typename TChar>
|
template <typename TChar>
|
||||||
DeserializationError deserializeMsgPack(
|
DeserializationError deserializeMsgPack(JsonDocument & doc, TChar * input, size_t inputSize, NestingLimit nestingLimit = NestingLimit()) {
|
||||||
JsonDocument &doc, TChar *input, size_t inputSize,
|
return deserialize<MsgPackDeserializer>(doc, input, inputSize, nestingLimit, AllowAllFilter());
|
||||||
NestingLimit nestingLimit = NestingLimit()) {
|
|
||||||
return deserialize<MsgPackDeserializer>(doc, input, inputSize, nestingLimit,
|
|
||||||
AllowAllFilter());
|
|
||||||
}
|
}
|
||||||
// ... = Filter, NestingLimit
|
// ... = Filter, NestingLimit
|
||||||
template <typename TChar>
|
template <typename TChar>
|
||||||
DeserializationError deserializeMsgPack(
|
DeserializationError deserializeMsgPack(JsonDocument & doc, TChar * input, size_t inputSize, Filter filter, NestingLimit nestingLimit = NestingLimit()) {
|
||||||
JsonDocument &doc, TChar *input, size_t inputSize, Filter filter,
|
return deserialize<MsgPackDeserializer>(doc, input, inputSize, nestingLimit, filter);
|
||||||
NestingLimit nestingLimit = NestingLimit()) {
|
|
||||||
return deserialize<MsgPackDeserializer>(doc, input, inputSize, nestingLimit,
|
|
||||||
filter);
|
|
||||||
}
|
}
|
||||||
// ... = NestingLimit, Filter
|
// ... = NestingLimit, Filter
|
||||||
template <typename TChar>
|
template <typename TChar>
|
||||||
DeserializationError deserializeMsgPack(JsonDocument &doc, TChar *input,
|
DeserializationError deserializeMsgPack(JsonDocument & doc, TChar * input, size_t inputSize, NestingLimit nestingLimit, Filter filter) {
|
||||||
size_t inputSize,
|
return deserialize<MsgPackDeserializer>(doc, input, inputSize, nestingLimit, filter);
|
||||||
NestingLimit nestingLimit,
|
|
||||||
Filter filter) {
|
|
||||||
return deserialize<MsgPackDeserializer>(doc, input, inputSize, nestingLimit,
|
|
||||||
filter);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ARDUINOJSON_NAMESPACE
|
} // namespace ARDUINOJSON_NAMESPACE
|
||||||
|
|||||||
Reference in New Issue
Block a user