Huge refactoring in progress...

This commit is contained in:
Benoit Blanchon
2014-10-29 14:24:34 +01:00
parent 10ab95522d
commit ba2b142c8a
27 changed files with 408 additions and 743 deletions

View File

@ -1,91 +0,0 @@
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include "ArduinoJson/Internals/JsonArrayImpl.hpp"
#include "ArduinoJson/JsonBuffer.hpp"
#include "ArduinoJson/Internals/JsonObjectImpl.hpp"
#include "ArduinoJson/Internals/JsonWriter.hpp"
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
JsonArrayImpl *JsonArrayImpl::createFrom(JsonBuffer *buffer) {
void *ptr = buffer->alloc(sizeof(JsonArrayImpl));
return ptr ? new (ptr) JsonArrayImpl(buffer) : NULL;
}
int JsonArrayImpl::size() const {
int nodeCount = 0;
for (JsonArrayNode *node = _firstNode; node; node = node->next) nodeCount++;
return nodeCount;
}
JsonValueImpl *JsonArrayImpl::operator[](int index) const {
JsonArrayNode *node = _firstNode;
while (node && index--) node = node->next;
return node ? &node->value : NULL;
}
JsonValueImpl *JsonArrayImpl::add() {
JsonArrayNode *node = JsonArrayNode::createFrom(_buffer);
if (!node) return NULL;
addNode(node);
return &node->value;
}
void JsonArrayImpl::addNode(JsonArrayNode *newNode) {
if (_firstNode) {
JsonArrayNode *lastNode = _firstNode;
while (lastNode->next) lastNode = lastNode->next;
lastNode->next = newNode;
} else {
_firstNode = newNode;
}
}
JsonArrayImpl *JsonArrayImpl::createNestedArray() {
JsonValueImpl *value = add();
if (!value) return NULL;
JsonArrayImpl *array = JsonArrayImpl::createFrom(_buffer);
value->set(array);
return array;
}
JsonObjectImpl *JsonArrayImpl::createNestedObject() {
JsonValueImpl *value = add();
if (!value) return NULL;
JsonObjectImpl *array = JsonObjectImpl::createFrom(_buffer);
value->set(array);
return array;
}
void JsonArrayImpl::writeTo(JsonWriter &writer) const {
JsonArrayNode *child = _firstNode;
if (child) {
writer.beginArray();
for (;;) {
child->value.writeTo(writer);
child = child->next;
if (!child) break;
writer.writeComma();
}
writer.endArray();
} else {
writer.writeEmptyArray();
}
}

View File

