Added JsonObject::createNestedArray()

This commit is contained in:
Benoit Blanchon
2014-10-07 12:11:10 +02:00
parent e28119f03b
commit 09b6f71853
3 changed files with 32 additions and 19 deletions

View File

@ -33,17 +33,15 @@ void JsonObject::remove(char const* key)
} }
} }
JsonArray JsonObject::createNestedArray(char const* key)
{
JsonNode* node = createContainerNodeAt(key, JSON_ARRAY);
return JsonArray(node);
}
JsonObject JsonObject::createNestedObject(char const* key) JsonObject JsonObject::createNestedObject(char const* key)
{ {
JsonNode* node = getOrCreateNodeAt(key); JsonNode* node = createContainerNodeAt(key, JSON_OBJECT);
if (node)
{
node->type = JSON_OBJECT;
node->content.asContainer.child = 0;
node->content.asContainer.buffer = _node->content.asContainer.buffer;
}
return JsonObject(node); return JsonObject(node);
} }
@ -72,3 +70,15 @@ JsonNode* JsonObject::getOrCreateNodeAt(const char* key)
return newValueNode; return newValueNode;
} }
JsonNode* JsonObject::createContainerNodeAt(char const* key, JsonNodeType type)
{
JsonNode* node = getOrCreateNodeAt(key);
if (!node) return 0;
node->type = type;
node->content.asContainer.child = 0;
node->content.asContainer.buffer = _node->content.asContainer.buffer;
return node;
}

View File

@ -2,6 +2,7 @@
#include "JsonContainer.h" #include "JsonContainer.h"
class JsonArray;
class JsonValue; class JsonValue;
struct JsonNode; struct JsonNode;
@ -21,8 +22,10 @@ public:
JsonValue operator[](const char* key); JsonValue operator[](const char* key);
void remove(const char* key); void remove(const char* key);
JsonArray createNestedArray(const char* key);
JsonObject createNestedObject(const char* key); JsonObject createNestedObject(const char* key);
private: private:
JsonNode* getOrCreateNodeAt(char const* key); JsonNode* getOrCreateNodeAt(const char* key);
JsonNode* createContainerNodeAt(const char* key, JsonNodeType type);
}; };

View File

@ -57,33 +57,33 @@ TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers)
"}"); "}");
} }
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedObjects) TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers)
{ {
object.createNestedObject("key1"); object.createNestedObject("key1");
object.createNestedObject("key2"); object.createNestedArray("key2");
outputMustBe( outputMustBe(
"{\r\n" "{\r\n"
" \"key1\": {},\r\n" " \"key1\": {},\r\n"
" \"key2\": {}\r\n" " \"key2\": []\r\n"
"}"); "}");
} }
TEST_F(JsonObject_PrettyPrintTo_Tests, NestedObjects) TEST_F(JsonObject_PrettyPrintTo_Tests, NestedContainers)
{ {
JsonObject nested1 = object.createNestedObject("key1"); JsonObject nested1 = object.createNestedObject("key1");
nested1["a"] = 1; nested1["a"] = 1;
JsonObject nested2 = object.createNestedObject("key2"); JsonArray nested2 = object.createNestedArray("key2");
nested2["b"] = 2; nested2.add(2);
outputMustBe( outputMustBe(
"{\r\n" "{\r\n"
" \"key1\": {\r\n" " \"key1\": {\r\n"
" \"a\": 1\r\n" " \"a\": 1\r\n"
" },\r\n" " },\r\n"
" \"key2\": {\r\n" " \"key2\": [\r\n"
" \"b\": 2\r\n" " 2\r\n"
" }\r\n" " ]\r\n"
"}"); "}");
} }