forked from bblanchon/ArduinoJson
Cleaned parseArray()
This commit is contained in:
@ -49,6 +49,10 @@ void JsonParser::parseAnythingTo(JsonVariant &destination) {
|
|||||||
destination = parseArray();
|
destination = parseArray();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case '{':
|
||||||
|
destination = parseObject();
|
||||||
|
break;
|
||||||
|
|
||||||
case 't':
|
case 't':
|
||||||
case 'f':
|
case 'f':
|
||||||
parseBooleanTo(destination);
|
parseBooleanTo(destination);
|
||||||
@ -73,10 +77,6 @@ void JsonParser::parseAnythingTo(JsonVariant &destination) {
|
|||||||
parseNullTo(destination);
|
parseNullTo(destination);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '{':
|
|
||||||
destination = parseObject();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '\'':
|
case '\'':
|
||||||
case '\"':
|
case '\"':
|
||||||
destination = parseString();
|
destination = parseString();
|
||||||
@ -87,23 +87,70 @@ void JsonParser::parseAnythingTo(JsonVariant &destination) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonArray &JsonParser::parseArray() {
|
JsonArray &JsonParser::parseArray() {
|
||||||
if (!skip('[')) return JsonArray::invalid(); // missing opening bracket
|
// Create an empty array
|
||||||
|
|
||||||
if (isEnd()) return JsonArray::invalid(); // end of stream
|
|
||||||
|
|
||||||
JsonArray &array = _buffer->createArray();
|
JsonArray &array = _buffer->createArray();
|
||||||
if (skip(']')) return array; // empty array
|
|
||||||
|
|
||||||
|
// Check opening braket
|
||||||
|
if (!skip('[')) goto ERROR_MISSING_BRACKET;
|
||||||
|
if (skip(']')) goto SUCCESS_EMPTY_ARRAY;
|
||||||
|
|
||||||
|
// Read each value
|
||||||
for (;;) {
|
for (;;) {
|
||||||
JsonVariant &child = array.add();
|
// 1 - Parse value
|
||||||
|
JsonVariant &value = array.add();
|
||||||
|
parseAnythingTo(value);
|
||||||
|
if (!value.success()) goto ERROR_INVALID_VALUE;
|
||||||
|
|
||||||
parseAnythingTo(child);
|
// 2 - More values?
|
||||||
if (!child.success()) return JsonArray::invalid(); // child parsing failed
|
if (skip(']')) goto SUCCES_NON_EMPTY_ARRAY;
|
||||||
|
if (!skip(',')) goto ERROR_MISSING_COMMA;
|
||||||
if (skip(']')) return array; // end of the array
|
|
||||||
|
|
||||||
if (!skip(',')) return JsonArray::invalid(); // comma is missing
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SUCCESS_EMPTY_ARRAY:
|
||||||
|
SUCCES_NON_EMPTY_ARRAY:
|
||||||
|
return array;
|
||||||
|
|
||||||
|
ERROR_INVALID_VALUE:
|
||||||
|
ERROR_MISSING_BRACKET:
|
||||||
|
ERROR_MISSING_COMMA:
|
||||||
|
return JsonArray::invalid();
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonObject &JsonParser::parseObject() {
|
||||||
|
// Create an empty object
|
||||||
|
JsonObject &object = _buffer->createObject();
|
||||||
|
|
||||||
|
// Check opening brace
|
||||||
|
if (!skip('{')) goto ERROR_MISSING_BRACE;
|
||||||
|
if (skip('}')) goto SUCCESS_EMPTY_OBJECT;
|
||||||
|
|
||||||
|
// Read each key value pair
|
||||||
|
for (;;) {
|
||||||
|
// 1 - Parse key
|
||||||
|
const char *key = parseString();
|
||||||
|
if (!key) goto ERROR_INVALID_KEY;
|
||||||
|
if (!skip(':')) goto ERROR_MISSING_COLON;
|
||||||
|
|
||||||
|
// 2 - Parse value
|
||||||
|
JsonVariant &value = object.add(key);
|
||||||
|
parseAnythingTo(value);
|
||||||
|
if (!value.success()) goto ERROR_INVALID_VALUE;
|
||||||
|
|
||||||
|
// 3 - More keys/values?
|
||||||
|
if (skip('}')) goto SUCCESS_NON_EMPTY_OBJECT;
|
||||||
|
if (!skip(',')) goto ERROR_MISSING_COMMA;
|
||||||
|
}
|
||||||
|
|
||||||
|
SUCCESS_EMPTY_OBJECT:
|
||||||
|
SUCCESS_NON_EMPTY_OBJECT:
|
||||||
|
return object;
|
||||||
|
|
||||||
|
ERROR_INVALID_KEY:
|
||||||
|
ERROR_INVALID_VALUE:
|
||||||
|
ERROR_MISSING_BRACE:
|
||||||
|
ERROR_MISSING_COLON:
|
||||||
|
ERROR_MISSING_COMMA:
|
||||||
|
return JsonObject::invalid();
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonParser::parseBooleanTo(JsonVariant &destination) {
|
void JsonParser::parseBooleanTo(JsonVariant &destination) {
|
||||||
@ -137,41 +184,6 @@ void JsonParser::parseNullTo(JsonVariant &destination) {
|
|||||||
destination = JsonVariant::invalid();
|
destination = JsonVariant::invalid();
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonObject &JsonParser::parseObject() {
|
|
||||||
JsonObject &object = _buffer->createObject();
|
|
||||||
|
|
||||||
if (!skip('{')) goto ERROR_MISSING_OPENING_BRACE;
|
|
||||||
if (skip('}')) goto SUCCESS_EMPTY_OBJECT;
|
|
||||||
|
|
||||||
// Read each key value pair
|
|
||||||
for (;;) {
|
|
||||||
// 1 - Parse key
|
|
||||||
const char *key = parseString();
|
|
||||||
if (!key) goto ERROR_INVALID_KEY;
|
|
||||||
if (!skip(':')) goto ERROR_MISSING_COLON;
|
|
||||||
|
|
||||||
// 2 - Parse value
|
|
||||||
JsonVariant &value = object.add(key);
|
|
||||||
parseAnythingTo(value);
|
|
||||||
if (!value.success()) goto ERROR_INVALID_VALUE;
|
|
||||||
|
|
||||||
// 3 - More elements?
|
|
||||||
if (skip('}')) goto SUCCESS_NON_EMPTY_OBJECT;
|
|
||||||
if (!skip(',')) goto ERROR_MISSING_COMMA;
|
|
||||||
}
|
|
||||||
|
|
||||||
SUCCESS_EMPTY_OBJECT:
|
|
||||||
SUCCESS_NON_EMPTY_OBJECT:
|
|
||||||
return object;
|
|
||||||
|
|
||||||
ERROR_INVALID_KEY:
|
|
||||||
ERROR_INVALID_VALUE:
|
|
||||||
ERROR_MISSING_COLON:
|
|
||||||
ERROR_MISSING_COMMA:
|
|
||||||
ERROR_MISSING_OPENING_BRACE:
|
|
||||||
return JsonObject::invalid();
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *JsonParser::parseString() {
|
const char *JsonParser::parseString() {
|
||||||
return QuotedString::extractFrom(_ptr, &_ptr);
|
return QuotedString::extractFrom(_ptr, &_ptr);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user