mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-14 19:16:35 +02:00
Huge refactoring in progress...
This commit is contained in:
@ -10,6 +10,7 @@
|
||||
#include "JsonArrayIterator.hpp"
|
||||
#include "JsonArrayConstIterator.hpp"
|
||||
#include "JsonPrintable.hpp"
|
||||
#include "JsonObject.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
class JsonArray : public JsonPrintable {
|
||||
@ -22,6 +23,8 @@ class JsonArray : public JsonPrintable {
|
||||
|
||||
int size() const;
|
||||
|
||||
bool success() {return _buffer != NULL;}
|
||||
|
||||
value_type &operator[](int index) const;
|
||||
value_type &add();
|
||||
|
||||
@ -30,6 +33,14 @@ class JsonArray : public JsonPrintable {
|
||||
add().set(value);
|
||||
}
|
||||
|
||||
void add(JsonArray &nestedArray) {
|
||||
add().set(nestedArray);
|
||||
}
|
||||
|
||||
void add(JsonObject&nestedObject){
|
||||
add().set(nestedObject);
|
||||
}
|
||||
|
||||
JsonArray &createNestedArray();
|
||||
JsonObject &createNestedObject();
|
||||
|
||||
@ -44,6 +55,9 @@ class JsonArray : public JsonPrintable {
|
||||
virtual void writeTo(Internals::JsonWriter &writer) const;
|
||||
|
||||
private:
|
||||
JsonArray(const JsonArray&); // copy is forbidden, use a reference instead
|
||||
JsonArray& operator=(const JsonArray&); // copy is forbidden, use a reference instead
|
||||
|
||||
inline void addNode(Internals::JsonArrayNode *node);
|
||||
|
||||
JsonBuffer *_buffer;
|
||||
|
@ -10,6 +10,7 @@
|
||||
#include "JsonObjectIterator.hpp"
|
||||
#include "JsonPrintable.hpp"
|
||||
#include "Internals/JsonObjectNode.hpp"
|
||||
#include "JsonArray.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
class JsonObject : public JsonPrintable {
|
||||
@ -31,6 +32,14 @@ class JsonObject : public JsonPrintable {
|
||||
(*this)[key] = value;
|
||||
}
|
||||
|
||||
void add(key_type key, JsonArray &nestedArray) {
|
||||
(*this)[key] = nestedArray;
|
||||
}
|
||||
|
||||
void add(key_type key, JsonObject &nestedObject) {
|
||||
(*this)[key] = nestedObject;
|
||||
}
|
||||
|
||||
JsonArray &createNestedArray(key_type key);
|
||||
JsonObject &createNestedObject(key_type key);
|
||||
|
||||
@ -45,6 +54,9 @@ class JsonObject : public JsonPrintable {
|
||||
virtual void writeTo(Internals::JsonWriter &writer) const;
|
||||
|
||||
private:
|
||||
JsonObject(const JsonObject&); // copy is forbidden, use a reference instead
|
||||
JsonObject& operator=(const JsonObject&); // copy is forbidden, use a reference instead
|
||||
|
||||
void addNode(Internals::JsonObjectNode *nodeToAdd);
|
||||
void removeNode(Internals::JsonObjectNode *nodeToRemove);
|
||||
|
||||
|
@ -26,6 +26,22 @@ class JsonValue {
|
||||
void set(JsonArray &array);
|
||||
void set(JsonObject &object);
|
||||
|
||||
template <typename T>
|
||||
JsonValue &operator=(T value) {
|
||||
set(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
JsonValue &operator=(JsonArray& array) {
|
||||
set(array);
|
||||
return *this;
|
||||
}
|
||||
|
||||
JsonValue &operator=(JsonObject& object) {
|
||||
set(object);
|
||||
return *this;
|
||||
}
|
||||
|
||||
JsonArray &asArray();
|
||||
JsonObject &asObject();
|
||||
bool asBool() const;
|
||||
@ -34,10 +50,7 @@ class JsonValue {
|
||||
long asLong() const;
|
||||
|
||||
template <typename T>
|
||||
JsonValue &operator=(T value) {
|
||||
set(value);
|
||||
return *this;
|
||||
}
|
||||
T as(){}
|
||||
|
||||
static JsonValue &invalid() { return _invalid; }
|
||||
|
||||
@ -50,4 +63,8 @@ class JsonValue {
|
||||
Internals::JsonValueContent _content;
|
||||
static JsonValue _invalid;
|
||||
};
|
||||
|
||||
template <>
|
||||
int JsonValue::as<int>() { return asLong(); }
|
||||
|
||||
}
|
||||
|
@ -43,10 +43,10 @@ class Issue10 : public testing::Test {
|
||||
|
||||
TEST_F(Issue10, PopulateArrayByAddingAnObject) {
|
||||
StaticJsonBuffer<200> json;
|
||||
JsonArray array = json.createArray();
|
||||
JsonArray &array = json.createArray();
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
JsonObject object = json.createObject();
|
||||
JsonObject &object = json.createObject();
|
||||
|
||||
object["id"] = persons[i].id;
|
||||
object["name"] = persons[i].name;
|
||||
@ -60,10 +60,10 @@ TEST_F(Issue10, PopulateArrayByAddingAnObject) {
|
||||
|
||||
TEST_F(Issue10, PopulateArrayByCreatingNestedObjects) {
|
||||
StaticJsonBuffer<200> json;
|
||||
JsonArray array = json.createArray();
|
||||
JsonArray &array = json.createArray();
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
JsonObject object = array.createNestedObject();
|
||||
JsonObject &object = array.createNestedObject();
|
||||
|
||||
object["id"] = persons[i].id;
|
||||
object["name"] = persons[i].name;
|
||||
|
@ -15,9 +15,8 @@ using namespace ArduinoJson;
|
||||
|
||||
class JsonArray_Container_Tests : public ::testing::Test {
|
||||
protected:
|
||||
virtual void SetUp() {
|
||||
json.clear();
|
||||
array = json.createArray();
|
||||
JsonArray_Container_Tests()
|
||||
: array(json.createArray()) {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@ -33,7 +32,7 @@ class JsonArray_Container_Tests : public ::testing::Test {
|
||||
void sizeMustBe(int expected) { EXPECT_EQ(expected, array.size()); }
|
||||
|
||||
StaticJsonBuffer<256> json;
|
||||
JsonArray array;
|
||||
JsonArray &array;
|
||||
|
||||
private:
|
||||
template <typename T>
|
||||
@ -92,8 +91,8 @@ TEST_F(JsonArray_Container_Tests, CanStoreStrings) {
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreNestedArrays) {
|
||||
JsonArray innerarray1 = json.createArray();
|
||||
JsonArray innerarray2 = json.createArray();
|
||||
JsonArray &innerarray1 = json.createArray();
|
||||
JsonArray &innerarray2 = json.createArray();
|
||||
|
||||
array.add(innerarray1);
|
||||
array.add(innerarray2);
|
||||
|
@ -13,7 +13,7 @@ using namespace ArduinoJson;
|
||||
TEST(JsonArray_Iterator_Test, SimpleTest) {
|
||||
StaticJsonBuffer<100> jsonBuffer;
|
||||
|
||||
JsonArray array = jsonBuffer.createArray();
|
||||
JsonArray &array = jsonBuffer.createArray();
|
||||
array.add(12);
|
||||
array.add(34);
|
||||
|
||||
@ -26,5 +26,5 @@ TEST(JsonArray_Iterator_Test, SimpleTest) {
|
||||
EXPECT_NE(end, it);
|
||||
EXPECT_EQ(34, it->as<int>());
|
||||
++it;
|
||||
EXPECT_EQ(array.end(), it);
|
||||
EXPECT_EQ(end, it);
|
||||
}
|
||||
|
Reference in New Issue
Block a user