Files
ArduinoJson/src/Internals/JsonParser.cpp

190 lines
4.1 KiB
C++
Raw Normal View History

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
#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';
}
2014-11-04 09:51:25 +01:00
void JsonParser::parseAnythingTo(JsonVariant &destination) {
if (_nestingLimit == 0) return;
_nestingLimit--;
2014-10-23 19:54:00 +02:00
skipSpaces();
switch (*_ptr) {
case '[':
2014-10-26 21:18:09 +01:00
destination = parseArray();
break;
2014-11-07 09:30:00 +01:00
case '{':
destination = parseObject();
break;
case 't':
case 'f':
2014-10-26 21:18:09 +01:00
parseBooleanTo(destination);
break;
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;
case 'n':
2014-10-26 21:18:09 +01:00
parseNullTo(destination);
break;
case '\'':
case '\"':
2014-10-26 21:18:09 +01:00
destination = parseString();
break;
2014-10-23 19:54:00 +02: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) {
bool value = *_ptr == 't';
if (skip(value ? "true" : "false"))
destination = value;
else
destination = JsonVariant::invalid();
}
void JsonParser::parseNumberTo(JsonVariant &destination) {
char *endOfLong;
long longValue = strtol(_ptr, &endOfLong, 10);
if (*endOfLong == '.') {
// stopped on a decimal separator
double doubleValue = strtod(_ptr, &_ptr);
uint8_t decimals = static_cast<uint8_t>(_ptr - endOfLong - 1);
destination.set(doubleValue, decimals);
} else {
_ptr = endOfLong;
destination = longValue;
}
}
void JsonParser::parseNullTo(JsonVariant &destination) {
if (skip("null"))
destination = static_cast<const char *>(NULL);
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
}