forked from bblanchon/ArduinoJson
Epic refactoring in progress...
This commit is contained in:
@ -20,12 +20,13 @@ class IndentedPrint;
|
||||
class JsonArrayConstIterator;
|
||||
class JsonArrayImpl;
|
||||
class JsonArrayIterator;
|
||||
class JsonArrayNode;
|
||||
class JsonObjectConstIterator;
|
||||
class JsonObjectImpl;
|
||||
class JsonObjectIterator;
|
||||
class JsonObjectConstIterator;
|
||||
class JsonObjectNode;
|
||||
class JsonParser;
|
||||
class JsonValueImpl;
|
||||
class JsonWriter;
|
||||
struct JsonArrayNode;
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ class JsonArrayImpl {
|
||||
typedef JsonArrayIterator iterator;
|
||||
typedef JsonArrayConstIterator const_iterator;
|
||||
|
||||
JsonArrayImpl(JsonBuffer *buffer) : _buffer(buffer) {}
|
||||
static JsonArrayImpl *createFrom(JsonBuffer *buffer);
|
||||
|
||||
int size() const;
|
||||
|
||||
@ -37,6 +37,8 @@ class JsonArrayImpl {
|
||||
const_iterator end() const { return const_iterator(0); }
|
||||
|
||||
private:
|
||||
JsonArrayImpl(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {}
|
||||
|
||||
JsonBuffer *_buffer;
|
||||
Internals::JsonArrayNode *_firstNode;
|
||||
};
|
||||
|
@ -6,14 +6,24 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../JsonValue.hpp"
|
||||
#include "JsonValueImpl.hpp"
|
||||
#include "../JsonBuffer.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
struct JsonArrayNode {
|
||||
JsonArrayNode *next;
|
||||
class JsonArrayNode {
|
||||
public:
|
||||
static JsonArrayNode* createFrom(JsonBuffer* buffer) {
|
||||
void* ptr = buffer->alloc(sizeof(JsonArrayNode));
|
||||
return ptr ? new (ptr) JsonArrayNode() : NULL;
|
||||
}
|
||||
|
||||
JsonArrayNode* next;
|
||||
JsonValueImpl value;
|
||||
|
||||
private:
|
||||
JsonArrayNode() : next(0) {}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ class JsonObjectImpl {
|
||||
typedef JsonObjectIterator iterator;
|
||||
typedef JsonObjectConstIterator const_iterator;
|
||||
|
||||
JsonObjectImpl(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {}
|
||||
static JsonObjectImpl *createFrom(JsonBuffer *buffer);
|
||||
|
||||
int size() const;
|
||||
|
||||
@ -38,6 +38,8 @@ class JsonObjectImpl {
|
||||
void writeTo(JsonWriter &writer) const;
|
||||
|
||||
private:
|
||||
JsonObjectImpl(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {}
|
||||
|
||||
void addNode(JsonObjectNode *nodeToAdd);
|
||||
void removeNode(JsonObjectNode *nodeToRemove);
|
||||
|
||||
|
@ -7,16 +7,24 @@
|
||||
#pragma once
|
||||
|
||||
#include "JsonValueImpl.hpp"
|
||||
#include "../JsonBuffer.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
struct JsonObjectNode {
|
||||
JsonObjectNode(const char* k) : key(k) {}
|
||||
class JsonObjectNode {
|
||||
public:
|
||||
static JsonObjectNode* createFrom(JsonBuffer* buffer, const char* key) {
|
||||
void* ptr = buffer->alloc(sizeof(JsonObjectNode));
|
||||
return ptr ? new (ptr) JsonObjectNode(key) : NULL;
|
||||
}
|
||||
|
||||
const char* const key;
|
||||
JsonValueImpl value;
|
||||
JsonObjectNode* next;
|
||||
|
||||
private:
|
||||
JsonObjectNode(const char* k) : key(k) {}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,8 @@ class JsonValueImpl {
|
||||
public:
|
||||
JsonValueImpl() : _type(JSON_UNDEFINED) {}
|
||||
|
||||
static JsonValueImpl *createFrom(JsonBuffer *buffer);
|
||||
|
||||
void set(bool value) {
|
||||
_type = JSON_BOOLEAN;
|
||||
_content.asBoolean = value;
|
||||
|
@ -18,7 +18,7 @@ class JsonArray : public JsonPrintable {
|
||||
typedef Internals::JsonArrayIterator iterator;
|
||||
typedef Internals::JsonArrayConstIterator const_iterator;
|
||||
|
||||
JsonArray() {}
|
||||
JsonArray() : _impl(NULL) {}
|
||||
JsonArray(Internals::JsonArrayImpl* impl) : _impl(impl) {}
|
||||
|
||||
bool success() const { return _impl; }
|
||||
|
@ -35,5 +35,3 @@ class JsonBuffer {
|
||||
virtual void* alloc(size_t size) = 0;
|
||||
};
|
||||
}
|
||||
|
||||
void* operator new(size_t size, ArduinoJson::JsonBuffer* buffer);
|
||||
|
@ -13,6 +13,11 @@
|
||||
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++;
|
||||
@ -29,7 +34,7 @@ JsonValueImpl *JsonArrayImpl::operator[](int index) const {
|
||||
JsonValueImpl *JsonArrayImpl::add() {
|
||||
if (_buffer) return NULL;
|
||||
|
||||
JsonArrayNode *node = new (_buffer) JsonArrayNode();
|
||||
JsonArrayNode *node = JsonArrayNode::createFrom(_buffer);
|
||||
if (!node) return NULL;
|
||||
|
||||
return &node->value;
|
||||
@ -39,7 +44,7 @@ JsonArrayImpl *JsonArrayImpl::createNestedArray() {
|
||||
JsonValueImpl *value = add();
|
||||
if (!value) return NULL;
|
||||
|
||||
JsonArrayImpl *array = new (_buffer) JsonArrayImpl(_buffer);
|
||||
JsonArrayImpl *array = JsonArrayImpl::createFrom(_buffer);
|
||||
value->set(array);
|
||||
|
||||
return array;
|
||||
@ -49,7 +54,7 @@ JsonObjectImpl *JsonArrayImpl::createNestedObject() {
|
||||
JsonValueImpl *value = add();
|
||||
if (!value) return NULL;
|
||||
|
||||
JsonObjectImpl *array = new (_buffer) JsonObjectImpl(_buffer);
|
||||
JsonObjectImpl *array = JsonObjectImpl::createFrom(_buffer);
|
||||
value->set(array);
|
||||
|
||||
return array;
|
||||
|
@ -17,6 +17,11 @@
|
||||
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++;
|
||||
@ -34,7 +39,7 @@ JsonArrayImpl *JsonObjectImpl::createNestedArray(char const *key) {
|
||||
JsonObjectNode *node = getOrCreateNodeAt(key);
|
||||
if (!node) return NULL;
|
||||
|
||||
JsonArrayImpl *array = new (_buffer) JsonArrayImpl(_buffer);
|
||||
JsonArrayImpl *array = JsonArrayImpl::createFrom(_buffer);
|
||||
node->value.set(array);
|
||||
|
||||
return array;
|
||||
@ -44,7 +49,7 @@ JsonObjectImpl *JsonObjectImpl::createNestedObject(const char *key) {
|
||||
JsonObjectNode *node = getOrCreateNodeAt(key);
|
||||
if (!node) return NULL;
|
||||
|
||||
JsonObjectImpl *object = new (_buffer) JsonObjectImpl(_buffer);
|
||||
JsonObjectImpl *object = JsonObjectImpl::createFrom(_buffer);
|
||||
node->value.set(object);
|
||||
|
||||
return object;
|
||||
@ -61,7 +66,7 @@ JsonObjectNode *JsonObjectImpl::getOrCreateNodeAt(const char *key) {
|
||||
JsonObjectNode *existingNode = getNodeAt(key);
|
||||
if (existingNode) return existingNode;
|
||||
|
||||
JsonObjectNode *newNode = new (_buffer) JsonObjectNode(key);
|
||||
JsonObjectNode *newNode = JsonObjectNode::createFrom(_buffer, key);
|
||||
|
||||
if (newNode) addNode(newNode);
|
||||
|
||||
|
@ -9,9 +9,15 @@
|
||||
#include "ArduinoJson/Internals/JsonObjectImpl.hpp"
|
||||
#include "ArduinoJson/Internals/JsonWriter.hpp"
|
||||
|
||||
using namespace ArduinoJson;
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
void JsonValueImpl::writeTo(JsonWriter &writer) const {
|
||||
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);
|
||||
|
@ -17,21 +17,16 @@
|
||||
using namespace ArduinoJson;
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
// TODO: what happens if alloc returns NULL
|
||||
void* operator new(size_t size, ArduinoJson::JsonBuffer* buffer) {
|
||||
return buffer->alloc(size);
|
||||
}
|
||||
|
||||
JsonArray JsonBuffer::createArray() {
|
||||
return JsonArray(new (this) JsonArrayImpl(this));
|
||||
return JsonArray(JsonArrayImpl::createFrom(this));
|
||||
}
|
||||
|
||||
JsonObject JsonBuffer::createObject() {
|
||||
return JsonObject(new (this) JsonObjectImpl(this));
|
||||
return JsonObject(JsonObjectImpl::createFrom(this));
|
||||
}
|
||||
|
||||
JsonValue JsonBuffer::createValue() {
|
||||
return JsonValue(new (this) JsonValueImpl());
|
||||
return JsonValue(JsonValueImpl::createFrom(this));
|
||||
}
|
||||
|
||||
JsonArray JsonBuffer::parseArray(char* json) {
|
||||
|
@ -38,13 +38,11 @@ class Issue10 : public testing::Test {
|
||||
buffer);
|
||||
}
|
||||
|
||||
void nodeCountMustBe(int expected) { EXPECT_EQ(expected, json.size()); }
|
||||
|
||||
Person persons[2];
|
||||
StaticJsonBuffer<20> json;
|
||||
};
|
||||
|
||||
TEST_F(Issue10, PopulateArrayByAddingAnObject) {
|
||||
StaticJsonBuffer<20> json;
|
||||
JsonArray array = json.createArray();
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
@ -58,10 +56,10 @@ TEST_F(Issue10, PopulateArrayByAddingAnObject) {
|
||||
}
|
||||
|
||||
checkJsonString(array);
|
||||
nodeCountMustBe(15);
|
||||
}
|
||||
|
||||
TEST_F(Issue10, PopulateArrayByCreatingNestedObjects) {
|
||||
StaticJsonBuffer<20> json;
|
||||
JsonArray array = json.createArray();
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
@ -72,5 +70,4 @@ TEST_F(Issue10, PopulateArrayByCreatingNestedObjects) {
|
||||
}
|
||||
|
||||
checkJsonString(array);
|
||||
nodeCountMustBe(11);
|
||||
}
|
||||
|
Reference in New Issue
Block a user