Browse Source

Removed all friends of JsonValue

Benoit Blanchon 11 years ago
parent
commit
e748ce32bc

+ 38 - 0
include/ArduinoJson/Internals/JsonArrayConstIterator.hpp

@@ -0,0 +1,38 @@
+// Copyright Benoit Blanchon 2014
+// MIT License
+//
+// Arduino JSON library
+// https://github.com/bblanchon/ArduinoJson
+
+#pragma once
+
+#include "JsonValueInternal.hpp"
+
+namespace ArduinoJson {
+namespace Internals {
+
+class JsonArrayConstIterator {
+ public:
+  explicit JsonArrayConstIterator(Internals::JsonNode *node) : _value(node) {}
+
+  JsonValue operator*() const { return _value; }
+  JsonValue *operator->() { return &_value; }
+
+  bool operator==(const JsonArrayConstIterator &other) const {
+    return _value.isSameAs(other._value);
+  }
+
+  bool operator!=(const JsonArrayConstIterator &other) const {
+    return !_value.isSameAs(other._value);
+  }
+
+  JsonArrayConstIterator &operator++() {
+    _value.moveToNext();
+    return *this;
+  }
+
+ private:
+  JsonValueInternal _value;
+};
+}
+}

+ 38 - 0
include/ArduinoJson/Internals/JsonArrayIterator.hpp

@@ -0,0 +1,38 @@
+// Copyright Benoit Blanchon 2014
+// MIT License
+//
+// Arduino JSON library
+// https://github.com/bblanchon/ArduinoJson
+
+#pragma once
+
+#include "JsonValueInternal.hpp"
+
+namespace ArduinoJson {
+namespace Internals {
+
+class JsonArrayIterator {
+ public:
+  explicit JsonArrayIterator(Internals::JsonNode *node) : _value(node) {}
+
+  JsonValue operator*() const { return _value; }
+  JsonValue *operator->() { return &_value; }
+
+  bool operator==(const JsonArrayIterator &other) const {
+    return _value.isSameAs(other._value);
+  }
+
+  bool operator!=(const JsonArrayIterator &other) const {
+    return !_value.isSameAs(other._value);
+  }
+
+  JsonArrayIterator &operator++() {
+    _value.moveToNext();
+    return *this;
+  }
+
+ private:
+  JsonValueInternal _value;
+};
+}
+}

+ 38 - 0
include/ArduinoJson/Internals/JsonObjectConstIterator.hpp

@@ -0,0 +1,38 @@
+// Copyright Benoit Blanchon 2014
+// MIT License
+//
+// Arduino JSON library
+// https://github.com/bblanchon/ArduinoJson
+
+#pragma once
+
+#include "JsonPairInternal.hpp"
+
+namespace ArduinoJson {
+namespace Internals {
+
+class JsonObjectConstIterator {
+ public:
+  explicit JsonObjectConstIterator(Internals::JsonNode *node) : _pair(node) {}
+
+  JsonPair operator*() const { return _pair; }
+  JsonPair *operator->() { return &_pair; }
+
+  bool operator==(const JsonObjectConstIterator &other) const {
+    return _pair.isSameAs(other._pair);
+  }
+
+  bool operator!=(const JsonObjectConstIterator &other) const {
+    return !_pair.isSameAs(other._pair);
+  }
+
+  JsonObjectConstIterator &operator++() {
+    _pair.moveToNext();
+    return *this;
+  }
+
+ private:
+  JsonPairInternal _pair;
+};
+}
+}

+ 38 - 0
include/ArduinoJson/Internals/JsonObjectIterator.hpp