@ -1,116 +0,0 @@
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include "ArduinoJson/JsonObject.hpp"
#include <string.h> // for strcmp
#include "ArduinoJson/JsonBuffer.hpp"
#include "ArduinoJson/Internals/JsonArrayImpl.hpp"
#include "ArduinoJson/Internals/JsonValueImpl.hpp"
#include "ArduinoJson/Internals/JsonWriter.hpp"
#include "ArduinoJson/Internals/StringBuilder.hpp"
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
JsonObjectImpl *JsonObjectImpl::createFrom(JsonBuffer *buffer) {
void *ptr = buffer->alloc(sizeof(JsonObjectImpl));
return ptr ? new (ptr) JsonObjectImpl(buffer) : NULL;
}
int JsonObjectImpl::size() const {
int nodeCount = 0;
for (JsonObjectNode *node = _firstNode; node; node = node->next) nodeCount++;
return nodeCount;
}
JsonValueImpl *JsonObjectImpl::operator[](const char *key) {
JsonObjectNode *node = getOrCreateNodeAt(key);
return node ? &node->value : NULL;
}
void JsonObjectImpl::remove(char const *key) { removeNode(getNodeAt(key)); }
JsonArrayImpl *JsonObjectImpl::createNestedArray(char const *key) {
JsonObjectNode *node = getOrCreateNodeAt(key);
if (!node) return NULL;
JsonArrayImpl *array = JsonArrayImpl::createFrom(_buffer);
node->value.set(array);
return array;
}
JsonObjectImpl *JsonObjectImpl::createNestedObject(const char *key) {
JsonObjectNode *node = getOrCreateNodeAt(key);
if (!node) return NULL;
JsonObjectImpl *object = JsonObjectImpl::createFrom(_buffer);
node->value.set(object);
return object;
}
JsonObjectNode *JsonObjectImpl::getNodeAt(const char *key) {
for (JsonObjectNode *node = _firstNode; node; node = node->next) {
if (!strcmp(node->key, key)) return node;
}
return NULL;
}
JsonObjectNode *JsonObjectImpl::getOrCreateNodeAt(const char *key) {
JsonObjectNode *existingNode = getNodeAt(key);
if (existingNode) return existingNode;
JsonObjectNode *newNode = JsonObjectNode::createFrom(_buffer, key);
if (newNode) addNode(newNode);
return newNode;
}
void JsonObjectImpl::addNode(JsonObjectNode *nodeToAdd) {
if (!_firstNode) {
_firstNode = nodeToAdd;
} else {
JsonObjectNode *lastNode = _firstNode;
while (lastNode->next) lastNode = lastNode->next;
lastNode->next = nodeToAdd;
}
}
void JsonObjectImpl::removeNode(JsonObjectNode *nodeToRemove) {
if (nodeToRemove == _firstNode) {
_firstNode = nodeToRemove->next;
} else {
for (JsonObjectNode *node = _firstNode; node; node = node->next)
if (node->next == nodeToRemove) node->next = nodeToRemove->next;
}
}
void JsonObjectImpl::writeTo(JsonWriter &writer) const {
JsonObjectNode *node = _firstNode;
if (node) {
writer.beginObject();
for (;;) {
writer.writeString(node->key);
writer.writeColon();
node->value.writeTo(writer);
node = node->next;
if (!node) break;
writer.writeComma();
}
writer.endObject();
} else {
writer.writeEmptyObject();
}
}

View File

