Added JsonArray::createNestedObject()

This commit is contained in:
Benoit Blanchon
2014-10-07 15:27:24 +02:00
parent 09b6f71853
commit f7fa9e9467
5 changed files with 34 additions and 24 deletions

View File

@ -1,5 +1,6 @@
#include "JsonArray.h"
#include "JsonObject.h"
#include "JsonValue.h"
JsonValue JsonArray::operator[](int index) const
@ -49,7 +50,7 @@ void JsonArray::add(long value)
addChild(node);
}
void JsonArray::add(JsonContainer& innerContainer)
void JsonArray::add(JsonContainer innerContainer)
{
JsonNode* node = createNode(JSON_PROXY);
if (!node) return;
@ -60,13 +61,23 @@ void JsonArray::add(JsonContainer& innerContainer)
JsonArray JsonArray::createNestedArray()
{
JsonNode* node = createNode(JSON_ARRAY);
if (node)
{
node->content.asContainer.buffer = _node->content.asContainer.buffer;
addChild(node);
}
JsonNode* node = createNestedContainer(JSON_ARRAY);
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;
}

View File

@ -21,8 +21,12 @@ public:
void add(double value, int decimals=2);
void add(int value) { add((long) value); }
void add(long value);
void add(JsonContainer& innerContainer);
void add(JsonContainer nestedArray);
JsonArray createNestedArray();
JsonObject createNestedObject();
private:
JsonNode* createNestedContainer(JsonNodeType type);
};

View File

@ -5,9 +5,9 @@
#include "Internals/JsonNode.h"
#include "Internals/IndentedPrint.h"
struct JsonNode;
class JsonValue;
class JsonArray;
class JsonObject;
class JsonValue;
class JsonContainer : public Printable
{

View File

@ -2,10 +2,6 @@
#include "JsonContainer.h"
class JsonArray;
class JsonValue;
struct JsonNode;
class JsonObject : public JsonContainer
{
public:

View File

@ -6,6 +6,7 @@
#include <gtest/gtest.h>
#include <JsonArray.h>
#include <JsonObject.h>
#include <JsonValue.h>
#include <StaticJsonBuffer.h>
class JsonArray_PrettyPrintTo_Tests : public testing::Test
@ -75,9 +76,8 @@ TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays)
nested1.add(1);
nested1.add(2);
JsonArray nested2 = array.createNestedArray();
nested2.add(3);
nested2.add(4);
JsonObject nested2 = array.createNestedObject();
nested2["key"] = 3;
outputMustBe(
"[\r\n"
@ -85,9 +85,8 @@ TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays)
" 1,\r\n"
" 2\r\n"
" ],\r\n"
" [\r\n"
" 3,\r\n"
" 4\r\n"
" ]\r\n"
" {\r\n"
" \"key\": 3\r\n"
" }\r\n"
"]");
}