From c329572d244011b1e200b6157289dda442a46a2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Blanchon?= Date: Thu, 17 Jul 2014 13:58:30 +0200 Subject: [PATCH] Added JsonArrayIterator (tests are failing) --- JsonParser/JsonArray.h | 13 ++++- JsonParser/JsonArrayIterator.h | 56 +++++++++++++++++++ JsonParser/JsonValue.h | 2 + JsonParserTests/JsonArrayIteratorTests.cpp | 31 ++++++++++ JsonParserTests/JsonParserTests.vcxproj | 2 + .../JsonParserTests.vcxproj.filters | 6 ++ 6 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 JsonParser/JsonArrayIterator.h create mode 100644 JsonParserTests/JsonArrayIteratorTests.cpp diff --git a/JsonParser/JsonArray.h b/JsonParser/JsonArray.h index e9937ef8..0fe50e69 100644 --- a/JsonParser/JsonArray.h +++ b/JsonParser/JsonArray.h @@ -6,13 +6,14 @@ #pragma once #include "JsonValue.h" +#include "JsonArrayIterator.h" namespace ArduinoJson { namespace Parser { class JsonHashTable; - + class JsonArray { public: @@ -40,6 +41,16 @@ namespace ArduinoJson return value[index]; } + JsonArrayIterator begin() + { + return JsonArrayIterator(value); + } + + JsonArrayIterator end() + { + return JsonArrayIterator(); + } + DEPRECATED int getLength() { return size(); diff --git a/JsonParser/JsonArrayIterator.h b/JsonParser/JsonArrayIterator.h new file mode 100644 index 00000000..db755512 --- /dev/null +++ b/JsonParser/JsonArrayIterator.h @@ -0,0 +1,56 @@ +/* +* Arduino JSON library +* Benoit Blanchon 2014 - MIT License +*/ + +#pragma once + +#include "JsonValue.h" + +namespace ArduinoJson +{ + namespace Parser + { + class JsonHashTable; + + class JsonArrayIterator + { + friend class JsonArray; + + public: + + JsonArrayIterator operator++() + { + tokens++; + return *this; + } + + JsonValue operator*() + { + return JsonValue(json, tokens); + } + + bool operator !=(const JsonArrayIterator& other) + { + return tokens != other.tokens || json != other.json; + } + + private: + + char* json; + jsmntok_t* tokens; + + JsonArrayIterator() + : json(0), tokens(0) + { + + } + + JsonArrayIterator(JsonValue& value) + : json(value.json), tokens(value.tokens) + { + + } + }; + } +} \ No newline at end of file diff --git a/JsonParser/JsonValue.h b/JsonParser/JsonValue.h index c452dd58..7cb0bbc1 100644 --- a/JsonParser/JsonValue.h +++ b/JsonParser/JsonValue.h @@ -26,6 +26,8 @@ namespace ArduinoJson class JsonValue { + friend class JsonArrayIterator; + public: JsonValue() : json(0), tokens(0) diff --git a/JsonParserTests/JsonArrayIteratorTests.cpp b/JsonParserTests/JsonArrayIteratorTests.cpp new file mode 100644 index 00000000..9f46dd36 --- /dev/null +++ b/JsonParserTests/JsonArrayIteratorTests.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(TestMethod1) + { + char json [] = "[1,2,3]"; + JsonParser<4> parser; + + JsonArray a = parser.parse(json); + + long expected = 1; + + for (auto i : a) + { + Assert::AreEqual(expected, (long)*i); + expected++; + } + } + + }; +} \ No newline at end of file diff --git a/JsonParserTests/JsonParserTests.vcxproj b/JsonParserTests/JsonParserTests.vcxproj index 15407be6..fb05706c 100644 --- a/JsonParserTests/JsonParserTests.vcxproj +++ b/JsonParserTests/JsonParserTests.vcxproj @@ -90,6 +90,7 @@ + @@ -97,6 +98,7 @@ + diff --git a/JsonParserTests/JsonParserTests.vcxproj.filters b/JsonParserTests/JsonParserTests.vcxproj.filters index 74cb6322..f632b7b6 100644 --- a/JsonParserTests/JsonParserTests.vcxproj.filters +++ b/JsonParserTests/JsonParserTests.vcxproj.filters @@ -39,6 +39,9 @@ Source Files + + Source Files + @@ -59,5 +62,8 @@ Header Files + + Header Files + \ No newline at end of file