forked from bblanchon/ArduinoJson
Epic refactoring in progress...
This commit is contained in:
@ -4,49 +4,68 @@
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
#include "ArduinoJson/JsonArray.hpp"
|
||||
#include "ArduinoJson/JsonObject.hpp"
|
||||
#include "ArduinoJson/JsonValue.hpp"
|
||||
#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;
|
||||
|
||||
JsonValueImpl *JsonArray::operator[](int index) const {
|
||||
for (const_iterator it = begin(); it != end(); ++it) {
|
||||
if (!index) return *it;
|
||||
index--;
|
||||
}
|
||||
JsonValueImpl *JsonArrayImpl::operator[](int index) const {
|
||||
JsonArrayNode *node = _firstChild;
|
||||
while (node && index--) node = node->next;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
JsonValueImpl *JsonArray::add() {
|
||||
JsonValueImpl *JsonArrayImpl::add() {
|
||||
if (_buffer) return NULL;
|
||||
|
||||
JsonArrayNode *node = _buffer->create<JsonArrayNode>();
|
||||
JsonArrayNode *node = new (_buffer) JsonArrayNode();
|
||||
if (!node) return NULL;
|
||||
|
||||
return &node.value;
|
||||
return &node->value;
|
||||
}
|
||||
|
||||
JsonArrayImpl *JsonArray::createNestedArray() {
|
||||
JsonNode *node = createNode();
|
||||
JsonArrayImpl *JsonArrayImpl::createNestedArray() {
|
||||
JsonValueImpl *value = add();
|
||||
if (!value) return NULL;
|
||||
|
||||
if (node) {
|
||||
node->setAsArray(_node->getContainerBuffer());
|
||||
addChild(node);
|
||||
JsonArrayImpl *array = new (_buffer) JsonArrayImpl(_buffer);
|
||||
value->set(array);
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
JsonObjectImpl *JsonArrayImpl::createNestedObject() {
|
||||
JsonValueImpl *value = add();
|
||||
if (!value) return NULL;
|
||||
|
||||
JsonObjectImpl *array = new (_buffer) JsonObjectImpl(_buffer);
|
||||
value->set(array);
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
void JsonArrayImpl::writeTo(JsonWriter &writer) const {
|
||||
JsonArrayNode *child = _firstChild;
|
||||
|
||||
if (child) {
|
||||
writer.beginArray();
|
||||
|
||||
for (;;) {
|
||||
child->value.writeTo(writer);
|
||||
|
||||
child = child->next;
|
||||
if (!child) break;
|
||||
|
||||
writer.writeComma();
|
||||
}
|
||||
|
||||
writer.endArray();
|
||||
} else {
|
||||
writer.writeEmptyArray();
|
||||
}
|
||||
|
||||
return JsonArray(node);
|
||||
}
|
||||
|
||||
JsonObject JsonArray::createNestedObject() {
|
||||
JsonNode *node = createNode();
|
||||
|
||||
if (node) {
|
||||
node->setAsObject(_node->getContainerBuffer());
|
||||
addChild(node);
|
||||
}
|
||||
|
||||
return JsonObject(node);
|
||||
}
|
||||
|
118
src/Internals/JsonObjectImpl.cpp
Normal file
118
src/Internals/JsonObjectImpl.cpp
Normal file
@ -0,0 +1,118 @@
|
||||
// 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/StringBuilder.hpp"
|
||||
|
||||
using namespace ArduinoJson;
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
void JsonObjectImpl::remove(char const *key) { removeNode(getNodeAt(key)); }
|
||||
|
||||
JsonArrayImpl *JsonObjectImpl::createNestedArray(char const *key) {
|
||||
JsonValueImpl *node = getOrCreateValueAt(key);
|
||||
if (!node) return NULL;
|
||||
|
||||
JsonArrayImpl *array = new (_buffer) JsonArrayImpl(_buffer);
|
||||
node->set(array);
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
JsonObject JsonObject::createNestedObject(char const *key) {
|
||||
JsonNode *node = getOrCreateValueAt(key);
|
||||
|
||||
if (node) node->setAsObject(_node->getContainerBuffer());
|
||||
|
||||
return JsonObject(node);
|
||||
}
|
||||
|
||||
JsonNode *JsonObject::getPairAt(const char *key) {
|
||||
for (JsonNode *node = firstChild(); node; node = node->next) {
|
||||
if (!strcmp(node->getAsObjectKey(), key)) return node;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
JsonNode *JsonObject::getOrCreateValueAt(const char *key) {
|
||||
JsonNode *existingNode = getPairAt(key);
|
||||
if (existingNode) return existingNode->getAsObjectValue();
|
||||
|
||||
JsonNode *newValueNode = createNode();
|
||||
if (!newValueNode) return 0;
|
||||
|
||||
JsonNode *newKeyNode = createNode();
|
||||
if (!newKeyNode) return 0;
|
||||
|
||||
newKeyNode->setAsObjectKeyValue(key, newValueNode);
|
||||
|
||||
addChild(newKeyNode);
|
||||
|
||||
return newValueNode;
|
||||
}
|
||||
|
||||
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::writeObjectTo(JsonWriter &writer) {
|
||||
JsonObjectNode *child = _firstChild;
|
||||
|
||||
if (child) {
|
||||
writer.beginObject();
|
||||
|
||||
for (;;) {
|
||||
writer.writeString(child->content.asKeyValue.key);
|
||||
writer.writeColon();
|
||||
child->value->writeTo(writer);
|
||||
|
||||
child = child->next;
|
||||
if (!child) break;
|
||||
|
||||
writer.writeComma();
|
||||
}
|
||||
|
||||
writer.endObject();
|
||||
} else {
|
||||
writer.writeEmptyObject();
|
||||
}
|
||||
}
|
@ -142,7 +142,8 @@ JsonObject JsonParser::parseObject() {
|
||||
const char *key = parseString();
|
||||
if (!key) return NULL;
|
||||
|
||||
skip(':')
|
||||
if (!skip(':'))
|
||||
return NULL;
|
||||
|
||||
JsonValue value = object[key];
|
||||
|
||||
|
@ -1,147 +0,0 @@
|
||||
// Copyright Benoit Blanchon 2014
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
#include "ArduinoJson/Internals/JsonNode.hpp"
|
||||
|
||||
#include "ArduinoJson/Internals/JsonWriter.hpp"
|
||||
#include "ArduinoJson/JsonArray.hpp"
|
||||
#include "ArduinoJson/JsonObject.hpp"
|
||||
#include "ArduinoJson/JsonBuffer.hpp"
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
void JsonNode::writeTo(JsonWriter &writer) {
|
||||
switch (type) {
|
||||
case JSON_PROXY:
|
||||
content.asProxy.target->writeTo(writer);
|
||||
break;
|
||||
|
||||
case JSON_ARRAY:
|
||||
writeArrayTo(writer);
|
||||
break;
|
||||
|
||||
case JSON_OBJECT:
|
||||
writeObjectTo(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;
|
||||
}
|
||||
}
|
||||
|
||||
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 JsonNode::writeArrayTo(JsonWriter &writer) {
|
||||
JsonNode *child = content.asContainer.child;
|
||||
|
||||
if (child) {
|
||||
writer.beginArray();
|
||||
|
||||
for (;;) {
|
||||
child->writeTo(writer);
|
||||
|
||||
child = child->next;
|
||||
if (!child) break;
|
||||
|
||||
writer.writeComma();
|
||||
}
|
||||
|
||||
writer.endArray();
|
||||
} else {
|
||||
writer.writeEmptyArray();
|
||||
}
|
||||
}
|
||||
|
||||
void JsonNode::writeObjectTo(JsonWriter &writer) {
|
||||
JsonNode *child = content.asContainer.child;
|
||||
|
||||
if (child) {
|
||||
writer.beginObject();
|
||||
|
||||
for (;;) {
|
||||
writer.writeString(child->content.asKeyValue.key);
|
||||
writer.writeColon();
|
||||
child->content.asKeyValue.value->writeTo(writer);
|
||||
|
||||
child = child->next;
|
||||
if (!child) break;
|
||||
|
||||
writer.writeComma();
|
||||
}
|
||||
|
||||
writer.endObject();
|
||||
} else {
|
||||
writer.writeEmptyObject();
|
||||
}
|
||||
}
|
||||
|
||||
void JsonNode::setAsProxyOfSelf() {
|
||||
JsonBuffer *buffer = content.asContainer.buffer;
|
||||
if (!buffer) return;
|
||||
|
||||
JsonNode *newNode = buffer->createNode();
|
||||
if (!newNode) return;
|
||||
|
||||
*newNode = *this;
|
||||
|
||||
setAsProxyOf(newNode);
|
||||
}
|
||||
|
||||
void JsonNode::duplicate(JsonNode *other) {
|
||||
if (!other) {
|
||||
type = JSON_UNDEFINED;
|
||||
} else if (other->type == JSON_ARRAY || other->type == JSON_OBJECT) {
|
||||
other->setAsProxyOfSelf();
|
||||
setAsProxyOf(other->content.asProxy.target);
|
||||
} else {
|
||||
*this = *other;
|
||||
}
|
||||
}
|
35
src/Internals/JsonValueImpl.cpp
Normal file
35
src/Internals/JsonValueImpl.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
// Copyright Benoit Blanchon 2014
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
#include "ArduinoJson/Internals/JsonValueImpl.hpp"
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user