Просмотр исходного кода

Added JsonArray::createNestedObject()

Benoit Blanchon 11 лет назад
Родитель
Сommit
f7fa9e9467
5 измененных файлов с 34 добавлено и 24 удалено
  1. 20 9
      srcs/JsonArray.cpp
  2. 6 2
      srcs/JsonArray.h
  3. 2 2
      srcs/JsonContainer.h
  4. 0 4
      srcs/JsonObject.h
  5. 6 7
      tests/JsonArray_PrettyPrintTo_Tests.cpp

+ 20 - 9
srcs/JsonArray.cpp

@@ -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;
 }

+ 6 - 2
srcs/JsonArray.h

@@ -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);
 };
 

+ 2 - 2
srcs/JsonContainer.h

@@ -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
 {

+ 0 - 4
srcs/JsonObject.h

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

+ 6 - 7
tests/JsonArray_PrettyPrintTo_Tests.cpp

@@ -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"
         "]");
 }