From cc1926647016eb19c7ff76cd069b5124f61a0e63 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Sat, 27 Sep 2014 21:24:29 +0200 Subject: [PATCH] Added missing newline at end-of-file (issue #24) --- FixEndOfFile.sh | 1 + JsonGenerator.cpp | 30 +- JsonGenerator.h | 14 +- JsonGenerator/EscapedString.cpp | 82 +-- JsonGenerator/EscapedString.h | 34 +- JsonGenerator/IndentedPrint.cpp | 90 ++-- JsonGenerator/IndentedPrint.h | 100 ++-- JsonGenerator/JsonArray.h | 56 +- JsonGenerator/JsonArrayBase.cpp | 68 +-- JsonGenerator/JsonArrayBase.h | 158 +++--- JsonGenerator/JsonObject.h | 84 +-- JsonGenerator/JsonObjectBase.cpp | 178 +++---- JsonGenerator/JsonObjectBase.h | 124 ++--- JsonGenerator/JsonPrettyPrint.cpp | 186 +++---- JsonGenerator/JsonPrettyPrint.h | 98 ++-- JsonGenerator/JsonPrintable.cpp | 64 +-- JsonGenerator/JsonPrintable.h | 80 +-- JsonGenerator/JsonValue.cpp | 58 +-- JsonGenerator/JsonValue.h | 264 +++++----- JsonGenerator/Print.cpp | 98 ++-- JsonGenerator/Print.h | 52 +- JsonGenerator/Printable.h | 40 +- JsonGenerator/StringBuilder.cpp | 28 +- JsonGenerator/StringBuilder.h | 62 +-- JsonGeneratorTests/EscapedStringTests.cpp | 184 +++---- JsonGeneratorTests/Issue10.cpp | 146 +++--- JsonGeneratorTests/JsonArrayTests.cpp | 318 ++++++------ .../JsonObject_Indexer_Tests.cpp | 140 ++--- .../JsonObject_PrintTo_Tests.cpp | 298 +++++------ JsonGeneratorTests/JsonValue_Cast_Tests.cpp | 150 +++--- .../JsonValue_PrintTo_Tests.cpp | 212 ++++---- .../PrettyPrint_Array_Tests.cpp | 176 +++---- .../PrettyPrint_Object_Tests.cpp | 172 +++---- .../PrettyPrint_String_Tests.cpp | 146 +++--- JsonGeneratorTests/StringBuilderTests.cpp | 164 +++--- JsonParser.cpp | 26 +- JsonParser.h | 12 +- JsonParser/JsonArray.cpp | 20 +- JsonParser/JsonArray.h | 164 +++--- JsonParser/JsonArrayIterator.h | 84 +-- JsonParser/JsonObject.cpp | 24 +- JsonParser/JsonObject.h | 196 +++---- JsonParser/JsonObjectIterator.h | 108 ++-- JsonParser/JsonPair.h | 68 +-- JsonParser/JsonParser.h | 64 +-- JsonParser/JsonParserBase.cpp | 34 +- JsonParser/JsonParserBase.h | 92 ++-- JsonParser/JsonToken.cpp | 136 ++--- JsonParser/JsonToken.h | 192 +++---- JsonParser/JsonValue.cpp | 214 ++++---- JsonParser/JsonValue.h | 134 ++--- JsonParserTests/GbathreeBug.cpp | 482 +++++++++--------- JsonParserTests/JsonArrayIteratorTests.cpp | 128 ++--- JsonParserTests/JsonArrayTests.cpp | 348 ++++++------- JsonParserTests/JsonObjectIteratorTests.cpp | 136 ++--- JsonParserTests/JsonObjectTests.cpp | 324 ++++++------ JsonParserTests/JsonStringTests.cpp | 206 ++++---- JsonParserTests/TestHashGenerator.cpp | 50 +- 58 files changed, 3699 insertions(+), 3698 deletions(-) create mode 100644 FixEndOfFile.sh diff --git a/FixEndOfFile.sh b/FixEndOfFile.sh new file mode 100644 index 00000000..6a98e7bb --- /dev/null +++ b/FixEndOfFile.sh @@ -0,0 +1 @@ +find \( -iname '*.cpp' -o -iname '*.h' \) -exec sed -i -e '$a\' {} \; diff --git a/JsonGenerator.cpp b/JsonGenerator.cpp index 58a3cfdf..1b686dd3 100644 --- a/JsonGenerator.cpp +++ b/JsonGenerator.cpp @@ -1,15 +1,15 @@ -/* -* malloc-free JSON parser for Arduino -* Benoit Blanchon 2014 - MIT License -*/ - -// This file is here to help the Arduino IDE find the .cpp files - -#include "JsonGenerator/EscapedString.cpp" -#include "JsonGenerator/IndentedPrint.cpp" -#include "JsonGenerator/JsonArrayBase.cpp" -#include "JsonGenerator/JsonObjectBase.cpp" -#include "JsonGenerator/JsonValue.cpp" -#include "JsonGenerator/JsonPrettyPrint.cpp" -#include "JsonGenerator/JsonPrintable.cpp" -#include "JsonGenerator/StringBuilder.cpp" \ No newline at end of file +/* +* malloc-free JSON parser for Arduino +* Benoit Blanchon 2014 - MIT License +*/ + +// This file is here to help the Arduino IDE find the .cpp files + +#include "JsonGenerator/EscapedString.cpp" +#include "JsonGenerator/IndentedPrint.cpp" +#include "JsonGenerator/JsonArrayBase.cpp" +#include "JsonGenerator/JsonObjectBase.cpp" +#include "JsonGenerator/JsonValue.cpp" +#include "JsonGenerator/JsonPrettyPrint.cpp" +#include "JsonGenerator/JsonPrintable.cpp" +#include "JsonGenerator/StringBuilder.cpp" diff --git a/JsonGenerator.h b/JsonGenerator.h index 00bab65c..98b8dd12 100644 --- a/JsonGenerator.h +++ b/JsonGenerator.h @@ -1,7 +1,7 @@ -/* -* malloc-free JSON parser for Arduino -* Benoit Blanchon 2014 - MIT License -*/ - -#include "JsonGenerator/JsonArray.h" -#include "JsonGenerator/JsonObject.h" \ No newline at end of file +/* +* malloc-free JSON parser for Arduino +* Benoit Blanchon 2014 - MIT License +*/ + +#include "JsonGenerator/JsonArray.h" +#include "JsonGenerator/JsonObject.h" diff --git a/JsonGenerator/EscapedString.cpp b/JsonGenerator/EscapedString.cpp index 4253d323..793515f7 100644 --- a/JsonGenerator/EscapedString.cpp +++ b/JsonGenerator/EscapedString.cpp @@ -2,44 +2,44 @@ * Arduino JSON library * Benoit Blanchon 2014 - MIT License */ - -#include "EscapedString.h" - -using namespace ArduinoJson::Internals; - -static inline char getSpecialChar(char c) -{ - // Optimized for code size on a 8-bit AVR - - const char* p = "\"\"\\\\\bb\ff\nn\rr\tt\0"; - - while (p[0] && p[0] != c) - { - p += 2; - } - - return p[1]; -} - -static inline size_t printCharTo(char c, Print& p) -{ - char specialChar = getSpecialChar(c); - - return specialChar != 0 - ? p.write('\\') + p.write(specialChar) - : p.write(c); -} - -size_t EscapedString::printTo(const char* s, Print& p) -{ - if (!s) return p.print("null"); - - size_t n = p.write('\"'); - - while (*s) - { - n += printCharTo(*s++, p); - } - - return n + p.write('\"'); -} \ No newline at end of file + +#include "EscapedString.h" + +using namespace ArduinoJson::Internals; + +static inline char getSpecialChar(char c) +{ + // Optimized for code size on a 8-bit AVR + + const char* p = "\"\"\\\\\bb\ff\nn\rr\tt\0"; + + while (p[0] && p[0] != c) + { + p += 2; + } + + return p[1]; +} + +static inline size_t printCharTo(char c, Print& p) +{ + char specialChar = getSpecialChar(c); + + return specialChar != 0 + ? p.write('\\') + p.write(specialChar) + : p.write(c); +} + +size_t EscapedString::printTo(const char* s, Print& p) +{ + if (!s) return p.print("null"); + + size_t n = p.write('\"'); + + while (*s) + { + n += printCharTo(*s++, p); + } + + return n + p.write('\"'); +} diff --git a/JsonGenerator/EscapedString.h b/JsonGenerator/EscapedString.h index 22db62c7..c6d95bb2 100644 --- a/JsonGenerator/EscapedString.h +++ b/JsonGenerator/EscapedString.h @@ -1,20 +1,20 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License - */ - -#pragma once - -#include "Print.h" - -namespace ArduinoJson -{ - namespace Internals - { - class EscapedString - { - public: - static size_t printTo(const char*, Print&); - }; - } -} \ No newline at end of file + */ + +#pragma once + +#include "Print.h" + +namespace ArduinoJson +{ + namespace Internals + { + class EscapedString + { + public: + static size_t printTo(const char*, Print&); + }; + } +} diff --git a/JsonGenerator/IndentedPrint.cpp b/JsonGenerator/IndentedPrint.cpp index 75032831..97a5c51f 100644 --- a/JsonGenerator/IndentedPrint.cpp +++ b/JsonGenerator/IndentedPrint.cpp @@ -1,45 +1,45 @@ -#include "IndentedPrint.h" - -using namespace ArduinoJson::Generator; - -void IndentedPrint::indent() -{ - if (level < MAX_LEVEL) - level++; -} - -void IndentedPrint::unindent() -{ - if (level > 0) - level--; -} - -void IndentedPrint::setTabSize(uint8_t n) -{ - if (n < MAX_TAB_SIZE) - tabSize = n; -} - -size_t IndentedPrint::write(uint8_t c) -{ - size_t n = 0; - - if (isNewLine) - n += writeTabs(); - - n += sink.write(c); - - isNewLine = c == '\n'; - - return n; -} - -inline size_t IndentedPrint::writeTabs() -{ - size_t n = 0; - - for (int i = 0; i < level*tabSize; i++) - n += sink.write(' '); - - return n; -} \ No newline at end of file +#include "IndentedPrint.h" + +using namespace ArduinoJson::Generator; + +void IndentedPrint::indent() +{ + if (level < MAX_LEVEL) + level++; +} + +void IndentedPrint::unindent() +{ + if (level > 0) + level--; +} + +void IndentedPrint::setTabSize(uint8_t n) +{ + if (n < MAX_TAB_SIZE) + tabSize = n; +} + +size_t IndentedPrint::write(uint8_t c) +{ + size_t n = 0; + + if (isNewLine) + n += writeTabs(); + + n += sink.write(c); + + isNewLine = c == '\n'; + + return n; +} + +inline size_t IndentedPrint::writeTabs() +{ + size_t n = 0; + + for (int i = 0; i < level*tabSize; i++) + n += sink.write(' '); + + return n; +} diff --git a/JsonGenerator/IndentedPrint.h b/JsonGenerator/IndentedPrint.h index ace14144..35c0aef4 100644 --- a/JsonGenerator/IndentedPrint.h +++ b/JsonGenerator/IndentedPrint.h @@ -1,53 +1,53 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#pragma once - -#include "Print.h" - -namespace ArduinoJson -{ - namespace Generator - { - // Decorator on top of Print to allow indented output. - // This class is used by JsonPrintable::prettyPrintTo() but can also be used - // for your own purpose, like logging. - class IndentedPrint : public Print - { - public: - - IndentedPrint(Print& p) - : sink(p) - { - level = 0; - tabSize = 2; - isNewLine = true; - } - - virtual size_t write(uint8_t); - - // Adds one level of indentation - void indent(); - - // Removes one level of indentation - void unindent(); - - // Set the number of space printed for each level of indentation - void setTabSize(uint8_t n); - - private: - Print& sink; - uint8_t level : 4; - uint8_t tabSize : 3; - bool isNewLine : 1; - - size_t writeTabs(); - - static const int MAX_LEVEL = 15; // because it's only 4 bits - static const int MAX_TAB_SIZE = 7; // because it's only 3 bits - }; - } -} - +*/ + +#pragma once + +#include "Print.h" + +namespace ArduinoJson +{ + namespace Generator + { + // Decorator on top of Print to allow indented output. + // This class is used by JsonPrintable::prettyPrintTo() but can also be used + // for your own purpose, like logging. + class IndentedPrint : public Print + { + public: + + IndentedPrint(Print& p) + : sink(p) + { + level = 0; + tabSize = 2; + isNewLine = true; + } + + virtual size_t write(uint8_t); + + // Adds one level of indentation + void indent(); + + // Removes one level of indentation + void unindent(); + + // Set the number of space printed for each level of indentation + void setTabSize(uint8_t n); + + private: + Print& sink; + uint8_t level : 4; + uint8_t tabSize : 3; + bool isNewLine : 1; + + size_t writeTabs(); + + static const int MAX_LEVEL = 15; // because it's only 4 bits + static const int MAX_TAB_SIZE = 7; // because it's only 3 bits + }; + } +} + diff --git a/JsonGenerator/JsonArray.h b/JsonGenerator/JsonArray.h index f0251fcf..817e9019 100644 --- a/JsonGenerator/JsonArray.h +++ b/JsonGenerator/JsonArray.h @@ -1,28 +1,28 @@ -/* - * Arduino JSON library - * Benoit Blanchon 2014 - MIT License - */ - -#pragma once - -#include "JsonArrayBase.h" - -namespace ArduinoJson -{ - namespace Generator - { - template - class JsonArray : public JsonArrayBase - { - public: - JsonArray() - : JsonArrayBase(items, N) - { - - } - - private: - JsonValue items[N]; - }; - } -} \ No newline at end of file +/* + * Arduino JSON library + * Benoit Blanchon 2014 - MIT License + */ + +#pragma once + +#include "JsonArrayBase.h" + +namespace ArduinoJson +{ + namespace Generator + { + template + class JsonArray : public JsonArrayBase + { + public: + JsonArray() + : JsonArrayBase(items, N) + { + + } + + private: + JsonValue items[N]; + }; + } +} diff --git a/JsonGenerator/JsonArrayBase.cpp b/JsonGenerator/JsonArrayBase.cpp index 2c620b00..b90b39ed 100644 --- a/JsonGenerator/JsonArrayBase.cpp +++ b/JsonGenerator/JsonArrayBase.cpp @@ -1,34 +1,34 @@ -/* -* Arduino JSON library -* Benoit Blanchon 2014 - MIT License -*/ - -#include "JsonArrayBase.h" - -using namespace ArduinoJson::Generator; -using namespace ArduinoJson::Internals; - -size_t JsonArrayBase::printTo(Print& p) const -{ - size_t n = 0; - - n += p.write('['); - - // NB: the code has been optimized for a small size on a 8-bit AVR - - const JsonValue* current = items; - for (int i = count; i > 0; i--) - { - n += current->printTo(p); - current++; - - if (i > 1) - { - n += p.write(','); - } - } - - n += p.write(']'); - - return n; -} \ No newline at end of file +/* +* Arduino JSON library +* Benoit Blanchon 2014 - MIT License +*/ + +#include "JsonArrayBase.h" + +using namespace ArduinoJson::Generator; +using namespace ArduinoJson::Internals; + +size_t JsonArrayBase::printTo(Print& p) const +{ + size_t n = 0; + + n += p.write('['); + + // NB: the code has been optimized for a small size on a 8-bit AVR + + const JsonValue* current = items; + for (int i = count; i > 0; i--) + { + n += current->printTo(p); + current++; + + if (i > 1) + { + n += p.write(','); + } + } + + n += p.write(']'); + + return n; +} diff --git a/JsonGenerator/JsonArrayBase.h b/JsonGenerator/JsonArrayBase.h index 359fd137..10bbc4e4 100644 --- a/JsonGenerator/JsonArrayBase.h +++ b/JsonGenerator/JsonArrayBase.h @@ -1,79 +1,79 @@ -/* -* Arduino JSON library -* Benoit Blanchon 2014 - MIT License -*/ - -#pragma once - -#include "JsonPrintable.h" -#include "JsonValue.h" - -namespace ArduinoJson -{ - namespace Generator - { - class JsonArrayBase : public JsonPrintable - { - public: - JsonArrayBase(JsonValue* items, int capacity) - : items(items), capacity(capacity), count(0) - { - - } - - void add(const Printable& value) - { - addIfPossible(value); - } - - void add(bool value) - { - addIfPossible(value); - } - - void add(int value) - { - addIfPossible(value); - } - - void add(long value) - { - addIfPossible(value); - } - - void add(double value) - { - addIfPossible(value); - } - - void add(const char* value) - { - addIfPossible(value); - } - - template - void add(double value) - { - if (count >= capacity) return; - - JsonValue& v = items[count++]; - v.set(value); - } - - virtual size_t printTo(Print& p) const; - - using JsonPrintable::printTo; - - private: - JsonValue* items; - int capacity, count; - - template - void addIfPossible(T value) - { - if (count < capacity) - items[count++] = value; - } - }; - } -} \ No newline at end of file +/* +* Arduino JSON library +* Benoit Blanchon 2014 - MIT License +*/ + +#pragma once + +#include "JsonPrintable.h" +#include "JsonValue.h" + +namespace ArduinoJson +{ + namespace Generator + { + class JsonArrayBase : public JsonPrintable + { + public: + JsonArrayBase(JsonValue* items, int capacity) + : items(items), capacity(capacity), count(0) + { + + } + + void add(const Printable& value) + { + addIfPossible(value); + } + + void add(bool value) + { + addIfPossible(value); + } + + void add(int value) + { + addIfPossible(value); + } + + void add(long value) + { + addIfPossible(value); + } + + void add(double value) + { + addIfPossible(value); + } + + void add(const char* value) + { + addIfPossible(value); + } + + template + void add(double value) + { + if (count >= capacity) return; + + JsonValue& v = items[count++]; + v.set(value); + } + + virtual size_t printTo(Print& p) const; + + using JsonPrintable::printTo; + + private: + JsonValue* items; + int capacity, count; + + template + void addIfPossible(T value) + { + if (count < capacity) + items[count++] = value; + } + }; + } +} diff --git a/JsonGenerator/JsonObject.h b/JsonGenerator/JsonObject.h index 76048c43..fadd0167 100644 --- a/JsonGenerator/JsonObject.h +++ b/JsonGenerator/JsonObject.h @@ -1,44 +1,44 @@ -/* - * Arduino JSON library - * Benoit Blanchon 2014 - MIT License - */ - -#pragma once - -#include "JsonObjectBase.h" - -#ifndef ARDUINO_JSON_NO_DEPRECATION_WARNING -#ifdef __GNUC__ -#define DEPRECATED __attribute__((deprecated)) -#elif defined(_MSC_VER) -#define DEPRECATED __declspec(deprecated) -#endif +/* + * Arduino JSON library + * Benoit Blanchon 2014 - MIT License + */ + +#pragma once + +#include "JsonObjectBase.h" + +#ifndef ARDUINO_JSON_NO_DEPRECATION_WARNING +#ifdef __GNUC__ +#define DEPRECATED __attribute__((deprecated)) +#elif defined(_MSC_VER) +#define DEPRECATED __declspec(deprecated) +#endif #else #define DEPRECATED -#endif - -namespace ArduinoJson -{ - namespace Generator - { - template - class JsonObject : public JsonObjectBase - { - public: - JsonObject() - : JsonObjectBase(items, N) - { - } - - private: - KeyValuePair items[N]; - }; - - - // Obsolete: use JsonObject instead - template - class DEPRECATED JsonHashTable : public JsonObject - { - }; - } -} \ No newline at end of file +#endif + +namespace ArduinoJson +{ + namespace Generator + { + template + class JsonObject : public JsonObjectBase + { + public: + JsonObject() + : JsonObjectBase(items, N) + { + } + + private: + KeyValuePair items[N]; + }; + + + // Obsolete: use JsonObject instead + template + class DEPRECATED JsonHashTable : public JsonObject + { + }; + } +} diff --git a/JsonGenerator/JsonObjectBase.cpp b/JsonGenerator/JsonObjectBase.cpp index 45fdb6bf..79753b4d 100644 --- a/JsonGenerator/JsonObjectBase.cpp +++ b/JsonGenerator/JsonObjectBase.cpp @@ -1,92 +1,92 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#include "JsonObjectBase.h" -#include // for strcmp - -using namespace ArduinoJson::Generator; -using namespace ArduinoJson::Internals; - -JsonValue JsonObjectBase::nullValue; - -size_t JsonObjectBase::printTo(Print& p) const -{ - size_t n = 0; - - n += p.write('{'); - - // NB: the code has been optimized for a small size on a 8-bit AVR - - const KeyValuePair* current = items; - for (int i = count; i > 0; i--) - { - n += EscapedString::printTo(current->key, p); - n += p.write(':'); - n += current->value.printTo(p); - - current++; - - if (i > 1) - { - n += p.write(','); - } - } - - n += p.write('}'); - - return n; -} - -JsonObjectBase::KeyValuePair* JsonObjectBase::getMatchingPair(JsonKey key) const -{ - KeyValuePair* p = items; - - for (int i = count; i > 0; --i) - { - if (!strcmp(p->key, key)) - return p; - - p++; - } - - return 0; -} - -JsonValue& JsonObjectBase::operator[](JsonKey key) -{ - KeyValuePair* match = getMatchingPair(key); - - if (match) - return match->value; - - JsonValue* value; - - if (count < capacity) - { - items[count].key = key; - value = &items[count].value; - count++; - } - else - { - value = &nullValue; - } - - value->reset(); - return *value; -} - -bool JsonObjectBase::containsKey(JsonKey key) const -{ - return getMatchingPair(key) != 0; -} - -void JsonObjectBase::remove(JsonKey key) -{ - KeyValuePair* match = getMatchingPair(key); - if (match == 0) return; - - *match = items[--count]; -} \ No newline at end of file +*/ + +#include "JsonObjectBase.h" +#include // for strcmp + +using namespace ArduinoJson::Generator; +using namespace ArduinoJson::Internals; + +JsonValue JsonObjectBase::nullValue; + +size_t JsonObjectBase::printTo(Print& p) const +{ + size_t n = 0; + + n += p.write('{'); + + // NB: the code has been optimized for a small size on a 8-bit AVR + + const KeyValuePair* current = items; + for (int i = count; i > 0; i--) + { + n += EscapedString::printTo(current->key, p); + n += p.write(':'); + n += current->value.printTo(p); + + current++; + + if (i > 1) + { + n += p.write(','); + } + } + + n += p.write('}'); + + return n; +} + +JsonObjectBase::KeyValuePair* JsonObjectBase::getMatchingPair(JsonKey key) const +{ + KeyValuePair* p = items; + + for (int i = count; i > 0; --i) + { + if (!strcmp(p->key, key)) + return p; + + p++; + } + + return 0; +} + +JsonValue& JsonObjectBase::operator[](JsonKey key) +{ + KeyValuePair* match = getMatchingPair(key); + + if (match) + return match->value; + + JsonValue* value; + + if (count < capacity) + { + items[count].key = key; + value = &items[count].value; + count++; + } + else + { + value = &nullValue; + } + + value->reset(); + return *value; +} + +bool JsonObjectBase::containsKey(JsonKey key) const +{ + return getMatchingPair(key) != 0; +} + +void JsonObjectBase::remove(JsonKey key) +{ + KeyValuePair* match = getMatchingPair(key); + if (match == 0) return; + + *match = items[--count]; +} diff --git a/JsonGenerator/JsonObjectBase.h b/JsonGenerator/JsonObjectBase.h index 5e27d34f..d7eb7b64 100644 --- a/JsonGenerator/JsonObjectBase.h +++ b/JsonGenerator/JsonObjectBase.h @@ -1,62 +1,62 @@ -/* -* Arduino JSON library -* Benoit Blanchon 2014 - MIT License -*/ - -#pragma once - -#include "JsonPrintable.h" -#include "JsonValue.h" -#include "EscapedString.h" - -namespace ArduinoJson -{ - namespace Generator - { - typedef const char* JsonKey; - - class JsonObjectBase : public JsonPrintable - { - public: - JsonValue& operator[](JsonKey); - bool containsKey(JsonKey) const; - void remove(JsonKey key); - - template - void add(JsonKey key, T value) - { - operator[](key) = value; - } - - template - void add(JsonKey key, double value) - { - operator[](key).set(value); - } - - using JsonPrintable::printTo; - - virtual size_t printTo(Print& p) const; - - protected: - - struct KeyValuePair - { - JsonKey key; - JsonValue value; - }; - - JsonObjectBase(KeyValuePair* items, int capacity) - : items(items), capacity(capacity), count(0) - { - } - - private: - KeyValuePair* items; - int capacity, count; - static JsonValue nullValue; - - KeyValuePair* getMatchingPair(JsonKey key) const; - }; - } -} \ No newline at end of file +/* +* Arduino JSON library +* Benoit Blanchon 2014 - MIT License +*/ + +#pragma once + +#include "JsonPrintable.h" +#include "JsonValue.h" +#include "EscapedString.h" + +namespace ArduinoJson +{ + namespace Generator + { + typedef const char* JsonKey; + + class JsonObjectBase : public JsonPrintable + { + public: + JsonValue& operator[](JsonKey); + bool containsKey(JsonKey) const; + void remove(JsonKey key); + + template + void add(JsonKey key, T value) + { + operator[](key) = value; + } + + template + void add(JsonKey key, double value) + { + operator[](key).set(value); + } + + using JsonPrintable::printTo; + + virtual size_t printTo(Print& p) const; + + protected: + + struct KeyValuePair + { + JsonKey key; + JsonValue value; + }; + + JsonObjectBase(KeyValuePair* items, int capacity) + : items(items), capacity(capacity), count(0) + { + } + + private: + KeyValuePair* items; + int capacity, count; + static JsonValue nullValue; + + KeyValuePair* getMatchingPair(JsonKey key) const; + }; + } +} diff --git a/JsonGenerator/JsonPrettyPrint.cpp b/JsonGenerator/JsonPrettyPrint.cpp index a6992497..efa11908 100644 --- a/JsonGenerator/JsonPrettyPrint.cpp +++ b/JsonGenerator/JsonPrettyPrint.cpp @@ -2,96 +2,96 @@ * Arduino JSON library * Benoit Blanchon 2014 - MIT License */ - -#include "JsonPrettyPrint.h" - -using namespace ArduinoJson::Generator; - -size_t JsonPrettyPrint::write(uint8_t c) -{ - size_t n = inString ? handleStringChar(c) : handleMarkupChar(c); - previousChar = c; - return n; -} - -inline size_t JsonPrettyPrint::handleStringChar(uint8_t c) -{ - bool isQuote = c == '"' && previousChar != '\\'; - - if (isQuote) inString = false; - - return sink.write(c); -} - -inline size_t JsonPrettyPrint::handleMarkupChar(uint8_t c) -{ - switch (c) - { - case '{': - case '[': - return handleBlockOpen(c); - - case '}': - case ']': - return handleBlockClose(c); - - case ':': - return handleColumn(); - - case ',': - return handleComma(); - - case '"': - return handleQuoteOpen(); - - default: - return handleNormalChar(c); - } -} - -inline size_t JsonPrettyPrint::handleBlockOpen(uint8_t c) -{ - return indentIfNeeded() + sink.write(c); -} - -inline size_t JsonPrettyPrint::handleBlockClose(uint8_t c) -{ - return unindentIfNeeded() + sink.write(c); -} - -inline size_t JsonPrettyPrint::handleColumn() -{ - return sink.write(':') + sink.write(' '); -} - -inline size_t JsonPrettyPrint::handleComma() -{ - return sink.write(',') + sink.println(); -} - -inline size_t JsonPrettyPrint::handleQuoteOpen() -{ - inString = true; - return indentIfNeeded() + sink.write('"'); -} - -inline size_t JsonPrettyPrint::handleNormalChar(uint8_t c) -{ - return indentIfNeeded() + sink.write(c); -} - -size_t JsonPrettyPrint::indentIfNeeded() -{ - if (!inEmptyBlock()) return 0; - - sink.indent(); - return sink.println(); -} - -size_t JsonPrettyPrint::unindentIfNeeded() -{ - if (inEmptyBlock()) return 0; - - sink.unindent(); - return sink.println(); -} \ No newline at end of file + +#include "JsonPrettyPrint.h" + +using namespace ArduinoJson::Generator; + +size_t JsonPrettyPrint::write(uint8_t c) +{ + size_t n = inString ? handleStringChar(c) : handleMarkupChar(c); + previousChar = c; + return n; +} + +inline size_t JsonPrettyPrint::handleStringChar(uint8_t c) +{ + bool isQuote = c == '"' && previousChar != '\\'; + + if (isQuote) inString = false; + + return sink.write(c); +} + +inline size_t JsonPrettyPrint::handleMarkupChar(uint8_t c) +{ + switch (c) + { + case '{': + case '[': + return handleBlockOpen(c); + + case '}': + case ']': + return handleBlockClose(c); + + case ':': + return handleColumn(); + + case ',': + return handleComma(); + + case '"': + return handleQuoteOpen(); + + default: + return handleNormalChar(c); + } +} + +inline size_t JsonPrettyPrint::handleBlockOpen(uint8_t c) +{ + return indentIfNeeded() + sink.write(c); +} + +inline size_t JsonPrettyPrint::handleBlockClose(uint8_t c) +{ + return unindentIfNeeded() + sink.write(c); +} + +inline size_t JsonPrettyPrint::handleColumn() +{ + return sink.write(':') + sink.write(' '); +} + +inline size_t JsonPrettyPrint::handleComma() +{ + return sink.write(',') + sink.println(); +} + +inline size_t JsonPrettyPrint::handleQuoteOpen() +{ + inString = true; + return indentIfNeeded() + sink.write('"'); +} + +inline size_t JsonPrettyPrint::handleNormalChar(uint8_t c) +{ + return indentIfNeeded() + sink.write(c); +} + +size_t JsonPrettyPrint::indentIfNeeded() +{ + if (!inEmptyBlock()) return 0; + + sink.indent(); + return sink.println(); +} + +size_t JsonPrettyPrint::unindentIfNeeded() +{ + if (inEmptyBlock()) return 0; + + sink.unindent(); + return sink.println(); +} diff --git a/JsonGenerator/JsonPrettyPrint.h b/JsonGenerator/JsonPrettyPrint.h index decff82d..6a4c5074 100644 --- a/JsonGenerator/JsonPrettyPrint.h +++ b/JsonGenerator/JsonPrettyPrint.h @@ -1,52 +1,52 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#pragma once - -#include "Print.h" -#include "IndentedPrint.h" - -namespace ArduinoJson -{ - namespace Generator - { - // Converts a compact JSON string into an indented one. - class JsonPrettyPrint : public Print - { - public: - - JsonPrettyPrint(IndentedPrint& p) - : sink(p) - { - previousChar = 0; - inString = false; - } - - virtual size_t write(uint8_t); - - private: - uint8_t previousChar; - IndentedPrint& sink; - bool inString; - - bool inEmptyBlock() - { - return previousChar == '{' || previousChar == '['; - } - - size_t handleStringChar(uint8_t); - size_t handleMarkupChar(uint8_t); - - size_t handleBlockClose(uint8_t); - size_t handleBlockOpen(uint8_t); - size_t handleColumn(); - size_t handleComma(); - size_t handleQuoteOpen(); - size_t handleNormalChar(uint8_t); - size_t indentIfNeeded(); - size_t unindentIfNeeded(); - }; - } -} \ No newline at end of file +*/ + +#pragma once + +#include "Print.h" +#include "IndentedPrint.h" + +namespace ArduinoJson +{ + namespace Generator + { + // Converts a compact JSON string into an indented one. + class JsonPrettyPrint : public Print + { + public: + + JsonPrettyPrint(IndentedPrint& p) + : sink(p) + { + previousChar = 0; + inString = false; + } + + virtual size_t write(uint8_t); + + private: + uint8_t previousChar; + IndentedPrint& sink; + bool inString; + + bool inEmptyBlock() + { + return previousChar == '{' || previousChar == '['; + } + + size_t handleStringChar(uint8_t); + size_t handleMarkupChar(uint8_t); + + size_t handleBlockClose(uint8_t); + size_t handleBlockOpen(uint8_t); + size_t handleColumn(); + size_t handleComma(); + size_t handleQuoteOpen(); + size_t handleNormalChar(uint8_t); + size_t indentIfNeeded(); + size_t unindentIfNeeded(); + }; + } +} diff --git a/JsonGenerator/JsonPrintable.cpp b/JsonGenerator/JsonPrintable.cpp index 5d6cfa03..47e98c5c 100644 --- a/JsonGenerator/JsonPrintable.cpp +++ b/JsonGenerator/JsonPrintable.cpp @@ -1,35 +1,35 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#include "JsonPrintable.h" -#include "JsonPrettyPrint.h" -#include "StringBuilder.h" - -using namespace ArduinoJson::Generator; -using namespace ArduinoJson::Internals; - -size_t JsonPrintable::printTo(char* buffer, size_t bufferSize) const -{ - StringBuilder sb(buffer, bufferSize); - return printTo(sb); -} - -size_t JsonPrintable::prettyPrintTo(char* buffer, size_t bufferSize) const -{ - StringBuilder sb(buffer, bufferSize); - return prettyPrintTo(sb); -} - -size_t JsonPrintable::prettyPrintTo(IndentedPrint& p) const -{ - JsonPrettyPrint prettyPrint(p); - return printTo(prettyPrint); -} - -size_t JsonPrintable::prettyPrintTo(Print& p) const -{ - IndentedPrint indentedPrint(p); - return prettyPrintTo(indentedPrint); -} \ No newline at end of file +*/ + +#include "JsonPrintable.h" +#include "JsonPrettyPrint.h" +#include "StringBuilder.h" + +using namespace ArduinoJson::Generator; +using namespace ArduinoJson::Internals; + +size_t JsonPrintable::printTo(char* buffer, size_t bufferSize) const +{ + StringBuilder sb(buffer, bufferSize); + return printTo(sb); +} + +size_t JsonPrintable::prettyPrintTo(char* buffer, size_t bufferSize) const +{ + StringBuilder sb(buffer, bufferSize); + return prettyPrintTo(sb); +} + +size_t JsonPrintable::prettyPrintTo(IndentedPrint& p) const +{ + JsonPrettyPrint prettyPrint(p); + return printTo(prettyPrint); +} + +size_t JsonPrintable::prettyPrintTo(Print& p) const +{ + IndentedPrint indentedPrint(p); + return prettyPrintTo(indentedPrint); +} diff --git a/JsonGenerator/JsonPrintable.h b/JsonGenerator/JsonPrintable.h index 3eec80e2..7d94e9de 100644 --- a/JsonGenerator/JsonPrintable.h +++ b/JsonGenerator/JsonPrintable.h @@ -1,40 +1,40 @@ -/* - * Arduino JSON library - * Benoit Blanchon 2014 - MIT License - */ - -#pragma once - -#include "Print.h" -#include "Printable.h" -#include "IndentedPrint.h" - -namespace ArduinoJson -{ - namespace Generator - { - // Contains methods to generate a JSON string. - // Implemented by both JsonObject and JsonArray - class JsonPrintable : public Printable - { - public: - - // Generates the compact JSON string and sends it to a Print stream - virtual size_t printTo(Print& p) const = 0; - - // Generates the compact JSON string and writes it in a buffer - size_t printTo(char* buffer, size_t bufferSize) const; - - // Generates the indented JSON string and sends it to a Print stream - size_t prettyPrintTo(Print& p) const; - - // Generates the indented JSON string and sends it to a IndentedPrint stream - // This overload allows a finer control of the output because you can customize - // the IndentedPrint. - size_t prettyPrintTo(IndentedPrint& p) const; - - // Generates the indented JSON string and writes it in a buffer - size_t prettyPrintTo(char* buffer, size_t bufferSize) const; - }; - } -} \ No newline at end of file +/* + * Arduino JSON library + * Benoit Blanchon 2014 - MIT License + */ + +#pragma once + +#include "Print.h" +#include "Printable.h" +#include "IndentedPrint.h" + +namespace ArduinoJson +{ + namespace Generator + { + // Contains methods to generate a JSON string. + // Implemented by both JsonObject and JsonArray + class JsonPrintable : public Printable + { + public: + + // Generates the compact JSON string and sends it to a Print stream + virtual size_t printTo(Print& p) const = 0; + + // Generates the compact JSON string and writes it in a buffer + size_t printTo(char* buffer, size_t bufferSize) const; + + // Generates the indented JSON string and sends it to a Print stream + size_t prettyPrintTo(Print& p) const; + + // Generates the indented JSON string and sends it to a IndentedPrint stream + // This overload allows a finer control of the output because you can customize + // the IndentedPrint. + size_t prettyPrintTo(IndentedPrint& p) const; + + // Generates the indented JSON string and writes it in a buffer + size_t prettyPrintTo(char* buffer, size_t bufferSize) const; + }; + } +} diff --git a/JsonGenerator/JsonValue.cpp b/JsonGenerator/JsonValue.cpp index d7de4673..8bfe4d10 100644 --- a/JsonGenerator/JsonValue.cpp +++ b/JsonGenerator/JsonValue.cpp @@ -2,32 +2,32 @@ * Arduino JSON library * Benoit Blanchon 2014 - MIT License */ - -#include "EscapedString.h" -#include "JsonValue.h" - -using namespace ArduinoJson::Generator; -using namespace ArduinoJson::Internals; - -size_t JsonValue::printBoolTo(const Content& c, Print& p) -{ - return p.print(c.asBool ? "true" : "false"); -} - -size_t JsonValue::printLongTo(const Content& c, Print& p) -{ - return p.print(c.asLong); -} - -size_t JsonValue::printPrintableTo(const Content& c, Print& p) -{ - if (c.asPrintable) - return c.asPrintable->printTo(p); - else - return p.print("null"); -} - -size_t JsonValue::printStringTo(const Content& c, Print& p) -{ - return EscapedString::printTo(c.asString, p); -} \ No newline at end of file + +#include "EscapedString.h" +#include "JsonValue.h" + +using namespace ArduinoJson::Generator; +using namespace ArduinoJson::Internals; + +size_t JsonValue::printBoolTo(const Content& c, Print& p) +{ + return p.print(c.asBool ? "true" : "false"); +} + +size_t JsonValue::printLongTo(const Content& c, Print& p) +{ + return p.print(c.asLong); +} + +size_t JsonValue::printPrintableTo(const Content& c, Print& p) +{ + if (c.asPrintable) + return c.asPrintable->printTo(p); + else + return p.print("null"); +} + +size_t JsonValue::printStringTo(const Content& c, Print& p) +{ + return EscapedString::printTo(c.asString, p); +} diff --git a/JsonGenerator/JsonValue.h b/JsonGenerator/JsonValue.h index 5c080818..b7671bbf 100644 --- a/JsonGenerator/JsonValue.h +++ b/JsonGenerator/JsonValue.h @@ -1,135 +1,135 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License - */ - -#pragma once - -#include "EscapedString.h" -#include "Printable.h" -#include "StringBuilder.h" - -namespace ArduinoJson -{ - namespace Generator - { - class JsonValue - { - public: - - void operator=(bool value) - { - printToImpl = &printBoolTo; - content.asBool = value; - } - - void operator=(long value) - { - printToImpl = &printLongTo; - content.asLong = value; - } - - void operator=(int value) - { - printToImpl = &printLongTo; - content.asLong = value; - } - - void operator=(const Printable& value) - { - printToImpl = &printPrintableTo; - content.asPrintable = &value; - } - - void operator=(const char* value) - { - printToImpl = &printStringTo; - content.asString = value; - } - - void operator=(double value) - { - set<2>(value); - } - - template - void set(double value) - { - printToImpl = &printDoubleTo < DIGITS > ; - content.asDouble = value; - } - - operator bool() - { - return content.asBool; - } - - operator const char*() - { - return content.asString; - } - - operator double() - { - return content.asDouble; - } - - operator float() - { - return (float)content.asDouble; - } - - operator int() - { - return content.asLong; - } - - operator long() - { - return content.asLong; - } - - operator const Printable&() - { - return *content.asPrintable; - } - - size_t printTo(Print& p) const - { - // handmade polymorphism - return printToImpl(content, p); - } - - void reset() - { - content.asDouble = 0; - printToImpl = printStringTo; - } - - private: - union Content - { - bool asBool; - double asDouble; - long asLong; - const Printable* asPrintable; - const char* asString; - }; - - Content content; - - size_t(*printToImpl)(const Content&, Print&); - - static size_t printBoolTo(const Content&, Print&); - static size_t printLongTo(const Content&, Print&); - static size_t printPrintableTo(const Content&, Print&); - static size_t printStringTo(const Content&, Print&); - - template - static size_t printDoubleTo(const Content& c, Print& p) - { - return p.print(c.asDouble, DIGITS); - } - }; - } -} \ No newline at end of file + */ + +#pragma once + +#include "EscapedString.h" +#include "Printable.h" +#include "StringBuilder.h" + +namespace ArduinoJson +{ + namespace Generator + { + class JsonValue + { + public: + + void operator=(bool value) + { + printToImpl = &printBoolTo; + content.asBool = value; + } + + void operator=(long value) + { + printToImpl = &printLongTo; + content.asLong = value; + } + + void operator=(int value) + { + printToImpl = &printLongTo; + content.asLong = value; + } + + void operator=(const Printable& value) + { + printToImpl = &printPrintableTo; + content.asPrintable = &value; + } + + void operator=(const char* value) + { + printToImpl = &printStringTo; + content.asString = value; + } + + void operator=(double value) + { + set<2>(value); + } + + template + void set(double value) + { + printToImpl = &printDoubleTo < DIGITS > ; + content.asDouble = value; + } + + operator bool() + { + return content.asBool; + } + + operator const char*() + { + return content.asString; + } + + operator double() + { + return content.asDouble; + } + + operator float() + { + return (float)content.asDouble; + } + + operator int() + { + return content.asLong; + } + + operator long() + { + return content.asLong; + } + + operator const Printable&() + { + return *content.asPrintable; + } + + size_t printTo(Print& p) const + { + // handmade polymorphism + return printToImpl(content, p); + } + + void reset() + { + content.asDouble = 0; + printToImpl = printStringTo; + } + + private: + union Content + { + bool asBool; + double asDouble; + long asLong; + const Printable* asPrintable; + const char* asString; + }; + + Content content; + + size_t(*printToImpl)(const Content&, Print&); + + static size_t printBoolTo(const Content&, Print&); + static size_t printLongTo(const Content&, Print&); + static size_t printPrintableTo(const Content&, Print&); + static size_t printStringTo(const Content&, Print&); + + template + static size_t printDoubleTo(const Content& c, Print& p) + { + return p.print(c.asDouble, DIGITS); + } + }; + } +} diff --git a/JsonGenerator/Print.cpp b/JsonGenerator/Print.cpp index f90cd13c..61a8f54c 100644 --- a/JsonGenerator/Print.cpp +++ b/JsonGenerator/Print.cpp @@ -5,52 +5,52 @@ #ifndef ARDUINO -#include "Print.h" -#include // for sprintf, strchr and strcat - -size_t Print::print(const char s[]) -{ - size_t n = 0; - while (*s) - { - n += write(*s++); - } - return n; -} - -static inline void ensureStringContainsPoint(char* s) -{ - // Ensures that the decimal point is present. - // For example, we don't want "0" but "0.0". - // Otherwise, the value would be considered as an integer by some parsers - // See issue #22 - - if (!strchr(s, '.')) - strcat(s, ".0"); -} - -size_t Print::print(double value, int digits) -{ - char tmp[32]; - - sprintf(tmp, "%.*lg", digits+1, value); - - if (digits>0) - ensureStringContainsPoint(tmp); - - return print(tmp); -} - -size_t Print::print(long value) -{ - char tmp[32]; - sprintf(tmp, "%ld", value); - return print(tmp); -} - -size_t Print::println() -{ - return write('\r') + write('\n'); -} - -#endif \ No newline at end of file +#include "Print.h" +#include // for sprintf, strchr and strcat + +size_t Print::print(const char s[]) +{ + size_t n = 0; + while (*s) + { + n += write(*s++); + } + return n; +} + +static inline void ensureStringContainsPoint(char* s) +{ + // Ensures that the decimal point is present. + // For example, we don't want "0" but "0.0". + // Otherwise, the value would be considered as an integer by some parsers + // See issue #22 + + if (!strchr(s, '.')) + strcat(s, ".0"); +} + +size_t Print::print(double value, int digits) +{ + char tmp[32]; + + sprintf(tmp, "%.*lg", digits+1, value); + + if (digits>0) + ensureStringContainsPoint(tmp); + + return print(tmp); +} + +size_t Print::print(long value) +{ + char tmp[32]; + sprintf(tmp, "%ld", value); + return print(tmp); +} + +size_t Print::println() +{ + return write('\r') + write('\n'); +} + +#endif diff --git a/JsonGenerator/Print.h b/JsonGenerator/Print.h index e5b3381d..e55f99f0 100644 --- a/JsonGenerator/Print.h +++ b/JsonGenerator/Print.h @@ -2,29 +2,29 @@ * Arduino JSON library * Benoit Blanchon 2014 - MIT License */ - -#pragma once - -#ifndef ARDUINO - -#include -#include - -// This class reproduces Arduino's Print -class Print -{ -public: - - virtual size_t write(uint8_t) = 0; - - size_t print(const char[]); - size_t print(double, int = 2); - size_t print(long); - size_t println(); -}; - -#else - -#include - -#endif + +#pragma once + +#ifndef ARDUINO + +#include +#include + +// This class reproduces Arduino's Print +class Print +{ +public: + + virtual size_t write(uint8_t) = 0; + + size_t print(const char[]); + size_t print(double, int = 2); + size_t print(long); + size_t println(); +}; + +#else + +#include + +#endif diff --git a/JsonGenerator/Printable.h b/JsonGenerator/Printable.h index c953b1e5..b0b8baa9 100644 --- a/JsonGenerator/Printable.h +++ b/JsonGenerator/Printable.h @@ -2,23 +2,23 @@ * Arduino JSON library * Benoit Blanchon 2014 - MIT License */ - -#pragma once - -#ifndef ARDUINO - -class Print; - -class Printable -{ -public: - - virtual size_t printTo(Print& p) const = 0; -}; - -#else - -#include - -#endif - + +#pragma once + +#ifndef ARDUINO + +class Print; + +class Printable +{ +public: + + virtual size_t printTo(Print& p) const = 0; +}; + +#else + +#include + +#endif + diff --git a/JsonGenerator/StringBuilder.cpp b/JsonGenerator/StringBuilder.cpp index debf4fd7..ed372c43 100644 --- a/JsonGenerator/StringBuilder.cpp +++ b/JsonGenerator/StringBuilder.cpp @@ -1,17 +1,17 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License - */ - -#include "StringBuilder.h" - -using namespace ArduinoJson::Internals; - -size_t StringBuilder::write(uint8_t c) -{ - if (length >= capacity) return 0; - - buffer[length++] = c; - buffer[length] = 0; - return 1; -} \ No newline at end of file + */ + +#include "StringBuilder.h" + +using namespace ArduinoJson::Internals; + +size_t StringBuilder::write(uint8_t c) +{ + if (length >= capacity) return 0; + + buffer[length++] = c; + buffer[length] = 0; + return 1; +} diff --git a/JsonGenerator/StringBuilder.h b/JsonGenerator/StringBuilder.h index 3e0bc96f..4523cdde 100644 --- a/JsonGenerator/StringBuilder.h +++ b/JsonGenerator/StringBuilder.h @@ -1,31 +1,31 @@ -/* - * Arduino JSON library - * Benoit Blanchon 2014 - MIT License - */ - -#pragma once - -#include "Print.h" - -namespace ArduinoJson -{ - namespace Internals - { - class StringBuilder : public Print - { - public: - StringBuilder(char* buf, int size) - : buffer(buf), capacity(size - 1), length(0) - { - buffer[0] = 0; - } - - virtual size_t write(uint8_t c); - - private: - char* buffer; - int capacity; - int length; - }; - } -} \ No newline at end of file +/* + * Arduino JSON library + * Benoit Blanchon 2014 - MIT License + */ + +#pragma once + +#include "Print.h" + +namespace ArduinoJson +{ + namespace Internals + { + class StringBuilder : public Print + { + public: + StringBuilder(char* buf, int size) + : buffer(buf), capacity(size - 1), length(0) + { + buffer[0] = 0; + } + + virtual size_t write(uint8_t c); + + private: + char* buffer; + int capacity; + int length; + }; + } +} diff --git a/JsonGeneratorTests/EscapedStringTests.cpp b/JsonGeneratorTests/EscapedStringTests.cpp index ea0c7980..6a9c2dd2 100644 --- a/JsonGeneratorTests/EscapedStringTests.cpp +++ b/JsonGeneratorTests/EscapedStringTests.cpp @@ -1,95 +1,95 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#include "CppUnitTest.h" -#include "EscapedString.h" -#include "StringBuilder.h" - -using namespace Microsoft::VisualStudio::CppUnitTestFramework; -using namespace ArduinoJson::Internals; - -namespace JsonGeneratorTests -{ - TEST_CLASS(EscapedStringTests) - { - char buffer[1024]; - size_t returnValue; - - public: - - TEST_METHOD(Null) - { - whenInputIs(0); - outputMustBe("null"); - } - - TEST_METHOD(EmptyString) - { - whenInputIs(""); - outputMustBe("\"\""); - } - - TEST_METHOD(QuotationMark) - { - whenInputIs("\""); - outputMustBe("\"\\\"\""); - } - - TEST_METHOD(ReverseSolidus) - { - whenInputIs("\\"); - outputMustBe("\"\\\\\""); - } - - TEST_METHOD(Solidus) - { - whenInputIs("/"); - outputMustBe("\"/\""); // but the JSON format allows \/ - } - - TEST_METHOD(Backspace) - { - whenInputIs("\b"); - outputMustBe("\"\\b\""); - } - - TEST_METHOD(Formfeed) - { - whenInputIs("\f"); - outputMustBe("\"\\f\""); - } - - TEST_METHOD(Newline) - { - whenInputIs("\n"); - outputMustBe("\"\\n\""); - } - - TEST_METHOD(CarriageReturn) - { - whenInputIs("\r"); - outputMustBe("\"\\r\""); - } - - TEST_METHOD(HorizontalTab) - { - whenInputIs("\t"); - outputMustBe("\"\\t\""); - } - - private: - void whenInputIs(const char* input) - { - StringBuilder sb(buffer, sizeof(buffer)); - returnValue = EscapedString::printTo(input, sb); - } - - void outputMustBe(const char* expected) - { - Assert::AreEqual(expected, buffer); - Assert::AreEqual(strlen(expected), returnValue); - } - }; -} \ No newline at end of file +*/ + +#include "CppUnitTest.h" +#include "EscapedString.h" +#include "StringBuilder.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace ArduinoJson::Internals; + +namespace JsonGeneratorTests +{ + TEST_CLASS(EscapedStringTests) + { + char buffer[1024]; + size_t returnValue; + + public: + + TEST_METHOD(Null) + { + whenInputIs(0); + outputMustBe("null"); + } + + TEST_METHOD(EmptyString) + { + whenInputIs(""); + outputMustBe("\"\""); + } + + TEST_METHOD(QuotationMark) + { + whenInputIs("\""); + outputMustBe("\"\\\"\""); + } + + TEST_METHOD(ReverseSolidus) + { + whenInputIs("\\"); + outputMustBe("\"\\\\\""); + } + + TEST_METHOD(Solidus) + { + whenInputIs("/"); + outputMustBe("\"/\""); // but the JSON format allows \/ + } + + TEST_METHOD(Backspace) + { + whenInputIs("\b"); + outputMustBe("\"\\b\""); + } + + TEST_METHOD(Formfeed) + { + whenInputIs("\f"); + outputMustBe("\"\\f\""); + } + + TEST_METHOD(Newline) + { + whenInputIs("\n"); + outputMustBe("\"\\n\""); + } + + TEST_METHOD(CarriageReturn) + { + whenInputIs("\r"); + outputMustBe("\"\\r\""); + } + + TEST_METHOD(HorizontalTab) + { + whenInputIs("\t"); + outputMustBe("\"\\t\""); + } + + private: + void whenInputIs(const char* input) + { + StringBuilder sb(buffer, sizeof(buffer)); + returnValue = EscapedString::printTo(input, sb); + } + + void outputMustBe(const char* expected) + { + Assert::AreEqual(expected, buffer); + Assert::AreEqual(strlen(expected), returnValue); + } + }; +} diff --git a/JsonGeneratorTests/Issue10.cpp b/JsonGeneratorTests/Issue10.cpp index 5ecfcc49..7cdd5547 100644 --- a/JsonGeneratorTests/Issue10.cpp +++ b/JsonGeneratorTests/Issue10.cpp @@ -1,73 +1,73 @@ -#include "CppUnitTest.h" -#include "JsonArray.h" -#include "JsonObject.h" - -using namespace ArduinoJson::Generator; -using namespace Microsoft::VisualStudio::CppUnitTestFramework; - -namespace JsonGeneratorTests -{ - TEST_CLASS(Issue10) - { - struct Person { - int id; - char name[32]; - }; - - Person persons[2]; - - public: - - TEST_METHOD_INITIALIZE(Initialize) - { - Person boss; - boss.id = 1; - strcpy(boss.name, "Jeff"); - Person employee; - employee.id = 2; - strcpy(employee.name, "John"); - persons[0] = boss; - persons[1] = employee; - } - - TEST_METHOD(WrongWayToAddObjectInAnArray) - { - JsonArray<2> json; - - for (int i = 0; i < 2; i++) - { - JsonObject<2> object; - - object["id"] = persons[i].id; - object["name"] = persons[i].name; - - json.add(object); // <- Adding a reference to a temporary variable - } - - char buffer[256]; - json.printTo(buffer, sizeof(buffer)); - - // the same values are repeated, that's normal - Assert::AreEqual("[{\"id\":2,\"name\":\"John\"},{\"id\":2,\"name\":\"John\"}]", buffer); - } - - TEST_METHOD(RightWayToAddObjectInAnArray) - { - JsonArray<2> json; - JsonObject<2> object[2]; - - for (int i = 0; i < 2; i++) - { - object[i]["id"] = persons[i].id; - object[i]["name"] = persons[i].name; - - json.add(object[i]); - } - - char buffer[256]; - json.printTo(buffer, sizeof(buffer)); - - Assert::AreEqual("[{\"id\":1,\"name\":\"Jeff\"},{\"id\":2,\"name\":\"John\"}]", buffer); - } - }; -} \ No newline at end of file +#include "CppUnitTest.h" +#include "JsonArray.h" +#include "JsonObject.h" + +using namespace ArduinoJson::Generator; +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +namespace JsonGeneratorTests +{ + TEST_CLASS(Issue10) + { + struct Person { + int id; + char name[32]; + }; + + Person persons[2]; + + public: + + TEST_METHOD_INITIALIZE(Initialize) + { + Person boss; + boss.id = 1; + strcpy(boss.name, "Jeff"); + Person employee; + employee.id = 2; + strcpy(employee.name, "John"); + persons[0] = boss; + persons[1] = employee; + } + + TEST_METHOD(WrongWayToAddObjectInAnArray) + { + JsonArray<2> json; + + for (int i = 0; i < 2; i++) + { + JsonObject<2> object; + + object["id"] = persons[i].id; + object["name"] = persons[i].name; + + json.add(object); // <- Adding a reference to a temporary variable + } + + char buffer[256]; + json.printTo(buffer, sizeof(buffer)); + + // the same values are repeated, that's normal + Assert::AreEqual("[{\"id\":2,\"name\":\"John\"},{\"id\":2,\"name\":\"John\"}]", buffer); + } + + TEST_METHOD(RightWayToAddObjectInAnArray) + { + JsonArray<2> json; + JsonObject<2> object[2]; + + for (int i = 0; i < 2; i++) + { + object[i]["id"] = persons[i].id; + object[i]["name"] = persons[i].name; + + json.add(object[i]); + } + + char buffer[256]; + json.printTo(buffer, sizeof(buffer)); + + Assert::AreEqual("[{\"id\":1,\"name\":\"Jeff\"},{\"id\":2,\"name\":\"John\"}]", buffer); + } + }; +} diff --git a/JsonGeneratorTests/JsonArrayTests.cpp b/JsonGeneratorTests/JsonArrayTests.cpp index 5dcdd96a..3b3f21f2 100644 --- a/JsonGeneratorTests/JsonArrayTests.cpp +++ b/JsonGeneratorTests/JsonArrayTests.cpp @@ -1,162 +1,162 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#include "CppUnitTest.h" -#include "JsonArray.h" -#include "JsonObject.h" - -using namespace Microsoft::VisualStudio::CppUnitTestFramework; -using namespace ArduinoJson::Generator; - -namespace JsonGeneratorTests -{ - TEST_CLASS(JsonArrayTests) - { - JsonArray<2> array; - char buffer[256]; - - public: - - TEST_METHOD(Empty) - { - outputMustBe("[]"); - } - - TEST_METHOD(Null) - { - array.add((char*) 0); - - outputMustBe("[null]"); - } - - TEST_METHOD(OneString) - { - array.add("hello"); - - outputMustBe("[\"hello\"]"); - } - - TEST_METHOD(TwoStrings) - { - array.add("hello"); - array.add("world"); - - outputMustBe("[\"hello\",\"world\"]"); - } - - TEST_METHOD(OneStringOverCapacity) - { - array.add("hello"); - array.add("world"); - array.add("lost"); - - outputMustBe("[\"hello\",\"world\"]"); - } - - TEST_METHOD(OneDoubleDefaultDigits) - { - array.add(3.14159265358979323846); - outputMustBe("[3.14]"); - } - - TEST_METHOD(OneDoubleFourDigits) - { - array.add<4>(3.14159265358979323846); - outputMustBe("[3.1416]"); - } - - TEST_METHOD(OneInteger) - { - array.add(1); - - outputMustBe("[1]"); - } - - TEST_METHOD(TwoIntegers) - { - array.add(1); - array.add(2); - - outputMustBe("[1,2]"); - } - - TEST_METHOD(OneIntegerOverCapacity) - { - array.add(1); - array.add(2); - array.add(3); - - outputMustBe("[1,2]"); - } - - TEST_METHOD(OneTrue) - { - array.add(true); - - outputMustBe("[true]"); - } - - TEST_METHOD(OneFalse) - { - array.add(false); - - outputMustBe("[false]"); - } - - TEST_METHOD(TwoBooleans) - { - array.add(false); - array.add(true); - - outputMustBe("[false,true]"); - } - - TEST_METHOD(OneBooleanOverCapacity) - { - array.add(false); - array.add(true); - array.add(false); - - outputMustBe("[false,true]"); - } - - TEST_METHOD(OneEmptyNestedArray) - { - JsonArray<1> nestedArray; - - array.add(nestedArray); - - outputMustBe("[[]]"); - } - - TEST_METHOD(OneEmptyNestedHash) - { - JsonObject<1> nestedObject; - - array.add(nestedObject); - - outputMustBe("[{}]"); - } - - TEST_METHOD(OneNestedArrayWithOneInteger) - { - JsonArray<1> nestedArray; - nestedArray.add(1); - - array.add(nestedArray); - - outputMustBe("[[1]]"); - } - - private: - - void outputMustBe(const char* expected) - { - size_t n = array.printTo(buffer, sizeof(buffer)); - Assert::AreEqual(expected, buffer); - Assert::AreEqual(strlen(expected), n); - } - }; -} \ No newline at end of file +*/ + +#include "CppUnitTest.h" +#include "JsonArray.h" +#include "JsonObject.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace ArduinoJson::Generator; + +namespace JsonGeneratorTests +{ + TEST_CLASS(JsonArrayTests) + { + JsonArray<2> array; + char buffer[256]; + + public: + + TEST_METHOD(Empty) + { + outputMustBe("[]"); + } + + TEST_METHOD(Null) + { + array.add((char*) 0); + + outputMustBe("[null]"); + } + + TEST_METHOD(OneString) + { + array.add("hello"); + + outputMustBe("[\"hello\"]"); + } + + TEST_METHOD(TwoStrings) + { + array.add("hello"); + array.add("world"); + + outputMustBe("[\"hello\",\"world\"]"); + } + + TEST_METHOD(OneStringOverCapacity) + { + array.add("hello"); + array.add("world"); + array.add("lost"); + + outputMustBe("[\"hello\",\"world\"]"); + } + + TEST_METHOD(OneDoubleDefaultDigits) + { + array.add(3.14159265358979323846); + outputMustBe("[3.14]"); + } + + TEST_METHOD(OneDoubleFourDigits) + { + array.add<4>(3.14159265358979323846); + outputMustBe("[3.1416]"); + } + + TEST_METHOD(OneInteger) + { + array.add(1); + + outputMustBe("[1]"); + } + + TEST_METHOD(TwoIntegers) + { + array.add(1); + array.add(2); + + outputMustBe("[1,2]"); + } + + TEST_METHOD(OneIntegerOverCapacity) + { + array.add(1); + array.add(2); + array.add(3); + + outputMustBe("[1,2]"); + } + + TEST_METHOD(OneTrue) + { + array.add(true); + + outputMustBe("[true]"); + } + + TEST_METHOD(OneFalse) + { + array.add(false); + + outputMustBe("[false]"); + } + + TEST_METHOD(TwoBooleans) + { + array.add(false); + array.add(true); + + outputMustBe("[false,true]"); + } + + TEST_METHOD(OneBooleanOverCapacity) + { + array.add(false); + array.add(true); + array.add(false); + + outputMustBe("[false,true]"); + } + + TEST_METHOD(OneEmptyNestedArray) + { + JsonArray<1> nestedArray; + + array.add(nestedArray); + + outputMustBe("[[]]"); + } + + TEST_METHOD(OneEmptyNestedHash) + { + JsonObject<1> nestedObject; + + array.add(nestedObject); + + outputMustBe("[{}]"); + } + + TEST_METHOD(OneNestedArrayWithOneInteger) + { + JsonArray<1> nestedArray; + nestedArray.add(1); + + array.add(nestedArray); + + outputMustBe("[[1]]"); + } + + private: + + void outputMustBe(const char* expected) + { + size_t n = array.printTo(buffer, sizeof(buffer)); + Assert::AreEqual(expected, buffer); + Assert::AreEqual(strlen(expected), n); + } + }; +} diff --git a/JsonGeneratorTests/JsonObject_Indexer_Tests.cpp b/JsonGeneratorTests/JsonObject_Indexer_Tests.cpp index f3138331..0e93d796 100644 --- a/JsonGeneratorTests/JsonObject_Indexer_Tests.cpp +++ b/JsonGeneratorTests/JsonObject_Indexer_Tests.cpp @@ -1,73 +1,73 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#include "CppUnitTest.h" -#include "JsonArray.h" -#include "JsonObject.h" - -using namespace Microsoft::VisualStudio::CppUnitTestFramework; -using namespace ArduinoJson::Generator; - -namespace JsonGeneratorTests -{ - TEST_CLASS(JsonObject_Indexer_Tests) - { - JsonObject<2> object; - - public: - - TEST_METHOD(Empty) - { - mustNotContain("key"); - } - - TEST_METHOD(TwoStrings) - { - object["key1"] = "value1"; - object["key2"] = "value2"; - - mustContain("key1", "value1"); - mustContain("key2", "value2"); - } - - TEST_METHOD(RemoveFirst) - { - object["key1"] = "value1"; - object["key2"] = "value2"; - object.remove("key1"); - - mustNotContain("key1"); - mustContain("key2", "value2"); - } - - TEST_METHOD(RemoveLast) - { - object["key1"] = "value1"; - object["key2"] = "value2"; - object.remove("key2"); - - mustContain("key1", "value1"); - mustNotContain("key2"); - } - - private: - - void mustContain(const char* key, const char* expected) - { - Assert::IsTrue(object.containsKey(key)); - - const char* actual = object[key]; - Assert::AreEqual(expected, actual); - } - - void mustNotContain(const char* key) - { - Assert::IsFalse(object.containsKey(key)); - - const char* actual = object[key]; - Assert::IsNull(actual); - } - }; -} \ No newline at end of file +*/ + +#include "CppUnitTest.h" +#include "JsonArray.h" +#include "JsonObject.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace ArduinoJson::Generator; + +namespace JsonGeneratorTests +{ + TEST_CLASS(JsonObject_Indexer_Tests) + { + JsonObject<2> object; + + public: + + TEST_METHOD(Empty) + { + mustNotContain("key"); + } + + TEST_METHOD(TwoStrings) + { + object["key1"] = "value1"; + object["key2"] = "value2"; + + mustContain("key1", "value1"); + mustContain("key2", "value2"); + } + + TEST_METHOD(RemoveFirst) + { + object["key1"] = "value1"; + object["key2"] = "value2"; + object.remove("key1"); + + mustNotContain("key1"); + mustContain("key2", "value2"); + } + + TEST_METHOD(RemoveLast) + { + object["key1"] = "value1"; + object["key2"] = "value2"; + object.remove("key2"); + + mustContain("key1", "value1"); + mustNotContain("key2"); + } + + private: + + void mustContain(const char* key, const char* expected) + { + Assert::IsTrue(object.containsKey(key)); + + const char* actual = object[key]; + Assert::AreEqual(expected, actual); + } + + void mustNotContain(const char* key) + { + Assert::IsFalse(object.containsKey(key)); + + const char* actual = object[key]; + Assert::IsNull(actual); + } + }; +} diff --git a/JsonGeneratorTests/JsonObject_PrintTo_Tests.cpp b/JsonGeneratorTests/JsonObject_PrintTo_Tests.cpp index 277ab2f0..ac818dbc 100644 --- a/JsonGeneratorTests/JsonObject_PrintTo_Tests.cpp +++ b/JsonGeneratorTests/JsonObject_PrintTo_Tests.cpp @@ -1,152 +1,152 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#include "CppUnitTest.h" -#include "JsonArray.h" -#include "JsonObject.h" - -using namespace Microsoft::VisualStudio::CppUnitTestFramework; -using namespace ArduinoJson::Generator; - -namespace JsonGeneratorTests -{ - TEST_CLASS(JsonObject_PrintTo_Tests) - { - JsonObject<2> object; - - public: - - TEST_METHOD(Empty) - { - outputMustBe("{}"); - } - - TEST_METHOD(OneString) - { - object["key"] = "value"; - - outputMustBe("{\"key\":\"value\"}"); - } - - TEST_METHOD(TwoStrings) - { - object["key1"] = "value1"; - object["key2"] = "value2"; - - outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}"); - } - - TEST_METHOD(RemoveFirst) - { - object["key1"] = "value1"; - object["key2"] = "value2"; - object.remove("key1"); - - outputMustBe("{\"key2\":\"value2\"}"); - } - - TEST_METHOD(RemoveLast) - { - object["key1"] = "value1"; - object["key2"] = "value2"; - object.remove("key2"); - - outputMustBe("{\"key1\":\"value1\"}"); - } - - TEST_METHOD(RemoveUnexistingKey) - { - object["key1"] = "value1"; - object["key2"] = "value2"; - object.remove("key3"); - - outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}"); - } - - TEST_METHOD(ReplaceExistingKey) - { - object["key"] = "value1"; - object["key"] = "value2"; - - outputMustBe("{\"key\":\"value2\"}"); - } - - TEST_METHOD(OneStringOverCapacity) - { - object["key1"] = "value1"; - object["key2"] = "value2"; - object["key3"] = "value3"; - - outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}"); - } - - TEST_METHOD(OneInteger) - { - object["key"] = 1; - outputMustBe("{\"key\":1}"); - } - - TEST_METHOD(OneDoubleFourDigits) - { - object["key"].set<4>(3.14159265358979323846); - outputMustBe("{\"key\":3.1416}"); - } - - TEST_METHOD(OneDoubleDefaultDigits) - { - object["key"] = 3.14159265358979323846; - outputMustBe("{\"key\":3.14}"); - } - - TEST_METHOD(OneNull) - { - object["key"] = (char*) 0; - outputMustBe("{\"key\":null}"); - } - - TEST_METHOD(OneTrue) - { - object["key"] = true; - outputMustBe("{\"key\":true}"); - } - - TEST_METHOD(OneFalse) - { - object["key"] = false; - outputMustBe("{\"key\":false}"); - } - - TEST_METHOD(OneEmptyNestedArray) - { - auto nestedArray = JsonArray<1>(); - - object["key"] = nestedArray; - - outputMustBe("{\"key\":[]}"); - } - - TEST_METHOD(OneEmptyNestedObject) - { - auto nestedObject = JsonObject<1>(); - - object["key"] = nestedObject; - - outputMustBe("{\"key\":{}}"); - } - - private: - - void outputMustBe(const char* expected) - { - char buffer[256]; - size_t result; - - result = object.printTo(buffer, sizeof(buffer)); - - Assert::AreEqual(strlen(expected), result); - Assert::AreEqual(expected, buffer); - } - }; -} \ No newline at end of file +*/ + +#include "CppUnitTest.h" +#include "JsonArray.h" +#include "JsonObject.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace ArduinoJson::Generator; + +namespace JsonGeneratorTests +{ + TEST_CLASS(JsonObject_PrintTo_Tests) + { + JsonObject<2> object; + + public: + + TEST_METHOD(Empty) + { + outputMustBe("{}"); + } + + TEST_METHOD(OneString) + { + object["key"] = "value"; + + outputMustBe("{\"key\":\"value\"}"); + } + + TEST_METHOD(TwoStrings) + { + object["key1"] = "value1"; + object["key2"] = "value2"; + + outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}"); + } + + TEST_METHOD(RemoveFirst) + { + object["key1"] = "value1"; + object["key2"] = "value2"; + object.remove("key1"); + + outputMustBe("{\"key2\":\"value2\"}"); + } + + TEST_METHOD(RemoveLast) + { + object["key1"] = "value1"; + object["key2"] = "value2"; + object.remove("key2"); + + outputMustBe("{\"key1\":\"value1\"}"); + } + + TEST_METHOD(RemoveUnexistingKey) + { + object["key1"] = "value1"; + object["key2"] = "value2"; + object.remove("key3"); + + outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}"); + } + + TEST_METHOD(ReplaceExistingKey) + { + object["key"] = "value1"; + object["key"] = "value2"; + + outputMustBe("{\"key\":\"value2\"}"); + } + + TEST_METHOD(OneStringOverCapacity) + { + object["key1"] = "value1"; + object["key2"] = "value2"; + object["key3"] = "value3"; + + outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}"); + } + + TEST_METHOD(OneInteger) + { + object["key"] = 1; + outputMustBe("{\"key\":1}"); + } + + TEST_METHOD(OneDoubleFourDigits) + { + object["key"].set<4>(3.14159265358979323846); + outputMustBe("{\"key\":3.1416}"); + } + + TEST_METHOD(OneDoubleDefaultDigits) + { + object["key"] = 3.14159265358979323846; + outputMustBe("{\"key\":3.14}"); + } + + TEST_METHOD(OneNull) + { + object["key"] = (char*) 0; + outputMustBe("{\"key\":null}"); + } + + TEST_METHOD(OneTrue) + { + object["key"] = true; + outputMustBe("{\"key\":true}"); + } + + TEST_METHOD(OneFalse) + { + object["key"] = false; + outputMustBe("{\"key\":false}"); + } + + TEST_METHOD(OneEmptyNestedArray) + { + auto nestedArray = JsonArray<1>(); + + object["key"] = nestedArray; + + outputMustBe("{\"key\":[]}"); + } + + TEST_METHOD(OneEmptyNestedObject) + { + auto nestedObject = JsonObject<1>(); + + object["key"] = nestedObject; + + outputMustBe("{\"key\":{}}"); + } + + private: + + void outputMustBe(const char* expected) + { + char buffer[256]; + size_t result; + + result = object.printTo(buffer, sizeof(buffer)); + + Assert::AreEqual(strlen(expected), result); + Assert::AreEqual(expected, buffer); + } + }; +} diff --git a/JsonGeneratorTests/JsonValue_Cast_Tests.cpp b/JsonGeneratorTests/JsonValue_Cast_Tests.cpp index a863f9a5..2266bdb5 100644 --- a/JsonGeneratorTests/JsonValue_Cast_Tests.cpp +++ b/JsonGeneratorTests/JsonValue_Cast_Tests.cpp @@ -1,78 +1,78 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#include "CppUnitTest.h" -#include "StringBuilder.h" -#include "JsonValue.h" -#include "JsonArray.h" - -using namespace Microsoft::VisualStudio::CppUnitTestFramework; -using namespace ArduinoJson::Generator; -using namespace ArduinoJson::Internals; - -namespace JsonGeneratorTests -{ - TEST_CLASS(JsonValue_Cast_Tests) - { - JsonValue value; - - public: - - TEST_METHOD(Bool) - { - setValueAndCheckCast(true); - setValueAndCheckCast(false); - } - - TEST_METHOD(Double) - { - setValueAndCheckCast(3.14156); - } - - TEST_METHOD(Float) - { - setValueAndCheckCast(3.14f); - } - - TEST_METHOD(Integer) - { - setValueAndCheckCast(42); - } - - TEST_METHOD(Long) - { - setValueAndCheckCast(42L); - } - - TEST_METHOD(Array) - { - JsonArray<2> array; - setValueAndCheckCast(array); - } - - TEST_METHOD(String) - { - setValueAndCheckCast("hello"); - } - - private: - - template - void setValueAndCheckCast(T expected) - { - value = expected; - T actual = value; - Assert::AreEqual(expected, actual); - } - - template - void setValueAndCheckCast(JsonArray& expected) - { - value = expected; - const Printable& actual = value; - Assert::AreEqual((void*) &expected, (void*) &actual); - } - }; -} \ No newline at end of file +*/ + +#include "CppUnitTest.h" +#include "StringBuilder.h" +#include "JsonValue.h" +#include "JsonArray.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace ArduinoJson::Generator; +using namespace ArduinoJson::Internals; + +namespace JsonGeneratorTests +{ + TEST_CLASS(JsonValue_Cast_Tests) + { + JsonValue value; + + public: + + TEST_METHOD(Bool) + { + setValueAndCheckCast(true); + setValueAndCheckCast(false); + } + + TEST_METHOD(Double) + { + setValueAndCheckCast(3.14156); + } + + TEST_METHOD(Float) + { + setValueAndCheckCast(3.14f); + } + + TEST_METHOD(Integer) + { + setValueAndCheckCast(42); + } + + TEST_METHOD(Long) + { + setValueAndCheckCast(42L); + } + + TEST_METHOD(Array) + { + JsonArray<2> array; + setValueAndCheckCast(array); + } + + TEST_METHOD(String) + { + setValueAndCheckCast("hello"); + } + + private: + + template + void setValueAndCheckCast(T expected) + { + value = expected; + T actual = value; + Assert::AreEqual(expected, actual); + } + + template + void setValueAndCheckCast(JsonArray& expected) + { + value = expected; + const Printable& actual = value; + Assert::AreEqual((void*) &expected, (void*) &actual); + } + }; +} diff --git a/JsonGeneratorTests/JsonValue_PrintTo_Tests.cpp b/JsonGeneratorTests/JsonValue_PrintTo_Tests.cpp index f37c03c8..87c8380d 100644 --- a/JsonGeneratorTests/JsonValue_PrintTo_Tests.cpp +++ b/JsonGeneratorTests/JsonValue_PrintTo_Tests.cpp @@ -1,109 +1,109 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#include "CppUnitTest.h" -#include "StringBuilder.h" -#include "JsonValue.h" - -using namespace Microsoft::VisualStudio::CppUnitTestFramework; -using namespace ArduinoJson::Generator; -using namespace ArduinoJson::Internals; - -namespace JsonGeneratorTests -{ - TEST_CLASS(JsonValue_PrintTo_Tests) - { - char buffer[1024]; - size_t returnValue; - - public: - - TEST_METHOD(String) - { - setValueTo("hello"); - outputMustBe("\"hello\""); - } - - TEST_METHOD(ZeroFloat) - { - setValueTo(0.0f); - outputMustBe("0.0"); - } - - TEST_METHOD(Float) - { - setValueTo(3.1415f); - outputMustBe("3.14"); - } - - TEST_METHOD(DoubleZeroDigits) - { - setValueTo<0>(3.14159265358979323846); - outputMustBe("3"); - } - - TEST_METHOD(DoubleOneDigit) - { - setValueTo<1>(3.14159265358979323846); - outputMustBe("3.1"); - } - - TEST_METHOD(DoubleTwoDigits) - { - setValueTo<2>(3.14159265358979323846); - outputMustBe("3.14"); - } - - TEST_METHOD(Integer) - { - setValueTo(314); - outputMustBe("314"); - } - - TEST_METHOD(Char) - { - setValueTo('A'); - outputMustBe("65"); - } - - TEST_METHOD(Short) - { - setValueTo((short)314); - outputMustBe("314"); - } - - TEST_METHOD(Long) - { - setValueTo(314159265L); - outputMustBe("314159265"); - } - - private: - - template - void setValueTo(double value) - { - StringBuilder sb(buffer, sizeof(buffer)); - JsonValue jsonValue; - jsonValue.set(value); - returnValue = jsonValue.printTo(sb); - } - - template - void setValueTo(T value) - { - StringBuilder sb(buffer, sizeof(buffer)); - JsonValue jsonValue; - jsonValue = value; - returnValue = jsonValue.printTo(sb); - } - - void outputMustBe(const char* expected) - { - Assert::AreEqual(expected, buffer); - Assert::AreEqual(strlen(expected), returnValue); - } - }; -} \ No newline at end of file +*/ + +#include "CppUnitTest.h" +#include "StringBuilder.h" +#include "JsonValue.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace ArduinoJson::Generator; +using namespace ArduinoJson::Internals; + +namespace JsonGeneratorTests +{ + TEST_CLASS(JsonValue_PrintTo_Tests) + { + char buffer[1024]; + size_t returnValue; + + public: + + TEST_METHOD(String) + { + setValueTo("hello"); + outputMustBe("\"hello\""); + } + + TEST_METHOD(ZeroFloat) + { + setValueTo(0.0f); + outputMustBe("0.0"); + } + + TEST_METHOD(Float) + { + setValueTo(3.1415f); + outputMustBe("3.14"); + } + + TEST_METHOD(DoubleZeroDigits) + { + setValueTo<0>(3.14159265358979323846); + outputMustBe("3"); + } + + TEST_METHOD(DoubleOneDigit) + { + setValueTo<1>(3.14159265358979323846); + outputMustBe("3.1"); + } + + TEST_METHOD(DoubleTwoDigits) + { + setValueTo<2>(3.14159265358979323846); + outputMustBe("3.14"); + } + + TEST_METHOD(Integer) + { + setValueTo(314); + outputMustBe("314"); + } + + TEST_METHOD(Char) + { + setValueTo('A'); + outputMustBe("65"); + } + + TEST_METHOD(Short) + { + setValueTo((short)314); + outputMustBe("314"); + } + + TEST_METHOD(Long) + { + setValueTo(314159265L); + outputMustBe("314159265"); + } + + private: + + template + void setValueTo(double value) + { + StringBuilder sb(buffer, sizeof(buffer)); + JsonValue jsonValue; + jsonValue.set(value); + returnValue = jsonValue.printTo(sb); + } + + template + void setValueTo(T value) + { + StringBuilder sb(buffer, sizeof(buffer)); + JsonValue jsonValue; + jsonValue = value; + returnValue = jsonValue.printTo(sb); + } + + void outputMustBe(const char* expected) + { + Assert::AreEqual(expected, buffer); + Assert::AreEqual(strlen(expected), returnValue); + } + }; +} diff --git a/JsonGeneratorTests/PrettyPrint_Array_Tests.cpp b/JsonGeneratorTests/PrettyPrint_Array_Tests.cpp index 82d1760a..b769a3ff 100644 --- a/JsonGeneratorTests/PrettyPrint_Array_Tests.cpp +++ b/JsonGeneratorTests/PrettyPrint_Array_Tests.cpp @@ -1,91 +1,91 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#include "CppUnitTest.h" -#include "JsonPrettyPrint.h" -#include "StringBuilder.h" - -using namespace ArduinoJson::Internals; -using namespace ArduinoJson::Generator; -using namespace Microsoft::VisualStudio::CppUnitTestFramework; - -namespace JsonGeneratorTests -{ - TEST_CLASS(PrettyPrint_Array_Tests) - { - char buffer[1024]; - size_t returnValue; - - public: - - TEST_METHOD(EmptyArray) - { - whenInputIs("[]"); - outputMustBe("[]"); - } - - TEST_METHOD(OneElement) - { - whenInputIs("[1]"); - outputMustBe( - "[\r\n" - " 1\r\n" - "]"); - } - - TEST_METHOD(TwoElements) - { - whenInputIs("[1,2]"); - outputMustBe( - "[\r\n" - " 1,\r\n" - " 2\r\n" - "]"); - } - - TEST_METHOD(EmptyNestedArrays) - { - whenInputIs("[[],[]]"); - outputMustBe( - "[\r\n" - " [],\r\n" - " []\r\n" - "]"); - } - - TEST_METHOD(NestedArrays) - { - whenInputIs("[[1,2],[3,4]]"); - outputMustBe( - "[\r\n" - " [\r\n" - " 1,\r\n" - " 2\r\n" - " ],\r\n" - " [\r\n" - " 3,\r\n" - " 4\r\n" - " ]\r\n" - "]"); - } - - private: - - void whenInputIs(const char input[]) - { - StringBuilder sb(buffer, sizeof(buffer)); - IndentedPrint indentedPrint(sb); - JsonPrettyPrint decorator(indentedPrint); - - returnValue = decorator.print(input); - } - - void outputMustBe(const char* expected) - { - Assert::AreEqual(expected, buffer); - Assert::AreEqual(strlen(expected), returnValue); - } - }; -} \ No newline at end of file +*/ + +#include "CppUnitTest.h" +#include "JsonPrettyPrint.h" +#include "StringBuilder.h" + +using namespace ArduinoJson::Internals; +using namespace ArduinoJson::Generator; +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +namespace JsonGeneratorTests +{ + TEST_CLASS(PrettyPrint_Array_Tests) + { + char buffer[1024]; + size_t returnValue; + + public: + + TEST_METHOD(EmptyArray) + { + whenInputIs("[]"); + outputMustBe("[]"); + } + + TEST_METHOD(OneElement) + { + whenInputIs("[1]"); + outputMustBe( + "[\r\n" + " 1\r\n" + "]"); + } + + TEST_METHOD(TwoElements) + { + whenInputIs("[1,2]"); + outputMustBe( + "[\r\n" + " 1,\r\n" + " 2\r\n" + "]"); + } + + TEST_METHOD(EmptyNestedArrays) + { + whenInputIs("[[],[]]"); + outputMustBe( + "[\r\n" + " [],\r\n" + " []\r\n" + "]"); + } + + TEST_METHOD(NestedArrays) + { + whenInputIs("[[1,2],[3,4]]"); + outputMustBe( + "[\r\n" + " [\r\n" + " 1,\r\n" + " 2\r\n" + " ],\r\n" + " [\r\n" + " 3,\r\n" + " 4\r\n" + " ]\r\n" + "]"); + } + + private: + + void whenInputIs(const char input[]) + { + StringBuilder sb(buffer, sizeof(buffer)); + IndentedPrint indentedPrint(sb); + JsonPrettyPrint decorator(indentedPrint); + + returnValue = decorator.print(input); + } + + void outputMustBe(const char* expected) + { + Assert::AreEqual(expected, buffer); + Assert::AreEqual(strlen(expected), returnValue); + } + }; +} diff --git a/JsonGeneratorTests/PrettyPrint_Object_Tests.cpp b/JsonGeneratorTests/PrettyPrint_Object_Tests.cpp index 99c96cb9..66642255 100644 --- a/JsonGeneratorTests/PrettyPrint_Object_Tests.cpp +++ b/JsonGeneratorTests/PrettyPrint_Object_Tests.cpp @@ -1,89 +1,89 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#include "CppUnitTest.h" -#include "JsonPrettyPrint.h" -#include "StringBuilder.h" - -using namespace ArduinoJson::Internals; -using namespace ArduinoJson::Generator; -using namespace Microsoft::VisualStudio::CppUnitTestFramework; - -namespace JsonGeneratorTests -{ - TEST_CLASS(PrettyPrint_Object_Tests) - { - char buffer[1024]; - size_t returnValue; - - public: - - TEST_METHOD(EmptyObject) - { - whenInputIs("{}"); - outputMustBe("{}"); - } - - TEST_METHOD(OneMember) - { - whenInputIs("{\"key\":\"value\"}"); - outputMustBe( - "{\r\n" - " \"key\": \"value\"\r\n" - "}"); - } - - TEST_METHOD(TwoMembers) - { - whenInputIs("{\"key1\":\"value1\",\"key2\":\"value2\"}"); - outputMustBe( - "{\r\n" - " \"key1\": \"value1\",\r\n" - " \"key2\": \"value2\"\r\n" - "}"); - } - - TEST_METHOD(EmptyNestedObjects) - { - whenInputIs("{\"key1\":{},\"key2\":{}}"); - outputMustBe( - "{\r\n" - " \"key1\": {},\r\n" - " \"key2\": {}\r\n" - "}"); - } - - TEST_METHOD(NestedObjects) - { - whenInputIs("{\"key1\":{\"a\":1},\"key2\":{\"b\":2}}"); - outputMustBe( - "{\r\n" - " \"key1\": {\r\n" - " \"a\": 1\r\n" - " },\r\n" - " \"key2\": {\r\n" - " \"b\": 2\r\n" - " }\r\n" - "}"); - } - - private: - - void whenInputIs(const char input[]) - { - StringBuilder sb(buffer, sizeof(buffer)); - IndentedPrint indentedPrint(sb); - JsonPrettyPrint decorator(indentedPrint); - - returnValue = decorator.print(input); - } - - void outputMustBe(const char* expected) - { - Assert::AreEqual(expected, buffer); - Assert::AreEqual(strlen(expected), returnValue); - } - }; -} \ No newline at end of file +*/ + +#include "CppUnitTest.h" +#include "JsonPrettyPrint.h" +#include "StringBuilder.h" + +using namespace ArduinoJson::Internals; +using namespace ArduinoJson::Generator; +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +namespace JsonGeneratorTests +{ + TEST_CLASS(PrettyPrint_Object_Tests) + { + char buffer[1024]; + size_t returnValue; + + public: + + TEST_METHOD(EmptyObject) + { + whenInputIs("{}"); + outputMustBe("{}"); + } + + TEST_METHOD(OneMember) + { + whenInputIs("{\"key\":\"value\"}"); + outputMustBe( + "{\r\n" + " \"key\": \"value\"\r\n" + "}"); + } + + TEST_METHOD(TwoMembers) + { + whenInputIs("{\"key1\":\"value1\",\"key2\":\"value2\"}"); + outputMustBe( + "{\r\n" + " \"key1\": \"value1\",\r\n" + " \"key2\": \"value2\"\r\n" + "}"); + } + + TEST_METHOD(EmptyNestedObjects) + { + whenInputIs("{\"key1\":{},\"key2\":{}}"); + outputMustBe( + "{\r\n" + " \"key1\": {},\r\n" + " \"key2\": {}\r\n" + "}"); + } + + TEST_METHOD(NestedObjects) + { + whenInputIs("{\"key1\":{\"a\":1},\"key2\":{\"b\":2}}"); + outputMustBe( + "{\r\n" + " \"key1\": {\r\n" + " \"a\": 1\r\n" + " },\r\n" + " \"key2\": {\r\n" + " \"b\": 2\r\n" + " }\r\n" + "}"); + } + + private: + + void whenInputIs(const char input[]) + { + StringBuilder sb(buffer, sizeof(buffer)); + IndentedPrint indentedPrint(sb); + JsonPrettyPrint decorator(indentedPrint); + + returnValue = decorator.print(input); + } + + void outputMustBe(const char* expected) + { + Assert::AreEqual(expected, buffer); + Assert::AreEqual(strlen(expected), returnValue); + } + }; +} diff --git a/JsonGeneratorTests/PrettyPrint_String_Tests.cpp b/JsonGeneratorTests/PrettyPrint_String_Tests.cpp index a6660973..7085634a 100644 --- a/JsonGeneratorTests/PrettyPrint_String_Tests.cpp +++ b/JsonGeneratorTests/PrettyPrint_String_Tests.cpp @@ -1,76 +1,76 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#include "CppUnitTest.h" -#include "JsonPrettyPrint.h" -#include "StringBuilder.h" - -using namespace ArduinoJson::Internals; -using namespace ArduinoJson::Generator; -using namespace Microsoft::VisualStudio::CppUnitTestFramework; - -namespace JsonGeneratorTests -{ - TEST_CLASS(PrettyPrint_String_Tests) - { - char buffer[1024]; - size_t returnValue; - - public: - - TEST_METHOD(EmptyString) - { - whenInputIs(""); - outputMustBe(""); - } - - TEST_METHOD(TrickyCharacters) - { - whenInputIs ("\":\\\"',\""); - outputMustBe("\":\\\"',\""); - } - - TEST_METHOD(OpeningCurlyBrace) - { - whenInputIs ("\"{\""); - outputMustBe("\"{\""); - } - - TEST_METHOD(OpeningSquareBrace) - { - whenInputIs("\"[\""); - outputMustBe("\"[\""); - } - - TEST_METHOD(ClosingCurlyBrace) - { - whenInputIs("\"}\""); - outputMustBe("\"}\""); - } - - TEST_METHOD(ClosingSquareBrace) - { - whenInputIs("\"]\""); - outputMustBe("\"]\""); - } - - private: - - void whenInputIs(const char input[]) - { - StringBuilder sb(buffer, sizeof(buffer)); - IndentedPrint indentedPrint(sb); - JsonPrettyPrint decorator(indentedPrint); - - returnValue = decorator.print(input); - } - - void outputMustBe(const char* expected) - { - Assert::AreEqual(expected, buffer); - Assert::AreEqual(strlen(expected), returnValue); - } - }; -} \ No newline at end of file +*/ + +#include "CppUnitTest.h" +#include "JsonPrettyPrint.h" +#include "StringBuilder.h" + +using namespace ArduinoJson::Internals; +using namespace ArduinoJson::Generator; +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +namespace JsonGeneratorTests +{ + TEST_CLASS(PrettyPrint_String_Tests) + { + char buffer[1024]; + size_t returnValue; + + public: + + TEST_METHOD(EmptyString) + { + whenInputIs(""); + outputMustBe(""); + } + + TEST_METHOD(TrickyCharacters) + { + whenInputIs ("\":\\\"',\""); + outputMustBe("\":\\\"',\""); + } + + TEST_METHOD(OpeningCurlyBrace) + { + whenInputIs ("\"{\""); + outputMustBe("\"{\""); + } + + TEST_METHOD(OpeningSquareBrace) + { + whenInputIs("\"[\""); + outputMustBe("\"[\""); + } + + TEST_METHOD(ClosingCurlyBrace) + { + whenInputIs("\"}\""); + outputMustBe("\"}\""); + } + + TEST_METHOD(ClosingSquareBrace) + { + whenInputIs("\"]\""); + outputMustBe("\"]\""); + } + + private: + + void whenInputIs(const char input[]) + { + StringBuilder sb(buffer, sizeof(buffer)); + IndentedPrint indentedPrint(sb); + JsonPrettyPrint decorator(indentedPrint); + + returnValue = decorator.print(input); + } + + void outputMustBe(const char* expected) + { + Assert::AreEqual(expected, buffer); + Assert::AreEqual(strlen(expected), returnValue); + } + }; +} diff --git a/JsonGeneratorTests/StringBuilderTests.cpp b/JsonGeneratorTests/StringBuilderTests.cpp index fcbd9abb..757cead1 100644 --- a/JsonGeneratorTests/StringBuilderTests.cpp +++ b/JsonGeneratorTests/StringBuilderTests.cpp @@ -1,85 +1,85 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#include "CppUnitTest.h" -#include "StringBuilder.h" - -using namespace Microsoft::VisualStudio::CppUnitTestFramework; -using namespace ArduinoJson::Internals; - -namespace JsonGeneratorTests -{ - TEST_CLASS(StringBuilderTests) - { - char buffer[20]; - Print* sb; - size_t returnValue; - - public: - - TEST_METHOD_INITIALIZE(Initialize) - { - sb = new StringBuilder(buffer, sizeof(buffer)); - } - - TEST_METHOD(InitialState) - { - outputMustBe(""); - } - - TEST_METHOD(OverCapacity) - { - print("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); - resultMustBe(19); - - print("ABC"); - resultMustBe(0); - - outputMustBe("ABCDEFGHIJKLMNOPQRS"); - } - - TEST_METHOD(EmptyString) - { - print(""); - resultMustBe(0); - outputMustBe(""); - } - - TEST_METHOD(OneString) - { - print("ABCD"); - resultMustBe(4); - outputMustBe("ABCD"); - } - - TEST_METHOD(TwoStrings) - { - print("ABCD"); - resultMustBe(4); - - print("EFGH"); - resultMustBe(4); - - outputMustBe("ABCDEFGH"); - } - - private: - - void print(const char* value) - { - returnValue = sb->print(value); - } - - void outputMustBe(const char* expected) - { - Assert::AreEqual(expected, buffer); - } - - void resultMustBe(size_t expected) - { - Assert::AreEqual(expected, returnValue); - } - }; -} \ No newline at end of file +*/ + +#include "CppUnitTest.h" +#include "StringBuilder.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace ArduinoJson::Internals; + +namespace JsonGeneratorTests +{ + TEST_CLASS(StringBuilderTests) + { + char buffer[20]; + Print* sb; + size_t returnValue; + + public: + + TEST_METHOD_INITIALIZE(Initialize) + { + sb = new StringBuilder(buffer, sizeof(buffer)); + } + + TEST_METHOD(InitialState) + { + outputMustBe(""); + } + + TEST_METHOD(OverCapacity) + { + print("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + resultMustBe(19); + + print("ABC"); + resultMustBe(0); + + outputMustBe("ABCDEFGHIJKLMNOPQRS"); + } + + TEST_METHOD(EmptyString) + { + print(""); + resultMustBe(0); + outputMustBe(""); + } + + TEST_METHOD(OneString) + { + print("ABCD"); + resultMustBe(4); + outputMustBe("ABCD"); + } + + TEST_METHOD(TwoStrings) + { + print("ABCD"); + resultMustBe(4); + + print("EFGH"); + resultMustBe(4); + + outputMustBe("ABCDEFGH"); + } + + private: + + void print(const char* value) + { + returnValue = sb->print(value); + } + + void outputMustBe(const char* expected) + { + Assert::AreEqual(expected, buffer); + } + + void resultMustBe(size_t expected) + { + Assert::AreEqual(expected, returnValue); + } + }; +} diff --git a/JsonParser.cpp b/JsonParser.cpp index 0fc7597e..de8cf128 100644 --- a/JsonParser.cpp +++ b/JsonParser.cpp @@ -1,13 +1,13 @@ -/* - * Arduino JSON library - * Benoit Blanchon 2014 - MIT License - */ - -// This file is here to help the Arduino IDE find the .cpp files - -#include "JsonParser/JsonArray.cpp" -#include "JsonParser/JsonObject.cpp" -#include "JsonParser/JsonParserBase.cpp" -#include "JsonParser/JsonValue.cpp" -#include "JsonParser/JsonToken.cpp" -#include "JsonParser/jsmn.cpp" \ No newline at end of file +/* + * Arduino JSON library + * Benoit Blanchon 2014 - MIT License + */ + +// This file is here to help the Arduino IDE find the .cpp files + +#include "JsonParser/JsonArray.cpp" +#include "JsonParser/JsonObject.cpp" +#include "JsonParser/JsonParserBase.cpp" +#include "JsonParser/JsonValue.cpp" +#include "JsonParser/JsonToken.cpp" +#include "JsonParser/jsmn.cpp" diff --git a/JsonParser.h b/JsonParser.h index 52a28741..23538c96 100644 --- a/JsonParser.h +++ b/JsonParser.h @@ -1,6 +1,6 @@ -/* -* malloc-free JSON parser for Arduino -* Benoit Blanchon 2014 - MIT License -*/ - -#include "JsonParser/JsonParser.h" \ No newline at end of file +/* +* malloc-free JSON parser for Arduino +* Benoit Blanchon 2014 - MIT License +*/ + +#include "JsonParser/JsonParser.h" diff --git a/JsonParser/JsonArray.cpp b/JsonParser/JsonArray.cpp index 342f0fb8..a250986d 100644 --- a/JsonParser/JsonArray.cpp +++ b/JsonParser/JsonArray.cpp @@ -2,13 +2,13 @@ * Arduino JSON library * Benoit Blanchon 2014 - MIT License */ - -#include "JsonArray.h" -#include "JsonObject.h" - -using namespace ArduinoJson::Parser; - -DEPRECATED JsonObject JsonArray::getHashTable(int index) -{ - return operator[](index); -} \ No newline at end of file + +#include "JsonArray.h" +#include "JsonObject.h" + +using namespace ArduinoJson::Parser; + +DEPRECATED JsonObject JsonArray::getHashTable(int index) +{ + return operator[](index); +} diff --git a/JsonParser/JsonArray.h b/JsonParser/JsonArray.h index ba5bae55..83de024b 100644 --- a/JsonParser/JsonArray.h +++ b/JsonParser/JsonArray.h @@ -2,102 +2,102 @@ * Arduino JSON library * Benoit Blanchon 2014 - MIT License */ - -#pragma once - -#include "JsonValue.h" -#include "JsonArrayIterator.h" - -namespace ArduinoJson -{ - namespace Parser - { - class JsonObject; + +#pragma once + +#include "JsonValue.h" +#include "JsonArrayIterator.h" + +namespace ArduinoJson +{ + namespace Parser + { + class JsonObject; // A JSON array - class JsonArray : JsonValue - { - public: - + class JsonArray : JsonValue + { + public: + // Create an invalid array - JsonArray() - { - } + JsonArray() + { + } // Convert a JsonValue into a JsonArray - JsonArray(JsonValue value) - : JsonValue(value) - { - } + JsonArray(JsonValue value) + : JsonValue(value) + { + } // Tell if the array is valid - bool success() - { - return isArray(); - } + bool success() + { + return isArray(); + } // Get the JsonValue at specified index - JsonValue operator[](int index) - { - return JsonValue::operator[](index); - } + JsonValue operator[](int index) + { + return JsonValue::operator[](index); + } // Get the size of the array - int size() - { - return isArray() ? childrenCount() : 0; - } + int size() + { + return isArray() ? childrenCount() : 0; + } // Get an iterator pointing to the beginning of the array - JsonArrayIterator begin() - { - return isArray() ? firstChild() : null(); - } + JsonArrayIterator begin() + { + return isArray() ? firstChild() : null(); + } // Gets an iterator pointing to the end of the array - JsonArrayIterator end() - { - return isArray() ? nextSibling() : null(); - } + JsonArrayIterator end() + { + return isArray() ? nextSibling() : null(); + } // Obsolete: Use size() instead - DEPRECATED int getLength() - { - return size(); - } - - // Obsolete: Use operator[] instead - DEPRECATED JsonArray getArray(int index) - { - return operator[](index); - } - - // Obsolete: Use operator[] instead - DEPRECATED bool getBool(int index) - { - return operator[](index); - } - - // Obsolete: Use operator[] instead - DEPRECATED double getDouble(int index) - { - return operator[](index); - } - - // Obsolete: Use operator[] instead - DEPRECATED JsonObject getHashTable(int index); - - // Obsolete: Use operator[] instead - DEPRECATED long getLong(int index) - { - return operator[](index); - } - - // Obsolete: Use operator[] instead - DEPRECATED char* getString(int index) - { - return operator[](index); - } - }; - } -} \ No newline at end of file + DEPRECATED int getLength() + { + return size(); + } + + // Obsolete: Use operator[] instead + DEPRECATED JsonArray getArray(int index) + { + return operator[](index); + } + + // Obsolete: Use operator[] instead + DEPRECATED bool getBool(int index) + { + return operator[](index); + } + + // Obsolete: Use operator[] instead + DEPRECATED double getDouble(int index) + { + return operator[](index); + } + + // Obsolete: Use operator[] instead + DEPRECATED JsonObject getHashTable(int index); + + // Obsolete: Use operator[] instead + DEPRECATED long getLong(int index) + { + return operator[](index); + } + + // Obsolete: Use operator[] instead + DEPRECATED char* getString(int index) + { + return operator[](index); + } + }; + } +} diff --git a/JsonParser/JsonArrayIterator.h b/JsonParser/JsonArrayIterator.h index 9eb0725e..ae8f94ed 100644 --- a/JsonParser/JsonArrayIterator.h +++ b/JsonParser/JsonArrayIterator.h @@ -2,45 +2,45 @@ * Arduino JSON library * Benoit Blanchon 2014 - MIT License */ - -#pragma once - -#include "JsonValue.h" -#include "JsonToken.h" - -namespace ArduinoJson -{ - namespace Parser - { - // An iterator for JsonArray - class JsonArrayIterator : JsonToken - { - public: - - // Create an iterator pointing at the specified JsonToken - JsonArrayIterator(JsonToken token) - : JsonToken(token) - { - - } - - // Move iterator forward - void operator++() - { - *this = JsonArrayIterator(nextSibling()); - } - - // Get the value pointed by the iterator - JsonValue operator*() const - { - return JsonValue(*this); - } - - // Test iterator equality - bool operator!= (const JsonArrayIterator& other) const - { - return JsonToken::operator!=(other); - } - }; - } -} \ No newline at end of file + +#pragma once + +#include "JsonValue.h" +#include "JsonToken.h" + +namespace ArduinoJson +{ + namespace Parser + { + // An iterator for JsonArray + class JsonArrayIterator : JsonToken + { + public: + + // Create an iterator pointing at the specified JsonToken + JsonArrayIterator(JsonToken token) + : JsonToken(token) + { + + } + + // Move iterator forward + void operator++() + { + *this = JsonArrayIterator(nextSibling()); + } + + // Get the value pointed by the iterator + JsonValue operator*() const + { + return JsonValue(*this); + } + + // Test iterator equality + bool operator!= (const JsonArrayIterator& other) const + { + return JsonToken::operator!=(other); + } + }; + } +} diff --git a/JsonParser/JsonObject.cpp b/JsonParser/JsonObject.cpp index 22453f32..bebc6261 100644 --- a/JsonParser/JsonObject.cpp +++ b/JsonParser/JsonObject.cpp @@ -1,15 +1,15 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#include "JsonArray.h" -#include "JsonObject.h" -#include "JsonValue.h" - -using namespace ArduinoJson::Parser; - -DEPRECATED JsonArray JsonObject::getArray(const char* key) -{ - return operator[](key); -} \ No newline at end of file +*/ + +#include "JsonArray.h" +#include "JsonObject.h" +#include "JsonValue.h" + +using namespace ArduinoJson::Parser; + +DEPRECATED JsonArray JsonObject::getArray(const char* key) +{ + return operator[](key); +} diff --git a/JsonParser/JsonObject.h b/JsonParser/JsonObject.h index c6ab8aec..e75e1d44 100644 --- a/JsonParser/JsonObject.h +++ b/JsonParser/JsonObject.h @@ -2,101 +2,101 @@ * Arduino JSON library * Benoit Blanchon 2014 - MIT License */ - -#pragma once - -#include "JsonValue.h" -#include "JsonObjectIterator.h" - -namespace ArduinoJson -{ - namespace Parser - { - class JsonArray; - - // A JSON Object (ie hash-table/dictionary) - class JsonObject : JsonValue - { - public: - - // Create an invalid JsonObject - JsonObject() - { - - } - - // Convert a JsonValue into a JsonObject - JsonObject(JsonValue value) - : JsonValue(value) - { - - } - - // Tell if the object is valid - bool success() - { - return isObject(); - } - - // Get the value associated with the specified key. - JsonValue operator[](const char* key) - { - return JsonValue::operator[](key); - } - - // Tell if the specified key exists in the object. - bool containsKey(const char* key) - { - return operator[](key).success(); - } - - // Get an iterator pointing at the beginning of the object - JsonObjectIterator begin() - { - return isObject() ? firstChild() : null(); - } - - // Get an iterator pointing at the end of the object - JsonObjectIterator end() - { - return isObject() ? nextSibling() : null(); - } - - // Obsolete: Use operator[] instead - DEPRECATED JsonArray getArray(const char* key); - - // Obsolete: Use operator[] instead - DEPRECATED bool getBool(const char* key) - { - return operator[](key); - } - - // Obsolete: Use operator[] instead - DEPRECATED double getDouble(const char* key) - { - return operator[](key); - } - - // Obsolete: Use operator[] instead - DEPRECATED JsonObject getHashTable(const char* key) - { - return operator[](key); - } - - // Obsolete: Use operator[] instead - DEPRECATED long getLong(const char* key) - { - return operator[](key); - } - - // Obsolete: Use operator[] instead - DEPRECATED char* getString(const char* key) - { - return operator[](key); - } - }; - - // Obsolete: Use JsonObject instead - DEPRECATED typedef JsonObject JsonHashTable; - } -} \ No newline at end of file + +#pragma once + +#include "JsonValue.h" +#include "JsonObjectIterator.h" + +namespace ArduinoJson +{ + namespace Parser + { + class JsonArray; + + // A JSON Object (ie hash-table/dictionary) + class JsonObject : JsonValue + { + public: + + // Create an invalid JsonObject + JsonObject() + { + + } + + // Convert a JsonValue into a JsonObject + JsonObject(JsonValue value) + : JsonValue(value) + { + + } + + // Tell if the object is valid + bool success() + { + return isObject(); + } + + // Get the value associated with the specified key. + JsonValue operator[](const char* key) + { + return JsonValue::operator[](key); + } + + // Tell if the specified key exists in the object. + bool containsKey(const char* key) + { + return operator[](key).success(); + } + + // Get an iterator pointing at the beginning of the object + JsonObjectIterator begin() + { + return isObject() ? firstChild() : null(); + } + + // Get an iterator pointing at the end of the object + JsonObjectIterator end() + { + return isObject() ? nextSibling() : null(); + } + + // Obsolete: Use operator[] instead + DEPRECATED JsonArray getArray(const char* key); + + // Obsolete: Use operator[] instead + DEPRECATED bool getBool(const char* key) + { + return operator[](key); + } + + // Obsolete: Use operator[] instead + DEPRECATED double getDouble(const char* key) + { + return operator[](key); + } + + // Obsolete: Use operator[] instead + DEPRECATED JsonObject getHashTable(const char* key) + { + return operator[](key); + } + + // Obsolete: Use operator[] instead + DEPRECATED long getLong(const char* key) + { + return operator[](key); + } + + // Obsolete: Use operator[] instead + DEPRECATED char* getString(const char* key) + { + return operator[](key); + } + }; + + // Obsolete: Use JsonObject instead + DEPRECATED typedef JsonObject JsonHashTable; + } +} diff --git a/JsonParser/JsonObjectIterator.h b/JsonParser/JsonObjectIterator.h index d3c1367b..e2694df1 100644 --- a/JsonParser/JsonObjectIterator.h +++ b/JsonParser/JsonObjectIterator.h @@ -2,57 +2,57 @@ * Arduino JSON library * Benoit Blanchon 2014 - MIT License */ - -#pragma once - -#include "JsonValue.h" -#include "JsonPair.h" -#include "JsonToken.h" - -namespace ArduinoJson -{ - namespace Parser - { - // An iterator for JsonObject - class JsonObjectIterator : JsonToken - { - public: - - // Create an iterator pointing at the specified token - JsonObjectIterator(JsonToken token) - : JsonToken(token) - { - } - - // Move to the next JsonPair - void operator++() - { - *this = JsonObjectIterator(nextSibling().nextSibling()); - } - - // Get the JsonPair pointed by the iterator - JsonPair operator*() const - { - return JsonPair(*this); - } - - // Test iterator equality - bool operator!= (const JsonObjectIterator& other) const - { - return JsonToken::operator!=(other); - } - - // Get the key of the JsonPair pointed by the iterator - const char* key() const - { - return operator*().key(); - } - - // Get the key of the JsonPair pointed by the iterator - JsonValue value() const - { - return operator*().value(); - } - }; - } -} \ No newline at end of file + +#pragma once + +#include "JsonValue.h" +#include "JsonPair.h" +#include "JsonToken.h" + +namespace ArduinoJson +{ + namespace Parser + { + // An iterator for JsonObject + class JsonObjectIterator : JsonToken + { + public: + + // Create an iterator pointing at the specified token + JsonObjectIterator(JsonToken token) + : JsonToken(token) + { + } + + // Move to the next JsonPair + void operator++() + { + *this = JsonObjectIterator(nextSibling().nextSibling()); + } + + // Get the JsonPair pointed by the iterator + JsonPair operator*() const + { + return JsonPair(*this); + } + + // Test iterator equality + bool operator!= (const JsonObjectIterator& other) const + { + return JsonToken::operator!=(other); + } + + // Get the key of the JsonPair pointed by the iterator + const char* key() const + { + return operator*().key(); + } + + // Get the key of the JsonPair pointed by the iterator + JsonValue value() const + { + return operator*().value(); + } + }; + } +} diff --git a/JsonParser/JsonPair.h b/JsonParser/JsonPair.h index 2f8d8732..4f15be27 100644 --- a/JsonParser/JsonPair.h +++ b/JsonParser/JsonPair.h @@ -1,37 +1,37 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#pragma once - -#include "JsonValue.h" - -namespace ArduinoJson -{ - namespace Parser - { - // A JSON key-value pair, as a part of a JSON object - class JsonPair : JsonToken - { - public: - // Convert a JsonToken to a JsonPair - JsonPair(JsonToken token) - : JsonToken(token) - { - } - - // Get the key - const char* key() - { - return getText(); - } - - // Get the value - JsonValue value() - { - return nextSibling(); - } - }; - } -} \ No newline at end of file +*/ + +#pragma once + +#include "JsonValue.h" + +namespace ArduinoJson +{ + namespace Parser + { + // A JSON key-value pair, as a part of a JSON object + class JsonPair : JsonToken + { + public: + // Convert a JsonToken to a JsonPair + JsonPair(JsonToken token) + : JsonToken(token) + { + } + + // Get the key + const char* key() + { + return getText(); + } + + // Get the value + JsonValue value() + { + return nextSibling(); + } + }; + } +} diff --git a/JsonParser/JsonParser.h b/JsonParser/JsonParser.h index d2c0ffe3..c7d2b2f1 100644 --- a/JsonParser/JsonParser.h +++ b/JsonParser/JsonParser.h @@ -1,35 +1,35 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#pragma once - -#include "JsonParserBase.h" - -namespace ArduinoJson -{ - namespace Parser - { - // The JSON parser. - // - // You need to specifiy the number of token to be allocated for that parser. - // - // CAUTION: JsonArray, JsonObject and JsonValue contain pointers to tokens of the - // JsonParser, so they need the JsonParser to be in memory to work. - // As a result, you must not create JsonArray, JsonObject or JsonValue that have a - // longer life that the JsonParser. - template - class JsonParser : public JsonParserBase - { - public: - JsonParser() - : JsonParserBase(tokens, MAX_TOKENS) - { - } - - private: - jsmntok_t tokens[MAX_TOKENS]; - }; - } -} \ No newline at end of file +*/ + +#pragma once + +#include "JsonParserBase.h" + +namespace ArduinoJson +{ + namespace Parser + { + // The JSON parser. + // + // You need to specifiy the number of token to be allocated for that parser. + // + // CAUTION: JsonArray, JsonObject and JsonValue contain pointers to tokens of the + // JsonParser, so they need the JsonParser to be in memory to work. + // As a result, you must not create JsonArray, JsonObject or JsonValue that have a + // longer life that the JsonParser. + template + class JsonParser : public JsonParserBase + { + public: + JsonParser() + : JsonParserBase(tokens, MAX_TOKENS) + { + } + + private: + jsmntok_t tokens[MAX_TOKENS]; + }; + } +} diff --git a/JsonParser/JsonParserBase.cpp b/JsonParser/JsonParserBase.cpp index 8263d8cc..fa168554 100644 --- a/JsonParser/JsonParserBase.cpp +++ b/JsonParser/JsonParserBase.cpp @@ -1,20 +1,20 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#include "JsonParserBase.h" -#include "JsonToken.h" - -using namespace ArduinoJson::Parser; - -JsonValue JsonParserBase::parse(char* json) -{ - jsmn_parser parser; - jsmn_init(&parser); - - if (JSMN_SUCCESS != jsmn_parse(&parser, json, tokens, maxTokens)) - return JsonToken::null(); - - return JsonToken(json, tokens); -} +*/ + +#include "JsonParserBase.h" +#include "JsonToken.h" + +using namespace ArduinoJson::Parser; + +JsonValue JsonParserBase::parse(char* json) +{ + jsmn_parser parser; + jsmn_init(&parser); + + if (JSMN_SUCCESS != jsmn_parse(&parser, json, tokens, maxTokens)) + return JsonToken::null(); + + return JsonToken(json, tokens); +} diff --git a/JsonParser/JsonParserBase.h b/JsonParser/JsonParserBase.h index 8f6418af..a49cafd0 100644 --- a/JsonParser/JsonParserBase.h +++ b/JsonParser/JsonParserBase.h @@ -1,49 +1,49 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#pragma once - -#include "JsonArray.h" -#include "JsonObject.h" - -namespace ArduinoJson -{ - namespace Parser - { - // Base class for the JSON parser, in case you want to provide your own buffer - class JsonParserBase - { - public: - - // Create a JSON parser using the provided buffer - JsonParserBase(jsmntok_t* tokens, int maxTokens) - : tokens(tokens), maxTokens(maxTokens) - { - } - - // Parse the JSON string and return a array - // - // The content of the string may be altered to add '\0' at the - // end of string tokens - JsonValue parse(char* json); - - // Obsolete: use parse() instead - DEPRECATED JsonArray parseArray(char* json) - { - return parse(json); - } - - // Obsolete: use parse() instead - DEPRECATED JsonObject parseHashTable(char* json) - { - return parse(json); - } - - private: - jsmntok_t* tokens; - int maxTokens; - }; - } -} \ No newline at end of file +*/ + +#pragma once + +#include "JsonArray.h" +#include "JsonObject.h" + +namespace ArduinoJson +{ + namespace Parser + { + // Base class for the JSON parser, in case you want to provide your own buffer + class JsonParserBase + { + public: + + // Create a JSON parser using the provided buffer + JsonParserBase(jsmntok_t* tokens, int maxTokens) + : tokens(tokens), maxTokens(maxTokens) + { + } + + // Parse the JSON string and return a array + // + // The content of the string may be altered to add '\0' at the + // end of string tokens + JsonValue parse(char* json); + + // Obsolete: use parse() instead + DEPRECATED JsonArray parseArray(char* json) + { + return parse(json); + } + + // Obsolete: use parse() instead + DEPRECATED JsonObject parseHashTable(char* json) + { + return parse(json); + } + + private: + jsmntok_t* tokens; + int maxTokens; + }; + } +} diff --git a/JsonParser/JsonToken.cpp b/JsonParser/JsonToken.cpp index 27685e3f..2b4eac4c 100644 --- a/JsonParser/JsonToken.cpp +++ b/JsonParser/JsonToken.cpp @@ -1,71 +1,71 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#include "JsonToken.h" - -using namespace ArduinoJson::Parser; - -char* JsonToken::getText() -{ - char* s = json + token->start; - json[token->end] = 0; - - unescapeString(s); - - return s; -} - -inline void JsonToken::unescapeString(char* s) -{ - char* readPtr = s; - char* writePtr = s; - char c; - - do - { - c = *readPtr++; - - if (c == '\\') - { - c = unescapeChar(*readPtr++); - } - - *writePtr++ = c; - - } while (c != 0); -} - -inline char JsonToken::unescapeChar(char c) -{ - // Optimized for code size on a 8-bit AVR - - const char* p = "b\bf\fn\nr\rt\t"; - - while (true) - { - if (p[0] == 0) return c; - if (p[0] == c) return p[1]; - p += 2; - } -} - -JsonToken JsonToken::nextSibling() const -{ - // start with current token - jsmntok_t* t = token; - - // count the number of token to skip - int yetToVisit = 1; - - // skip all nested tokens - while (yetToVisit) - { - yetToVisit += t->size - 1; - t++; - } - - // build a JsonToken at the new location - return JsonToken(json, t); -} \ No newline at end of file +*/ + +#include "JsonToken.h" + +using namespace ArduinoJson::Parser; + +char* JsonToken::getText() +{ + char* s = json + token->start; + json[token->end] = 0; + + unescapeString(s); + + return s; +} + +inline void JsonToken::unescapeString(char* s) +{ + char* readPtr = s; + char* writePtr = s; + char c; + + do + { + c = *readPtr++; + + if (c == '\\') + { + c = unescapeChar(*readPtr++); + } + + *writePtr++ = c; + + } while (c != 0); +} + +inline char JsonToken::unescapeChar(char c) +{ + // Optimized for code size on a 8-bit AVR + + const char* p = "b\bf\fn\nr\rt\t"; + + while (true) + { + if (p[0] == 0) return c; + if (p[0] == c) return p[1]; + p += 2; + } +} + +JsonToken JsonToken::nextSibling() const +{ + // start with current token + jsmntok_t* t = token; + + // count the number of token to skip + int yetToVisit = 1; + + // skip all nested tokens + while (yetToVisit) + { + yetToVisit += t->size - 1; + t++; + } + + // build a JsonToken at the new location + return JsonToken(json, t); +} diff --git a/JsonParser/JsonToken.h b/JsonParser/JsonToken.h index 3733a7cb..6e492ba5 100644 --- a/JsonParser/JsonToken.h +++ b/JsonParser/JsonToken.h @@ -1,99 +1,99 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#pragma once - -#include "jsmn.h" - -namespace ArduinoJson -{ - namespace Parser - { - // A pointer to a JSON token - class JsonToken - { - public: - - // Create a "null" pointer - JsonToken() - : token(0) - { - } - - // Create a pointer to the specified JSON token - JsonToken(char* json, jsmntok_t* token) - : json(json), token(token) - { - } - - // Get content of the JSON token - char* getText(); - - // Get the number of children tokens - int childrenCount() - { - return token->size; - } - - // Get a pointer to the first child of the current token - JsonToken firstChild() const - { - return JsonToken(json, token + 1); - } - - // Get a pointer to the next sibling token (ie skiping the children tokens) - JsonToken nextSibling() const; - - // Test equality - bool operator!=(const JsonToken& other) const - { - return token != other.token; - } - - // Tell if the pointer is "null" - bool isValid() - { - return token != 0; - } - - // Tell if the JSON token is a JSON object - bool isObject() - { - return token != 0 && token->type == JSMN_OBJECT; - } - - // Tell if the JSON token is a JSON array - bool isArray() - { - return token != 0 && token->type == JSMN_ARRAY; - } - - // Tell if the JSON token is a primitive - bool isPrimitive() - { - return token != 0 && token->type == JSMN_PRIMITIVE; - } - - // Tell if the JSON token is a string - bool isString() - { - return token != 0 && token->type == JSMN_STRING; - } - - // Explicit wait to create a "null" JsonToken - static JsonToken null() - { - return JsonToken(); - } - - private: - char* json; - jsmntok_t* token; - - static char unescapeChar(char c); - static void unescapeString(char* s); - }; - } -} \ No newline at end of file +*/ + +#pragma once + +#include "jsmn.h" + +namespace ArduinoJson +{ + namespace Parser + { + // A pointer to a JSON token + class JsonToken + { + public: + + // Create a "null" pointer + JsonToken() + : token(0) + { + } + + // Create a pointer to the specified JSON token + JsonToken(char* json, jsmntok_t* token) + : json(json), token(token) + { + } + + // Get content of the JSON token + char* getText(); + + // Get the number of children tokens + int childrenCount() + { + return token->size; + } + + // Get a pointer to the first child of the current token + JsonToken firstChild() const + { + return JsonToken(json, token + 1); + } + + // Get a pointer to the next sibling token (ie skiping the children tokens) + JsonToken nextSibling() const; + + // Test equality + bool operator!=(const JsonToken& other) const + { + return token != other.token; + } + + // Tell if the pointer is "null" + bool isValid() + { + return token != 0; + } + + // Tell if the JSON token is a JSON object + bool isObject() + { + return token != 0 && token->type == JSMN_OBJECT; + } + + // Tell if the JSON token is a JSON array + bool isArray() + { + return token != 0 && token->type == JSMN_ARRAY; + } + + // Tell if the JSON token is a primitive + bool isPrimitive() + { + return token != 0 && token->type == JSMN_PRIMITIVE; + } + + // Tell if the JSON token is a string + bool isString() + { + return token != 0 && token->type == JSMN_STRING; + } + + // Explicit wait to create a "null" JsonToken + static JsonToken null() + { + return JsonToken(); + } + + private: + char* json; + jsmntok_t* token; + + static char unescapeChar(char c); + static void unescapeString(char* s); + }; + } +} diff --git a/JsonParser/JsonValue.cpp b/JsonParser/JsonValue.cpp index b21ac93d..03684c48 100644 --- a/JsonParser/JsonValue.cpp +++ b/JsonParser/JsonValue.cpp @@ -1,110 +1,110 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License - */ - -#include // for strtol, strtod -#include // for strcmp() -#include "JsonArray.h" -#include "JsonObject.h" -#include "JsonValue.h" - -using namespace ArduinoJson::Parser; - -// Convert the JsonValue to a bool. -// Returns false if the JsonValue is not a primitve. -JsonValue::operator bool() -{ - if (!isPrimitive()) return 0; - - char *text = getText(); - - // "true" - if (text[0] == 't') return true; - - // "false" - if (text[0] == 'f') return false; - - // "null" - if (text[0] == 'n') return false; - - // number - return strtol(text, 0, 0) != 0; -} - -// Convert the JsonValue to a floating point value. -// Returns false if the JsonValue is not a number. -JsonValue::operator double() -{ - return isPrimitive() ? strtod(getText(), 0) : 0; -} - -// Convert the JsonValue to a floating point value. -// Returns false if the JsonValue is not a number. -JsonValue::operator long() -{ - return isPrimitive() ? strtol(getText(), 0, 0) : 0; -} - -// Convert the JsonValue to a string. -// Returns 0 if the JsonValue is not a string. -JsonValue::operator char*() -{ - return isString() || isPrimitive() ? getText() : 0; -} - -// Get the nested value at the specified index. -// Returns an invalid JsonValue if the current value is not an array. -JsonValue JsonValue::operator[](int index) -{ - // sanity check - if (index < 0 || !isArray() || index >= childrenCount()) - return null(); - - // skip first token, it's the whole object - JsonToken runningToken = firstChild(); - - // skip all tokens before the specified index - for (int i = 0; i < index; i++) - { - // move forward: current + nested tokens - runningToken = runningToken.nextSibling(); - } - - return runningToken; -} - -// Get the nested value matching the specified index. -// Returns an invalid JsonValue if the current value is not an object. -JsonValue JsonValue::operator[](const char* desiredKey) -{ - // sanity check - if (desiredKey == 0 || !isObject()) - return null(); - - // skip first token, it's the whole object - JsonToken runningToken = firstChild(); - - // scan each keys - for (int i = 0; i < childrenCount() / 2; i++) - { - // get 'key' token string - char* key = runningToken.getText(); - - // move to the 'value' token - runningToken = runningToken.nextSibling(); - - // compare with desired name - if (strcmp(desiredKey, key) == 0) - { - // return the value token that follows the key token - return runningToken; - } - - // skip nested tokens - runningToken = runningToken.nextSibling(); - } - - // nothing found, return NULL - return null(); -} + */ + +#include // for strtol, strtod +#include // for strcmp() +#include "JsonArray.h" +#include "JsonObject.h" +#include "JsonValue.h" + +using namespace ArduinoJson::Parser; + +// Convert the JsonValue to a bool. +// Returns false if the JsonValue is not a primitve. +JsonValue::operator bool() +{ + if (!isPrimitive()) return 0; + + char *text = getText(); + + // "true" + if (text[0] == 't') return true; + + // "false" + if (text[0] == 'f') return false; + + // "null" + if (text[0] == 'n') return false; + + // number + return strtol(text, 0, 0) != 0; +} + +// Convert the JsonValue to a floating point value. +// Returns false if the JsonValue is not a number. +JsonValue::operator double() +{ + return isPrimitive() ? strtod(getText(), 0) : 0; +} + +// Convert the JsonValue to a floating point value. +// Returns false if the JsonValue is not a number. +JsonValue::operator long() +{ + return isPrimitive() ? strtol(getText(), 0, 0) : 0; +} + +// Convert the JsonValue to a string. +// Returns 0 if the JsonValue is not a string. +JsonValue::operator char*() +{ + return isString() || isPrimitive() ? getText() : 0; +} + +// Get the nested value at the specified index. +// Returns an invalid JsonValue if the current value is not an array. +JsonValue JsonValue::operator[](int index) +{ + // sanity check + if (index < 0 || !isArray() || index >= childrenCount()) + return null(); + + // skip first token, it's the whole object + JsonToken runningToken = firstChild(); + + // skip all tokens before the specified index + for (int i = 0; i < index; i++) + { + // move forward: current + nested tokens + runningToken = runningToken.nextSibling(); + } + + return runningToken; +} + +// Get the nested value matching the specified index. +// Returns an invalid JsonValue if the current value is not an object. +JsonValue JsonValue::operator[](const char* desiredKey) +{ + // sanity check + if (desiredKey == 0 || !isObject()) + return null(); + + // skip first token, it's the whole object + JsonToken runningToken = firstChild(); + + // scan each keys + for (int i = 0; i < childrenCount() / 2; i++) + { + // get 'key' token string + char* key = runningToken.getText(); + + // move to the 'value' token + runningToken = runningToken.nextSibling(); + + // compare with desired name + if (strcmp(desiredKey, key) == 0) + { + // return the value token that follows the key token + return runningToken; + } + + // skip nested tokens + runningToken = runningToken.nextSibling(); + } + + // nothing found, return NULL + return null(); +} diff --git a/JsonParser/JsonValue.h b/JsonParser/JsonValue.h index 1a49dfaa..55dda399 100644 --- a/JsonParser/JsonValue.h +++ b/JsonParser/JsonValue.h @@ -1,72 +1,72 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License - */ - -#pragma once - -#include "JsonToken.h" - -#ifndef ARDUINO_JSON_NO_DEPRECATION_WARNING -#ifdef __GNUC__ -#define DEPRECATED __attribute__((deprecated)) -#elif defined(_MSC_VER) -#define DEPRECATED __declspec(deprecated) -#endif + */ + +#pragma once + +#include "JsonToken.h" + +#ifndef ARDUINO_JSON_NO_DEPRECATION_WARNING +#ifdef __GNUC__ +#define DEPRECATED __attribute__((deprecated)) +#elif defined(_MSC_VER) +#define DEPRECATED __declspec(deprecated) +#endif #else #define DEPRECATED -#endif - -namespace ArduinoJson -{ - namespace Parser - { - // A JSON value - // Can be converted to string, double, bool, array or object. - class JsonValue : protected JsonToken - { - public: - - // Create a invalid value - JsonValue() - { - } - - // Convert a JsonToken to a JsonValue - JsonValue(JsonToken token) - : JsonToken(token) - { - } - - // Tell is the JsonValue is valid - bool success() - { - return isValid(); - } - - // Convert the JsonValue to a bool. - // Returns false if the JsonValue is not a primitve. - operator bool(); - - // Convert the JsonValue to a floating point value. - // Returns false if the JsonValue is not a number. - operator double(); - - // Convert the JsonValue to a long integer. - // Returns 0 if the JsonValue is not a number. - operator long(); - - // Convert the JsonValue to a string. - // Returns 0 if the JsonValue is not a string. - operator char*(); - - // Get the nested value at the specified index. - // Returns an invalid JsonValue if the current value is not an array. - JsonValue operator[](int index); - - // Get the nested value matching the specified index. - // Returns an invalid JsonValue if the current value is not an object. - JsonValue operator[](const char* key); - }; - } -} \ No newline at end of file +#endif + +namespace ArduinoJson +{ + namespace Parser + { + // A JSON value + // Can be converted to string, double, bool, array or object. + class JsonValue : protected JsonToken + { + public: + + // Create a invalid value + JsonValue() + { + } + + // Convert a JsonToken to a JsonValue + JsonValue(JsonToken token) + : JsonToken(token) + { + } + + // Tell is the JsonValue is valid + bool success() + { + return isValid(); + } + + // Convert the JsonValue to a bool. + // Returns false if the JsonValue is not a primitve. + operator bool(); + + // Convert the JsonValue to a floating point value. + // Returns false if the JsonValue is not a number. + operator double(); + + // Convert the JsonValue to a long integer. + // Returns 0 if the JsonValue is not a number. + operator long(); + + // Convert the JsonValue to a string. + // Returns 0 if the JsonValue is not a string. + operator char*(); + + // Get the nested value at the specified index. + // Returns an invalid JsonValue if the current value is not an array. + JsonValue operator[](int index); + + // Get the nested value matching the specified index. + // Returns an invalid JsonValue if the current value is not an object. + JsonValue operator[](const char* key); + }; + } +} diff --git a/JsonParserTests/GbathreeBug.cpp b/JsonParserTests/GbathreeBug.cpp index 8499f576..613276eb 100644 --- a/JsonParserTests/GbathreeBug.cpp +++ b/JsonParserTests/GbathreeBug.cpp @@ -1,244 +1,244 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#include "CppUnitTest.h" -#include "JsonParser.h" -#include - -using namespace std; -using namespace Microsoft::VisualStudio::CppUnitTestFramework; -using namespace ArduinoJson::Parser; - -namespace ArduinoJsonParserTests -{ - TEST_CLASS(GbathreeBug) - { - char json[1024]; - JsonParser<200> parser; - JsonHashTable root; - - - public: - - TEST_METHOD_INITIALIZE(Initialize) - { - // BUG described here: - // http://forum.arduino.cc/index.php?topic=172578.msg1608219#msg1608219 - strcpy(json, "{ \"protocol_name\":\"fluorescence\",\"repeats\":1,\"wait\":0,\"averages\":1,\"measurements\":3,\"meas2_light\":15,\"meas1_baseline\":0,\"act_light\":20,\"pulsesize\":25,\"pulsedistance\":10000,\"actintensity1\":50,\"actintensity2\":255,\"measintensity\":255,\"calintensity\":255,\"pulses\":[50,50,50],\"act\":[2,1,2,2],\"red\":[2,2,2,2],\"detectors\":[[34,34,34,34],[34,34,34,34],[34,34,34,34],[34,34,34,34]],\"alta\":[2,2,2,2],\"altb\":[2,2,2,2],\"measlights\":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]],\"measlights2\":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]],\"altc\":[2,2,2,2],\"altd\":[2,2,2,2]}"); - root = parser.parseHashTable(json); - } - - TEST_METHOD(Root) - { - Assert::IsTrue(root.success()); - } - - TEST_METHOD(ProtocolName) - { - string protocol_name = root.getString("protocol_name"); - Assert::AreEqual(string("fluorescence"), protocol_name); - } - - TEST_METHOD(Repeats) - { - Assert::AreEqual(1L, root.getLong("repeats")); - } - - TEST_METHOD(Wait) - { - Assert::AreEqual(0L, root.getLong("wait")); - } - - TEST_METHOD(Measurements) - { - Assert::AreEqual(3L, root.getLong("measurements")); - } - - TEST_METHOD(Meas2_Light) - { - Assert::AreEqual(15L, root.getLong("meas2_light")); - } - - TEST_METHOD(Meas1_Baseline) - { - Assert::AreEqual(0L, root.getLong("meas1_baseline")); - } - - TEST_METHOD(Act_Light) - { - Assert::AreEqual(20L, root.getLong("act_light")); - } - - TEST_METHOD(Pulsesize) - { - Assert::AreEqual(25L, root.getLong("pulsesize")); - } - - TEST_METHOD(Pulsedistance) - { - Assert::AreEqual(10000L, root.getLong("pulsedistance")); - } - - TEST_METHOD(Actintensity1) - { - Assert::AreEqual(50L, root.getLong("actintensity1")); - } - - TEST_METHOD(Actintensity2) - { - Assert::AreEqual(255L, root.getLong("actintensity2")); - } - - TEST_METHOD(Measintensity) - { - Assert::AreEqual(255L, root.getLong("measintensity")); - } - - TEST_METHOD(Calintensity) - { - Assert::AreEqual(255L, root.getLong("calintensity")); - } - - TEST_METHOD(Pulses) - { - // "pulses":[50,50,50] - - JsonArray array = root.getArray("pulses"); - Assert::IsTrue(array.success()); - - Assert::AreEqual(3, array.getLength()); - - for (int i = 0; i < 3; i++) - { - Assert::AreEqual(50L, array.getLong(i)); - } - } - - TEST_METHOD(Act) - { - // "act":[2,1,2,2] - - JsonArray array = root.getArray("act"); - Assert::IsTrue(array.success()); - - Assert::AreEqual(4, array.getLength()); - Assert::AreEqual(2L, array.getLong(0)); - Assert::AreEqual(1L, array.getLong(1)); - Assert::AreEqual(2L, array.getLong(2)); - Assert::AreEqual(2L, array.getLong(3)); - } - - TEST_METHOD(Detectors) - { - // "detectors":[[34,34,34,34],[34,34,34,34],[34,34,34,34],[34,34,34,34]] - - JsonArray array = root.getArray("detectors"); - Assert::IsTrue(array.success()); - Assert::AreEqual(4, array.getLength()); - - for (int i = 0; i < 4; i++) - { - Assert::AreEqual(4, array.getArray(i).getLength()); - - for (int j = 0; j < 4; j++) - Assert::AreEqual(34L, array.getArray(i).getLong(j)); - } - } - - TEST_METHOD(Alta) - { - // alta:[2,2,2,2] - - JsonArray array = root.getArray("alta"); - Assert::IsTrue(array.success()); - - Assert::AreEqual(4, array.getLength()); - - for (int i = 0; i < 4; i++) - { - Assert::AreEqual(2L, array.getLong(i)); - } - } - - TEST_METHOD(Altb) - { - // altb:[2,2,2,2] - - JsonArray array = root.getArray("altb"); - Assert::IsTrue(array.success()); - - Assert::AreEqual(4, array.getLength()); - - for (int i = 0; i < 4; i++) - { - Assert::AreEqual(2L, array.getLong(i)); - } - } - - TEST_METHOD(Measlights) - { - // "measlights":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]] - - JsonArray array = root.getArray("measlights"); - Assert::IsTrue(array.success()); - Assert::AreEqual(4, array.getLength()); - - for (int i = 0; i < 4; i++) - { - Assert::AreEqual(4, array.getArray(i).getLength()); - - for (int j = 0; j < 4; j++) - Assert::AreEqual(15L, array.getArray(i).getLong(j)); - } - } - - TEST_METHOD(Measlights2) - { - // "measlights2":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]] - - JsonArray array = root.getArray("measlights2"); - Assert::IsTrue(array.success()); - Assert::AreEqual(4, array.getLength()); - - for (int i = 0; i < 4; i++) - { - Assert::AreEqual(4, array.getArray(i).getLength()); - - for (int j = 0; j < 4; j++) - Assert::AreEqual(15L, array.getArray(i).getLong(j)); - } - } - - TEST_METHOD(Altc) - { - // altc:[2,2,2,2] - - JsonArray array = root.getArray("altc"); - Assert::IsTrue(array.success()); - - Assert::AreEqual(4, array.getLength()); - - for (int i = 0; i < 4; i++) - { - Assert::AreEqual(2L, array.getLong(i)); - } - } - - TEST_METHOD(Altd) - { - // altd:[2,2,2,2] - - JsonArray array = root.getArray("altd"); - Assert::IsTrue(array.success()); - - Assert::AreEqual(4, array.getLength()); - - for (int i = 0; i < 4; i++) - { - Assert::AreEqual(2L, array.getLong(i)); - } - } - }; -} \ No newline at end of file +*/ + +#include "CppUnitTest.h" +#include "JsonParser.h" +#include + +using namespace std; +using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace ArduinoJson::Parser; + +namespace ArduinoJsonParserTests +{ + TEST_CLASS(GbathreeBug) + { + char json[1024]; + JsonParser<200> parser; + JsonHashTable root; + + + public: + + TEST_METHOD_INITIALIZE(Initialize) + { + // BUG described here: + // http://forum.arduino.cc/index.php?topic=172578.msg1608219#msg1608219 + strcpy(json, "{ \"protocol_name\":\"fluorescence\",\"repeats\":1,\"wait\":0,\"averages\":1,\"measurements\":3,\"meas2_light\":15,\"meas1_baseline\":0,\"act_light\":20,\"pulsesize\":25,\"pulsedistance\":10000,\"actintensity1\":50,\"actintensity2\":255,\"measintensity\":255,\"calintensity\":255,\"pulses\":[50,50,50],\"act\":[2,1,2,2],\"red\":[2,2,2,2],\"detectors\":[[34,34,34,34],[34,34,34,34],[34,34,34,34],[34,34,34,34]],\"alta\":[2,2,2,2],\"altb\":[2,2,2,2],\"measlights\":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]],\"measlights2\":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]],\"altc\":[2,2,2,2],\"altd\":[2,2,2,2]}"); + root = parser.parseHashTable(json); + } + + TEST_METHOD(Root) + { + Assert::IsTrue(root.success()); + } + + TEST_METHOD(ProtocolName) + { + string protocol_name = root.getString("protocol_name"); + Assert::AreEqual(string("fluorescence"), protocol_name); + } + + TEST_METHOD(Repeats) + { + Assert::AreEqual(1L, root.getLong("repeats")); + } + + TEST_METHOD(Wait) + { + Assert::AreEqual(0L, root.getLong("wait")); + } + + TEST_METHOD(Measurements) + { + Assert::AreEqual(3L, root.getLong("measurements")); + } + + TEST_METHOD(Meas2_Light) + { + Assert::AreEqual(15L, root.getLong("meas2_light")); + } + + TEST_METHOD(Meas1_Baseline) + { + Assert::AreEqual(0L, root.getLong("meas1_baseline")); + } + + TEST_METHOD(Act_Light) + { + Assert::AreEqual(20L, root.getLong("act_light")); + } + + TEST_METHOD(Pulsesize) + { + Assert::AreEqual(25L, root.getLong("pulsesize")); + } + + TEST_METHOD(Pulsedistance) + { + Assert::AreEqual(10000L, root.getLong("pulsedistance")); + } + + TEST_METHOD(Actintensity1) + { + Assert::AreEqual(50L, root.getLong("actintensity1")); + } + + TEST_METHOD(Actintensity2) + { + Assert::AreEqual(255L, root.getLong("actintensity2")); + } + + TEST_METHOD(Measintensity) + { + Assert::AreEqual(255L, root.getLong("measintensity")); + } + + TEST_METHOD(Calintensity) + { + Assert::AreEqual(255L, root.getLong("calintensity")); + } + + TEST_METHOD(Pulses) + { + // "pulses":[50,50,50] + + JsonArray array = root.getArray("pulses"); + Assert::IsTrue(array.success()); + + Assert::AreEqual(3, array.getLength()); + + for (int i = 0; i < 3; i++) + { + Assert::AreEqual(50L, array.getLong(i)); + } + } + + TEST_METHOD(Act) + { + // "act":[2,1,2,2] + + JsonArray array = root.getArray("act"); + Assert::IsTrue(array.success()); + + Assert::AreEqual(4, array.getLength()); + Assert::AreEqual(2L, array.getLong(0)); + Assert::AreEqual(1L, array.getLong(1)); + Assert::AreEqual(2L, array.getLong(2)); + Assert::AreEqual(2L, array.getLong(3)); + } + + TEST_METHOD(Detectors) + { + // "detectors":[[34,34,34,34],[34,34,34,34],[34,34,34,34],[34,34,34,34]] + + JsonArray array = root.getArray("detectors"); + Assert::IsTrue(array.success()); + Assert::AreEqual(4, array.getLength()); + + for (int i = 0; i < 4; i++) + { + Assert::AreEqual(4, array.getArray(i).getLength()); + + for (int j = 0; j < 4; j++) + Assert::AreEqual(34L, array.getArray(i).getLong(j)); + } + } + + TEST_METHOD(Alta) + { + // alta:[2,2,2,2] + + JsonArray array = root.getArray("alta"); + Assert::IsTrue(array.success()); + + Assert::AreEqual(4, array.getLength()); + + for (int i = 0; i < 4; i++) + { + Assert::AreEqual(2L, array.getLong(i)); + } + } + + TEST_METHOD(Altb) + { + // altb:[2,2,2,2] + + JsonArray array = root.getArray("altb"); + Assert::IsTrue(array.success()); + + Assert::AreEqual(4, array.getLength()); + + for (int i = 0; i < 4; i++) + { + Assert::AreEqual(2L, array.getLong(i)); + } + } + + TEST_METHOD(Measlights) + { + // "measlights":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]] + + JsonArray array = root.getArray("measlights"); + Assert::IsTrue(array.success()); + Assert::AreEqual(4, array.getLength()); + + for (int i = 0; i < 4; i++) + { + Assert::AreEqual(4, array.getArray(i).getLength()); + + for (int j = 0; j < 4; j++) + Assert::AreEqual(15L, array.getArray(i).getLong(j)); + } + } + + TEST_METHOD(Measlights2) + { + // "measlights2":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]] + + JsonArray array = root.getArray("measlights2"); + Assert::IsTrue(array.success()); + Assert::AreEqual(4, array.getLength()); + + for (int i = 0; i < 4; i++) + { + Assert::AreEqual(4, array.getArray(i).getLength()); + + for (int j = 0; j < 4; j++) + Assert::AreEqual(15L, array.getArray(i).getLong(j)); + } + } + + TEST_METHOD(Altc) + { + // altc:[2,2,2,2] + + JsonArray array = root.getArray("altc"); + Assert::IsTrue(array.success()); + + Assert::AreEqual(4, array.getLength()); + + for (int i = 0; i < 4; i++) + { + Assert::AreEqual(2L, array.getLong(i)); + } + } + + TEST_METHOD(Altd) + { + // altd:[2,2,2,2] + + JsonArray array = root.getArray("altd"); + Assert::IsTrue(array.success()); + + Assert::AreEqual(4, array.getLength()); + + for (int i = 0; i < 4; i++) + { + Assert::AreEqual(2L, array.getLong(i)); + } + } + }; +} diff --git a/JsonParserTests/JsonArrayIteratorTests.cpp b/JsonParserTests/JsonArrayIteratorTests.cpp index 2e8dec4e..b3872fc7 100644 --- a/JsonParserTests/JsonArrayIteratorTests.cpp +++ b/JsonParserTests/JsonArrayIteratorTests.cpp @@ -1,67 +1,67 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#include "CppUnitTest.h" -#include "JsonParser.h" - -using namespace Microsoft::VisualStudio::CppUnitTestFramework; -using namespace ArduinoJson::Parser; - -namespace JsonParserTests -{ - TEST_CLASS(JsonArrayIteratorTests) - { - public: - - TEST_METHOD(EmptyJson) - { - char json[] = ""; - JsonParser<1> parser; - - JsonArray a = parser.parse(json); - - int loopCount = 0; - - for (long i : a) - { - loopCount++; - } - - Assert::AreEqual(0, loopCount); - } - - 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); - } - } - - TEST_METHOD(ThreeStrings) - { - char json[] = "[\"1\",\"2\",\"3\"]"; - char* expected[] = {"1", "2", "3"}; - JsonParser<4> parser; - - JsonArray a = parser.parse(json); - - int index = 0; - - for (const char* i : a) - { - Assert::AreEqual(expected[index++], i); - } - } - }; -} \ No newline at end of file +*/ + +#include "CppUnitTest.h" +#include "JsonParser.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace ArduinoJson::Parser; + +namespace JsonParserTests +{ + TEST_CLASS(JsonArrayIteratorTests) + { + public: + + TEST_METHOD(EmptyJson) + { + char json[] = ""; + JsonParser<1> parser; + + JsonArray a = parser.parse(json); + + int loopCount = 0; + + for (long i : a) + { + loopCount++; + } + + Assert::AreEqual(0, loopCount); + } + + 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); + } + } + + TEST_METHOD(ThreeStrings) + { + char json[] = "[\"1\",\"2\",\"3\"]"; + char* expected[] = {"1", "2", "3"}; + JsonParser<4> parser; + + JsonArray a = parser.parse(json); + + int index = 0; + + for (const char* i : a) + { + Assert::AreEqual(expected[index++], i); + } + } + }; +} diff --git a/JsonParserTests/JsonArrayTests.cpp b/JsonParserTests/JsonArrayTests.cpp index a9c7721f..09a3c7fc 100644 --- a/JsonParserTests/JsonArrayTests.cpp +++ b/JsonParserTests/JsonArrayTests.cpp @@ -1,181 +1,181 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#include "CppUnitTest.h" -#include "JsonParser.h" - -using namespace Microsoft::VisualStudio::CppUnitTestFramework; -using namespace ArduinoJson::Parser; - -namespace ArduinoJsonParserTests -{ - TEST_CLASS(JsonArrayTests) - { - JsonArray array; - char json[256]; +*/ + +#include "CppUnitTest.h" +#include "JsonParser.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace ArduinoJson::Parser; + +namespace ArduinoJsonParserTests +{ + TEST_CLASS(JsonArrayTests) + { + JsonArray array; + char json[256]; jsmntok_t tokens[32]; - JsonParserBase parser = JsonParserBase(tokens, 32); - - public: - - TEST_METHOD(TooFewClosingBrackets) - { - whenInputIs("[[]"); - parseMustFail(); - } - - TEST_METHOD(TooManyClosingBrackets) - { - whenInputIs("[]]"); - parseMustFail(); - } - - TEST_METHOD(EmptyArray) - { - whenInputIs("[]"); - parseMustSucceed(); - lengthMustBe(0); - } - - TEST_METHOD(NotEnoughTokens) - { - setTokenCountTo(2); - - whenInputIs("[1,2]"); - - parseMustFail(); - itemMustNotExist(0); - } - - TEST_METHOD(TwoIntegers) - { - setTokenCountTo(3); - - whenInputIs("[1,2]"); - - parseMustSucceed(); - lengthMustBe(2); - itemMustBe(0, 1L); - itemMustBe(1, 2L); - itemMustNotExist(2); - } - - TEST_METHOD(TwoBooleans) - { - setTokenCountTo(3); - - whenInputIs("[true,false]"); - - parseMustSucceed(); - lengthMustBe(2); - itemMustBe(0, true); - itemMustBe(1, false); - itemMustNotExist(2); - } - - TEST_METHOD(TwoStrings) - { - setTokenCountTo(3); - - whenInputIs("[\"hello\",\"world\"]"); - - parseMustSucceed(); - lengthMustBe(2); - itemMustBe(0, "hello"); - itemMustBe(1, "world"); - itemMustNotExist(2); - } - - TEST_METHOD(TwoDimensionsArray) - { - setTokenCountTo(7); - - whenInputIs("[[1,2],[3,4]]"); - - parseMustSucceed(); - lengthMustBe(2); - itemMustBe(0, 0, 1L); - itemMustBe(0, 1, 2L); - itemMustBe(1, 0, 3L); - itemMustBe(1, 1, 4L); - itemMustNotExist(2); - } - - TEST_METHOD(ThreeDimensionsArray) - { - setTokenCountTo(15); - - whenInputIs("[[[1,2],[3,4]],[[5,6],[7,8]]]"); - - parseMustSucceed(); - lengthMustBe(2); - itemMustBe(0, 0, 0, 1L); - itemMustBe(0, 0, 1, 2L); - itemMustBe(0, 1, 0, 3L); - itemMustBe(0, 1, 1, 4L); - itemMustBe(1, 0, 0, 5L); - itemMustBe(1, 0, 1, 6L); - itemMustBe(1, 1, 0, 7L); - itemMustBe(1, 1, 1, 8L); - itemMustNotExist(2); - } - - private: - + JsonParserBase parser = JsonParserBase(tokens, 32); + + public: + + TEST_METHOD(TooFewClosingBrackets) + { + whenInputIs("[[]"); + parseMustFail(); + } + + TEST_METHOD(TooManyClosingBrackets) + { + whenInputIs("[]]"); + parseMustFail(); + } + + TEST_METHOD(EmptyArray) + { + whenInputIs("[]"); + parseMustSucceed(); + lengthMustBe(0); + } + + TEST_METHOD(NotEnoughTokens) + { + setTokenCountTo(2); + + whenInputIs("[1,2]"); + + parseMustFail(); + itemMustNotExist(0); + } + + TEST_METHOD(TwoIntegers) + { + setTokenCountTo(3); + + whenInputIs("[1,2]"); + + parseMustSucceed(); + lengthMustBe(2); + itemMustBe(0, 1L); + itemMustBe(1, 2L); + itemMustNotExist(2); + } + + TEST_METHOD(TwoBooleans) + { + setTokenCountTo(3); + + whenInputIs("[true,false]"); + + parseMustSucceed(); + lengthMustBe(2); + itemMustBe(0, true); + itemMustBe(1, false); + itemMustNotExist(2); + } + + TEST_METHOD(TwoStrings) + { + setTokenCountTo(3); + + whenInputIs("[\"hello\",\"world\"]"); + + parseMustSucceed(); + lengthMustBe(2); + itemMustBe(0, "hello"); + itemMustBe(1, "world"); + itemMustNotExist(2); + } + + TEST_METHOD(TwoDimensionsArray) + { + setTokenCountTo(7); + + whenInputIs("[[1,2],[3,4]]"); + + parseMustSucceed(); + lengthMustBe(2); + itemMustBe(0, 0, 1L); + itemMustBe(0, 1, 2L); + itemMustBe(1, 0, 3L); + itemMustBe(1, 1, 4L); + itemMustNotExist(2); + } + + TEST_METHOD(ThreeDimensionsArray) + { + setTokenCountTo(15); + + whenInputIs("[[[1,2],[3,4]],[[5,6],[7,8]]]"); + + parseMustSucceed(); + lengthMustBe(2); + itemMustBe(0, 0, 0, 1L); + itemMustBe(0, 0, 1, 2L); + itemMustBe(0, 1, 0, 3L); + itemMustBe(0, 1, 1, 4L); + itemMustBe(1, 0, 0, 5L); + itemMustBe(1, 0, 1, 6L); + itemMustBe(1, 1, 0, 7L); + itemMustBe(1, 1, 1, 8L); + itemMustNotExist(2); + } + + private: + void setTokenCountTo(int n) { parser = JsonParserBase(tokens, n); - } - - void whenInputIs(const char* input) - { - strcpy(json, input); - array = parser.parseArray(json); - } - - void parseMustFail() - { - Assert::IsFalse(array.success()); - lengthMustBe(0); - } - - void parseMustSucceed() - { - Assert::IsTrue(array.success()); - } - - void lengthMustBe(int expected) - { - Assert::AreEqual(expected, array.getLength()); - } - - void itemMustBe(int index, long expected) - { - Assert::AreEqual(expected, array.getLong(index)); - } - - void itemMustBe(int index, bool expected) - { - Assert::AreEqual(expected, array.getBool(index)); - } - - void itemMustBe(int index, const char* expected) - { - Assert::AreEqual(expected, array.getString(index)); - } - - void itemMustBe(int index0, int index1, long expected) - { - Assert::AreEqual(expected, array.getArray(index0).getLong(index1)); - } - - void itemMustBe(int index0, int index1, int index2, long expected) - { - Assert::AreEqual(expected, array.getArray(index0).getArray(index1).getLong(index2)); - } - + } + + void whenInputIs(const char* input) + { + strcpy(json, input); + array = parser.parseArray(json); + } + + void parseMustFail() + { + Assert::IsFalse(array.success()); + lengthMustBe(0); + } + + void parseMustSucceed() + { + Assert::IsTrue(array.success()); + } + + void lengthMustBe(int expected) + { + Assert::AreEqual(expected, array.getLength()); + } + + void itemMustBe(int index, long expected) + { + Assert::AreEqual(expected, array.getLong(index)); + } + + void itemMustBe(int index, bool expected) + { + Assert::AreEqual(expected, array.getBool(index)); + } + + void itemMustBe(int index, const char* expected) + { + Assert::AreEqual(expected, array.getString(index)); + } + + void itemMustBe(int index0, int index1, long expected) + { + Assert::AreEqual(expected, array.getArray(index0).getLong(index1)); + } + + void itemMustBe(int index0, int index1, int index2, long expected) + { + Assert::AreEqual(expected, array.getArray(index0).getArray(index1).getLong(index2)); + } + void itemMustNotExist(int index) { Assert::IsFalse(array.getHashTable(index).success()); @@ -184,6 +184,6 @@ namespace ArduinoJsonParserTests Assert::AreEqual(0.0, array.getDouble(index)); Assert::AreEqual(0L, array.getLong(index)); Assert::IsNull(array.getString(index)); - } - }; -} \ No newline at end of file + } + }; +} diff --git a/JsonParserTests/JsonObjectIteratorTests.cpp b/JsonParserTests/JsonObjectIteratorTests.cpp index 2a88f504..72ab4ee9 100644 --- a/JsonParserTests/JsonObjectIteratorTests.cpp +++ b/JsonParserTests/JsonObjectIteratorTests.cpp @@ -1,71 +1,71 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#include "CppUnitTest.h" -#include "JsonParser.h" - -using namespace Microsoft::VisualStudio::CppUnitTestFramework; -using namespace ArduinoJson::Parser; - -namespace JsonParserTests -{ - TEST_CLASS(JsonObjectIteratorTests) - { - public: - - TEST_METHOD(EmptyObject) - { - char json [] = "{}"; - JsonParser<1> parser; - - JsonHashTable a = parser.parse(json); - - int loopCount = 0; - - for (auto i : a) - { - loopCount++; - } - - Assert::AreEqual(0, loopCount); - } - - TEST_METHOD(EmptyJson) - { - char json[] = ""; - JsonParser<1> parser; - - JsonHashTable a = parser.parse(json); - - int loopCount = 0; - - for (auto i : a) - { - loopCount++; - } - - Assert::AreEqual(0, loopCount); - } - - 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 (auto i : a) - { - Assert::AreEqual(expectedKeys[index], i.key()); - Assert::AreEqual(expectedValues[index], (const char*) i.value()); - index++; - } - } - }; -} \ No newline at end of file +*/ + +#include "CppUnitTest.h" +#include "JsonParser.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace ArduinoJson::Parser; + +namespace JsonParserTests +{ + TEST_CLASS(JsonObjectIteratorTests) + { + public: + + TEST_METHOD(EmptyObject) + { + char json [] = "{}"; + JsonParser<1> parser; + + JsonHashTable a = parser.parse(json); + + int loopCount = 0; + + for (auto i : a) + { + loopCount++; + } + + Assert::AreEqual(0, loopCount); + } + + TEST_METHOD(EmptyJson) + { + char json[] = ""; + JsonParser<1> parser; + + JsonHashTable a = parser.parse(json); + + int loopCount = 0; + + for (auto i : a) + { + loopCount++; + } + + Assert::AreEqual(0, loopCount); + } + + 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 (auto i : a) + { + Assert::AreEqual(expectedKeys[index], i.key()); + Assert::AreEqual(expectedValues[index], (const char*) i.value()); + index++; + } + } + }; +} diff --git a/JsonParserTests/JsonObjectTests.cpp b/JsonParserTests/JsonObjectTests.cpp index 6b0e6be8..77c7759f 100644 --- a/JsonParserTests/JsonObjectTests.cpp +++ b/JsonParserTests/JsonObjectTests.cpp @@ -1,165 +1,165 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#include "CppUnitTest.h" -#include "JsonParser.h" -#include - -using namespace std; -using namespace Microsoft::VisualStudio::CppUnitTestFramework; -using namespace ArduinoJson::Parser; - -namespace ArduinoJsonParserTests -{ - TEST_CLASS(JsonHashTableTests) - { - JsonHashTable hashTable; - JsonArray nestedArray; - char json[256]; - jsmntok_t tokens[32]; - JsonParserBase parser = JsonParserBase(tokens, 32); - - public: - - TEST_METHOD(EmptyHashTable) - { - whenInputIs("{}"); - parseMustSucceed(); - } - - TEST_METHOD(NotEnoughTokens) - { - setTokenCountTo(2); - - whenInputIs("{\"key\":0}"); - - parseMustFail(); - itemMustNotExist("key"); - } - - TEST_METHOD(TwoIntegers) - { - setTokenCountTo(5); - - whenInputIs("{\"key1\":1,\"key2\":2}"); - - parseMustSucceed(); - itemMustBe("key1", 1L); - itemMustBe("key2", 2L); - itemMustNotExist("key3"); - } - - TEST_METHOD(TwoBooleans) - { - setTokenCountTo(5); - - whenInputIs("{\"key1\":true,\"key2\":false}"); - - parseMustSucceed(); - itemMustBe("key1", true); - itemMustBe("key2", false); - itemMustNotExist("key3"); - } - - TEST_METHOD(TwoStrings) - { - setTokenCountTo(5); - - whenInputIs("{\"key1\":\"hello\",\"key2\":\"world\"}"); - - parseMustSucceed(); - itemMustBe("key1", "hello"); - itemMustBe("key2", "world"); - itemMustNotExist("key3"); - } - - TEST_METHOD(TwoNestedArrays) - { - setTokenCountTo(9); - - whenInputIs("{\"key1\":[1,2],\"key2\":[3,4]}"); - parseMustSucceed(); - - itemMustBeAnArray("key1"); - arrayLengthMustBe(2); - arrayItemMustBe(0, 1L); - arrayItemMustBe(1, 2L); - arrayItemMustBe(2, 0L); - - itemMustBeAnArray("key2"); - arrayLengthMustBe(2); - arrayItemMustBe(0, 3L); - arrayItemMustBe(1, 4L); - arrayItemMustBe(2, 0L); - - itemMustNotExist("key3"); - } - - private: - - void setTokenCountTo(int n) - { - parser = JsonParserBase(tokens, n); - } - - void whenInputIs(const char* input) - { - strcpy(json, input); - hashTable = parser.parseHashTable(json); - } - - void parseMustFail() - { - Assert::IsFalse(hashTable.success()); - } - - void parseMustSucceed() - { - Assert::IsTrue(hashTable.success()); - } - - void itemMustBe(const char* key, long expected) - { - Assert::AreEqual(expected, hashTable.getLong(key)); - } - - void itemMustBe(const char* key, bool expected) - { - Assert::AreEqual(expected, hashTable.getBool(key)); - } - - void itemMustBe(const char* key, const char* expected) - { - Assert::AreEqual(expected, hashTable.getString(key)); - } - - void itemMustNotExist(const char* key) - { - Assert::IsFalse(hashTable.containsKey(key)); - Assert::IsFalse(hashTable.getHashTable(key).success()); - Assert::IsFalse(hashTable.getArray(key).success()); - Assert::IsFalse(hashTable.getBool(key)); - Assert::AreEqual(0.0, hashTable.getDouble(key)); - Assert::AreEqual(0L, hashTable.getLong(key)); - Assert::IsNull(hashTable.getString(key)); - } - - void itemMustBeAnArray(const char* key) - { - nestedArray = hashTable.getArray(key); - Assert::IsTrue(nestedArray.success()); - } - - void arrayLengthMustBe(int expected) - { - Assert::AreEqual(expected, nestedArray.getLength()); - } - - void arrayItemMustBe(int index, long expected) - { - Assert::AreEqual(expected, nestedArray.getLong(index)); - } - }; -} \ No newline at end of file +*/ + +#include "CppUnitTest.h" +#include "JsonParser.h" +#include + +using namespace std; +using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace ArduinoJson::Parser; + +namespace ArduinoJsonParserTests +{ + TEST_CLASS(JsonHashTableTests) + { + JsonHashTable hashTable; + JsonArray nestedArray; + char json[256]; + jsmntok_t tokens[32]; + JsonParserBase parser = JsonParserBase(tokens, 32); + + public: + + TEST_METHOD(EmptyHashTable) + { + whenInputIs("{}"); + parseMustSucceed(); + } + + TEST_METHOD(NotEnoughTokens) + { + setTokenCountTo(2); + + whenInputIs("{\"key\":0}"); + + parseMustFail(); + itemMustNotExist("key"); + } + + TEST_METHOD(TwoIntegers) + { + setTokenCountTo(5); + + whenInputIs("{\"key1\":1,\"key2\":2}"); + + parseMustSucceed(); + itemMustBe("key1", 1L); + itemMustBe("key2", 2L); + itemMustNotExist("key3"); + } + + TEST_METHOD(TwoBooleans) + { + setTokenCountTo(5); + + whenInputIs("{\"key1\":true,\"key2\":false}"); + + parseMustSucceed(); + itemMustBe("key1", true); + itemMustBe("key2", false); + itemMustNotExist("key3"); + } + + TEST_METHOD(TwoStrings) + { + setTokenCountTo(5); + + whenInputIs("{\"key1\":\"hello\",\"key2\":\"world\"}"); + + parseMustSucceed(); + itemMustBe("key1", "hello"); + itemMustBe("key2", "world"); + itemMustNotExist("key3"); + } + + TEST_METHOD(TwoNestedArrays) + { + setTokenCountTo(9); + + whenInputIs("{\"key1\":[1,2],\"key2\":[3,4]}"); + parseMustSucceed(); + + itemMustBeAnArray("key1"); + arrayLengthMustBe(2); + arrayItemMustBe(0, 1L); + arrayItemMustBe(1, 2L); + arrayItemMustBe(2, 0L); + + itemMustBeAnArray("key2"); + arrayLengthMustBe(2); + arrayItemMustBe(0, 3L); + arrayItemMustBe(1, 4L); + arrayItemMustBe(2, 0L); + + itemMustNotExist("key3"); + } + + private: + + void setTokenCountTo(int n) + { + parser = JsonParserBase(tokens, n); + } + + void whenInputIs(const char* input) + { + strcpy(json, input); + hashTable = parser.parseHashTable(json); + } + + void parseMustFail() + { + Assert::IsFalse(hashTable.success()); + } + + void parseMustSucceed() + { + Assert::IsTrue(hashTable.success()); + } + + void itemMustBe(const char* key, long expected) + { + Assert::AreEqual(expected, hashTable.getLong(key)); + } + + void itemMustBe(const char* key, bool expected) + { + Assert::AreEqual(expected, hashTable.getBool(key)); + } + + void itemMustBe(const char* key, const char* expected) + { + Assert::AreEqual(expected, hashTable.getString(key)); + } + + void itemMustNotExist(const char* key) + { + Assert::IsFalse(hashTable.containsKey(key)); + Assert::IsFalse(hashTable.getHashTable(key).success()); + Assert::IsFalse(hashTable.getArray(key).success()); + Assert::IsFalse(hashTable.getBool(key)); + Assert::AreEqual(0.0, hashTable.getDouble(key)); + Assert::AreEqual(0L, hashTable.getLong(key)); + Assert::IsNull(hashTable.getString(key)); + } + + void itemMustBeAnArray(const char* key) + { + nestedArray = hashTable.getArray(key); + Assert::IsTrue(nestedArray.success()); + } + + void arrayLengthMustBe(int expected) + { + Assert::AreEqual(expected, nestedArray.getLength()); + } + + void arrayItemMustBe(int index, long expected) + { + Assert::AreEqual(expected, nestedArray.getLong(index)); + } + }; +} diff --git a/JsonParserTests/JsonStringTests.cpp b/JsonParserTests/JsonStringTests.cpp index 55df13a0..ac6394e8 100644 --- a/JsonParserTests/JsonStringTests.cpp +++ b/JsonParserTests/JsonStringTests.cpp @@ -1,107 +1,107 @@ /* * Arduino JSON library * Benoit Blanchon 2014 - MIT License -*/ - -#include "CppUnitTest.h" -#include "JsonParser.h" - -using namespace Microsoft::VisualStudio::CppUnitTestFramework; -using namespace ArduinoJson::Parser; - -namespace ArduinoJsonParserTests -{ - TEST_CLASS(JsonStringTests) - { - const char* actual; +*/ + +#include "CppUnitTest.h" +#include "JsonParser.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace ArduinoJson::Parser; + +namespace ArduinoJsonParserTests +{ + TEST_CLASS(JsonStringTests) + { + const char* actual; char json[256]; - JsonParser<32> parser; - - public: - - TEST_METHOD(EmptyString) - { - whenInputIs(""); - outputMustBe(0); - } - - TEST_METHOD(JustOneQuote) - { - whenInputIs("\""); - outputMustBe(0); - } - - TEST_METHOD(SimpleString) - { - whenInputIs("\"Hi!\""); - outputMustBe("Hi!"); - } - - TEST_METHOD(EscapedQuote) - { - whenInputIs("\"12\\\"34\""); // ie 12\"34 - outputMustBe("12\"34"); - } - - TEST_METHOD(EscapedReverseSolidus) - { - whenInputIs("\"12\\\\34\""); // ie 12\\34 - outputMustBe("12\\34"); - } - - TEST_METHOD(EscapedSolidus) - { - whenInputIs("\"12\\/34\""); - outputMustBe("12/34"); - } - - TEST_METHOD(EscapedBackspace) - { - whenInputIs("\"12\\b34\""); - outputMustBe("12\b34"); - } - - TEST_METHOD(EscapedFormfeed) - { - whenInputIs("\"12\\f34\""); - outputMustBe("12\f34"); - } - - TEST_METHOD(EscapedNewline) - { - whenInputIs("\"12\\n34\""); - outputMustBe("12\n34"); - } - - TEST_METHOD(EscapedCarriageReturn) - { - whenInputIs("\"12\\r34\""); - outputMustBe("12\r34"); - } - - TEST_METHOD(EscapedTab) - { - whenInputIs("\"12\\t34\""); - outputMustBe("12\t34"); - } - - TEST_METHOD(AllEscapedCharsTogether) - { - whenInputIs("\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\""); - outputMustBe("1\"2\\3/4\b5\f6\n7\r8\t9"); - } - - private: - - void whenInputIs(const char* input) - { - strcpy(json, input); - actual = parser.parse(json); - } - - void outputMustBe(const char* expected) - { - Assert::AreEqual(expected, actual); - } - }; -} \ No newline at end of file + JsonParser<32> parser; + + public: + + TEST_METHOD(EmptyString) + { + whenInputIs(""); + outputMustBe(0); + } + + TEST_METHOD(JustOneQuote) + { + whenInputIs("\""); + outputMustBe(0); + } + + TEST_METHOD(SimpleString) + { + whenInputIs("\"Hi!\""); + outputMustBe("Hi!"); + } + + TEST_METHOD(EscapedQuote) + { + whenInputIs("\"12\\\"34\""); // ie 12\"34 + outputMustBe("12\"34"); + } + + TEST_METHOD(EscapedReverseSolidus) + { + whenInputIs("\"12\\\\34\""); // ie 12\\34 + outputMustBe("12\\34"); + } + + TEST_METHOD(EscapedSolidus) + { + whenInputIs("\"12\\/34\""); + outputMustBe("12/34"); + } + + TEST_METHOD(EscapedBackspace) + { + whenInputIs("\"12\\b34\""); + outputMustBe("12\b34"); + } + + TEST_METHOD(EscapedFormfeed) + { + whenInputIs("\"12\\f34\""); + outputMustBe("12\f34"); + } + + TEST_METHOD(EscapedNewline) + { + whenInputIs("\"12\\n34\""); + outputMustBe("12\n34"); + } + + TEST_METHOD(EscapedCarriageReturn) + { + whenInputIs("\"12\\r34\""); + outputMustBe("12\r34"); + } + + TEST_METHOD(EscapedTab) + { + whenInputIs("\"12\\t34\""); + outputMustBe("12\t34"); + } + + TEST_METHOD(AllEscapedCharsTogether) + { + whenInputIs("\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\""); + outputMustBe("1\"2\\3/4\b5\f6\n7\r8\t9"); + } + + private: + + void whenInputIs(const char* input) + { + strcpy(json, input); + actual = parser.parse(json); + } + + void outputMustBe(const char* expected) + { + Assert::AreEqual(expected, actual); + } + }; +} diff --git a/JsonParserTests/TestHashGenerator.cpp b/JsonParserTests/TestHashGenerator.cpp index 754e2119..86389bde 100644 --- a/JsonParserTests/TestHashGenerator.cpp +++ b/JsonParserTests/TestHashGenerator.cpp @@ -1,25 +1,25 @@ -#include "stdafx.h" -#include "CppUnitTest.h" - -using namespace Microsoft::VisualStudio::CppUnitTestFramework; - -namespace ArduinoJsonParserTests -{ - TEST_CLASS(TestHashGenerator) - { - public: - - TEST_METHOD(TestMethod1) - { - JsonArray<5> arr; - arr.Add(1); - arr.Add("Hi!"); - - JsonHashTable<4> hash; - hash.Add("key1", 1); - hash.Add("key2", "Hello!"); - hash.Add("key3", arr); - } - - }; -} \ No newline at end of file +#include "stdafx.h" +#include "CppUnitTest.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +namespace ArduinoJsonParserTests +{ + TEST_CLASS(TestHashGenerator) + { + public: + + TEST_METHOD(TestMethod1) + { + JsonArray<5> arr; + arr.Add(1); + arr.Add("Hi!"); + + JsonHashTable<4> hash; + hash.Add("key1", 1); + hash.Add("key2", "Hello!"); + hash.Add("key3", arr); + } + + }; +}