@ -30,7 +30,7 @@ bool JsonParser::skip(char charToSkip) {
return true;
}
void JsonParser::parseValueTo(JsonValue destination) {
void JsonParser::parseAnythingTo(JsonValue &destination) {
skipSpaces();
switch (*_ptr) {
@ -73,23 +73,23 @@ void JsonParser::parseValueTo(JsonValue destination) {
}
}
JsonArray JsonParser::parseArray() {
JsonArray &JsonParser::parseArray() {
skip('[');
if (isEnd()) return NULL;
if (isEnd()) return JsonArray::invalid();
JsonArray array = _buffer->createArray();
JsonArray &array = _buffer->createArray();
if (skip(']')) return array; // empty array
for (;;) {
JsonValue child = array.add();
JsonValue &child = array.add();
parseValueTo(child);
if (!child.success()) return NULL;
parseAnythingTo(child);
if (!child.success()) return JsonArray::invalid(); // child parsing failed
if (skip(']')) return array; // end of the array
if (!skip(',')) return NULL; // comma is missing
if (!skip(',')) return JsonArray::invalid(); // comma is missing
}
}
@ -126,38 +126,34 @@ void JsonParser::parseNullTo(JsonValue &destination) {
destination = static_cast<const char *>(NULL);
}
JsonObject JsonParser::parseObject() {
JsonObject &JsonParser::parseObject() {
skip('{');
if (isEnd()) return NULL; // premature ending
if (isEnd()) return JsonObject::invalid(); // premature ending
JsonObject object = _buffer->createObject();
JsonObject &object = _buffer->createObject();
if (skip('}')) return object; // empty object
for (;;) {
const char *key = parseString();
if (!key) return NULL;
if (!key) break; // key parsing failed
if (!skip(':')) return NULL;
if (!skip(':')) break; // colon is missing
JsonValue value = object[key];
JsonValue &value = object[key];
parseValueTo(value);
if (!value.success()) return NULL;
parseAnythingTo(value);
if (!value.success()) break; // value parsing failed
if (skip('}')) return object; // end of the object
if (!skip(',')) return 0; // comma is missing
if (!skip(',')) break; // comma is missing
}
return JsonObject::invalid();
}
const char *JsonParser::parseString() {
return QuotedString::extractFrom(_ptr, &_ptr);
}
JsonValue JsonParser::parseValue() {
JsonValue value = _buffer->createValue();
parseValueTo(value);
return value;
}

View File

@ -1,46 +0,0 @@
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include "ArduinoJson/Internals/JsonValueImpl.hpp"
#include "ArduinoJson/Internals/JsonArrayImpl.hpp"
#include "ArduinoJson/Internals/JsonObjectImpl.hpp"
#include "ArduinoJson/Internals/JsonWriter.hpp"
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
JsonValueImpl* JsonValueImpl::createFrom(JsonBuffer* buffer) {
void* ptr = buffer->alloc(sizeof(JsonValueImpl));
return ptr ? new (ptr) JsonValueImpl() : NULL;
}
void JsonValueImpl::writeTo(JsonWriter& writer) const {
switch (_type) {
case JSON_ARRAY:
_content.asArray->writeTo(writer);
break;
case JSON_OBJECT:
_content.asObject->writeTo(writer);
break;
case JSON_STRING:
writer.writeString(_content.asString);
break;
case JSON_LONG:
writer.writeInteger(_content.asInteger);
break;
case JSON_BOOLEAN:
writer.writeBoolean(_content.asBoolean);
break;
default: // >= JSON_DOUBLE_0_DECIMALS
writer.writeDouble(_content.asDouble, _type - JSON_DOUBLE_0_DECIMALS);
break;
}
}

View File

@ -5,22 +5,76 @@
// https://github.com/bblanchon/ArduinoJson
#include "ArduinoJson/JsonArray.hpp"
#include "ArduinoJson/JsonBuffer.hpp"
#include "ArduinoJson/JsonObject.hpp"
#include "ArduinoJson/JsonValue.hpp"
#include "ArduinoJson/Internals/JsonWriter.hpp"
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
JsonValue JsonArray::add() { return JsonValue(_impl ? _impl->add() : NULL); }
JsonValue JsonArray::operator[](int index) const {
return JsonValue(_impl ? (*_impl)[index] : NULL);
int JsonArray::size() const {
int nodeCount = 0;
for (JsonArrayNode *node = _firstNode; node; node = node->next) nodeCount++;
return nodeCount;
}
JsonArray JsonArray::createNestedArray() {
return JsonArray(_impl ? _impl->createNestedArray() : NULL);
JsonValue &JsonArray::operator[](int index) const {
JsonArrayNode *node = _firstNode;
while (node && index--) node = node->next;
return node ? node->value : JsonValue::invalid();
}
JsonObject JsonArray::createNestedObject() {
return JsonObject(_impl ? _impl->createNestedObject() : NULL);
}
JsonValue &JsonArray::add() {
JsonArrayNode *node = JsonArrayNode::createFrom(_buffer);
if (!node) return JsonValue::invalid();
addNode(node);
return node->value;
}
void JsonArray::addNode(JsonArrayNode *newNode) {
if (_firstNode) {
JsonArrayNode *lastNode = _firstNode;
while (lastNode->next) lastNode = lastNode->next;
lastNode->next = newNode;
} else {
_firstNode = newNode;
}
}
JsonArray &JsonArray::createNestedArray() {
if (!_buffer) return JsonArray::invalid();
JsonArray &array = _buffer->createArray();
add(array);
return array;
}
JsonObject &JsonArray::createNestedObject() {
if (!_buffer) return JsonObject::invalid();
JsonObject &object = _buffer->createObject();
add(object);
return object;
}
void JsonArray::writeTo(JsonWriter &writer) const {
JsonArrayNode *child = _firstNode;
if (child) {
writer.beginArray();
for (;;) {
child->value.writeTo(writer);
child = child->next;
if (!child) break;
writer.writeComma();
}
writer.endArray();
} else {
writer.writeEmptyArray();
}
}

View File

@ -7,39 +7,31 @@
#include "ArduinoJson/JsonBuffer.hpp"
#include "ArduinoJson/JsonArray.hpp"
#include "ArduinoJson/Internals/JsonArrayImpl.hpp"
#include "ArduinoJson/JsonObject.hpp"
#include "ArduinoJson/Internals/JsonObjectImpl.hpp"
#include "ArduinoJson/JsonValue.hpp"
#include "ArduinoJson/Internals/JsonValueImpl.hpp"
#include "ArduinoJson/Internals/JsonParser.hpp"
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
JsonArray JsonBuffer::createArray() {
return JsonArray(JsonArrayImpl::createFrom(this));
JsonArray &JsonBuffer::createArray() {
void *ptr = alloc(sizeof(JsonArray));
if (ptr) return *new (ptr) JsonArray(this);
return JsonArray::invalid();
}
JsonObject JsonBuffer::createObject() {
return JsonObject(JsonObjectImpl::createFrom(this));
JsonObject &JsonBuffer::createObject() {
void *ptr = alloc(sizeof(JsonObject));
if (ptr) return *new (ptr) JsonObject(this);
return JsonObject::invalid();
}
JsonValue JsonBuffer::createValue() {
return JsonValue(JsonValueImpl::createFrom(this));
}
JsonArray JsonBuffer::parseArray(char* json) {
JsonArray &JsonBuffer::parseArray(char *json) {
JsonParser parser(this, json);
return JsonArray(parser.parseArray());
return parser.parseArray();
}
JsonObject JsonBuffer::parseObject(char* json) {
JsonObject &JsonBuffer::parseObject(char *json) {
JsonParser parser(this, json);
return JsonObject(parser.parseObject());
return parser.parseObject();
}
JsonValue JsonBuffer::parseValue(char* json) {
JsonParser parser(this, json);
return JsonValue(parser.parseValue());
}

View File

@ -4,20 +4,102 @@
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include "ArduinoJson/JsonArray.hpp"
#include "ArduinoJson/JsonObject.hpp"
#include <string.h> // for strcmp
#include "ArduinoJson/JsonBuffer.hpp"
#include "ArduinoJson/JsonArray.hpp"
#include "ArduinoJson/JsonValue.hpp"
#include "ArduinoJson/Internals/JsonWriter.hpp"
#include "ArduinoJson/Internals/StringBuilder.hpp"
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
JsonValue JsonObject::operator[](char const *key) {
return JsonValue(_impl ? (*_impl)[key] : NULL);
int JsonObject::size() const {
int nodeCount = 0;
for (JsonObjectNode *node = _firstNode; node; node = node->next) nodeCount++;
return nodeCount;
}
JsonArray JsonObject::createNestedArray(key_type key) {
return JsonArray(_impl ? _impl->createNestedArray(key) : NULL);
JsonValue &JsonObject::operator[](const char *key) {
JsonObjectNode *node = getOrCreateNodeAt(key);
return node ? node->pair.value : JsonValue::invalid();
}
JsonObject JsonObject::createNestedObject(key_type key) {
return JsonObject(_impl ? _impl->createNestedObject(key) : NULL);
void JsonObject::remove(char const *key) { removeNode(getNodeAt(key)); }
JsonArray &JsonObject::createNestedArray(char const *key) {
if (!_buffer) return JsonArray::invalid();
JsonArray &array = _buffer->createArray();
add(key, array);
return array;
}
JsonObject &JsonObject::createNestedObject(const char *key) {
if (!_buffer) return JsonObject::invalid();
JsonObject &object = _buffer->createObject();
add(key, object);
return object;
}
JsonObjectNode *JsonObject::getNodeAt(const char *key) {
for (JsonObjectNode *node = _firstNode; node; node = node->next) {
if (!strcmp(node->pair.key, key)) return node;
}
return NULL;
}
JsonObjectNode *JsonObject::getOrCreateNodeAt(const char *key) {
JsonObjectNode *existingNode = getNodeAt(key);
if (existingNode) return existingNode;
JsonObjectNode *newNode = JsonObjectNode::createFrom(_buffer, key);
if (newNode) addNode(newNode);
return newNode;
}
void JsonObject::addNode(JsonObjectNode *nodeToAdd) {
if (!_firstNode) {
_firstNode = nodeToAdd;
} else {
JsonObjectNode *lastNode = _firstNode;
while (lastNode->next) lastNode = lastNode->next;
lastNode->next = nodeToAdd;
}
}
void JsonObject::removeNode(JsonObjectNode *nodeToRemove) {
if (nodeToRemove == _firstNode) {
_firstNode = nodeToRemove->next;
} else {
for (JsonObjectNode *node = _firstNode; node; node = node->next)
if (node->next == nodeToRemove) node->next = nodeToRemove->next;
}
}
void JsonObject::writeTo(JsonWriter &writer) const {
JsonObjectNode *node = _firstNode;
if (node) {
writer.beginObject();
for (;;) {
writer.writeString(node->pair.key);
writer.writeColon();
node->pair.value.writeTo(writer);
node = node->next;
if (!node) break;
writer.writeComma();
}
writer.endObject();
} else {
writer.writeEmptyObject();
}
}

View File

@ -4,24 +4,98 @@
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include "ArduinoJson/JsonValue.hpp"
#include "ArduinoJson/JsonArray.hpp"
#include "ArduinoJson/JsonObject.hpp"
#include "ArduinoJson/JsonValue.hpp"
#include "ArduinoJson/Internals/JsonWriter.hpp"
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
JsonValue::operator JsonArray() {
return JsonArray(_impl ? _impl->asArray() : NULL);
JsonArray &JsonValue::asArray() {
return _type == JSON_ARRAY ? *_content.asArray : JsonArray::invalid();
}
JsonValue::operator JsonObject() {
return JsonObject(_impl ? _impl->asObject() : NULL);
JsonObject &JsonValue::asObject() {
return _type == JSON_OBJECT ? *_content.asObject : JsonObject::invalid();
}
void JsonValue::set(JsonArray array) {
if (_impl) _impl->set(array._impl);
bool JsonValue::asBool() const {
return _type == JSON_BOOLEAN ? _content.asBoolean : false;
}
void JsonValue::set(JsonObject object) {
if (_impl) _impl->set(object._impl);
const char *JsonValue::asString() const {
return _type == JSON_STRING ? _content.asString : NULL;
}
double JsonValue::asDouble() const {
return _type >= JSON_DOUBLE_0_DECIMALS ? _content.asDouble : 0;
}
long JsonValue::asLong() const {
return _type == JSON_LONG ? _content.asInteger : 0;
}
void JsonValue::set(bool value) {
if (_type == JSON_INVALID) return;
_type = Internals::JSON_BOOLEAN;
_content.asBoolean = value;
}
void JsonValue::set(const char *value) {
if (_type == JSON_INVALID) return;
_type = JSON_STRING;
_content.asString = value;
}
void JsonValue::set(double value, int decimals) {
if (_type == JSON_INVALID) return;
_type = static_cast<JsonValueType>(JSON_DOUBLE_0_DECIMALS + decimals);
_content.asDouble = value;
}
void JsonValue::set(long value) {
if (_type == JSON_INVALID) return;
_type = JSON_LONG;
_content.asInteger = value;
}
void JsonValue::set(JsonArray &array) {
if (_type == JSON_INVALID) return;
_type = JSON_ARRAY;
_content.asArray = &array;
}
void JsonValue::set(JsonObject &object) {
if (_type == JSON_INVALID) return;
_type = JSON_OBJECT;
_content.asObject = &object;
}
void JsonValue::writeTo(JsonWriter &writer) const {
switch (_type) {
case JSON_ARRAY:
_content.asArray->writeTo(writer);
break;
case JSON_OBJECT:
_content.asObject->writeTo(writer);
break;
case JSON_STRING:
writer.writeString(_content.asString);
break;
case JSON_LONG:
writer.writeInteger(_content.asInteger);
break;
case JSON_BOOLEAN:
writer.writeBoolean(_content.asBoolean);
break;
default: // >= JSON_DOUBLE_0_DECIMALS
writer.writeDouble(_content.asDouble, _type - JSON_DOUBLE_0_DECIMALS);
break;
}
}