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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -13,6 +13,7 @@
#include "JsonObject.hpp" #include "JsonObject.hpp"
namespace ArduinoJson { namespace ArduinoJson {
class JsonArray : public JsonPrintable { class JsonArray : public JsonPrintable {
friend class JsonBuffer; friend class JsonBuffer;
@ -25,14 +26,15 @@ class JsonArray : public JsonPrintable {
bool success() { return _buffer != NULL; } bool success() { return _buffer != NULL; }
value_type &operator[](int index) const; value_type &operator[](int index) const { return at(index); }
value_type &add(); value_type &at(int index) const;
template <typename T> template <typename T>
void add(T value) { void add(T value) {
add().set(value); add().set(value);
} }
value_type &add();
void add(double value, int decimals) { add().set(value, decimals); } void add(double value, int decimals) { add().set(value, decimals); }
void add(JsonArray &nestedArray) { add().set(nestedArray); } void add(JsonArray &nestedArray) { add().set(nestedArray); }
void add(JsonObject &nestedObject) { add().set(nestedObject); } 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 // constructor is private: instance must be created via a JsonBuffer
JsonArray(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {} JsonArray(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {}
JsonArray(const JsonArray &); // copy is forbidden, use a reference instead // copy is forbidden, use a reference instead
JsonArray &operator=( JsonArray(const JsonArray &);
const JsonArray &); // copy is forbidden, use a reference instead JsonArray &operator=(const JsonArray &);
Internals::JsonArrayNode *createNode();
inline void addNode(Internals::JsonArrayNode *node); inline void addNode(Internals::JsonArrayNode *node);
JsonBuffer *_buffer; JsonBuffer *_buffer;
Internals::JsonArrayNode *_firstNode; Internals::JsonArrayNode *_firstNode;
static JsonArray _invalid; 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 #pragma once
#include <new>
#include "ForwardDeclarations.hpp" #include "ForwardDeclarations.hpp"
#include "JsonValue.hpp" #include "JsonValue.hpp"

View File

@ -13,6 +13,7 @@
#include "JsonArray.hpp" #include "JsonArray.hpp"
namespace ArduinoJson { namespace ArduinoJson {
class JsonObject : public JsonPrintable { class JsonObject : public JsonPrintable {
friend class JsonBuffer; friend class JsonBuffer;
@ -22,16 +23,18 @@ class JsonObject : public JsonPrintable {
typedef JsonObjectIterator iterator; typedef JsonObjectIterator iterator;
typedef JsonObjectConstIterator const_iterator; typedef JsonObjectConstIterator const_iterator;
bool success() const { return _buffer != NULL; }
int size() const; 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); void remove(key_type key);
template <typename T> template <typename T>
void add(key_type key, T value) { void add(key_type key, T value) {
add(key).set(value); add(key).set(value);
} }
void add(key_type key, JsonArray &array) { add(key).set(array); } void add(key_type key, JsonArray &array) { add(key).set(array); }
void add(key_type key, JsonObject &object) { add(key).set(object); } 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 const JsonObject &); // copy is forbidden, use a reference instead
JsonValue &add(key_type key) { return (*this)[key]; } JsonValue &add(key_type key) { return (*this)[key]; }
Internals::JsonObjectNode *createNode(key_type key);
void addNode(Internals::JsonObjectNode *nodeToAdd); void addNode(Internals::JsonObjectNode *nodeToAdd);
void removeNode(Internals::JsonObjectNode *nodeToRemove); void removeNode(Internals::JsonObjectNode *nodeToRemove);
@ -67,4 +71,10 @@ class JsonObject : public JsonPrintable {
Internals::JsonObjectNode *_firstNode; Internals::JsonObjectNode *_firstNode;
static JsonObject _invalid; 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" #include "Internals/JsonObjectNode.hpp"
namespace ArduinoJson { namespace ArduinoJson {
struct JsonPair { struct JsonPair {
JsonPair(const char* k) : key(k) {} JsonPair(const char* k) : key(k) {}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -15,17 +15,17 @@ class JsonParser_Array_Tests : public testing::Test {
protected: protected:
void whenInputIs(const char *json) { void whenInputIs(const char *json) {
strcpy(_jsonString, 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() { void parseMustFail() {
EXPECT_FALSE(_array.success()); EXPECT_FALSE(_array->success());
EXPECT_EQ(0, _array.size()); 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> template <typename T>
void firstElementMustBe(T expected) { void firstElementMustBe(T expected) {
@ -39,15 +39,15 @@ class JsonParser_Array_Tests : public testing::Test {
template <typename T> template <typename T>
void elementAtIndexMustBe(int index, T expected) { 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) { 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; StaticJsonBuffer<42> _jsonBuffer;
JsonArray _array; JsonArray *_array;
char _jsonString[256]; char _jsonString[256];
}; };

View File

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

View File

@ -15,27 +15,27 @@ class JsonParser_Object_Test : public testing::Test {
protected: protected:
void whenInputIs(const char *jsonString) { void whenInputIs(const char *jsonString) {
strcpy(_jsonString, 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) { 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> template <typename T>
void keyMustHaveValue(const char *key, T expected) { void keyMustHaveValue(const char *key, T expected) {
EXPECT_EQ(expected, _object[key].as<T>()); EXPECT_EQ(expected, _object->at(key).as<T>());
} }
private: private:
StaticJsonBuffer<10> _jsonBuffer; StaticJsonBuffer<10> _jsonBuffer;
JsonObject _object; JsonObject *_object;
char _jsonString[256]; char _jsonString[256];
}; };

View File

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