forked from bblanchon/ArduinoJson
Added JsonArray::createNestedObject()
This commit is contained in:
@ -1,5 +1,6 @@
|
|||||||
#include "JsonArray.h"
|
#include "JsonArray.h"
|
||||||
|
|
||||||
|
#include "JsonObject.h"
|
||||||
#include "JsonValue.h"
|
#include "JsonValue.h"
|
||||||
|
|
||||||
JsonValue JsonArray::operator[](int index) const
|
JsonValue JsonArray::operator[](int index) const
|
||||||
@ -49,7 +50,7 @@ void JsonArray::add(long value)
|
|||||||
addChild(node);
|
addChild(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonArray::add(JsonContainer& innerContainer)
|
void JsonArray::add(JsonContainer innerContainer)
|
||||||
{
|
{
|
||||||
JsonNode* node = createNode(JSON_PROXY);
|
JsonNode* node = createNode(JSON_PROXY);
|
||||||
if (!node) return;
|
if (!node) return;
|
||||||
@ -60,13 +61,23 @@ void JsonArray::add(JsonContainer& innerContainer)
|
|||||||
|
|
||||||
JsonArray JsonArray::createNestedArray()
|
JsonArray JsonArray::createNestedArray()
|
||||||
{
|
{
|
||||||
JsonNode* node = createNode(JSON_ARRAY);
|
JsonNode* node = createNestedContainer(JSON_ARRAY);
|
||||||
|
|
||||||
if (node)
|
|
||||||
{
|
|
||||||
node->content.asContainer.buffer = _node->content.asContainer.buffer;
|
|
||||||
addChild(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
return JsonArray(node);
|
return JsonArray(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonObject JsonArray::createNestedObject()
|
||||||
|
{
|
||||||
|
JsonNode* node = createNestedContainer(JSON_OBJECT);
|
||||||
|
return JsonObject(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonNode* JsonArray::createNestedContainer(JsonNodeType type)
|
||||||
|
{
|
||||||
|
JsonNode* node = createNode(type);
|
||||||
|
if (!node) return 0;
|
||||||
|
|
||||||
|
node->content.asContainer.buffer = _node->content.asContainer.buffer;
|
||||||
|
addChild(node);
|
||||||
|
|
||||||
|
return node;
|
||||||
}
|
}
|
@ -21,8 +21,12 @@ public:
|
|||||||
void add(double value, int decimals=2);
|
void add(double value, int decimals=2);
|
||||||
void add(int value) { add((long) value); }
|
void add(int value) { add((long) value); }
|
||||||
void add(long value);
|
void add(long value);
|
||||||
void add(JsonContainer& innerContainer);
|
void add(JsonContainer nestedArray);
|
||||||
|
|
||||||
JsonArray createNestedArray();
|
JsonArray createNestedArray();
|
||||||
|
JsonObject createNestedObject();
|
||||||
|
|
||||||
|
private:
|
||||||
|
JsonNode* createNestedContainer(JsonNodeType type);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -5,9 +5,9 @@
|
|||||||
#include "Internals/JsonNode.h"
|
#include "Internals/JsonNode.h"
|
||||||
#include "Internals/IndentedPrint.h"
|
#include "Internals/IndentedPrint.h"
|
||||||
|
|
||||||
struct JsonNode;
|
|
||||||
class JsonValue;
|
|
||||||
class JsonArray;
|
class JsonArray;
|
||||||
|
class JsonObject;
|
||||||
|
class JsonValue;
|
||||||
|
|
||||||
class JsonContainer : public Printable
|
class JsonContainer : public Printable
|
||||||
{
|
{
|
||||||
|
@ -2,10 +2,6 @@
|
|||||||
|
|
||||||
#include "JsonContainer.h"
|
#include "JsonContainer.h"
|
||||||
|
|
||||||
class JsonArray;
|
|
||||||
class JsonValue;
|
|
||||||
struct JsonNode;
|
|
||||||
|
|
||||||
class JsonObject : public JsonContainer
|
class JsonObject : public JsonContainer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <JsonArray.h>
|
#include <JsonArray.h>
|
||||||
#include <JsonObject.h>
|
#include <JsonObject.h>
|
||||||
|
#include <JsonValue.h>
|
||||||
#include <StaticJsonBuffer.h>
|
#include <StaticJsonBuffer.h>
|
||||||
|
|
||||||
class JsonArray_PrettyPrintTo_Tests : public testing::Test
|
class JsonArray_PrettyPrintTo_Tests : public testing::Test
|
||||||
@ -75,9 +76,8 @@ TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays)
|
|||||||
nested1.add(1);
|
nested1.add(1);
|
||||||
nested1.add(2);
|
nested1.add(2);
|
||||||
|
|
||||||
JsonArray nested2 = array.createNestedArray();
|
JsonObject nested2 = array.createNestedObject();
|
||||||
nested2.add(3);
|
nested2["key"] = 3;
|
||||||
nested2.add(4);
|
|
||||||
|
|
||||||
outputMustBe(
|
outputMustBe(
|
||||||
"[\r\n"
|
"[\r\n"
|
||||||
@ -85,9 +85,8 @@ TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays)
|
|||||||
" 1,\r\n"
|
" 1,\r\n"
|
||||||
" 2\r\n"
|
" 2\r\n"
|
||||||
" ],\r\n"
|
" ],\r\n"
|
||||||
" [\r\n"
|
" {\r\n"
|
||||||
" 3,\r\n"
|
" \"key\": 3\r\n"
|
||||||
" 4\r\n"
|
" }\r\n"
|
||||||
" ]\r\n"
|
|
||||||
"]");
|
"]");
|
||||||
}
|
}
|
Reference in New Issue
Block a user