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,8 +6,6 @@
#pragma once
// TODO: cleanup
namespace ArduinoJson {
class JsonArray;
class JsonArrayConstIterator;

View File

@ -10,6 +10,7 @@
namespace ArduinoJson {
namespace Internals {
class CompactJsonWriter : public JsonWriter {
public:
explicit CompactJsonWriter(Print *sink) : JsonWriter(sink) {}

View File

@ -14,16 +14,10 @@ namespace Internals {
class JsonArrayNode {
public:
static JsonArrayNode* createFrom(JsonBuffer* buffer) {
void* ptr = buffer->alloc(sizeof(JsonArrayNode));
return ptr ? new (ptr) JsonArrayNode() : NULL;
}
JsonArrayNode() : next(0) {}
JsonArrayNode* next;
JsonValue value;
private:
JsonArrayNode() : next(0) {}
};
}
}

View File

@ -14,16 +14,10 @@ namespace Internals {
class JsonObjectNode {
public:
static JsonObjectNode* createFrom(JsonBuffer* buffer, const char* key) {
void* ptr = buffer->alloc(sizeof(JsonObjectNode));
return ptr ? new (ptr) JsonObjectNode(key) : NULL;
}
JsonObjectNode(const char* k) : pair(k), next(NULL) {}
JsonPair pair;
JsonObjectNode* next;
private:
JsonObjectNode(const char* k) : pair(k), next(NULL) {}
};
}
}

View File

@ -10,6 +10,7 @@
namespace ArduinoJson {
namespace Internals {
class JsonWriter {
public:
explicit JsonWriter(Print *sink) : _sink(sink), _length(0) {}

View File

@ -11,6 +11,7 @@
namespace ArduinoJson {
namespace Internals {
class PrettyJsonWriter : public JsonWriter {
public:
explicit PrettyJsonWriter(IndentedPrint *sink)

View File

@ -10,10 +10,10 @@
namespace ArduinoJson {
namespace Internals {
class QuotedString {
public:
static size_t printTo(const char *, Print *);
static char *extractFrom(char *input, char **end);
};
}

View File

@ -13,6 +13,7 @@
#include "JsonObject.hpp"
namespace ArduinoJson {
class JsonArray : public JsonPrintable {
friend class JsonBuffer;
@ -25,14 +26,15 @@ class JsonArray : public JsonPrintable {
bool success() { return _buffer != NULL; }
value_type &operator[](int index) const;
value_type &add();
value_type &operator[](int index) const { return at(index); }
value_type &at(int index) const;
template <typename T>
void add(T value) {
add().set(value);
}
value_type &add();
void add(double value, int decimals) { add().set(value, decimals); }
void add(JsonArray &nestedArray) { add().set(nestedArray); }
void add(JsonObject &nestedObject) { add().set(nestedObject); }
@ -54,14 +56,21 @@ class JsonArray : public JsonPrintable {
// constructor is private: instance must be created via a JsonBuffer
JsonArray(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {}
JsonArray(const JsonArray &); // copy is forbidden, use a reference instead
JsonArray &operator=(
const JsonArray &); // copy is forbidden, use a reference instead
// copy is forbidden, use a reference instead
JsonArray(const JsonArray &);
JsonArray &operator=(const JsonArray &);
Internals::JsonArrayNode *createNode();
inline void addNode(Internals::JsonArrayNode *node);
JsonBuffer *_buffer;
Internals::JsonArrayNode *_firstNode;
static JsonArray _invalid;
};
bool operator==(const JsonArray &left, const JsonArray &right) {
// two JsonArray are equal if they are the same instance
// (we don't compare the content)
return &left == &right;
}
}

View File

@ -6,8 +6,6 @@
#pragma once
#include <new>
#include "ForwardDeclarations.hpp"
#include "JsonValue.hpp"

View File

@ -13,6 +13,7 @@
#include "JsonArray.hpp"
namespace ArduinoJson {
class JsonObject : public JsonPrintable {
friend class JsonBuffer;
@ -22,16 +23,18 @@ class JsonObject : public JsonPrintable {
typedef JsonObjectIterator iterator;
typedef JsonObjectConstIterator const_iterator;
bool success() const { return _buffer != NULL; }
int size() const;
JsonValue &operator[](key_type key);
JsonValue &at(key_type key);
JsonValue &operator[](key_type key) { return at(key); }
void remove(key_type key);
template <typename T>
void add(key_type key, T value) {
add(key).set(value);
}
void add(key_type key, JsonArray &array) { add(key).set(array); }
void add(key_type key, JsonObject &object) { add(key).set(object); }
@ -57,6 +60,7 @@ class JsonObject : public JsonPrintable {
const JsonObject &); // copy is forbidden, use a reference instead
JsonValue &add(key_type key) { return (*this)[key]; }
Internals::JsonObjectNode *createNode(key_type key);
void addNode(Internals::JsonObjectNode *nodeToAdd);
void removeNode(Internals::JsonObjectNode *nodeToRemove);
@ -67,4 +71,10 @@ class JsonObject : public JsonPrintable {
Internals::JsonObjectNode *_firstNode;
static JsonObject _invalid;
};
bool operator==(const JsonObject &left, const JsonObject &right) {
// two JsonObject are equal if they are the same instance
// (we don't compare the content)
return &left == &right;
}
}

View File

@ -10,6 +10,7 @@
#include "Internals/JsonObjectNode.hpp"
namespace ArduinoJson {
struct JsonPair {
JsonPair(const char* k) : key(k) {}

View File

@ -13,7 +13,6 @@
#include "Internals/JsonValueType.hpp"
namespace ArduinoJson {
class JsonValue {
public:
JsonValue() : _type(Internals::JSON_UNDEFINED) {}
@ -64,7 +63,10 @@ class JsonValue {
void writeTo(Internals::JsonWriter &writer) const;
private:
JsonValue(Internals::JsonValueType type) : _type(type) {}
Internals::JsonValueType _type;
Internals::JsonValueContent _content;
static JsonValue _invalid;
};
}

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

View File

@ -13,11 +13,12 @@
using namespace ArduinoJson;
class JsonObject_PrettyPrintTo_Tests : public testing::Test {
protected:
JsonObject object;
StaticJsonBuffer<30> json;
public:
JsonObject_PrettyPrintTo_Tests() : object(json.createObject()) {}
virtual void SetUp() { object = json.createObject(); }
protected:
JsonObject &object;
StaticJsonBuffer<30> json;
void outputMustBe(const char *expected) {
size_t n = object.prettyPrintTo(buffer, sizeof(buffer));
@ -63,10 +64,10 @@ TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers) {
}
TEST_F(JsonObject_PrettyPrintTo_Tests, NestedContainers) {
JsonObject nested1 = object.createNestedObject("key1");
JsonObject &nested1 = object.createNestedObject("key1");
nested1["a"] = 1;
JsonArray nested2 = object.createNestedArray("key2");
JsonArray &nested2 = object.createNestedArray("key2");
nested2.add(2);
outputMustBe(

View File

@ -13,9 +13,10 @@
using namespace ArduinoJson;
class JsonObject_Serialization_Tests : public testing::Test {
protected:
virtual void SetUp() { object = json.createObject(); }
public:
JsonObject_Serialization_Tests() : object(json.createObject()) {}
protected:
void outputMustBe(const char *expected) {
char actual[256];
int result = object.printTo(actual, sizeof(actual));
@ -24,7 +25,7 @@ class JsonObject_Serialization_Tests : public testing::Test {
EXPECT_EQ(strlen(expected), result);
}
JsonObject object;
JsonObject &object;
StaticJsonBuffer<5> json;
};
@ -113,7 +114,7 @@ TEST_F(JsonObject_Serialization_Tests, OneFalse) {
}
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedArrayViaProxy) {
JsonArray nestedArray = json.createArray();
JsonArray &nestedArray = json.createArray();
object["key"] = nestedArray;
@ -121,7 +122,7 @@ TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedArrayViaProxy) {
}
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedObjectViaProxy) {
JsonObject nestedArray = json.createObject();
JsonObject &nestedArray = json.createObject();
object["key"] = nestedArray;

View File

@ -15,17 +15,17 @@ class JsonParser_Array_Tests : public testing::Test {
protected:
void whenInputIs(const char *json) {
strcpy(_jsonString, json);
_array = _jsonBuffer.parseArray(_jsonString);
_array = &_jsonBuffer.parseArray(_jsonString);
}
void parseMustSucceed() { EXPECT_TRUE(_array.success()); }
void parseMustSucceed() { EXPECT_TRUE(_array->success()); }
void parseMustFail() {
EXPECT_FALSE(_array.success());
EXPECT_EQ(0, _array.size());
EXPECT_FALSE(_array->success());
EXPECT_EQ(0, _array->size());
}
void sizeMustBe(int expected) { EXPECT_EQ(expected, _array.size()); }
void sizeMustBe(int expected) { EXPECT_EQ(expected, _array->size()); }
template <typename T>
void firstElementMustBe(T expected) {
@ -39,15 +39,15 @@ class JsonParser_Array_Tests : public testing::Test {
template <typename T>
void elementAtIndexMustBe(int index, T expected) {
EXPECT_EQ(expected, _array[index].as<T>());
EXPECT_EQ(expected, _array->at(index).as<T>());
}
void elementAtIndexMustBe(int index, const char *expected) {
EXPECT_STREQ(expected, _array[index].as<const char *>());
EXPECT_STREQ(expected, _array->at(index).as<const char *>());
}
StaticJsonBuffer<42> _jsonBuffer;
JsonArray _array;
JsonArray *_array;
char _jsonString[256];
};

View File

@ -15,9 +15,9 @@ TEST(JsonParser_Nested_Tests, ArrayNestedInObject) {
StaticJsonBuffer<42> jsonBuffer;
char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } ";
JsonObject object = jsonBuffer.parseObject(jsonString);
JsonArray array1 = object["ab"];
JsonArray array2 = object["cd"];
JsonObject &object = jsonBuffer.parseObject(jsonString);
JsonArray &array1 = object["ab"];
JsonArray &array2 = object["cd"];
ASSERT_TRUE(object.success());
@ -39,9 +39,9 @@ TEST(JsonParser_Nested_Tests, ObjectNestedInArray) {
char jsonString[] =
" [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] ";
JsonArray array = jsonBuffer.parseArray(jsonString);
JsonObject object1 = array[0];
JsonObject object2 = array[1];
JsonArray &array = jsonBuffer.parseArray(jsonString);
JsonObject &object1 = array[0];
JsonObject &object2 = array[1];
ASSERT_TRUE(array.success());

View File

@ -15,27 +15,27 @@ class JsonParser_Object_Test : public testing::Test {
protected:
void whenInputIs(const char *jsonString) {
strcpy(_jsonString, jsonString);
_object = _jsonBuffer.parseObject(_jsonString);
_object = &_jsonBuffer.parseObject(_jsonString);
}
void parseMustSucceed() { EXPECT_TRUE(_object.success()); }
void parseMustSucceed() { EXPECT_TRUE(_object->success()); }
void parseMustFail() { EXPECT_FALSE(_object.success()); }
void parseMustFail() { EXPECT_FALSE(_object->success()); }
void sizeMustBe(int expected) { EXPECT_EQ(expected, _object.size()); }
void sizeMustBe(int expected) { EXPECT_EQ(expected, _object->size()); }
void keyMustHaveValue(const char *key, const char *expected) {
EXPECT_STREQ(expected, _object[key].as<const char *>());
EXPECT_STREQ(expected, _object->at(key).as<const char *>());
}
template <typename T>
void keyMustHaveValue(const char *key, T expected) {
EXPECT_EQ(expected, _object[key].as<T>());
EXPECT_EQ(expected, _object->at(key).as<T>());
}
private:
StaticJsonBuffer<10> _jsonBuffer;
JsonObject _object;
JsonObject *_object;
char _jsonString[256];
};

View File

@ -15,11 +15,6 @@ using namespace ArduinoJson;
class JsonValueTests : public ::testing::Test {
protected:
virtual void SetUp() {
jsonValue1 = json.createValue();
jsonValue2 = json.createValue();
}
StaticJsonBuffer<42> json;
JsonValue jsonValue1;
JsonValue jsonValue2;
@ -54,11 +49,11 @@ TEST_F(JsonValueTests, CanStoreString) {
}
TEST_F(JsonValueTests, CanStoreObject) {
JsonObject innerObject1 = json.createObject();
JsonObject &innerObject1 = json.createObject();
jsonValue1 = innerObject1;
EXPECT_EQ(innerObject1, jsonValue1.as<JsonObject>());
EXPECT_EQ(innerObject1, jsonValue1.asObject());
}
TEST_F(JsonValueTests, IntegersAreCopiedByValue) {
@ -94,21 +89,21 @@ TEST_F(JsonValueTests, StringsAreCopiedByValue) {
}
TEST_F(JsonValueTests, ObjectsAreCopiedByReference) {
JsonObject object = json.createObject();
JsonObject &object = json.createObject();
jsonValue1 = object;
object["hello"] = "world";
EXPECT_EQ(1, jsonValue1.as<JsonObject>().size());
EXPECT_EQ(1, jsonValue1.asObject().size());
}
TEST_F(JsonValueTests, ArraysAreCopiedByReference) {
JsonArray array = json.createArray();
JsonArray &array = json.createArray();
jsonValue1 = array;
array.add("world");
EXPECT_EQ(1, jsonValue1.as<JsonArray>().size());
EXPECT_EQ(1, jsonValue1.asArray().size());
}

View File

@ -46,14 +46,14 @@ TEST(StaticJsonBuffer,
WhenCreateObjectIsCalled_ThenAnEmptyJsonObjectIsReturned) {
StaticJsonBuffer<42> json;
JsonObject obj = json.createObject();
JsonObject &obj = json.createObject();
EXPECT_EQ(0, obj.size());
}
TEST(StaticJsonBuffer,
GivenAJsonObject_WhenValuesAreAdded_ThenSizeIsIncreasedByTwo) {
StaticJsonBuffer<42> json;
JsonObject obj = json.createObject();
JsonObject &obj = json.createObject();
obj["hello"];
EXPECT_EQ(3, json.size());
@ -66,7 +66,7 @@ TEST(
StaticJsonBuffer,
GivenAJsonObject_WhenSameValuesAreAddedTwice_ThenSizeIsOnlyIncreasedByTwo) {
StaticJsonBuffer<42> json;
JsonObject obj = json.createObject();
JsonObject &obj = json.createObject();
obj["hello"];
EXPECT_EQ(3, json.size());