Epic refactoring int progress...

This commit is contained in:
Benoit Blanchon
2014-10-27 22:50:50 +01:00
parent 8988cb4761
commit 852256c1af
34 changed files with 334 additions and 256 deletions

View File

@ -13,8 +13,14 @@
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
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 = _firstChild;
JsonArrayNode *node = _firstNode;
while (node && index--) node = node->next;
return NULL;
@ -50,7 +56,7 @@ JsonObjectImpl *JsonArrayImpl::createNestedObject() {
}
void JsonArrayImpl::writeTo(JsonWriter &writer) const {
JsonArrayNode *child = _firstChild;
JsonArrayNode *child = _firstNode;
if (child) {
writer.beginArray();

View File

@ -11,102 +11,95 @@
#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;
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) {
JsonValueImpl *node = getOrCreateValueAt(key);
JsonObjectNode *node = getOrCreateNodeAt(key);
if (!node) return NULL;
JsonArrayImpl *array = new (_buffer) JsonArrayImpl(_buffer);
node->set(array);
node->value.set(array);
return array;
}
JsonObject JsonObject::createNestedObject(char const *key) {
JsonNode *node = getOrCreateValueAt(key);
JsonObjectImpl *JsonObjectImpl::createNestedObject(const char *key) {
JsonObjectNode *node = getOrCreateNodeAt(key);
if (!node) return NULL;
if (node) node->setAsObject(_node->getContainerBuffer());
JsonObjectImpl *object = new (_buffer) JsonObjectImpl(_buffer);
node->value.set(object);
return JsonObject(node);
return object;
}
JsonNode *JsonObject::getPairAt(const char *key) {
for (JsonNode *node = firstChild(); node; node = node->next) {
if (!strcmp(node->getAsObjectKey(), key)) return node;
JsonObjectNode *JsonObjectImpl::getNodeAt(const char *key) {
for (JsonObjectNode *node = _firstNode; node; node = node->next) {
if (!strcmp(node->key, key)) return node;
}
return NULL;
}
JsonNode *JsonObject::getOrCreateValueAt(const char *key) {
JsonNode *existingNode = getPairAt(key);
if (existingNode) return existingNode->getAsObjectValue();
JsonObjectNode *JsonObjectImpl::getOrCreateNodeAt(const char *key) {
JsonObjectNode *existingNode = getNodeAt(key);
if (existingNode) return existingNode;
JsonNode *newValueNode = createNode();
if (!newValueNode) return 0;
JsonObjectNode *newNode = new (_buffer) JsonObjectNode(key);
JsonNode *newKeyNode = createNode();
if (!newKeyNode) return 0;
if (newNode) addNode(newNode);
newKeyNode->setAsObjectKeyValue(key, newValueNode);
addChild(newKeyNode);
return newValueNode;
return newNode;
}
void JsonNode::addChild(JsonNode *childToAdd) {
if (type == JSON_PROXY) return content.asProxy.target->addChild(childToAdd);
if (type != JSON_ARRAY && type != JSON_OBJECT) return;
JsonNode *lastChild = content.asContainer.child;
if (!lastChild) {
content.asContainer.child = childToAdd;
return;
}
while (lastChild->next) lastChild = lastChild->next;
lastChild->next = childToAdd;
}
void JsonNode::removeChild(JsonNode *childToRemove) {
if (type == JSON_PROXY)
return content.asProxy.target->removeChild(childToRemove);
if (type != JSON_ARRAY && type != JSON_OBJECT) return;
if (content.asContainer.child == childToRemove) {
content.asContainer.child = childToRemove->next;
return;
}
for (JsonNode *child = content.asContainer.child; child;
child = child->next) {
if (child->next == childToRemove) child->next = childToRemove->next;
void JsonObjectImpl::addNode(JsonObjectNode *nodeToAdd) {
if (!_firstNode) {
_firstNode = nodeToAdd;
} else {
JsonObjectNode *lastNode = _firstNode;
while (lastNode->next) lastNode = lastNode->next;
lastNode->next = nodeToAdd;
}
}
void JsonObjectImpl::writeObjectTo(JsonWriter &writer) {
JsonObjectNode *child = _firstChild;
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;
}
}
if (child) {
void JsonObjectImpl::writeTo(JsonWriter &writer) const {
JsonObjectNode *node = _firstNode;
if (node) {
writer.beginObject();
for (;;) {
writer.writeString(child->content.asKeyValue.key);
writer.writeString(node->key);
writer.writeColon();
child->value->writeTo(writer);
node->value.writeTo(writer);
child = child->next;
if (!child) break;
node = node->next;
if (!node) break;
writer.writeComma();
}

View File

@ -70,9 +70,6 @@ void JsonParser::parseValueTo(JsonValue destination) {
case '\"':
destination = parseString();
break;
default:
destination = NULL; // invalid JSON
}
}
@ -142,10 +139,9 @@ JsonObject JsonParser::parseObject() {
const char *key = parseString();
if (!key) return NULL;
if (!skip(':'))
return NULL;
if (!skip(':')) return NULL;
JsonValue value = object[key];
JsonValue value = object[key];
parseValueTo(value);
if (!value.success()) return NULL;
@ -159,3 +155,9 @@ JsonObject JsonParser::parseObject() {
const char *JsonParser::parseString() {
return QuotedString::extractFrom(_ptr, &_ptr);
}
JsonValue JsonParser::parseValue() {
JsonValue value = _buffer->createValue();
parseValueTo(value);
return value;
}

View File

@ -5,6 +5,11 @@
// 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::Internals;
void JsonValueImpl::writeTo(JsonWriter &writer) const {
switch (_type) {
@ -17,19 +22,19 @@ void JsonValueImpl::writeTo(JsonWriter &writer) const {
break;
case JSON_STRING:
writer.writeString(content.asString);
writer.writeString(_content.asString);
break;
case JSON_LONG:
writer.writeInteger(content.asInteger);
writer.writeInteger(_content.asInteger);
break;
case JSON_BOOLEAN:
writer.writeBoolean(content.asBoolean);
writer.writeBoolean(_content.asBoolean);
break;
default: // >= JSON_DOUBLE_0_DECIMALS
writer.writeDouble(content.asDouble, type - JSON_DOUBLE_0_DECIMALS);
writer.writeDouble(_content.asDouble, _type - JSON_DOUBLE_0_DECIMALS);
break;
}
}

View File

@ -11,17 +11,16 @@
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
JsonValue JsonArray::add() { return JsonValue(_impl ? _impl->add() : NULL); }
JsonValue JsonArray::operator[](int index) const {
if (!_impl) return JsonValue::null();
return JsonValue((*_impl)[index]);
return JsonValue(_impl ? (*_impl)[index] : NULL);
}
JsonArray JsonArray::createNestedArray() {
if (!_impl) return JsonArray::null();
return JsonArray(_impl->createNestedArray());
return JsonArray(_impl ? _impl->createNestedArray() : NULL);
}
JsonObject JsonArray::createNestedObject() {
if (!_impl) return JsonObject::null();
return JsonObject(_impl->createNestedObject()));
return JsonObject(_impl ? _impl->createNestedObject() : NULL);
}

View File

@ -6,6 +6,12 @@
#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;
@ -13,5 +19,32 @@ using namespace ArduinoJson::Internals;
// TODO: what happens if alloc returns NULL
void* operator new(size_t size, ArduinoJson::JsonBuffer* buffer) {
return _buffer->alloc(size);
return buffer->alloc(size);
}
JsonArray JsonBuffer::createArray() {
return JsonArray(new (this) JsonArrayImpl(this));
}
JsonObject JsonBuffer::createObject() {
return JsonObject(new (this) JsonObjectImpl(this));
}
JsonValue JsonBuffer::createValue() {
return JsonValue(new (this) JsonValueImpl());
}
JsonArray JsonBuffer::parseArray(char* json) {
JsonParser parser(this, json);
return JsonArray(parser.parseArray());
}
JsonObject JsonBuffer::parseObject(char* json) {
JsonParser parser(this, json);
return JsonObject(parser.parseObject());
}
JsonValue JsonBuffer::parseValue(char* json) {
JsonParser parser(this, json);
return JsonValue(parser.parseValue());
}

View File

@ -1,80 +0,0 @@
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include "ArduinoJson/JsonContainer.hpp"
#include "ArduinoJson/JsonBuffer.hpp"
#include "ArduinoJson/Internals/StringBuilder.hpp"
#include "ArduinoJson/Internals/CompactJsonWriter.hpp"
#include "ArduinoJson/Internals/PrettyJsonWriter.hpp"
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
size_t JsonContainer::printTo(char *buffer, size_t bufferSize) const {
StringBuilder sb(buffer, bufferSize);
return printTo(sb);
}
size_t JsonContainer::printTo(Print &p) const {
CompactJsonWriter writer(&p);
_node->writeTo(writer);
return writer.bytesWritten();
}
size_t JsonContainer::prettyPrintTo(char *buffer, size_t bufferSize) const {
StringBuilder sb(buffer, bufferSize);
return prettyPrintTo(sb);
}
size_t JsonContainer::prettyPrintTo(IndentedPrint &p) const {
PrettyJsonWriter writer(&p);
_node->writeTo(writer);
return writer.bytesWritten();
}
size_t JsonContainer::prettyPrintTo(Print &print) const {
IndentedPrint indentedPrint = IndentedPrint(print);
return prettyPrintTo(indentedPrint);
}
JsonNode *JsonContainer::createNode() {
if (!_node) return 0;
JsonBuffer *buffer = _node->getContainerBuffer();
if (!buffer) return 0;
return buffer->createNode();
}
bool JsonContainer::operator==(const JsonContainer &other) const {
if (_node == other._node) return true;
if (!_node || !other._node) return false;
return _node->getProxyTarget() == other._node->getProxyTarget();
}
void JsonContainer::addChild(JsonNode *childToAdd) {
if (_node) _node->addChild(childToAdd);
}
void JsonContainer::removeChild(JsonNode *childToRemove) {
if (_node) _node->removeChild(childToRemove);
}
size_t JsonContainer::size() const {
int n = 0;
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) {
n++;
}
return n;
}
JsonNode* JsonContainer::firstChild() const
{
return _node ? _node->getContainerChild() : 0;
}

View File

@ -4,7 +4,9 @@
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include "ArduinoJson/JsonArray.hpp"
#include "ArduinoJson/JsonObject.hpp"
#include "ArduinoJson/JsonValue.hpp"
using namespace ArduinoJson;

42
src/JsonPrintable.cpp Normal file
View File

@ -0,0 +1,42 @@
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include "ArduinoJson/JsonPrintable.hpp"
#include "ArduinoJson/JsonBuffer.hpp"
#include "ArduinoJson/Internals/StringBuilder.hpp"
#include "ArduinoJson/Internals/CompactJsonWriter.hpp"
#include "ArduinoJson/Internals/PrettyJsonWriter.hpp"
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
size_t JsonPrintable::printTo(char *buffer, size_t bufferSize) const {
StringBuilder sb(buffer, bufferSize);
return printTo(sb);
}
size_t JsonPrintable::printTo(Print &p) const {
CompactJsonWriter writer(&p);
writeTo(writer);
return writer.bytesWritten();
}
size_t JsonPrintable::prettyPrintTo(char *buffer, size_t bufferSize) const {
StringBuilder sb(buffer, bufferSize);
return prettyPrintTo(sb);
}
size_t JsonPrintable::prettyPrintTo(IndentedPrint &p) const {
PrettyJsonWriter writer(&p);
writeTo(writer);
return writer.bytesWritten();
}
size_t JsonPrintable::prettyPrintTo(Print &print) const {
IndentedPrint indentedPrint = IndentedPrint(print);
return prettyPrintTo(indentedPrint);
}

View File

@ -4,25 +4,24 @@
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include "ArduinoJson/JsonValue.hpp"
#include "ArduinoJson/JsonArray.hpp"
#include "ArduinoJson/JsonObject.hpp"
#include "ArduinoJson/JsonValue.hpp"
using namespace ArduinoJson;
JsonValue::operator JsonArray() const {
return _impl ? JsonArray(*_impl) : JsonArray();
JsonValue::operator JsonArray() {
return JsonArray(_impl ? _impl->asArray() : NULL);
}
JsonValue::operator JsonObject() const {
return _impl ? JsonObject(*_impl) : JsonObject();
JsonValue::operator JsonObject() {
return JsonObject(_impl ? _impl->asObject() : NULL);
}
void JsonValue::operator=(JsonArray array) {
void JsonValue::set(JsonArray array) {
if (_impl) _impl->set(array._impl);
}
void JsonValue::operator=(JsonObject object) {
void JsonValue::set(JsonObject object) {
if (_impl) _impl->set(object._impl);
}