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

Pulled up code from JsonObject to JsonContainer

Benoit Blanchon 11 лет назад
Родитель
Сommit
4d2d535a03

+ 37 - 0
srcs/Internals/JsonNodeIterator.h

@@ -0,0 +1,37 @@
+#pragma once
+
+#include "JsonNode.h"
+
+class JsonNodeIterator
+{
+public:
+
+    explicit JsonNodeIterator(JsonNode* node)
+        : node(node)
+    {
+    }
+
+    bool operator!= (const JsonNodeIterator& other) const
+    {
+        return node != other.node;
+    }
+
+    void operator++()
+    {
+        node = node->next;
+    }
+
+    JsonNode* operator*() const
+    {
+        return node;
+    }
+
+    JsonNode* operator->() const
+    {
+        return node;
+    }
+
+private:
+    JsonNode* node;
+};
+

+ 1 - 1
srcs/JsonBuffer.h

@@ -9,7 +9,7 @@ struct JsonNode;
 
 class JsonBuffer
 {
-    friend class JsonObject;
+    friend class JsonContainer;
 
 public:
 //    virtual ~JsonBuffer() = 0;

+ 47 - 1
srcs/JsonContainer.cpp

@@ -1,5 +1,6 @@
 #include "JsonContainer.h"
 
+#include "JsonBuffer.h"
 #include "Internals/JsonNodeSerializer.h"
 #include "Internals/StringBuilder.h"
 
@@ -13,4 +14,49 @@ size_t JsonContainer::printTo(Print& p) const
 {
     JsonNodeSerializer serializer(p);
     return serializer.serialize(_node);
-}
+}
+
+JsonNode* JsonContainer::createNode(JsonNodeType type)
+{
+    JsonBuffer* buffer = _node->content.asContainer.buffer;
+    return buffer->createNode(JSON_UNDEFINED);
+}
+
+bool JsonContainer::checkNodeType(JsonNodeType expectedType)
+{
+    return _node && _node->type == expectedType;
+}
+
+bool JsonContainer::operator==(const JsonContainer & other) const
+{
+    return _node == other._node;
+}
+
+void JsonContainer::insertChildAfter(JsonNode* newChild, JsonNode* previous)
+{
+    if (previous)
+        previous->next = newChild;
+    else
+        _node->content.asContainer.child = newChild;
+}
+
+void JsonContainer::removeChildAfter(JsonNode* child, JsonNode* previous)
+{
+    if (previous)
+        previous->next = child->next;
+    else
+        _node->content.asContainer.child = child->next;
+}
+
+size_t JsonContainer::size() const
+{
+    int size = 0;
+
+    for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
+    {
+        size++;
+    }
+
+    return size;
+}
+

+ 28 - 1
srcs/JsonContainer.h

@@ -1,11 +1,16 @@
 #pragma once
 
-#include "Arduino\Printable.h"
+#include "Arduino/Printable.h"
+#include "Internals/JsonNodeIterator.h"
+#include "Internals/JsonNode.h"
 
 struct JsonNode;
+class JsonValue;
 
 class JsonContainer : public Printable
 {
+    friend JsonValue;
+
 public:
     JsonContainer()
         : _node(0)
@@ -17,10 +22,32 @@ public:
     {
     }
 
+    size_t size() const;
+    
+    bool operator==(JsonContainer const& other) const;
+
     size_t printTo(char* buffer, size_t bufferSize) const;
     virtual size_t printTo(Print& print) const;
 
 protected:
+
+    JsonNodeIterator beginChildren() const
+    {
+        return JsonNodeIterator(_node ? _node->content.asContainer.child : 0);
+    }
+
+    JsonNodeIterator endChildren() const
+    {
+        return JsonNodeIterator(0);
+    }
+
+    void insertChildAfter(JsonNode* newChild, JsonNode* insertAfterMe);
+    void removeChildAfter(JsonNode* child, JsonNode* previous);
+    JsonNode* createNode(JsonNodeType type);
+
+    bool checkNodeType(JsonNodeType expectedType);
+
+private:
     JsonNode* _node;
 };
 

+ 16 - 44
srcs/JsonObject.cpp

@@ -11,20 +11,6 @@
 
 using namespace ArduinoJson::Internals;
 
