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

@ -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();
}
}