@@ -0,0 +1,38 @@
+// Copyright Benoit Blanchon 2014
+// MIT License
+//
+// Arduino JSON library
+// https://github.com/bblanchon/ArduinoJson
+
+#pragma once
+
+#include "JsonPairInternal.hpp"
+
+namespace ArduinoJson {
+namespace Internals {
+
+class JsonObjectIterator {
+ public:
+  explicit JsonObjectIterator(Internals::JsonNode *node) : _pair(node) {}
+
+  JsonPair operator*() const { return _pair; }
+  JsonPair *operator->() { return &_pair; }
+
+  bool operator==(const JsonObjectIterator &other) const {
+    return _pair.isSameAs(other._pair);
+  }
+
+  bool operator!=(const JsonObjectIterator &other) const {
+    return !_pair.isSameAs(other._pair);
+  }
+
+  JsonObjectIterator &operator++() {
+    _pair.moveToNext();
+    return *this;
+  }
+
+ private:
+  JsonPairInternal _pair;
+};
+}
+}

+ 22 - 0
include/ArduinoJson/Internals/JsonPairInternal.hpp

@@ -0,0 +1,22 @@
+// Copyright Benoit Blanchon 2014
+// MIT License
+//
+// Arduino JSON library
+// https://github.com/bblanchon/ArduinoJson
+
+#pragma once
+
+#include "../JsonPair.hpp"
+
+namespace ArduinoJson {
+class JsonPairInternal : public JsonPair {
+ public:
+  explicit JsonPairInternal(Internals::JsonNode* node) : JsonPair(node) {}
+
+  void moveToNext() { _node = _node->next; }
+
+  bool isSameAs(const JsonPairInternal& other) const {
+    return _node == other._node;
+  }
+};
+}

+ 25 - 0
include/ArduinoJson/Internals/JsonValueInternal.hpp

@@ -0,0 +1,25 @@
+// Copyright Benoit Blanchon 2014
+// MIT License
+//
+// Arduino JSON library
+// https://github.com/bblanchon/ArduinoJson
+
+#pragma once
+
+#include "../JsonValue.hpp"
+
+namespace ArduinoJson {
+namespace Internals {
+
+class JsonValueInternal : public JsonValue {
+ public:
+  explicit JsonValueInternal(Internals::JsonNode* node) : JsonValue(node) {}
+
+  void moveToNext() { _node = _node->next; }
+
+  bool isSameAs(const JsonValueInternal& other) const {
+    return _node == other._node;
+  }
+};
+}
+}

+ 5 - 3
include/ArduinoJson/JsonArray.hpp

@@ -6,14 +6,16 @@
 
 #pragma once
 
+#include "Internals/JsonArrayConstIterator.hpp"
+#include "Internals/JsonArrayIterator.hpp"
 #include "JsonContainer.hpp"
