2014-10-23 23:39:22 +02:00
|
|
|
// Copyright Benoit Blanchon 2014
|
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Arduino JSON library
|
|
|
|
// https://github.com/bblanchon/ArduinoJson
|
|
|
|
|
2014-10-19 15:46:36 +02:00
|
|
|
#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-10-26 21:18:09 +01:00
|
|
|
#include "ArduinoJson/JsonArray.hpp"
|
2014-10-19 15:46:36 +02:00
|
|
|
#include "ArduinoJson/JsonBuffer.hpp"
|
2014-10-26 21:18:09 +01:00
|
|
|
#include "ArduinoJson/JsonValue.hpp"
|
|
|
|
#include "ArduinoJson/JsonObject.hpp"
|
2014-10-19 15:46:36 +02:00
|
|
|
#include "ArduinoJson/Internals/QuotedString.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-26 21:18:09 +01:00
|
|
|
void JsonParser::parseValueTo(JsonValue destination) {
|
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
|
|
|
|
|
|
|
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 '{':
|
2014-10-26 21:18:09 +01:00
|
|
|
destination = parseObject();
|
|
|
|
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 23:13:13 +02:00
|
|
|
|
|
|
|
default:
|
2014-10-26 21:18:09 +01:00
|
|
|
destination = NULL; // invalid JSON
|
2014-10-23 19:54:00 +02:00
|
|
|
}
|
2014-10-14 21:24:26 +02:00
|
|
|
}
|
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
JsonArray JsonParser::parseArray() {
|
2014-10-23 19:54:00 +02:00
|
|
|
skip('[');
|
2014-10-14 21:48:22 +02:00
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
if (isEnd()) return NULL;
|
2014-10-14 21:24:26 +02:00
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
JsonArray array = _buffer->createArray();
|
|
|
|
if (skip(']')) return array; // empty array
|
2014-10-14 21:24:26 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
for (;;) {
|
2014-10-26 21:18:09 +01:00
|
|
|
JsonValue child = array.add();
|
2014-10-14 21:48:22 +02:00
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
parseValueTo(child);
|
|
|
|
if (!child.success()) return NULL;
|
2014-10-14 21:48:22 +02:00
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
if (skip(']')) return array; // end of the array
|
2014-10-14 21:48:22 +02:00
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
if (!skip(',')) return NULL; // comma is missing
|
2014-10-23 19:54:00 +02:00
|
|
|
}
|
2014-10-14 21:24:26 +02:00
|
|
|
}
|
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
void JsonParser::parseBooleanTo(JsonValue &destination) {
|
2014-10-23 19:54:00 +02:00
|
|
|
bool value = *_ptr == 't';
|
2014-10-14 21:24:26 +02:00
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
// TODO: bug if string ends here !!!
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
_ptr += value ? 4 : 5;
|
|
|
|
// 4 = strlen("true")
|
|
|
|
// 5 = strlen("false");
|
2014-10-15 23:27:38 +02:00
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
destination = value;
|
2014-10-15 14:54:31 +02:00
|
|
|
}
|
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
void JsonParser::parseNumberTo(JsonValue &destination) {
|
2014-10-23 19:54:00 +02:00
|
|
|
char *endOfLong;
|
|
|
|
long longValue = strtol(_ptr, &endOfLong, 10);
|
|
|
|
|
2014-10-24 00:08:25 +02:00
|
|
|
if (*endOfLong == '.') {
|
|
|
|
// stopped on a decimal separator
|
2014-10-26 21:18:09 +01:00
|
|
|
double douleValue = strtod(_ptr, &_ptr);
|
2014-10-23 19:54:00 +02:00
|
|
|
int decimals = _ptr - endOfLong - 1;
|
2014-10-26 21:18:09 +01:00
|
|
|
destination.set(douleValue, decimals);
|
2014-10-23 19:54:00 +02:00
|
|
|
} else {
|
|
|
|
_ptr = endOfLong;
|
2014-10-26 21:18:09 +01:00
|
|
|
destination = longValue;
|
2014-10-23 19:54:00 +02:00
|
|
|
}
|
2014-10-15 23:27:38 +02:00
|
|
|
}
|
2014-10-15 23:10:52 +02:00
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
void JsonParser::parseNullTo(JsonValue &destination) {
|
2014-10-23 23:13:13 +02:00
|
|
|
_ptr += 4; // strlen("null")
|
2014-10-15 23:27:38 +02:00
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
destination = static_cast<const char *>(NULL);
|
2014-10-15 23:10:52 +02:00
|
|
|
}
|
2014-10-15 23:39:25 +02:00
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
JsonObject JsonParser::parseObject() {
|
2014-10-23 19:54:00 +02:00
|
|
|
skip('{');
|
2014-10-21 23:37:17 +02:00
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
if (isEnd()) return NULL; // premature ending
|
|
|
|
|
|
|
|
JsonObject object = _buffer->createObject();
|
2014-10-21 23:37:17 +02:00
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
if (skip('}')) return object; // empty object
|
2014-10-21 23:37:17 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
for (;;) {
|
2014-10-26 21:18:09 +01:00
|
|
|
const char *key = parseString();
|
|
|
|
if (!key) return NULL;
|
|
|
|
|
|
|
|
skip(':')
|
2014-10-22 10:55:36 +02:00
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
JsonValue value = object[key];
|
2014-10-21 23:37:17 +02:00
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
parseValueTo(value);
|
|
|
|
if (!value.success()) return NULL;
|
2014-10-21 23:37:17 +02:00
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
if (skip('}')) return object; // end of the object
|
2014-10-21 23:37:17 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
if (!skip(',')) return 0; // comma is missing
|
2014-10-23 19:54:00 +02:00
|
|
|
}
|
2014-10-21 23:37:17 +02: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
|
|
|
}
|