-size_t JsonObject::size()
-{
-    JsonNode* firstChild = _node->content.asContainer.child;
-
-    int size = 0;
-
-    for (JsonNode* child = firstChild; child; child = child->next)
-    {
-        size++;
-    }
-
-    return size;
-}
-
 JsonValue JsonObject::operator[](char const* key)
 {
     JsonNode* node = getOrCreateNodeAt(key);
@@ -33,61 +19,47 @@ JsonValue JsonObject::operator[](char const* key)
 
 void JsonObject::remove(char const* key)
 {
-    JsonNode* firstChild = _node->content.asContainer.child;
     JsonNode* lastChild = 0;
 
-    for (JsonNode* child = firstChild; child; child = child->next)
+    for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
     {
-        const char* childKey = child->content.asKey.key;
+        const char* childKey = it->content.asKey.key;
 
         if (!strcmp(childKey, key))
         {
-            if (lastChild)
-                lastChild->next = child->next;
-            else
-                _node->content.asContainer.child = child->next;
-        }       
-        lastChild = child;
-    }
-}
+            removeChildAfter(*it, lastChild);
+        }      
 
-bool JsonObject::operator==(JsonObject const& other) const
-{
-    return _node == other._node;
+        lastChild = *it;
+    }
 }
 
-JsonNode* JsonObject::getOrCreateNodeAt(char const* key)
+JsonNode* JsonObject::getOrCreateNodeAt(const char* key)
 {
-    if (!_node || _node->type != JSON_OBJECT) return 0;
+    if (!checkNodeType(JSON_OBJECT)) return 0;
 
-    JsonNode* firstChild = _node->content.asContainer.child;
     JsonNode* lastChild = 0;
 
-    for (JsonNode* child = firstChild; child; child = child->next)
+    for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
     {
-        const char* childKey = child->content.asKey.key;
+        const char* childKey = it->content.asKey.key;
 
         if (!strcmp(childKey, key))
-            return child->content.asKey.value;
+            return it->content.asKey.value;
 
-        lastChild = child;
+        lastChild = *it;
     }
-
-    JsonBuffer* buffer = _node->content.asContainer.buffer;
-
-    JsonNode* newValueNode = buffer->createNode(JSON_UNDEFINED);
+      
+    JsonNode* newValueNode = createNode(JSON_UNDEFINED);
     if (!newValueNode) return 0;
     
-    JsonNode* newKeyNode = buffer->createNode(JSON_KEY);
+    JsonNode* newKeyNode = createNode(JSON_KEY);
     if (!newKeyNode) return 0;
 
     newKeyNode->content.asKey.key = key;
     newKeyNode->content.asKey.value = newValueNode;
 
-    if (lastChild)
-        lastChild->next = newKeyNode;
-    else
-        _node->content.asContainer.child = newKeyNode;
+    insertChildAfter(newKeyNode, lastChild);
 
     return newValueNode;
 }

+ 0 - 6
srcs/JsonObject.h

@@ -7,8 +7,6 @@ struct JsonNode;
 
 class JsonObject : public JsonContainer
 {
-    friend JsonValue;
-
 public:
     JsonObject()
     {
@@ -19,13 +17,9 @@ public:
     {
     }
 
-    size_t size();
-
     JsonValue operator[](const char* key);
     void remove(const char* key);
 
-    bool operator==(const JsonObject& other) const;
-
 private:
     JsonNode* getOrCreateNodeAt(char const* key);
 };

+ 1 - 0
srcs/srcs.vcxproj

@@ -79,6 +79,7 @@
     <ClInclude Include="Internals\JsonNode.h" />
     <ClInclude Include="Internals\JsonNodeSerializer.h" />
     <ClInclude Include="JsonContainer.h" />
+    <ClInclude Include="Internals\JsonNodeIterator.h" />
     <ClInclude Include="JsonObject.h" />
     <ClInclude Include="JsonValue.h" />
     <ClInclude Include="Arduino\Print.h" />

+ 3 - 0
srcs/srcs.vcxproj.filters

@@ -48,6 +48,9 @@
     <ClInclude Include="JsonContainer.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="Internals\JsonNodeIterator.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="JsonObject.cpp">

+ 5 - 0
tests/JsonObjectTests.cpp

@@ -14,6 +14,11 @@ protected:
     JsonObject object;
 };
 
+TEST_F(JsonObjectTests, InitialSizeIsZero)
+{
+    EXPECT_EQ(0, object.size());
+}
+
 TEST_F(JsonObjectTests, Grow_WhenValuesAreAdded)
 {   
     object["hello"];