2014-10-23 23:39:22 +02:00
|
|
|
// Copyright Benoit Blanchon 2014
|
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Arduino JSON library
|
|
|
|
// https://github.com/bblanchon/ArduinoJson
|
|
|
|
|
2014-11-03 18:35:22 +01:00
|
|
|
#include "../../include/ArduinoJson/Internals/JsonParser.hpp"
|
2014-10-17 19:57:00 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
#include <stdlib.h> // for strtol, strtod
|
2014-10-14 21:24:26 +02:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2014-11-04 10:01:21 +01:00
|
|
|
#include "../../include/ArduinoJson/Internals/QuotedString.hpp"
|
2014-11-03 18:35:22 +01:00
|
|
|
#include "../../include/ArduinoJson/JsonArray.hpp"
|
|
|
|
#include "../../include/ArduinoJson/JsonBuffer.hpp"
|
|
|
|
#include "../../include/ArduinoJson/JsonObject.hpp"
|
2014-10-17 19:57:00 +02:00
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
using namespace ArduinoJson;
|
2014-10-17 19:57:00 +02:00
|
|
|
using namespace ArduinoJson::Internals;
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
void JsonParser::skipSpaces() {
|
2014-10-23 23:13:13 +02:00
|
|
|
while (isspace(*_ptr)) _ptr++;
|
2014-10-15 23:10:52 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
bool JsonParser::skip(char charToSkip) {
|
|
|
|
skipSpaces();
|
2014-10-23 23:13:13 +02:00
|
|
|
if (*_ptr != charToSkip) return false;
|
2014-10-23 19:54:00 +02:00
|
|
|
_ptr++;
|
|
|
|
skipSpaces();
|
|
|
|
return true;
|
2014-10-14 21:24:26 +02:00
|
|
|
}
|
|
|
|
|
2014-10-31 21:08:04 +01:00
|
|
|
bool JsonParser::skip(const char *wordToSkip) {
|
|
|
|
const char *charToSkip = wordToSkip;
|
|
|
|
while (*charToSkip && *_ptr == *charToSkip) {
|
|
|
|
charToSkip++;
|
|
|
|
_ptr++;
|
|
|
|
}
|
|
|
|
return *charToSkip == '\0';
|
|
|
|
}
|
|
|
|
|
2014-11-04 09:51:25 +01:00
|
|
|
void JsonParser::parseAnythingTo(JsonVariant &destination) {
|
2014-11-06 10:24:37 +01:00
|
|
|
if (_nestingLimit == 0) return;
|
|
|
|
_nestingLimit--;
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
skipSpaces();
|
|
|
|
|
|
|
|
switch (*_ptr) {
|
2014-10-23 23:13:13 +02:00
|
|
|
case '[':
|
2014-10-26 21:18:09 +01:00
|
|
|
destination = parseArray();
|
|
|
|
break;
|
2014-10-23 23:13:13 +02:00
|
|
|
|
2014-11-07 09:30:00 +01:00
|
|
|
case '{':
|
|
|
|
destination = parseObject();
|
|
|
|
break;
|
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
case 't':
|
|
|
|
case 'f':
|
2014-10-26 21:18:09 +01:00
|
|
|
parseBooleanTo(destination);
|
|
|
|
break;
|
2014-10-23 23:13:13 +02:00
|
|
|
|
|
|
|
case '-':
|
|
|
|
case '.':
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
2014-10-26 21:18:09 +01:00
|
|
|
parseNumberTo(destination);
|
|
|
|
break;
|
2014-10-23 23:13:13 +02:00
|
|
|
|
|
|
|
case 'n':
|
2014-10-26 21:18:09 +01:00
|
|
|
parseNullTo(destination);
|
|
|
|
break;
|
2014-10-23 23:13:13 +02:00
|
|
|
|
|
|
|
case '\'':
|
|
|
|
case '\"':
|
2014-10-26 21:18:09 +01:00
|
|
|
destination = parseString();
|
|
|
|
break;
|
2014-10-23 19:54:00 +02:00
|
|
|
}
|
2014-11-06 10:24:37 +01:00
|
|
|
|
|
|
|
_nestingLimit++;
|
2014-10-14 21:24:26 +02:00
|
|
|
}
|
|
|
|
|
2014-10-29 14:24:34 +01:00
|
|
|
JsonArray &JsonParser::parseArray() {
|
2014-11-07 09:30:00 +01:00
|
|
|
// Create an empty array
|
2014-10-29 14:24:34 +01:00
|
|
|
JsonArray &array = _buffer->createArray();
|
2014-10-14 21:24:26 +02:00
|
|
|
|
2014-11-07 09:30:00 +01:00
|
|
|
// Check opening braket
|
|
|
|
if (!skip('[')) goto ERROR_MISSING_BRACKET;
|
|
|
|
if (skip(']')) goto SUCCESS_EMPTY_ARRAY;
|
2014-10-14 21:48:22 +02:00
|
|
|
|
2014-11-07 09:30:00 +01:00
|
|
|
// Read each value
|
|
|
|
for (;;) {
|
|
|
|
// 1 - Parse value
|
|
|
|
JsonVariant &value = array.add();
|
|
|
|
parseAnythingTo(value);
|
|
|
|
if (!value.success()) goto ERROR_INVALID_VALUE;
|
2014-10-14 21:48:22 +02:00
|
|
|
|
2014-11-07 09:30:00 +01:00
|
|
|
// 2 - More values?
|
|
|
|
if (skip(']')) goto SUCCES_NON_EMPTY_ARRAY;
|
|
|
|
if (!skip(',')) goto ERROR_MISSING_COMMA;
|
2014-10-23 19:54:00 +02:00
|
|
|
}
|
2014-10-14 21:24:26 +02:00
|
|
|
|
2014-11-07 09:30:00 +01:00
|
|
|
SUCCESS_EMPTY_ARRAY:
|
|
|
|
SUCCES_NON_EMPTY_ARRAY:
|
|
|
|
return array;
|
2014-10-15 23:10:52 +02:00
|
|
|
|
2014-11-07 09:30:00 +01:00
|
|
|
ERROR_INVALID_VALUE:
|
|
|
|
ERROR_MISSING_BRACKET:
|
|
|
|
ERROR_MISSING_COMMA:
|
|
|
|
return JsonArray::invalid();
|
2014-10-15 23:10:52 +02:00
|
|
|
}
|
2014-10-15 23:39:25 +02:00
|
|
|
|
2014-10-29 14:24:34 +01:00
|
|
|
JsonObject &JsonParser::parseObject() {
|
2014-11-07 09:30:00 +01:00
|
|
|
// Create an empty object
|
2014-10-29 14:24:34 +01:00
|
|
|
JsonObject &object = _buffer->createObject();
|
2014-10-21 23:37:17 +02:00
|
|
|
|
2014-11-07 09:30:00 +01:00
|
|
|
// Check opening brace
|
|
|
|
if (!skip('{')) goto ERROR_MISSING_BRACE;
|
2014-11-07 09:20:48 +01:00
|
|
|
if (skip('}')) goto SUCCESS_EMPTY_OBJECT;
|
2014-10-21 23:37:17 +02:00
|
|
|
|
2014-11-07 09:20:48 +01:00
|
|
|
// Read each key value pair
|
2014-10-23 19:54:00 +02:00
|
|
|
for (;;) {
|
2014-11-07 09:20:48 +01:00
|
|
|
// 1 - Parse key
|
2014-10-26 21:18:09 +01:00
|
|
|
const char *key = parseString();
|
2014-11-07 09:20:48 +01:00
|
|
|
if (!key) goto ERROR_INVALID_KEY;
|
|
|
|
if (!skip(':')) goto ERROR_MISSING_COLON;
|
2014-10-22 10:55:36 +02:00
|
|
|
|
2014-11-07 09:20:48 +01:00
|
|
|
// 2 - Parse value
|
2014-11-06 16:29:29 +01:00
|
|
|
JsonVariant &value = object.add(key);
|
2014-10-29 14:24:34 +01:00
|
|
|
parseAnythingTo(value);
|
2014-11-07 09:20:48 +01:00
|
|
|
if (!value.success()) goto ERROR_INVALID_VALUE;
|
2014-10-21 23:37:17 +02:00
|
|
|
|
2014-11-07 09:30:00 +01:00
|
|
|
// 3 - More keys/values?
|
2014-11-07 09:20:48 +01:00
|
|
|
if (skip('}')) goto SUCCESS_NON_EMPTY_OBJECT;
|
|
|
|
if (!skip(',')) goto ERROR_MISSING_COMMA;
|
2014-10-23 19:54:00 +02:00
|
|
|
}
|
2014-10-29 14:24:34 +01:00
|
|
|
|
2014-11-07 09:20:48 +01:00
|
|
|
SUCCESS_EMPTY_OBJECT:
|
|
|
|
SUCCESS_NON_EMPTY_OBJECT:
|
|
|
|
return object;
|
|
|
|
|
|
|
|
ERROR_INVALID_KEY:
|
|
|
|
ERROR_INVALID_VALUE:
|
2014-11-07 09:30:00 +01:00
|
|
|
ERROR_MISSING_BRACE:
|
2014-11-07 09:20:48 +01:00
|
|
|
ERROR_MISSING_COLON:
|
|
|
|
ERROR_MISSING_COMMA:
|
2014-10-29 14:24:34 +01:00
|
|
|
return JsonObject::invalid();
|
2014-10-21 23:37:17 +02:00
|
|
|
}
|
|
|
|
|
2014-11-07 09:30:00 +01:00
|
|
|
void JsonParser::parseBooleanTo(JsonVariant &destination) {
|
2014-11-07 09:35:53 +01:00
|
|
|
if (skip("true"))
|
|
|
|
destination = true;
|
|
|
|
else if (skip("false"))
|
|
|
|
destination = false;
|
2014-11-07 09:30:00 +01:00
|
|
|
else
|
|
|
|
destination = JsonVariant::invalid();
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonParser::parseNumberTo(JsonVariant &destination) {
|
|
|
|
char *endOfLong;
|
|
|
|
long longValue = strtol(_ptr, &endOfLong, 10);
|
2014-11-08 15:56:40 +01:00
|
|
|
char stopChar = *endOfLong;
|
2014-11-07 09:30:00 +01:00
|
|
|
|
2014-11-07 09:35:53 +01:00
|
|
|
// Could it be a floating point value?
|
2014-11-08 15:56:40 +01:00
|
|
|
bool couldBeFloat = stopChar == '.' || stopChar == 'e' || stopChar == 'E';
|
|
|
|
|
|
|
|
if (couldBeFloat) {
|
2014-11-07 09:35:53 +01:00
|
|
|
// Yes => parse it as a double
|
2014-11-07 09:30:00 +01:00
|
|
|
double doubleValue = strtod(_ptr, &_ptr);
|
2014-11-07 09:35:53 +01:00
|
|
|
// Count the decimal digits
|
2014-11-07 09:30:00 +01:00
|
|
|
uint8_t decimals = static_cast<uint8_t>(_ptr - endOfLong - 1);
|
2014-11-07 09:35:53 +01:00
|
|
|
// Set the variant as a double
|
2014-11-07 09:30:00 +01:00
|
|
|
destination.set(doubleValue, decimals);
|
|
|
|
} else {
|
2014-11-07 09:35:53 +01:00
|
|
|
// No => set the variant as a long
|
2014-11-07 09:30:00 +01:00
|
|
|
_ptr = endOfLong;
|
|
|
|
destination = longValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonParser::parseNullTo(JsonVariant &destination) {
|
2014-11-07 09:35:53 +01:00
|
|
|
const char *NULL_STRING = NULL;
|
2014-11-07 09:30:00 +01:00
|
|
|
if (skip("null"))
|
2014-11-07 09:35:53 +01:00
|
|
|
destination = NULL_STRING;
|
2014-11-07 09:30:00 +01:00
|
|
|
else
|
|
|
|
destination = JsonVariant::invalid();
|
|
|
|
}
|
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
const char *JsonParser::parseString() {
|
|
|
|
return QuotedString::extractFrom(_ptr, &_ptr);
|
2014-10-15 23:39:25 +02:00
|
|
|
}
|