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