2015-02-07 16:05:48 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2015
|
2014-10-23 23:39:22 +02:00
|
|
|
// 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>
|
|
|
|
|
2015-07-10 22:11:26 +02:00
|
|
|
#include "../../include/ArduinoJson/Internals/Encoding.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;
|
|
|
|
|
2015-07-10 22:11:26 +02:00
|
|
|
static const char *skipSpaces(const char *ptr) {
|
|
|
|
while (isspace(*ptr)) ptr++;
|
|
|
|
return ptr;
|
2014-10-15 23:10:52 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
bool JsonParser::skip(char charToSkip) {
|
2015-07-10 22:11:26 +02:00
|
|
|
register const char *ptr = skipSpaces(_readPtr);
|
|
|
|
if (*ptr != charToSkip) return false;
|
|
|
|
ptr++;
|
|
|
|
_readPtr = skipSpaces(ptr);
|
2014-10-23 19:54:00 +02:00
|
|
|
return true;
|
2014-10-14 21:24:26 +02:00
|
|
|
}
|
|
|
|
|
2014-10-31 21:08:04 +01:00
|
|
|
bool JsonParser::skip(const char *wordToSkip) {
|
2015-07-10 22:11:26 +02:00
|
|
|
register const char *ptr = _readPtr;
|
|
|
|
while (*wordToSkip && *ptr == *wordToSkip) {
|
|
|
|
wordToSkip++;
|
|
|
|
ptr++;
|
2014-10-31 21:08:04 +01:00
|
|
|
}
|
2015-07-10 22:11:26 +02:00
|
|
|
if (*wordToSkip != '\0') return false;
|
|
|
|
_readPtr = ptr;
|
|
|
|
return true;
|
2014-10-31 21:08:04 +01:00
|
|
|
}
|
|
|
|
|
2015-05-23 15:32:50 +02:00
|
|
|
bool JsonParser::parseAnythingTo(JsonVariant *destination) {
|
|
|
|
if (_nestingLimit == 0) return false;
|
2014-11-06 10:24:37 +01:00
|
|
|
_nestingLimit--;
|
2015-05-23 15:32:50 +02:00
|
|
|
bool success = parseAnythingToUnsafe(destination);
|
|
|
|
_nestingLimit++;
|
|
|
|
return success;
|
|
|
|
}
|
2014-11-06 10:24:37 +01:00
|
|
|
|
2015-05-23 15:32:50 +02:00
|
|
|
inline bool JsonParser::parseAnythingToUnsafe(JsonVariant *destination) {
|
2015-07-10 22:11:26 +02:00
|
|
|
_readPtr = skipSpaces(_readPtr);
|
2014-10-23 19:54:00 +02:00
|
|
|
|
2015-07-10 22:11:26 +02:00
|
|
|
switch (*_readPtr) {
|
2014-10-23 23:13:13 +02:00
|
|
|
case '[':
|
2015-05-23 15:32:50 +02:00
|
|
|
return parseArrayTo(destination);
|
2014-10-23 23:13:13 +02:00
|
|
|
|
2014-11-07 09:30:00 +01:00
|
|
|
case '{':
|
2015-05-23 15:32:50 +02:00
|
|
|
return parseObjectTo(destination);
|
2014-11-07 09:30:00 +01:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
case 't':
|
|
|
|
case 'f':
|
2015-05-23 15:32:50 +02:00
|
|
|
return parseBooleanTo(destination);
|
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':
|
2015-05-23 15:32:50 +02:00
|
|
|
return parseNumberTo(destination);
|
2014-10-23 23:13:13 +02:00
|
|
|
|
|
|
|
case 'n':
|
2015-05-23 15:32:50 +02:00
|
|
|
return parseNullTo(destination);
|
2014-10-23 23:13:13 +02:00
|
|
|
|
2015-05-23 15:32:50 +02:00
|
|
|
default:
|
|
|
|
return parseStringTo(destination);
|
2014-10-23 19:54:00 +02:00
|
|
|
}
|
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
|
2015-05-23 15:32:50 +02:00
|
|
|
JsonVariant value;
|
|
|
|
if (!parseAnythingTo(&value)) goto ERROR_INVALID_VALUE;
|
|
|
|
if (!array.add(value)) goto ERROR_NO_MEMORY;
|
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:
|
2015-05-23 15:32:50 +02:00
|
|
|
ERROR_NO_MEMORY:
|
2014-11-07 09:30:00 +01:00
|
|
|
return JsonArray::invalid();
|
2014-10-15 23:10:52 +02:00
|
|
|
}
|
2014-10-15 23:39:25 +02:00
|
|
|
|
2015-05-23 15:32:50 +02:00
|
|
|
bool JsonParser::parseArrayTo(JsonVariant *destination) {
|
|
|
|
JsonArray &array = parseArray();
|
|
|
|
if (!array.success()) return false;
|
|
|
|
|
|
|
|
*destination = array;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
2015-05-23 15:32:50 +02:00
|
|
|
JsonVariant value;
|
|
|
|
if (!parseAnythingTo(&value)) goto ERROR_INVALID_VALUE;
|
|
|
|
if (!object.set(key, value)) goto ERROR_NO_MEMORY;
|
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:
|
2015-05-23 15:32:50 +02:00
|
|
|
ERROR_NO_MEMORY:
|
2014-10-29 14:24:34 +01:00
|
|
|
return JsonObject::invalid();
|
2014-10-21 23:37:17 +02:00
|
|
|
}
|
|
|
|
|
2015-05-23 15:32:50 +02:00
|
|
|
bool JsonParser::parseObjectTo(JsonVariant *destination) {
|
|
|
|
JsonObject &object = parseObject();
|
|
|
|
if (!object.success()) return false;
|
|
|
|
|
|
|
|
*destination = object;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonParser::parseBooleanTo(JsonVariant *destination) {
|
|
|
|
if (skip("true")) {
|
|
|
|
*destination = true;
|
|
|
|
return true;
|
|
|
|
} else if (skip("false")) {
|
|
|
|
*destination = false;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-07 09:30:00 +01:00
|
|
|
}
|
|
|
|
|
2015-05-23 15:32:50 +02:00
|
|
|
bool JsonParser::parseNumberTo(JsonVariant *destination) {
|
2014-11-07 09:30:00 +01:00
|
|
|
char *endOfLong;
|
2015-07-10 22:11:26 +02:00
|
|
|
long longValue = strtol(_readPtr, &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
|
2015-07-10 22:11:26 +02:00
|
|
|
double doubleValue = strtod(_readPtr, const_cast<char **>(&_readPtr));
|
2014-11-07 09:35:53 +01:00
|
|
|
// Count the decimal digits
|
2015-07-10 22:11:26 +02:00
|
|
|
uint8_t decimals = static_cast<uint8_t>(_readPtr - endOfLong - 1);
|
2014-11-07 09:35:53 +01:00
|
|
|
// Set the variant as a double
|
2015-05-23 15:32:50 +02:00
|
|
|
*destination = JsonVariant(doubleValue, decimals);
|
2014-11-07 09:30:00 +01:00
|
|
|
} else {
|
2014-11-07 09:35:53 +01:00
|
|
|
// No => set the variant as a long
|
2015-07-10 22:11:26 +02:00
|
|
|
_readPtr = endOfLong;
|
2015-05-23 15:32:50 +02:00
|
|
|
*destination = longValue;
|
2014-11-07 09:30:00 +01:00
|
|
|
}
|
2015-05-23 15:32:50 +02:00
|
|
|
return true;
|
2014-11-07 09:30:00 +01:00
|
|
|
}
|
|
|
|
|
2015-05-23 15:32:50 +02:00
|
|
|
bool JsonParser::parseNullTo(JsonVariant *destination) {
|
2014-11-07 09:35:53 +01:00
|
|
|
const char *NULL_STRING = NULL;
|
2015-05-23 15:32:50 +02:00
|
|
|
if (!skip("null")) return false;
|
|
|
|
*destination = NULL_STRING;
|
|
|
|
return true;
|
2014-11-07 09:30:00 +01:00
|
|
|
}
|
|
|
|
|
2015-07-10 22:11:26 +02:00
|
|
|
static bool isStopChar(char c) {
|
|
|
|
return c == '\0' || c == ':' || c == '}' || c == ']' || c == ',';
|
|
|
|
}
|
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
const char *JsonParser::parseString() {
|
2015-07-10 22:11:26 +02:00
|
|
|
const char *readPtr = _readPtr;
|
|
|
|
char *writePtr = _writePtr;
|
|
|
|
|
|
|
|
char c = *readPtr;
|
|
|
|
|
|
|
|
if (c == '\'' || c == '\"') {
|
|
|
|
char stopChar = c;
|
|
|
|
for (;;) {
|
|
|
|
c = *++readPtr;
|
|
|
|
if (c == '\0') break;
|
|
|
|
|
|
|
|
if (c == stopChar) {
|
|
|
|
readPtr++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == '\\') {
|
|
|
|
// replace char
|
|
|
|
c = Encoding::unescapeChar(*++readPtr);
|
|
|
|
if (c == '\0') break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*writePtr++ = c;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (;;) {
|
|
|
|
if (isStopChar(c)) break;
|
|
|
|
*writePtr++ = c;
|
|
|
|
c = *++readPtr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// end the string here
|
|
|
|
*writePtr++ = '\0';
|
|
|
|
|
|
|
|
const char *startPtr = _writePtr;
|
|
|
|
|
|
|
|
// update end ptr
|
|
|
|
_readPtr = readPtr;
|
|
|
|
_writePtr = writePtr;
|
|
|
|
|
|
|
|
// return pointer to unquoted string
|
|
|
|
return startPtr;
|
2014-10-15 23:39:25 +02:00
|
|
|
}
|
2015-05-23 15:32:50 +02:00
|
|
|
|
|
|
|
bool JsonParser::parseStringTo(JsonVariant *destination) {
|
|
|
|
const char *value = parseString();
|
|
|
|
*destination = value;
|
|
|
|
return value != NULL;
|
|
|
|
}
|