Files
ArduinoJson/src/Internals/JsonParser.cpp

221 lines
5.0 KiB
C++
Raw Normal View History

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
#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() {
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();
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
}
bool JsonParser::skip(const char *wordToSkip) {
const char *charToSkip = wordToSkip;
while (*charToSkip && *_ptr == *charToSkip) {
charToSkip++;
_ptr++;
}
return *charToSkip == '\0';
}
bool JsonParser::parseAnythingTo(JsonVariant *destination) {
if (_nestingLimit == 0) return false;
_nestingLimit--;
bool success = parseAnythingToUnsafe(destination);
_nestingLimit++;
return success;
}
inline bool JsonParser::parseAnythingToUnsafe(JsonVariant *destination) {
2014-10-23 19:54:00 +02:00
skipSpaces();
switch (*_ptr) {
case '[':
return parseArrayTo(destination);
2014-11-07 09:30:00 +01:00
case '{':
return parseObjectTo(destination);
2014-11-07 09:30:00 +01:00
case 't':
case 'f':
return parseBooleanTo(destination);
case '-':
case '.':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return parseNumberTo(destination);
case 'n':
return parseNullTo(destination);
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
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:
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
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
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:
ERROR_NO_MEMORY:
2014-10-29 14:24:34 +01:00
return JsonObject::invalid();
2014-10-21 23:37:17 +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
}
bool JsonParser::parseNumberTo(JsonVariant *destination) {
2014-11-07 09:30:00 +01:00
char *endOfLong;
long longValue = strtol(_ptr, &endOfLong, 10);
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?
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
*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
2014-11-07 09:30:00 +01:00
_ptr = endOfLong;
*destination = longValue;
2014-11-07 09:30:00 +01:00
}
return true;
2014-11-07 09:30:00 +01:00
}
bool JsonParser::parseNullTo(JsonVariant *destination) {
2014-11-07 09:35:53 +01:00
const char *NULL_STRING = NULL;
if (!skip("null")) return false;
*destination = NULL_STRING;
return true;
2014-11-07 09:30:00 +01:00
}
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
}
bool JsonParser::parseStringTo(JsonVariant *destination) {
const char *value = parseString();
*destination = value;
return value != NULL;
}