diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d8d9eb2..888fc757 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ HEAD ---- * Added operator `==` to compare `JsonVariant` and strings (issue #402) +* Added support for `Stream` (issue #300) * Reduced memory consumption by not duplicating spaces and comments v5.7.3 diff --git a/README.md b/README.md index 4bd03fd2..b8ac1dd7 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,10 @@ Arduino JSON library *An elegant and efficient JSON library for embedded systems.* -It's designed to have the most intuitive API, the smallest footprint and works without any allocation on the heap (no malloc). +It's designed to have the most intuitive API, the smallest footprint and is able to work without any allocation on the heap (no malloc). It has been written with Arduino in mind, but it isn't linked to Arduino libraries so you can use this library in any other C++ project. +For instance, it supports Aduino's `String` and `Stream`, but also `std::string`, `std::istream` and `std::ostream`. Features -------- @@ -55,6 +56,8 @@ double latitude = root["data"][0]; double longitude = root["data"][1]; ``` +[See JsonParserExample.ino](examples/JsonParserExample/JsonParserExample.ino) + #### Encoding / Generating ```c++ @@ -73,6 +76,8 @@ root.printTo(Serial); // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]} ``` +[See JsonGeneratorExample.ino](examples/JsonGeneratorExample/JsonGeneratorExample.ino) + Documentation ------------- diff --git a/examples/JsonHttpClient/JsonHttpClient.ino b/examples/JsonHttpClient/JsonHttpClient.ino index d6532bbd..cf868fc2 100644 --- a/examples/JsonHttpClient/JsonHttpClient.ino +++ b/examples/JsonHttpClient/JsonHttpClient.ino @@ -9,8 +9,8 @@ // If you like this project, please add a star! #include -#include #include +#include EthernetClient client; @@ -36,11 +36,8 @@ void setup() { void loop() { if (connect(server)) { if (sendRequest(server, resource) && skipResponseHeaders()) { - char response[MAX_CONTENT_SIZE]; - readReponseContent(response, sizeof(response)); - UserData userData; - if (parseUserData(response, &userData)) { + if (readReponseContent(&userData)) { printUserData(&userData); } } @@ -89,7 +86,7 @@ bool sendRequest(const char* host, const char* resource) { client.print(resource); client.println(" HTTP/1.0"); client.print("Host: "); - client.println(server); + client.println(host); client.println("Connection: close"); client.println(); @@ -111,13 +108,6 @@ bool skipResponseHeaders() { return ok; } -// Read the body of the response from the HTTP server -void readReponseContent(char* content, size_t maxSize) { - size_t length = client.readBytes(content, maxSize); - content[length] = 0; - Serial.println(content); -} - // Parse the JSON from the input string and extract the interesting values // Here is the JSON we need to parse // { @@ -143,21 +133,20 @@ void readReponseContent(char* content, size_t maxSize) { // "bs": "harness real-time e-markets" // } // } -bool parseUserData(char* content, struct UserData* userData) { +bool readReponseContent(struct UserData* userData) { // Compute optimal size of the JSON buffer according to what we need to parse. // This is only required if you use StaticJsonBuffer. const size_t BUFFER_SIZE = - JSON_OBJECT_SIZE(8) // the root object has 8 elements - + JSON_OBJECT_SIZE(5) // the "address" object has 5 elements - + JSON_OBJECT_SIZE(2) // the "geo" object has 2 elements - + JSON_OBJECT_SIZE(3); // the "company" object has 3 elements + JSON_OBJECT_SIZE(8) // the root object has 8 elements + + JSON_OBJECT_SIZE(5) // the "address" object has 5 elements + + JSON_OBJECT_SIZE(2) // the "geo" object has 2 elements + + JSON_OBJECT_SIZE(3) // the "company" object has 3 elements + + MAX_CONTENT_SIZE; // additional space for strings - // Allocate a temporary memory pool on the stack - StaticJsonBuffer jsonBuffer; - // If the memory pool is too big for the stack, use this instead: - // DynamicJsonBuffer jsonBuffer; + // Allocate a temporary memory pool + DynamicJsonBuffer jsonBuffer(BUFFER_SIZE); - JsonObject& root = jsonBuffer.parseObject(content); + JsonObject& root = jsonBuffer.parseObject(client); if (!root.success()) { Serial.println("JSON parsing failed!"); diff --git a/include/ArduinoJson/Configuration.hpp b/include/ArduinoJson/Configuration.hpp index 315a2c1b..feb0e0e5 100644 --- a/include/ArduinoJson/Configuration.hpp +++ b/include/ArduinoJson/Configuration.hpp @@ -27,6 +27,10 @@ #define ARDUINOJSON_ENABLE_ARDUINO_STRING 1 #endif +#ifndef ARDUINOJSON_ENABLE_ARDUINO_STREAM +#define ARDUINOJSON_ENABLE_ARDUINO_STREAM 1 +#endif + // On AVR archiecture, we can use PROGMEM #ifndef ARDUINOJSON_ENABLE_PROGMEM #ifdef PROGMEM @@ -106,6 +110,11 @@ #define ARDUINOJSON_ENABLE_STD_STREAM 1 #endif +// on a computer, there is no reason to beleive Arduino Stream is available +#ifndef ARDUINOJSON_ENABLE_ARDUINO_STREAM +#define ARDUINOJSON_ENABLE_ARDUINO_STREAM 0 +#endif + #ifndef ARDUINOJSON_ENABLE_ALIGNMENT // even if not required, most cpu's are faster with aligned pointers #define ARDUINOJSON_ENABLE_ALIGNMENT 1 diff --git a/include/ArduinoJson/Data/StringFuncs.hpp b/include/ArduinoJson/Data/StringFuncs.hpp deleted file mode 100644 index a899ab76..00000000 --- a/include/ArduinoJson/Data/StringFuncs.hpp +++ /dev/null @@ -1,148 +0,0 @@ -// Copyright Benoit Blanchon 2014-2016 -// MIT License -// -// Arduino JSON library -// https://github.com/bblanchon/ArduinoJson -// If you like this project, please add a star! - -#pragma once - -#include "../Configuration.hpp" - -#if ARDUINOJSON_ENABLE_ARDUINO_STRING -#include -#endif - -#if ARDUINOJSON_ENABLE_STD_STRING -#include -#endif - -namespace ArduinoJson { -namespace Internals { - -template -struct StringFuncs {}; - -template -struct StringFuncs : StringFuncs {}; - -template -struct StringFuncs : StringFuncs {}; - -struct CharPtrFuncs { - class Iterator { - const char* _ptr; - - public: - Iterator(const char* ptr) : _ptr(ptr ? ptr : "") {} - - char next() { - return *_ptr++; - } - }; - - static bool equals(const char* str, const char* expected) { - return strcmp(str, expected) == 0; - } - - template - static char* duplicate(const char* str, Buffer* buffer) { - if (!str) return NULL; - size_t size = strlen(str) + 1; - void* dup = buffer->alloc(size); - if (dup != NULL) memcpy(dup, str, size); - return static_cast(dup); - } - - static const bool has_append = false; - static const bool has_equals = true; - static const bool should_duplicate = false; -}; - -template <> -struct StringFuncs : CharPtrFuncs {}; - -template <> -struct StringFuncs : CharPtrFuncs {}; - -template -struct StringFuncs : CharPtrFuncs {}; - -template -struct StringFuncs : CharPtrFuncs {}; - -template -struct StdStringFuncs { - template - static char* duplicate(const TString& str, Buffer* buffer) { - if (!str.c_str()) return NULL; // <- Arduino string can return NULL - size_t size = str.length() + 1; - void* dup = buffer->alloc(size); - if (dup != NULL) memcpy(dup, str.c_str(), size); - return static_cast(dup); - } - - struct Iterator : CharPtrFuncs::Iterator { - Iterator(const TString& str) : CharPtrFuncs::Iterator(str.c_str()) {} - }; - - static bool equals(const TString& str, const char* expected) { - return str == expected; - } - - static void append(TString& str, char c) { - str += c; - } - - static const bool has_append = true; - static const bool has_equals = true; - static const bool should_duplicate = true; -}; - -#if ARDUINOJSON_ENABLE_ARDUINO_STRING -template <> -struct StringFuncs : StdStringFuncs {}; -template <> -struct StringFuncs : StdStringFuncs {}; -#endif - -#if ARDUINOJSON_ENABLE_STD_STRING -template <> -struct StringFuncs : StdStringFuncs {}; -#endif - -#if ARDUINOJSON_ENABLE_PROGMEM -template <> -struct StringFuncs { - class Iterator { - const char* _ptr; - - public: - Iterator(const __FlashStringHelper* ptr) - : _ptr(reinterpret_cast(ptr)) {} - - char next() { - return pgm_read_byte_near(_ptr++); - } - }; - - static bool equals(const __FlashStringHelper* str, const char* expected) { - return strcmp_P(expected, (PGM_P)str) == 0; - } - - template - static char* duplicate(const __FlashStringHelper* str, Buffer* buffer) { - if (!str) return NULL; - size_t size = strlen_P((PGM_P)str) + 1; - void* dup = buffer->alloc(size); - if (dup != NULL) memcpy_P(dup, (PGM_P)str, size); - return static_cast(dup); - } - - static const bool has_append = false; - static const bool has_equals = true; - static const bool should_duplicate = true; -}; -#endif -} -} diff --git a/include/ArduinoJson/Data/ValueSetter.hpp b/include/ArduinoJson/Data/ValueSetter.hpp index 418c0022..fc007c9a 100644 --- a/include/ArduinoJson/Data/ValueSetter.hpp +++ b/include/ArduinoJson/Data/ValueSetter.hpp @@ -9,8 +9,8 @@ #include "../JsonBuffer.hpp" #include "../JsonVariant.hpp" +#include "../StringTraits/StringTraits.hpp" #include "../TypeTraits/EnableIf.hpp" -#include "StringFuncs.hpp" namespace ArduinoJson { namespace Internals { diff --git a/include/ArduinoJson/Deserialization/JsonParser.hpp b/include/ArduinoJson/Deserialization/JsonParser.hpp index b4aa771a..c27534ee 100644 --- a/include/ArduinoJson/Deserialization/JsonParser.hpp +++ b/include/ArduinoJson/Deserialization/JsonParser.hpp @@ -77,7 +77,7 @@ struct JsonParserBuilder { typedef typename Internals::StringFuncs::Iterator InputIterator; typedef JsonParser, TJsonBuffer &> TParser; - static TParser makeParser(TJsonBuffer *buffer, const TString &json, + static TParser makeParser(TJsonBuffer *buffer, TString &json, uint8_t nestingLimit) { return TParser(buffer, InputIterator(json), *buffer, nestingLimit); } @@ -94,9 +94,9 @@ struct JsonParserBuilder { } }; -template -struct JsonParserBuilder - : JsonParserBuilder {}; +template +struct JsonParserBuilder + : JsonParserBuilder {}; template inline typename JsonParserBuilder::TParser makeParser( diff --git a/include/ArduinoJson/JsonArray.hpp b/include/ArduinoJson/JsonArray.hpp index f60a286a..f4ad0edb 100644 --- a/include/ArduinoJson/JsonArray.hpp +++ b/include/ArduinoJson/JsonArray.hpp @@ -10,10 +10,10 @@ #include "Data/JsonBufferAllocated.hpp" #include "Data/List.hpp" #include "Data/ReferenceType.hpp" -#include "Data/StringFuncs.hpp" #include "Data/ValueSetter.hpp" #include "JsonVariant.hpp" #include "Serialization/JsonPrintable.hpp" +#include "StringTraits/StringTraits.hpp" #include "TypeTraits/ConstRefOrConstPtr.hpp" #include "TypeTraits/EnableIf.hpp" #include "TypeTraits/IsFloatingPoint.hpp" diff --git a/include/ArduinoJson/JsonObject.hpp b/include/ArduinoJson/JsonObject.hpp index e27cb959..ab8cbf7f 100644 --- a/include/ArduinoJson/JsonObject.hpp +++ b/include/ArduinoJson/JsonObject.hpp @@ -10,10 +10,10 @@ #include "Data/JsonBufferAllocated.hpp" #include "Data/List.hpp" #include "Data/ReferenceType.hpp" -#include "Data/StringFuncs.hpp" #include "Data/ValueSetter.hpp" #include "JsonPair.hpp" #include "Serialization/JsonPrintable.hpp" +#include "StringTraits/StringTraits.hpp" #include "TypeTraits/ConstRefOrConstPtr.hpp" #include "TypeTraits/EnableIf.hpp" #include "TypeTraits/IsFloatingPoint.hpp" diff --git a/include/ArduinoJson/JsonVariantComparisons.hpp b/include/ArduinoJson/JsonVariantComparisons.hpp index c9e7204a..6c038339 100644 --- a/include/ArduinoJson/JsonVariantComparisons.hpp +++ b/include/ArduinoJson/JsonVariantComparisons.hpp @@ -7,8 +7,8 @@ #pragma once -#include "Data/StringFuncs.hpp" #include "JsonVariantBase.hpp" +#include "StringTraits/StringTraits.hpp" #include "TypeTraits/EnableIf.hpp" namespace ArduinoJson { diff --git a/include/ArduinoJson/Serialization/DynamicStringBuilder.hpp b/include/ArduinoJson/Serialization/DynamicStringBuilder.hpp index 4651fbda..899a2989 100644 --- a/include/ArduinoJson/Serialization/DynamicStringBuilder.hpp +++ b/include/ArduinoJson/Serialization/DynamicStringBuilder.hpp @@ -7,8 +7,8 @@ #pragma once -#include "../Data/StringFuncs.hpp" #include "../Print.hpp" +#include "../StringTraits/StringTraits.hpp" namespace ArduinoJson { namespace Internals { diff --git a/include/ArduinoJson/StringTraits/ArduinoStream.hpp b/include/ArduinoJson/StringTraits/ArduinoStream.hpp new file mode 100644 index 00000000..977588c5 --- /dev/null +++ b/include/ArduinoJson/StringTraits/ArduinoStream.hpp @@ -0,0 +1,39 @@ +// Copyright Benoit Blanchon 2014-2016 +// MIT License +// +// Arduino JSON library +// https://github.com/bblanchon/ArduinoJson +// If you like this project, please add a star! + +#pragma once + +#include "../TypeTraits/EnableIf.hpp" +#include "../TypeTraits/IsBaseOf.hpp" +#include "../TypeTraits/RemoveReference.hpp" + +namespace ArduinoJson { +namespace Internals { + +struct StdStreamFuncs { + class Iterator { + Stream& _stream; + + public: + Iterator(Stream& stream) : _stream(stream) {} + + char next() { + int n = _stream.read(); + return n >= 0 ? static_cast(n) : '\0'; + } + }; +}; + +template +struct StringFuncs::type>::value>::type> + : StdStreamFuncs {}; +} +} diff --git a/include/ArduinoJson/StringTraits/CharPointer.hpp b/include/ArduinoJson/StringTraits/CharPointer.hpp new file mode 100644 index 00000000..b76ef647 --- /dev/null +++ b/include/ArduinoJson/StringTraits/CharPointer.hpp @@ -0,0 +1,57 @@ +// Copyright Benoit Blanchon 2014-2016 +// MIT License +// +// Arduino JSON library +// https://github.com/bblanchon/ArduinoJson +// If you like this project, please add a star! + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +struct CharPtrFuncs { + class Iterator { + const char* _ptr; + + public: + Iterator(const char* ptr) : _ptr(ptr ? ptr : "") {} + + char next() { + char c = *_ptr; + if (c) ++_ptr; + return c; + } + }; + + static bool equals(const char* str, const char* expected) { + return strcmp(str, expected) == 0; + } + + template + static char* duplicate(const char* str, Buffer* buffer) { + if (!str) return NULL; + size_t size = strlen(str) + 1; + void* dup = buffer->alloc(size); + if (dup != NULL) memcpy(dup, str, size); + return static_cast(dup); + } + + static const bool has_append = false; + static const bool has_equals = true; + static const bool should_duplicate = false; +}; + +template <> +struct StringFuncs : CharPtrFuncs {}; + +template <> +struct StringFuncs : CharPtrFuncs {}; + +template +struct StringFuncs : CharPtrFuncs {}; + +template +struct StringFuncs : CharPtrFuncs {}; +} +} diff --git a/include/ArduinoJson/StringTraits/FlashString.hpp b/include/ArduinoJson/StringTraits/FlashString.hpp new file mode 100644 index 00000000..e7e66ad0 --- /dev/null +++ b/include/ArduinoJson/StringTraits/FlashString.hpp @@ -0,0 +1,44 @@ +// Copyright Benoit Blanchon 2014-2016 +// MIT License +// +// Arduino JSON library +// https://github.com/bblanchon/ArduinoJson +// If you like this project, please add a star! + +#pragma once + +namespace ArduinoJson { +namespace Internals { +template <> +struct StringFuncs { + class Iterator { + const char* _ptr; + + public: + Iterator(const __FlashStringHelper* ptr) + : _ptr(reinterpret_cast(ptr)) {} + + char next() { + return pgm_read_byte_near(_ptr++); + } + }; + + static bool equals(const __FlashStringHelper* str, const char* expected) { + return strcmp_P(expected, (PGM_P)str) == 0; + } + + template + static char* duplicate(const __FlashStringHelper* str, Buffer* buffer) { + if (!str) return NULL; + size_t size = strlen_P((PGM_P)str) + 1; + void* dup = buffer->alloc(size); + if (dup != NULL) memcpy_P(dup, (PGM_P)str, size); + return static_cast(dup); + } + + static const bool has_append = false; + static const bool has_equals = true; + static const bool should_duplicate = true; +}; +} +} diff --git a/include/ArduinoJson/StringTraits/StdStream.hpp b/include/ArduinoJson/StringTraits/StdStream.hpp new file mode 100644 index 00000000..a1c9f2ea --- /dev/null +++ b/include/ArduinoJson/StringTraits/StdStream.hpp @@ -0,0 +1,42 @@ +// Copyright Benoit Blanchon 2014-2016 +// MIT License +// +// Arduino JSON library +// https://github.com/bblanchon/ArduinoJson +// If you like this project, please add a star! + +#pragma once + +#include +#include "../TypeTraits/EnableIf.hpp" +#include "../TypeTraits/IsBaseOf.hpp" +#include "../TypeTraits/RemoveReference.hpp" + +namespace ArduinoJson { +namespace Internals { + +struct StdStreamFuncs { + class Iterator { + std::istream& _stream; + + public: + Iterator(std::istream& stream) : _stream(stream) {} + + char next() { + return _stream.eof() ? '\0' : static_cast(_stream.get()); + } + + private: + Iterator& operator=(const Iterator&); // Visual Studio C4512 + }; +}; + +template +struct StringFuncs::type>::value>::type> + : StdStreamFuncs {}; +} +} diff --git a/include/ArduinoJson/StringTraits/StdString.hpp b/include/ArduinoJson/StringTraits/StdString.hpp new file mode 100644 index 00000000..b2e9fb95 --- /dev/null +++ b/include/ArduinoJson/StringTraits/StdString.hpp @@ -0,0 +1,61 @@ +// Copyright Benoit Blanchon 2014-2016 +// MIT License +// +// Arduino JSON library +// https://github.com/bblanchon/ArduinoJson +// If you like this project, please add a star! + +#pragma once + +#if ARDUINOJSON_ENABLE_ARDUINO_STRING +#include +#endif + +#if ARDUINOJSON_ENABLE_STD_STRING +#include +#endif + +namespace ArduinoJson { +namespace Internals { + +template +struct StdStringFuncs { + template + static char* duplicate(const TString& str, Buffer* buffer) { + if (!str.c_str()) return NULL; // <- Arduino string can return NULL + size_t size = str.length() + 1; + void* dup = buffer->alloc(size); + if (dup != NULL) memcpy(dup, str.c_str(), size); + return static_cast(dup); + } + + struct Iterator : CharPtrFuncs::Iterator { + Iterator(const TString& str) : CharPtrFuncs::Iterator(str.c_str()) {} + }; + + static bool equals(const TString& str, const char* expected) { + return str == expected; + } + + static void append(TString& str, char c) { + str += c; + } + + static const bool has_append = true; + static const bool has_equals = true; + static const bool should_duplicate = true; +}; + +#if ARDUINOJSON_ENABLE_ARDUINO_STRING +template <> +struct StringFuncs : StdStringFuncs {}; +template <> +struct StringFuncs : StdStringFuncs {}; +#endif + +#if ARDUINOJSON_ENABLE_STD_STRING +template <> +struct StringFuncs : StdStringFuncs {}; +#endif +} +} diff --git a/include/ArduinoJson/StringTraits/StringTraits.hpp b/include/ArduinoJson/StringTraits/StringTraits.hpp new file mode 100644 index 00000000..5f26167c --- /dev/null +++ b/include/ArduinoJson/StringTraits/StringTraits.hpp @@ -0,0 +1,42 @@ +// Copyright Benoit Blanchon 2014-2016 +// MIT License +// +// Arduino JSON library +// https://github.com/bblanchon/ArduinoJson +// If you like this project, please add a star! + +#pragma once + +#include "../Configuration.hpp" + +namespace ArduinoJson { +namespace Internals { + +template +struct StringFuncs {}; + +template +struct StringFuncs : StringFuncs {}; + +template +struct StringFuncs : StringFuncs {}; +} +} + +#include "CharPointer.hpp" + +#if ARDUINOJSON_ENABLE_STD_STRING || ARDUINOJSON_ENABLE_ARDUINO_STRING +#include "StdString.hpp" +#endif + +#if ARDUINOJSON_ENABLE_STD_STREAM +#include "StdStream.hpp" +#endif + +#if ARDUINOJSON_ENABLE_ARDUINO_STREAM +#include "ArduinoStream.hpp" +#endif + +#if ARDUINOJSON_ENABLE_PROGMEM +#include "FlashString.hpp" +#endif diff --git a/include/ArduinoJson/TypeTraits/IsBaseOf.hpp b/include/ArduinoJson/TypeTraits/IsBaseOf.hpp new file mode 100644 index 00000000..d3b4f06a --- /dev/null +++ b/include/ArduinoJson/TypeTraits/IsBaseOf.hpp @@ -0,0 +1,30 @@ +// Copyright Benoit Blanchon 2014-2016 +// MIT License +// +// Arduino JSON library +// https://github.com/bblanchon/ArduinoJson +// If you like this project, please add a star! + +#pragma once + +namespace ArduinoJson { +namespace TypeTraits { + +// A meta-function that returns true if Derived inherits from TBase is an +// integral type. +template +class IsBaseOf { + protected: // <- to avoid GCC's "all member functions in class are private" + typedef char Yes[1]; + typedef char No[2]; + + static Yes &probe(const TBase *); + static No &probe(const void *); + + public: + enum { + value = sizeof(probe(reinterpret_cast(0))) == sizeof(Yes) + }; +}; +} +} diff --git a/scripts/travis/sanitize.sh b/scripts/travis/sanitize.sh index 3631a580..7a42500d 100755 --- a/scripts/travis/sanitize.sh +++ b/scripts/travis/sanitize.sh @@ -3,5 +3,5 @@ curl https://cmake.org/files/v3.4/cmake-3.4.0-Linux-x86_64.tar.gz | tar xz -C /tmp --strip 1 /tmp/bin/cmake -DSANITIZE=true . -make -make test +cmake --build . +ctest -VV . diff --git a/test/JsonParser_Array_Tests.cpp b/test/JsonParser_Array_Tests.cpp index 05da0975..7f016d32 100644 --- a/test/JsonParser_Array_Tests.cpp +++ b/test/JsonParser_Array_Tests.cpp @@ -351,6 +351,16 @@ TEST_F(JsonParser_Array_Tests, UnfinishedCComment) { parseMustFail(); } +TEST_F(JsonParser_Array_Tests, EndsInCppComment) { + whenInputIs("[//COMMENT"); + parseMustFail(); +} + +TEST_F(JsonParser_Array_Tests, AfterClosingStar) { + whenInputIs("[/*COMMENT*"); + parseMustFail(); +} + TEST_F(JsonParser_Array_Tests, DeeplyNested) { whenInputIs("[[[[[[[[[[[[[[[[[[[\"Not too deep\"]]]]]]]]]]]]]]]]]]]"); parseMustSucceed(); diff --git a/test/StdStream.cpp b/test/StdStream_Tests.cpp similarity index 62% rename from test/StdStream.cpp rename to test/StdStream_Tests.cpp index 90356bfd..1d3fe118 100644 --- a/test/StdStream.cpp +++ b/test/StdStream_Tests.cpp @@ -5,25 +5,25 @@ // https://github.com/bblanchon/ArduinoJson // If you like this project, please add a star! -#include -#include #include +#include +#include -TEST(StdStream, JsonVariantFalse) { +TEST(StdStream_Tests, JsonVariantFalse) { std::ostringstream os; JsonVariant variant = false; os << variant; ASSERT_EQ("false", os.str()); } -TEST(StdStream, JsonVariantString) { +TEST(StdStream_Tests, JsonVariantString) { std::ostringstream os; JsonVariant variant = "coucou"; os << variant; ASSERT_EQ("\"coucou\"", os.str()); } -TEST(StdStream, JsonObject) { +TEST(StdStream_Tests, JsonObject) { std::ostringstream os; DynamicJsonBuffer jsonBuffer; JsonObject& object = jsonBuffer.createObject(); @@ -32,7 +32,7 @@ TEST(StdStream, JsonObject) { ASSERT_EQ("{\"key\":\"value\"}", os.str()); } -TEST(StdStream, JsonObjectSubscript) { +TEST(StdStream_Tests, JsonObjectSubscript) { std::ostringstream os; DynamicJsonBuffer jsonBuffer; JsonObject& object = jsonBuffer.createObject(); @@ -41,7 +41,7 @@ TEST(StdStream, JsonObjectSubscript) { ASSERT_EQ("\"value\"", os.str()); } -TEST(StdStream, JsonArray) { +TEST(StdStream_Tests, JsonArray) { std::ostringstream os; DynamicJsonBuffer jsonBuffer; JsonArray& array = jsonBuffer.createArray(); @@ -50,7 +50,7 @@ TEST(StdStream, JsonArray) { ASSERT_EQ("[\"value\"]", os.str()); } -TEST(StdStream, JsonArraySubscript) { +TEST(StdStream_Tests, JsonArraySubscript) { std::ostringstream os; DynamicJsonBuffer jsonBuffer; JsonArray& array = jsonBuffer.createArray(); @@ -58,3 +58,21 @@ TEST(StdStream, JsonArraySubscript) { os << array[0]; ASSERT_EQ("\"value\"", os.str()); } + +TEST(StdStream_Tests, ParseArray) { + std::istringstream json("[42]"); + DynamicJsonBuffer jsonBuffer; + JsonArray& arr = jsonBuffer.parseArray(json); + ASSERT_TRUE(arr.success()); + ASSERT_EQ(1, arr.size()); + ASSERT_EQ(42, arr[0]); +} + +TEST(StdStream_Tests, ParseObject) { + std::istringstream json("{hello:world}"); + DynamicJsonBuffer jsonBuffer; + JsonObject& obj = jsonBuffer.parseObject(json); + ASSERT_TRUE(obj.success()); + ASSERT_EQ(1, obj.size()); + ASSERT_STREQ("world", obj["hello"]); +} diff --git a/test/TypeTraits_Tests.cpp b/test/TypeTraits_Tests.cpp new file mode 100644 index 00000000..90b8da0f --- /dev/null +++ b/test/TypeTraits_Tests.cpp @@ -0,0 +1,17 @@ +// Copyright Benoit Blanchon 2014-2016 +// MIT License +// +// Arduino JSON library +// https://github.com/bblanchon/ArduinoJson +// If you like this project, please add a star! + +#include +#include +#include + +using namespace ArduinoJson::TypeTraits; + +TEST(StdStream, IsBaseOf) { + ASSERT_FALSE((IsBaseOf::value)); + ASSERT_TRUE((IsBaseOf::value)); +}