From 851d21e08ca72ec34f2bbdca68c8bdf1d65290c5 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Fri, 18 Jul 2014 22:40:50 +0200 Subject: [PATCH] Added JsonObjectIterator --- JsonParser/JsonArrayIterator.h | 3 +- JsonParser/JsonObject.h | 11 +++++ JsonParser/JsonObjectIterator.h | 47 +++++++++++++++++++ JsonParser/JsonPair.h | 39 +++++++++++++++ JsonParserTests/JsonArrayIteratorTests.cpp | 31 ++++++------ JsonParserTests/JsonObjectIteratorTests.cpp | 31 ++++++++++++ JsonParserTests/JsonParserTests.vcxproj | 3 ++ .../JsonParserTests.vcxproj.filters | 9 ++++ 8 files changed, 158 insertions(+), 16 deletions(-) create mode 100644 JsonParser/JsonObjectIterator.h create mode 100644 JsonParser/JsonPair.h create mode 100644 JsonParserTests/JsonObjectIteratorTests.cpp diff --git a/JsonParser/JsonArrayIterator.h b/JsonParser/JsonArrayIterator.h index 071af5d7..2aa76564 100644 --- a/JsonParser/JsonArrayIterator.h +++ b/JsonParser/JsonArrayIterator.h @@ -22,10 +22,9 @@ namespace ArduinoJson } - const JsonArrayIterator& operator++() + void operator++() { token = token.nextSibling(); - return *this; } JsonValue operator*() const diff --git a/JsonParser/JsonObject.h b/JsonParser/JsonObject.h index 1f546e1d..e02f7adf 100644 --- a/JsonParser/JsonObject.h +++ b/JsonParser/JsonObject.h @@ -6,6 +6,7 @@ #pragma once #include "JsonValue.h" +#include "JsonObjectIterator.h" namespace ArduinoJson { @@ -43,6 +44,16 @@ namespace ArduinoJson return getValue(key).success(); } + JsonObjectIterator begin() + { + return JsonObjectIterator(json, token.firstChild()); + } + + JsonObjectIterator end() + { + return JsonObjectIterator(json, token.nextSibling()); + } + DEPRECATED JsonArray getArray(const char* key); DEPRECATED bool getBool(const char* key) diff --git a/JsonParser/JsonObjectIterator.h b/JsonParser/JsonObjectIterator.h new file mode 100644 index 00000000..e5e5fa54 --- /dev/null +++ b/JsonParser/JsonObjectIterator.h @@ -0,0 +1,47 @@ +/* +* Arduino JSON library +* Benoit Blanchon 2014 - MIT License +*/ + +#pragma once + +#include "JsonValue.h" +#include "JsonPair.h" +#include "JsonToken.h" + +namespace ArduinoJson +{ + namespace Parser + { + class JsonObjectIterator + { + public: + + JsonObjectIterator(char* json, Internal::JsonToken token) + : json(json), token(token) + { + + } + + void operator++() + { + token = token.nextSibling().nextSibling(); + } + + JsonPair operator*() const + { + return JsonPair(json, token); + } + + bool operator !=(const JsonObjectIterator& other) + { + return token != other.token; + } + + private: + + char* json; + Internal::JsonToken token; + }; + } +} \ No newline at end of file diff --git a/JsonParser/JsonPair.h b/JsonParser/JsonPair.h new file mode 100644 index 00000000..f0093464 --- /dev/null +++ b/JsonParser/JsonPair.h @@ -0,0 +1,39 @@ +/* +* Arduino JSON library +* Benoit Blanchon 2014 - MIT License +*/ + +#pragma once + +#include "JsonValue.h" + +namespace ArduinoJson +{ + namespace Parser + { + class JsonPair + { + public: + + JsonPair(char* json, Internal::JsonToken token) + : json(json), token(token) + { + + } + + const char* key() + { + return token.getText(json); + } + + JsonValue value() + { + return JsonValue(json, token.nextSibling()); + } + + private: + char* json; + Internal::JsonToken token; + }; + } +} diff --git a/JsonParserTests/JsonArrayIteratorTests.cpp b/JsonParserTests/JsonArrayIteratorTests.cpp index 72429ea9..919e5cc6 100644 --- a/JsonParserTests/JsonArrayIteratorTests.cpp +++ b/JsonParserTests/JsonArrayIteratorTests.cpp @@ -7,25 +7,28 @@ using namespace ArduinoJson::Parser; namespace JsonParserTests { - TEST_CLASS(JsonArrayIteratorTests) - { - public: - - TEST_METHOD(ThreeIntegers) - { - char json [] = "[1,2,3]"; - long expected [] = { 1, 2, 3 }; - JsonParser<4> parser; + TEST_CLASS(JsonObjectIteratorTests) + { + public: - JsonArray a = parser.parse(json); + TEST_METHOD(ThreeStrings) + { + char json [] = "{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":\"value3\"}"; + char* expectedKeys [] = { "key1", "key2", "key3" }; + char* expectedValues [] = { "value1", "value2", "value3" }; + JsonParser<7> parser; + + JsonHashTable a = parser.parse(json); int index = 0; - for (long i : a) + for (auto i : a) { - Assert::AreEqual(expected[index++], i); + Assert::AreEqual(expectedKeys[index], i.key()); + Assert::AreEqual(expectedValues[index], (const char*) i.value()); + index++; } - } + } - }; + }; } \ No newline at end of file diff --git a/JsonParserTests/JsonObjectIteratorTests.cpp b/JsonParserTests/JsonObjectIteratorTests.cpp new file mode 100644 index 00000000..72429ea9 --- /dev/null +++ b/JsonParserTests/JsonObjectIteratorTests.cpp @@ -0,0 +1,31 @@ +#include "CppUnitTest.h" +#include "JsonParser.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace ArduinoJson::Parser; + + +namespace JsonParserTests +{ + TEST_CLASS(JsonArrayIteratorTests) + { + public: + + TEST_METHOD(ThreeIntegers) + { + char json [] = "[1,2,3]"; + long expected [] = { 1, 2, 3 }; + JsonParser<4> parser; + + JsonArray a = parser.parse(json); + + int index = 0; + + for (long i : a) + { + Assert::AreEqual(expected[index++], i); + } + } + + }; +} \ No newline at end of file diff --git a/JsonParserTests/JsonParserTests.vcxproj b/JsonParserTests/JsonParserTests.vcxproj index 7cab6d7d..75f5ab83 100644 --- a/JsonParserTests/JsonParserTests.vcxproj +++ b/JsonParserTests/JsonParserTests.vcxproj @@ -93,6 +93,7 @@ + @@ -101,6 +102,8 @@ + + diff --git a/JsonParserTests/JsonParserTests.vcxproj.filters b/JsonParserTests/JsonParserTests.vcxproj.filters index 4774c593..9291f4d1 100644 --- a/JsonParserTests/JsonParserTests.vcxproj.filters +++ b/JsonParserTests/JsonParserTests.vcxproj.filters @@ -45,6 +45,9 @@ Source Files + + Source Files + @@ -71,5 +74,11 @@ Header Files + + Header Files + + + Header Files + \ No newline at end of file