Remove member JsonDeserializer::_error

This reduces the code size by 72 bytes on AVR.
This commit is contained in:
Benoit Blanchon
2022-07-29 10:53:23 +02:00
parent e2a29eef24
commit a6da4ad452

View File

@ -25,20 +25,21 @@ class JsonDeserializer {
: _stringStorage(stringStorage), : _stringStorage(stringStorage),
_foundSomething(false), _foundSomething(false),
_latch(reader), _latch(reader),
_pool(&pool), _pool(&pool) {}
_error(DeserializationError::Ok) {}
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); DeserializationError::Code err;
if (!_error && _latch.last() != 0 && !variant.isEnclosed()) { err = parseVariant(variant, filter, nestingLimit);
if (!err && _latch.last() != 0 && !variant.isEnclosed()) {
// We don't detect trailing characters earlier, so we need to check now // We don't detect trailing characters earlier, so we need to check now
return DeserializationError::InvalidInput; return DeserializationError::InvalidInput;
} }
return _error; return err;
} }
private: private:
@ -58,10 +59,13 @@ class JsonDeserializer {
} }
template <typename TFilter> template <typename TFilter>
bool parseVariant(VariantData &variant, TFilter filter, DeserializationError::Code parseVariant(VariantData &variant, TFilter filter,
NestingLimit nestingLimit) { NestingLimit nestingLimit) {
if (!skipSpacesAndComments()) DeserializationError::Code err;
return false;
err = skipSpacesAndComments();
if (err)
return err;
switch (current()) { switch (current()) {
case '[': case '[':
@ -91,9 +95,12 @@ class JsonDeserializer {
} }
} }
bool skipVariant(NestingLimit nestingLimit) { DeserializationError::Code skipVariant(NestingLimit nestingLimit) {
if (!skipSpacesAndComments()) DeserializationError::Code err;
return false;
err = skipSpacesAndComments();
if (err)
return err;
switch (current()) { switch (current()) {
case '[': case '[':
@ -112,24 +119,25 @@ class JsonDeserializer {
} }
template <typename TFilter> template <typename TFilter>
bool parseArray(CollectionData &array, TFilter filter, DeserializationError::Code parseArray(CollectionData &array, TFilter filter,
NestingLimit nestingLimit) { NestingLimit nestingLimit) {
if (nestingLimit.reached()) { DeserializationError::Code err;
_error = DeserializationError::TooDeep;
return false; if (nestingLimit.reached())
} return DeserializationError::TooDeep;
// Skip opening braket // Skip opening braket
ARDUINOJSON_ASSERT(current() == '['); ARDUINOJSON_ASSERT(current() == '[');
move(); move();
// Skip spaces // Skip spaces
if (!skipSpacesAndComments()) err = skipSpacesAndComments();
return false; if (err)
return err;
// Empty array? // Empty array?
if (eat(']')) if (eat(']'))
return true; return DeserializationError::Ok;
TFilter memberFilter = filter[0UL]; TFilter memberFilter = filter[0UL];
@ -138,38 +146,37 @@ class JsonDeserializer {
if (memberFilter.allow()) { if (memberFilter.allow()) {
// Allocate slot in array // Allocate slot in array
VariantData *value = array.addElement(_pool); VariantData *value = array.addElement(_pool);
if (!value) { if (!value)
_error = DeserializationError::NoMemory; return DeserializationError::NoMemory;
return false;
}
// 1 - Parse value // 1 - Parse value
if (!parseVariant(*value, memberFilter, nestingLimit.decrement())) err = parseVariant(*value, memberFilter, nestingLimit.decrement());
return false; if (err)
return err;
} else { } else {
if (!skipVariant(nestingLimit.decrement())) err = skipVariant(nestingLimit.decrement());
return false; if (err)
return err;
} }
// 2 - Skip spaces // 2 - Skip spaces
if (!skipSpacesAndComments()) err = skipSpacesAndComments();
return false; if (err)
return err;
// 3 - More values? // 3 - More values?
if (eat(']')) if (eat(']'))
return true; return DeserializationError::Ok;
if (!eat(',')) { if (!eat(','))
_error = DeserializationError::InvalidInput; return DeserializationError::InvalidInput;
return false;
}
} }
} }
bool skipArray(NestingLimit nestingLimit) { DeserializationError::Code skipArray(NestingLimit nestingLimit) {
if (nestingLimit.reached()) { DeserializationError::Code err;
_error = DeserializationError::TooDeep;
return false; if (nestingLimit.reached())
} return DeserializationError::TooDeep;
// Skip opening braket // Skip opening braket
ARDUINOJSON_ASSERT(current() == '['); ARDUINOJSON_ASSERT(current() == '[');
@ -178,58 +185,59 @@ class JsonDeserializer {
// Read each value // Read each value
for (;;) { for (;;) {
// 1 - Skip value // 1 - Skip value
if (!skipVariant(nestingLimit.decrement())) err = skipVariant(nestingLimit.decrement());
return false; if (err)
return err;
// 2 - Skip spaces // 2 - Skip spaces
if (!skipSpacesAndComments()) err = skipSpacesAndComments();
return false; if (err)
return err;
// 3 - More values? // 3 - More values?
if (eat(']')) if (eat(']'))
return true; return DeserializationError::Ok;
if (!eat(',')) { if (!eat(','))
_error = DeserializationError::InvalidInput; return DeserializationError::InvalidInput;
return false;
}
} }
} }
template <typename TFilter> template <typename TFilter>
bool parseObject(CollectionData &object, TFilter filter, DeserializationError::Code parseObject(CollectionData &object, TFilter filter,
NestingLimit nestingLimit) { NestingLimit nestingLimit) {
if (nestingLimit.reached()) { DeserializationError::Code err;
_error = DeserializationError::TooDeep;
return false; if (nestingLimit.reached())
} return DeserializationError::TooDeep;
// Skip opening brace // Skip opening brace
ARDUINOJSON_ASSERT(current() == '{'); ARDUINOJSON_ASSERT(current() == '{');
move(); move();
// Skip spaces // Skip spaces
if (!skipSpacesAndComments()) err = skipSpacesAndComments();
return false; if (err)
return err;
// Empty object? // Empty object?
if (eat('}')) if (eat('}'))
return true; return DeserializationError::Ok;
// Read each key value pair // Read each key value pair
for (;;) { for (;;) {
// Parse key // Parse key
if (!parseKey()) err = parseKey();
return false; if (err)
return err;
// Skip spaces // Skip spaces
if (!skipSpacesAndComments()) err = skipSpacesAndComments();
return false; if (err)
return err;
// Colon // Colon
if (!eat(':')) { if (!eat(':'))
_error = DeserializationError::InvalidInput; return DeserializationError::InvalidInput;
return false;
}
String key = _stringStorage.str(); String key = _stringStorage.str();
@ -244,10 +252,8 @@ class JsonDeserializer {
// Allocate slot in object // Allocate slot in object
VariantSlot *slot = object.addSlot(_pool); VariantSlot *slot = object.addSlot(_pool);
if (!slot) { if (!slot)
_error = DeserializationError::NoMemory; return DeserializationError::NoMemory;
return false;
}
slot->setKey(key); slot->setKey(key);
@ -255,84 +261,87 @@ class JsonDeserializer {
} }
// Parse value // Parse value
if (!parseVariant(*variant, memberFilter, nestingLimit.decrement())) err = parseVariant(*variant, memberFilter, nestingLimit.decrement());
return false; if (err)
return err;
} else { } else {
if (!skipVariant(nestingLimit.decrement())) err = skipVariant(nestingLimit.decrement());
return false; if (err)
return err;
} }
// Skip spaces // Skip spaces
if (!skipSpacesAndComments()) err = skipSpacesAndComments();
return false; if (err)
return err;
// More keys/values? // More keys/values?
if (eat('}')) if (eat('}'))
return true; return DeserializationError::Ok;
if (!eat(',')) { if (!eat(','))
_error = DeserializationError::InvalidInput; return DeserializationError::InvalidInput;
return false;
}
// Skip spaces // Skip spaces
if (!skipSpacesAndComments()) err = skipSpacesAndComments();
return false; if (err)
return err;
} }
} }
bool skipObject(NestingLimit nestingLimit) { DeserializationError::Code skipObject(NestingLimit nestingLimit) {
if (nestingLimit.reached()) { DeserializationError::Code err;
_error = DeserializationError::TooDeep;
return false; if (nestingLimit.reached())
} return DeserializationError::TooDeep;
// Skip opening brace // Skip opening brace
ARDUINOJSON_ASSERT(current() == '{'); ARDUINOJSON_ASSERT(current() == '{');
move(); move();
// Skip spaces // Skip spaces
if (!skipSpacesAndComments()) err = skipSpacesAndComments();
return false; if (err)
return err;
// Empty object? // Empty object?
if (eat('}')) if (eat('}'))
return true; return DeserializationError::Ok;
// Read each key value pair // Read each key value pair
for (;;) { for (;;) {
// Skip key // Skip key
if (!skipVariant(nestingLimit.decrement())) err = skipVariant(nestingLimit.decrement());
return false; if (err)
return err;
// Skip spaces // Skip spaces
if (!skipSpacesAndComments()) err = skipSpacesAndComments();
return false; if (err)
return err;
// Colon // Colon
if (!eat(':')) { if (!eat(':'))
_error = DeserializationError::InvalidInput; return DeserializationError::InvalidInput;
return false;
}
// Skip value // Skip value
if (!skipVariant(nestingLimit.decrement())) err = skipVariant(nestingLimit.decrement());
return false; if (err)
return err;
// Skip spaces // Skip spaces
if (!skipSpacesAndComments()) err = skipSpacesAndComments();
return false; if (err)
return err;
// More keys/values? // More keys/values?
if (eat('}')) if (eat('}'))
return true; return DeserializationError::Ok;
if (!eat(',')) { if (!eat(','))
_error = DeserializationError::InvalidInput; return DeserializationError::InvalidInput;
return false;
}
} }
} }
bool parseKey() { DeserializationError::Code parseKey() {
_stringStorage.startString(); _stringStorage.startString();
if (isQuote(current())) { if (isQuote(current())) {
return parseQuotedString(); return parseQuotedString();
@ -341,17 +350,24 @@ class JsonDeserializer {
} }
} }
bool parseStringValue(VariantData &variant) { DeserializationError::Code parseStringValue(VariantData &variant) {
DeserializationError::Code err;
_stringStorage.startString(); _stringStorage.startString();
if (!parseQuotedString())
return false; err = parseQuotedString();
if (err)
return err;
variant.setString(_stringStorage.save()); variant.setString(_stringStorage.save());
return true;
return DeserializationError::Ok;
} }
bool parseQuotedString() { DeserializationError::Code parseQuotedString() {
#if ARDUINOJSON_DECODE_UNICODE #if ARDUINOJSON_DECODE_UNICODE
Utf16::Codepoint codepoint; Utf16::Codepoint codepoint;
DeserializationError::Code err;
#endif #endif
const char stopChar = current(); const char stopChar = current();
@ -362,25 +378,22 @@ class JsonDeserializer {
if (c == stopChar) if (c == stopChar)
break; break;
if (c == '\0') { if (c == '\0')
_error = DeserializationError::IncompleteInput; return DeserializationError::IncompleteInput;
return false;
}
if (c == '\\') { if (c == '\\') {
c = current(); c = current();
if (c == '\0') { if (c == '\0')
_error = DeserializationError::IncompleteInput; return DeserializationError::IncompleteInput;
return false;
}
if (c == 'u') { if (c == 'u') {
#if ARDUINOJSON_DECODE_UNICODE #if ARDUINOJSON_DECODE_UNICODE
move(); move();
uint16_t codeunit; uint16_t codeunit;
if (!parseHex4(codeunit)) err = parseHex4(codeunit);
return false; if (err)
return err;
if (codepoint.append(codeunit)) if (codepoint.append(codeunit))
Utf8::encodeCodepoint(codepoint.value(), _stringStorage); Utf8::encodeCodepoint(codepoint.value(), _stringStorage);
#else #else
@ -391,25 +404,21 @@ class JsonDeserializer {
// replace char // replace char
c = EscapeSequence::unescapeChar(c); c = EscapeSequence::unescapeChar(c);
if (c == '\0') { if (c == '\0')
_error = DeserializationError::InvalidInput; return DeserializationError::InvalidInput;
return false;
}
move(); move();
} }
_stringStorage.append(c); _stringStorage.append(c);
} }
if (!_stringStorage.isValid()) { if (!_stringStorage.isValid())
_error = DeserializationError::NoMemory; return DeserializationError::NoMemory;
return false;
}
return true; return DeserializationError::Ok;
} }
bool parseNonQuotedString() { DeserializationError::Code parseNonQuotedString() {
char c = current(); char c = current();
ARDUINOJSON_ASSERT(c); ARDUINOJSON_ASSERT(c);
@ -420,19 +429,16 @@ class JsonDeserializer {
c = current(); c = current();
} while (canBeInNonQuotedString(c)); } while (canBeInNonQuotedString(c));
} else { } else {
_error = DeserializationError::InvalidInput; return DeserializationError::InvalidInput;
return false;
} }
if (!_stringStorage.isValid()) { if (!_stringStorage.isValid())
_error = DeserializationError::NoMemory; return DeserializationError::NoMemory;
return false;
}
return true; return DeserializationError::Ok;
} }
bool skipString() { DeserializationError::Code skipString() {
const char stopChar = current(); const char stopChar = current();
move(); move();
@ -441,20 +447,18 @@ class JsonDeserializer {
move(); move();
if (c == stopChar) if (c == stopChar)
break; break;
if (c == '\0') { if (c == '\0')
_error = DeserializationError::IncompleteInput; return DeserializationError::IncompleteInput;
return false;
}
if (c == '\\') { if (c == '\\') {
if (current() != '\0') if (current() != '\0')
move(); move();
} }
} }
return true; return DeserializationError::Ok;
} }
bool parseNumericValue(VariantData &result) { DeserializationError::Code parseNumericValue(VariantData &result) {
uint8_t n = 0; uint8_t n = 0;
char c = current(); char c = current();
@ -468,63 +472,51 @@ class JsonDeserializer {
c = _buffer[0]; c = _buffer[0];
if (c == 't') { // true if (c == 't') { // true
result.setBoolean(true); result.setBoolean(true);
if (n != 4) { if (n != 4)
_error = DeserializationError::IncompleteInput; return DeserializationError::IncompleteInput;
return false; return DeserializationError::Ok;
}
return true;
} }
if (c == 'f') { // false if (c == 'f') { // false
result.setBoolean(false); result.setBoolean(false);
if (n != 5) { if (n != 5)
_error = DeserializationError::IncompleteInput; return DeserializationError::IncompleteInput;
return false; return DeserializationError::Ok;
}
return true;
} }
if (c == 'n') { // null if (c == 'n') { // null
// the variant is already null // the variant is already null
if (n != 4) { if (n != 4)
_error = DeserializationError::IncompleteInput; return DeserializationError::IncompleteInput;
return false; return DeserializationError::Ok;
}
return true;
} }
if (!parseNumber(_buffer, result)) { if (!parseNumber(_buffer, result))
_error = DeserializationError::InvalidInput; return DeserializationError::InvalidInput;
return false;
}
return true; return DeserializationError::Ok;
} }
bool skipNumericValue() { DeserializationError::Code skipNumericValue() {
char c = current(); char c = current();
while (canBeInNonQuotedString(c)) { while (canBeInNonQuotedString(c)) {
move(); move();
c = current(); c = current();
} }
return true; return DeserializationError::Ok;
} }
bool parseHex4(uint16_t &result) { DeserializationError::Code parseHex4(uint16_t &result) {
result = 0; result = 0;
for (uint8_t i = 0; i < 4; ++i) { for (uint8_t i = 0; i < 4; ++i) {
char digit = current(); char digit = current();
if (!digit) { if (!digit)
_error = DeserializationError::IncompleteInput; return DeserializationError::IncompleteInput;
return false;
}
uint8_t value = decodeHex(digit); uint8_t value = decodeHex(digit);
if (value > 0x0F) { if (value > 0x0F)
_error = DeserializationError::InvalidInput; return DeserializationError::InvalidInput;
return false;
}
result = uint16_t((result << 4) | value); result = uint16_t((result << 4) | value);
move(); move();
} }
return true; return DeserializationError::Ok;
} }
static inline bool isBetween(char c, char min, char max) { static inline bool isBetween(char c, char min, char max) {
@ -547,14 +539,13 @@ class JsonDeserializer {
return uint8_t(c - 'A' + 10); return uint8_t(c - 'A' + 10);
} }
bool skipSpacesAndComments() { DeserializationError::Code skipSpacesAndComments() {
for (;;) { for (;;) {
switch (current()) { switch (current()) {
// end of string // end of string
case '\0': case '\0':
_error = _foundSomething ? DeserializationError::IncompleteInput return _foundSomething ? DeserializationError::IncompleteInput
: DeserializationError::EmptyInput; : DeserializationError::EmptyInput;
return false;
// spaces // spaces
case ' ': case ' ':
@ -575,10 +566,8 @@ class JsonDeserializer {
bool wasStar = false; bool wasStar = false;
for (;;) { for (;;) {
char c = current(); char c = current();
if (c == '\0') { if (c == '\0')
_error = DeserializationError::IncompleteInput; return DeserializationError::IncompleteInput;
return false;
}
if (c == '/' && wasStar) { if (c == '/' && wasStar) {
move(); move();
break; break;
@ -595,10 +584,8 @@ class JsonDeserializer {
for (;;) { for (;;) {
move(); move();
char c = current(); char c = current();
if (c == '\0') { if (c == '\0')
_error = DeserializationError::IncompleteInput; return DeserializationError::IncompleteInput;
return false;
}
if (c == '\n') if (c == '\n')
break; break;
} }
@ -606,15 +593,14 @@ class JsonDeserializer {
// not a comment, just a '/' // not a comment, just a '/'
default: default:
_error = DeserializationError::InvalidInput; return DeserializationError::InvalidInput;
return false;
} }
break; break;
#endif #endif
default: default:
_foundSomething = true; _foundSomething = true;
return true; return DeserializationError::Ok;
} }
} }
} }
@ -626,7 +612,6 @@ class JsonDeserializer {
char _buffer[64]; // using a member instead of a local variable because it char _buffer[64]; // using a member instead of a local variable because it
// ended in the recursive path after compiler inlined the // ended in the recursive path after compiler inlined the
// code // code
DeserializationError _error;
}; };
// //