-#include "JsonIterator.hpp"
 
 namespace ArduinoJson {
 class JsonArray : public JsonContainer {
  public:
-  typedef JsonIterator<JsonValue> iterator;
-  typedef JsonConstIterator<JsonValue> const_iterator;
+  typedef JsonValue value_type;
+  typedef Internals::JsonArrayIterator iterator;
+  typedef Internals::JsonArrayConstIterator const_iterator;
 
   JsonArray() {}
 

+ 0 - 67
include/ArduinoJson/JsonIterator.hpp

@@ -1,67 +0,0 @@
-// Copyright Benoit Blanchon 2014
-// MIT License
-//
-// Arduino JSON library
-// https://github.com/bblanchon/ArduinoJson
-
-#pragma once
-
-#include "ForwardDeclarations.hpp"
-#include "JsonValue.hpp"
-
-namespace ArduinoJson {
-
-template <typename T>
-class JsonIterator {
-  friend class JsonArray;
-
- public:
-  explicit JsonIterator(Internals::JsonNode *node) : _value(node) {}
-
-  T operator*() const { return _value; }
-  T *operator->() { return &_value; }
-
-  bool operator==(const JsonIterator &other) const {
-    return _value._node == other._value._node;
-  }
-
-  bool operator!=(const JsonIterator &other) const {
-    return _value._node != other._value._node;
-  }
-
-  JsonIterator &operator++() {
-    _value._node = _value._node->next;
-    return *this;
-  }
-
- private:
-  T _value;
-};
-
-template <typename T>
-class JsonConstIterator {
-  friend class JsonArray;
-
- public:
-  explicit JsonConstIterator(Internals::JsonNode *node) : _value(node) {}
-
-  const JsonValue operator*() const { return _value; }
-  const JsonValue *operator->() { return &_value; }
-
-  bool operator==(const JsonConstIterator &other) const {
-    return _value._node == other._value._node;
-  }
-
-  bool operator!=(const JsonConstIterator &other) const {
-    return _value._node != other._value._node;
-  }
-
-  JsonConstIterator &operator++() {
-    _value._node = _value._node->next;
-    return *this;
-  }
-
- private:
-  JsonValue _value;
-};
-}

+ 7 - 5
include/ArduinoJson/JsonObject.hpp

@@ -6,15 +6,16 @@
 
 #pragma once
 
+#include "Internals/JsonObjectConstIterator.hpp"
+#include "Internals/JsonObjectIterator.hpp"
 #include "JsonContainer.hpp"
-#include "JsonIterator.hpp"
-#include "JsonObjectKeyValue.hpp"
 
 namespace ArduinoJson {
 class JsonObject : public JsonContainer {
  public:
-  typedef JsonIterator<JsonObjectKeyValue> iterator;
-  typedef JsonConstIterator<JsonObjectKeyValue> const_iterator;
+  typedef JsonPair value_type;
+  typedef Internals::JsonObjectIterator iterator;
+  typedef Internals::JsonObjectConstIterator const_iterator;
 
   JsonObject() {}
 
@@ -35,6 +36,7 @@ class JsonObject : public JsonContainer {
   const_iterator end() const { return const_iterator(0); }
 
  private:
-  Internals::JsonNode *getOrCreateNodeAt(const char *key);
+  Internals::JsonNode *getPairAt(const char *key);
+  Internals::JsonNode *getOrCreateValueAt(const char *key);
 };
 }

+ 0 - 27
include/ArduinoJson/JsonObjectKeyValue.hpp

@@ -1,27 +0,0 @@
-// Copyright Benoit Blanchon 2014
-// MIT License
-//
-// Arduino JSON library
-// https://github.com/bblanchon/ArduinoJson
-
-#pragma once
-
-#include "ForwardDeclarations.hpp"
-#include "JsonValue.hpp"
-
-namespace ArduinoJson {
-class JsonObjectKeyValue {
-  friend class JsonObject;
-  template <typename T>
-  friend class JsonIterator;
-
- public:
-  const char *key() const { return _node->getAsObjectKey(); }
-  JsonValue value() { return JsonValue(_node->getAsObjectValue()); }
-
- private:
-  explicit JsonObjectKeyValue(Internals::JsonNode *node) : _node(node) {}
-
-  Internals::JsonNode *_node;
-};
-}

+ 25 - 0
include/ArduinoJson/JsonPair.hpp

@@ -0,0 +1,25 @@
+// Copyright Benoit Blanchon 2014
+// MIT License
+//
+// Arduino JSON library
+// https://github.com/bblanchon/ArduinoJson
+
+#pragma once
+
+#include "Internals/JsonValueInternal.hpp"
+
+namespace ArduinoJson {
+class JsonPair {
+ public:
+  const char *key() const { return _node->getAsObjectKey(); }
+
+  JsonValue value() {
+    return Internals::JsonValueInternal(_node->getAsObjectValue());
+  }
+
+ protected:
+  explicit JsonPair(Internals::JsonNode *node) : _node(node) {}
+
+  Internals::JsonNode *_node;
+};
+}

+ 2 - 11
include/ArduinoJson/JsonValue.hpp

@@ -12,15 +12,6 @@
 namespace ArduinoJson {
 
 class JsonValue : public Internals::JsonNodeWrapper {
-  friend class JsonArray;
-  template <typename T>
-  friend class JsonIterator;
-  template <typename T>
-  friend class JsonConstIterator;
-  friend class JsonBuffer;
-  friend class JsonObject;
-  friend class JsonObjectKeyValue;
-
  public:
   JsonValue() {}
 
@@ -48,7 +39,7 @@ class JsonValue : public Internals::JsonNodeWrapper {
     return static_cast<T>(*this);
   }
 
- private:
-  explicit JsonValue(Internals::JsonNode *node) : JsonNodeWrapper(node) {}
+ protected:
+  JsonValue(Internals::JsonNode *node) : Internals::JsonNodeWrapper(node) {}
 };
 }

+ 3 - 3
src/JsonBuffer.cpp

@@ -8,14 +8,14 @@
 
 #include <new>
 
-#include "ArduinoJson/JsonValue.hpp"
+#include "ArduinoJson/Internals/JsonValueInternal.hpp"
 #include "ArduinoJson/Internals/JsonParser.hpp"
 #include "ArduinoJson/Internals/JsonNode.hpp"
 
 using namespace ArduinoJson;
 using namespace ArduinoJson::Internals;
 
-JsonValue JsonBuffer::createValue() { return JsonValue(createNode()); }
+JsonValue JsonBuffer::createValue() { return JsonValueInternal(createNode()); }
 
 JsonNode *JsonBuffer::createNode() {
   void *node = allocateNode();
@@ -36,7 +36,7 @@ JsonObject JsonBuffer::parseObject(char *json) {
 
 JsonValue JsonBuffer::parseValue(char *json) {
   JsonParser parser(this, json);
-  return JsonValue(parser.parseAnything());
+  return JsonValueInternal(parser.parseAnything());
 }
 
 JsonNode *JsonBuffer::createArrayNode() {

+ 15 - 12
src/JsonObject.cpp

@@ -17,20 +17,17 @@ using namespace ArduinoJson;
 using namespace ArduinoJson::Internals;
 
 JsonValue JsonObject::operator[](char const *key) {
-  JsonNode *node = getOrCreateNodeAt(key);
-  return JsonValue(node);
+  JsonNode *node = getOrCreateValueAt(key);
+  return JsonValueInternal(node);
 }
 
 void JsonObject::remove(char const *key) {
-  for (iterator it = begin(); it != end(); ++it) {
-    if (!strcmp(it->key(), key)) {
-      removeChild(it->_node);
-    }
-  }
+  JsonNode *nodeToRemove = getPairAt(key);
+  if (nodeToRemove) removeChild(nodeToRemove);
 }
 
 JsonArray JsonObject::createNestedArray(char const *key) {
-  JsonNode *node = getOrCreateNodeAt(key);
+  JsonNode *node = getOrCreateValueAt(key);
 
   if (node) node->setAsArray(_node->getContainerBuffer());
 
@@ -38,17 +35,23 @@ JsonArray JsonObject::createNestedArray(char const *key) {
 }
 
 JsonObject JsonObject::createNestedObject(char const *key) {
-  JsonNode *node = getOrCreateNodeAt(key);
+  JsonNode *node = getOrCreateValueAt(key);
 
   if (node) node->setAsObject(_node->getContainerBuffer());
 
   return JsonObject(node);
 }
 
-JsonNode *JsonObject::getOrCreateNodeAt(const char *key) {
-  for (iterator it = begin(); it != end(); ++it) {
-    if (!strcmp(it->key(), key)) return it->value()._node;
+JsonNode *JsonObject::getPairAt(const char *key) {
+  for (JsonNode *node = firstChild(); node; node = node->next) {
+    if (!strcmp(node->getAsObjectKey(), key)) return node;
   }
+  return NULL;
+}
+
+JsonNode *JsonObject::getOrCreateValueAt(const char *key) {
+  JsonNode *existingNode = getPairAt(key);
+  if (existingNode) return existingNode->getAsObjectValue();
 
   JsonNode *newValueNode = createNode();
   if (!newValueNode) return 0;