Huge refactoring in progress...

This commit is contained in:
Benoit Blanchon
2014-10-30 14:03:33 +01:00
parent c3001e9ea9
commit 4c204840e9
25 changed files with 108 additions and 79 deletions

View File

@ -6,6 +6,8 @@
#include "ArduinoJson/JsonArray.hpp"
#include <new> // required for placement new
#include "ArduinoJson/JsonBuffer.hpp"
#include "ArduinoJson/JsonObject.hpp"
#include "ArduinoJson/Internals/JsonWriter.hpp"
@ -13,20 +15,22 @@
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
JsonArray JsonArray::_invalid(NULL);
int JsonArray::size() const {
int nodeCount = 0;
for (JsonArrayNode *node = _firstNode; node; node = node->next) nodeCount++;
return nodeCount;
}
JsonValue &JsonArray::operator[](int index) const {
JsonValue &JsonArray::at(int index) const {
JsonArrayNode *node = _firstNode;
while (node && index--) node = node->next;
return node ? node->value : JsonValue::invalid();
}
JsonValue &JsonArray::add() {
JsonArrayNode *node = JsonArrayNode::createFrom(_buffer);
JsonArrayNode *node = createNode();
if (!node) return JsonValue::invalid();
addNode(node);
@ -34,6 +38,12 @@ JsonValue &JsonArray::add() {
return node->value;
}
JsonArrayNode *JsonArray::createNode() {
if (_buffer) return NULL;
void *ptr = _buffer->alloc(sizeof(JsonArrayNode));
return ptr ? new (ptr) JsonArrayNode() : NULL;
}
void JsonArray::addNode(JsonArrayNode *newNode) {
if (_firstNode) {
JsonArrayNode *lastNode = _firstNode;

View File

@ -6,6 +6,8 @@
#include "ArduinoJson/JsonBuffer.hpp"
#include <new> // required for the placement new
#include "ArduinoJson/JsonArray.hpp"
#include "ArduinoJson/JsonObject.hpp"
#include "ArduinoJson/JsonValue.hpp"

View File

@ -6,6 +6,7 @@
#include "ArduinoJson/JsonObject.hpp"
#include <new> // required for placement new
#include <string.h> // for strcmp
#include "ArduinoJson/JsonBuffer.hpp"
@ -17,13 +18,15 @@
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
JsonObject JsonObject::_invalid(NULL);
int JsonObject::size() const {
int nodeCount = 0;
for (JsonObjectNode *node = _firstNode; node; node = node->next) nodeCount++;
return nodeCount;
}
JsonValue &JsonObject::operator[](const char *key) {
JsonValue &JsonObject::at(const char *key) {
JsonObjectNode *node = getOrCreateNodeAt(key);
return node ? node->pair.value : JsonValue::invalid();
}
@ -55,13 +58,19 @@ JsonObjectNode *JsonObject::getOrCreateNodeAt(const char *key) {
JsonObjectNode *existingNode = getNodeAt(key);
if (existingNode) return existingNode;
JsonObjectNode *newNode = JsonObjectNode::createFrom(_buffer, key);
JsonObjectNode *newNode = createNode(key);
if (newNode) addNode(newNode);
return newNode;
}
JsonObjectNode *JsonObject::createNode(const char *key) {
if (!_buffer) return NULL;
void *ptr = _buffer->alloc(sizeof(JsonObjectNode));
return ptr ? new (ptr) JsonObjectNode(key) : NULL;
}
void JsonObject::addNode(JsonObjectNode *nodeToAdd) {
if (!_firstNode) {
_firstNode = nodeToAdd;

View File

@ -12,6 +12,8 @@
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
JsonValue JsonValue::_invalid(JSON_INVALID);
JsonValue::operator JsonArray &() const {
return _type == JSON_ARRAY ? *_content.asArray : JsonArray::invalid();
}