diff --git a/CHANGELOG.md b/CHANGELOG.md index 84e7ddfb..20aaa183 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,12 +4,16 @@ ArduinoJson: change log HEAD ---- -* Added DynamicJsonArray and StaticJsonArray -* Added DynamicJsonObject and StaticJsonObject -* Added DynamicJsonVariant and StaticJsonVariant -* Added deserializeJson() -* Removed JsonBuffer::parseArray(), parseObject() and parse() -* Removed JsonBuffer::createArray() and createObject() +* Added `DynamicJsonArray` and `StaticJsonArray` +* Added `DynamicJsonObject` and `StaticJsonObject` +* Added `DynamicJsonVariant` and `StaticJsonVariant` +* Added `deserializeJson()` +* Added `serializeJson()` and `serializeJsonPretty()` +* Added `measureJson()` and `measureJsonPretty()` +* Removed `JsonBuffer::parseArray()`, `parseObject()` and `parse()` +* Removed `JsonBuffer::createArray()` and `createObject()` +* Removed `printTo()` and `prettyPrintTo()` +* Removed `measureLength()` and `measurePrettyLength()` > ### BREAKING CHANGES > @@ -51,7 +55,7 @@ HEAD > ```c++ > DynamicJsonObject obj; > obj["key"] = "value"; -> obj.printTo(Serial); +> serializeJson(obj, Serial); > ``` v5.13.1 diff --git a/README.md b/README.md index 7a398aca..31fc6266 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ Here is a program that parses a JSON document with ArduinoJson. char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; DynamicJsonObject root; -derserializeJson(root, json); +deserializeJson(root, json); const char* sensor = root["sensor"]; long time = root["time"]; @@ -83,7 +83,7 @@ JsonArray& data = root.createNestedArray("data"); data.add(48.756080); data.add(2.302038); -root.printTo(Serial); +serializeJson(root, Serial); // This prints: // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]} ``` diff --git a/examples/JsonConfigFile/JsonConfigFile.ino b/examples/JsonConfigFile/JsonConfigFile.ino index 299447fc..442a8651 100644 --- a/examples/JsonConfigFile/JsonConfigFile.ino +++ b/examples/JsonConfigFile/JsonConfigFile.ino @@ -72,7 +72,7 @@ void saveConfiguration(const char *filename, const Config &config) { root["port"] = config.port; // Serialize JSON to file - if (root.printTo(file) == 0) { + if (serializeJson(root, file) == 0) { Serial.println(F("Failed to write to file")); } diff --git a/examples/JsonGeneratorExample/JsonGeneratorExample.ino b/examples/JsonGeneratorExample/JsonGeneratorExample.ino index 4903b114..ee22fe71 100644 --- a/examples/JsonGeneratorExample/JsonGeneratorExample.ino +++ b/examples/JsonGeneratorExample/JsonGeneratorExample.ino @@ -38,13 +38,13 @@ void setup() { data.add(48.756080); data.add(2.302038); - root.printTo(Serial); + serializeJson(root, Serial); // This prints: // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]} Serial.println(); - root.prettyPrintTo(Serial); + serializeJsonPretty(root, Serial); // This prints: // { // "sensor": "gps", diff --git a/examples/JsonServer/JsonServer.ino b/examples/JsonServer/JsonServer.ino index ded6d5ab..677d5039 100644 --- a/examples/JsonServer/JsonServer.ino +++ b/examples/JsonServer/JsonServer.ino @@ -76,7 +76,7 @@ void loop() { } Serial.print(F("Sending: ")); - root.printTo(Serial); + serializeJson(root, Serial); Serial.println(); // Write response headers @@ -86,7 +86,7 @@ void loop() { client.println(); // Write JSON document - root.prettyPrintTo(client); + serializeJsonPretty(root, client); // Disconnect client.stop(); diff --git a/examples/JsonUdpBeacon/JsonUdpBeacon.ino b/examples/JsonUdpBeacon/JsonUdpBeacon.ino index 3f912c68..82c8b8ff 100644 --- a/examples/JsonUdpBeacon/JsonUdpBeacon.ino +++ b/examples/JsonUdpBeacon/JsonUdpBeacon.ino @@ -72,11 +72,11 @@ void loop() { Serial.print(remoteIp); Serial.print(F(" on port ")); Serial.println(remotePort); - root.printTo(Serial); + serializeJson(root, Serial); // Send UDP packet udp.beginPacket(remoteIp, remotePort); - root.printTo(udp); + serializeJson(root, udp); udp.println(); udp.endPacket(); diff --git a/examples/StringExample/StringExample.ino b/examples/StringExample/StringExample.ino index 1ffed458..a497651a 100644 --- a/examples/StringExample/StringExample.ino +++ b/examples/StringExample/StringExample.ino @@ -54,7 +54,7 @@ void setup() { // Lastly, you can print the resulting JSON to a String String output; - root.printTo(output); + serializeJson(root, output); } void loop() { diff --git a/keywords.txt b/keywords.txt index 1bb6f140..ae31a74c 100644 --- a/keywords.txt +++ b/keywords.txt @@ -1,13 +1,18 @@ +add KEYWORD2 +as KEYWORD2 +createNestedArray KEYWORD2 +createNestedObject KEYWORD2 +deserializeJson KEYWORD2 +DynamicJsonArray KEYWORD1 +DynamicJsonObject KEYWORD1 +DynamicJsonVariant KEYWORD1 +get KEYWORD2 JsonArray KEYWORD1 JsonObject KEYWORD1 JsonVariant KEYWORD1 -StaticJsonBuffer KEYWORD1 -DynamicJsonBuffer KEYWORD1 -add KEYWORD2 -createNestedArray KEYWORD2 -createNestedObject KEYWORD2 -parseArray KEYWORD2 -parseObject KEYWORD2 -prettyPrintTo KEYWORD2 -printTo KEYWORD2 -success KEYWORD2 +serializeJson KEYWORD2 +serializeJsonPretty KEYWORD2 +set KEYWORD2 +StaticJsonArray KEYWORD1 +StaticJsonObject KEYWORD1 +StaticJsonVariant KEYWORD1 diff --git a/src/ArduinoJson/Deserialization/JsonParserImpl.hpp b/src/ArduinoJson/Deserialization/JsonParserImpl.hpp index 19ca8b7a..3543d870 100644 --- a/src/ArduinoJson/Deserialization/JsonParserImpl.hpp +++ b/src/ArduinoJson/Deserialization/JsonParserImpl.hpp @@ -4,6 +4,7 @@ #pragma once +#include "../Data/Encoding.hpp" #include "Comments.hpp" #include "JsonParser.hpp" diff --git a/src/ArduinoJson/JsonArray.hpp b/src/ArduinoJson/JsonArray.hpp index 6ca712e2..2a9dc26d 100644 --- a/src/ArduinoJson/JsonArray.hpp +++ b/src/ArduinoJson/JsonArray.hpp @@ -9,7 +9,6 @@ #include "Data/ReferenceType.hpp" #include "Data/ValueSaver.hpp" #include "JsonVariant.hpp" -#include "Serialization/JsonPrintable.hpp" #include "StringTraits/StringTraits.hpp" #include "TypeTraits/EnableIf.hpp" #include "TypeTraits/IsArray.hpp" @@ -30,8 +29,7 @@ namespace Internals { class JsonArraySubscript; } -class JsonArray : public Internals::JsonPrintable, - public Internals::ReferenceType, +class JsonArray : public Internals::ReferenceType, public Internals::NonCopyable, public Internals::List, public Internals::JsonBufferAllocated { diff --git a/src/ArduinoJson/JsonArraySubscript.hpp b/src/ArduinoJson/JsonArraySubscript.hpp index afb4dc1e..de40afc1 100644 --- a/src/ArduinoJson/JsonArraySubscript.hpp +++ b/src/ArduinoJson/JsonArraySubscript.hpp @@ -98,14 +98,7 @@ inline const JsonArraySubscript JsonVariantSubscripts::operator[]( size_t index) const { return impl()->template as()[index]; } - -#if ARDUINOJSON_ENABLE_STD_STREAM -inline std::ostream& operator<<(std::ostream& os, - const JsonArraySubscript& source) { - return source.printTo(os); -} -#endif -} +} // namespace Internals inline Internals::JsonArraySubscript JsonArray::operator[](size_t index) { return Internals::JsonArraySubscript(*this, index); @@ -115,7 +108,7 @@ inline const Internals::JsonArraySubscript JsonArray::operator[]( size_t index) const { return Internals::JsonArraySubscript(*const_cast(this), index); } -} +} // namespace ArduinoJson #ifdef _MSC_VER #pragma warning(pop) diff --git a/src/ArduinoJson/JsonObject.hpp b/src/ArduinoJson/JsonObject.hpp index 1802e59f..d182ae7a 100644 --- a/src/ArduinoJson/JsonObject.hpp +++ b/src/ArduinoJson/JsonObject.hpp @@ -9,7 +9,6 @@ #include "Data/ReferenceType.hpp" #include "Data/ValueSaver.hpp" #include "JsonPair.hpp" -#include "Serialization/JsonPrintable.hpp" #include "StringTraits/StringTraits.hpp" #include "TypeTraits/EnableIf.hpp" #include "TypeTraits/IsArray.hpp" @@ -31,8 +30,7 @@ template class JsonObjectSubscript; } -class JsonObject : public Internals::JsonPrintable, - public Internals::ReferenceType, +class JsonObject : public Internals::ReferenceType, public Internals::NonCopyable, public Internals::List, public Internals::JsonBufferAllocated { diff --git a/src/ArduinoJson/JsonObjectSubscript.hpp b/src/ArduinoJson/JsonObjectSubscript.hpp index 6ac47637..e108f3f5 100644 --- a/src/ArduinoJson/JsonObjectSubscript.hpp +++ b/src/ArduinoJson/JsonObjectSubscript.hpp @@ -94,16 +94,8 @@ class JsonObjectSubscript JsonObject& _object; TStringRef _key; }; - -#if ARDUINOJSON_ENABLE_STD_STREAM -template -inline std::ostream& operator<<(std::ostream& os, - const JsonObjectSubscript& source) { - return source.printTo(os); -} -#endif -} -} +} // namespace Internals +} // namespace ArduinoJson #ifdef _MSC_VER #pragma warning(pop) diff --git a/src/ArduinoJson/JsonVariant.hpp b/src/ArduinoJson/JsonVariant.hpp index e5cb9148..9fffb4af 100644 --- a/src/ArduinoJson/JsonVariant.hpp +++ b/src/ArduinoJson/JsonVariant.hpp @@ -12,7 +12,6 @@ #include "Data/JsonVariantType.hpp" #include "JsonVariantBase.hpp" #include "RawJson.hpp" -#include "Serialization/JsonPrintable.hpp" #include "TypeTraits/EnableIf.hpp" #include "TypeTraits/IsChar.hpp" #include "TypeTraits/IsFloatingPoint.hpp" @@ -28,6 +27,10 @@ namespace ArduinoJson { // Forward declarations. class JsonArray; class JsonObject; +namespace Internals { +template +class JsonSerializer; +} // A variant that can be a any value serializable to a JSON value. // @@ -194,7 +197,7 @@ class JsonVariant : public Internals::JsonVariantBase { const char *cstr = variantAsString(); if (cstr) return T(cstr); T s; - printTo(s); + serializeJson(*this, s); return s; } // diff --git a/src/ArduinoJson/JsonVariantBase.hpp b/src/ArduinoJson/JsonVariantBase.hpp index 44acf2e1..268326ca 100644 --- a/src/ArduinoJson/JsonVariantBase.hpp +++ b/src/ArduinoJson/JsonVariantBase.hpp @@ -8,17 +8,15 @@ #include "JsonVariantComparisons.hpp" #include "JsonVariantOr.hpp" #include "JsonVariantSubscripts.hpp" -#include "Serialization/JsonPrintable.hpp" namespace ArduinoJson { namespace Internals { template -class JsonVariantBase : public JsonPrintable, - public JsonVariantCasts, +class JsonVariantBase : public JsonVariantCasts, public JsonVariantComparisons, public JsonVariantOr, public JsonVariantSubscripts, public JsonVariantTag {}; -} -} +} // namespace Internals +} // namespace ArduinoJson diff --git a/src/ArduinoJson/JsonVariantImpl.hpp b/src/ArduinoJson/JsonVariantImpl.hpp index 31f96ce1..93f70f09 100644 --- a/src/ArduinoJson/JsonVariantImpl.hpp +++ b/src/ArduinoJson/JsonVariantImpl.hpp @@ -117,10 +117,4 @@ inline bool JsonVariant::variantIsFloat() const { (_type == JSON_UNPARSED && isFloat(_content.asString)); } -#if ARDUINOJSON_ENABLE_STD_STREAM -inline std::ostream &operator<<(std::ostream &os, const JsonVariant &source) { - return source.printTo(os); -} -#endif - } // namespace ArduinoJson diff --git a/src/ArduinoJson/Serialization/DummyPrint.hpp b/src/ArduinoJson/Serialization/DummyPrint.hpp index 9fdf2d6a..67ec33e1 100644 --- a/src/ArduinoJson/Serialization/DummyPrint.hpp +++ b/src/ArduinoJson/Serialization/DummyPrint.hpp @@ -7,7 +7,7 @@ namespace ArduinoJson { namespace Internals { -// A dummy Print implementation used in JsonPrintable::measureLength() +// A dummy Print implementation used in measureJson() class DummyPrint { public: size_t print(char) { @@ -18,5 +18,5 @@ class DummyPrint { return strlen(s); } }; -} -} +} // namespace Internals +} // namespace ArduinoJson diff --git a/src/ArduinoJson/Serialization/IndentedPrint.hpp b/src/ArduinoJson/Serialization/IndentedPrint.hpp index 864f9aaa..a71ceb66 100644 --- a/src/ArduinoJson/Serialization/IndentedPrint.hpp +++ b/src/ArduinoJson/Serialization/IndentedPrint.hpp @@ -8,7 +8,7 @@ namespace ArduinoJson { namespace Internals { // Decorator on top of Print to allow indented output. -// This class is used by JsonPrintable::prettyPrintTo() but can also be used +// This class is used by serializeJsonPretty() but can also be used // for your own purpose, like logging. template class IndentedPrint { @@ -64,5 +64,5 @@ class IndentedPrint { 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 }; -} -} +} // namespace Internals +} // namespace ArduinoJson diff --git a/src/ArduinoJson/Serialization/JsonPrintable.hpp b/src/ArduinoJson/Serialization/JsonPrintable.hpp deleted file mode 100644 index 43d413a8..00000000 --- a/src/ArduinoJson/Serialization/JsonPrintable.hpp +++ /dev/null @@ -1,117 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "../Configuration.hpp" -#include "../TypeTraits/EnableIf.hpp" -#include "DummyPrint.hpp" -#include "DynamicStringBuilder.hpp" -#include "IndentedPrint.hpp" -#include "JsonSerializer.hpp" -#include "JsonWriter.hpp" -#include "Prettyfier.hpp" -#include "StaticStringBuilder.hpp" - -#if ARDUINOJSON_ENABLE_STD_STREAM -#include "StreamPrintAdapter.hpp" -#endif - -namespace ArduinoJson { -namespace Internals { - -// Implements all the overloads of printTo() and prettyPrintTo() -// Caution: this class use a template parameter to avoid virtual methods. -// This is a bit curious but allows to reduce the size of JsonVariant, JsonArray -// and JsonObject. -template -class JsonPrintable { - public: - template - typename EnableIf::has_append, size_t>::type printTo( - Print &print) const { - JsonWriter writer(print); - JsonSerializer >::serialize(downcast(), writer); - return writer.bytesWritten(); - } - -#if ARDUINOJSON_ENABLE_STD_STREAM - std::ostream &printTo(std::ostream &os) const { - StreamPrintAdapter adapter(os); - printTo(adapter); - return os; - } -#endif - - size_t printTo(char *buffer, size_t bufferSize) const { - StaticStringBuilder sb(buffer, bufferSize); - return printTo(sb); - } - - template - size_t printTo(char (&buffer)[N]) const { - return printTo(buffer, N); - } - - template - typename EnableIf::has_append, size_t>::type printTo( - TString &str) const { - DynamicStringBuilder sb(str); - return printTo(sb); - } - - template - size_t prettyPrintTo(IndentedPrint &print) const { - Prettyfier p(print); - return printTo(p); - } - - size_t prettyPrintTo(char *buffer, size_t bufferSize) const { - StaticStringBuilder sb(buffer, bufferSize); - return prettyPrintTo(sb); - } - - template - size_t prettyPrintTo(char (&buffer)[N]) const { - return prettyPrintTo(buffer, N); - } - - template - typename EnableIf::has_append, size_t>::type - prettyPrintTo(Print &print) const { - IndentedPrint indentedPrint(print); - return prettyPrintTo(indentedPrint); - } - - template - typename EnableIf::has_append, size_t>::type - prettyPrintTo(TString &str) const { - DynamicStringBuilder sb(str); - return prettyPrintTo(sb); - } - - size_t measureLength() const { - DummyPrint dp; - return printTo(dp); - } - - size_t measurePrettyLength() const { - DummyPrint dp; - return prettyPrintTo(dp); - } - - private: - const T &downcast() const { - return *static_cast(this); - } -}; - -#if ARDUINOJSON_ENABLE_STD_STREAM -template -inline std::ostream &operator<<(std::ostream &os, const JsonPrintable &v) { - return v.printTo(os); -} -#endif -} -} diff --git a/src/ArduinoJson/Serialization/JsonSerializer.hpp b/src/ArduinoJson/Serialization/JsonSerializer.hpp index 0cb537f7..211e9fbf 100644 --- a/src/ArduinoJson/Serialization/JsonSerializer.hpp +++ b/src/ArduinoJson/Serialization/JsonSerializer.hpp @@ -4,7 +4,16 @@ #pragma once +#include "DummyPrint.hpp" +#include "DynamicStringBuilder.hpp" +#include "IndentedPrint.hpp" #include "JsonWriter.hpp" +#include "Prettyfier.hpp" +#include "StaticStringBuilder.hpp" + +#if ARDUINOJSON_ENABLE_STD_STREAM +#include "StreamPrintAdapter.hpp" +#endif namespace ArduinoJson { @@ -28,5 +37,122 @@ class JsonSerializer { static void serialize(const JsonObjectSubscript &, Writer &); static void serialize(const JsonVariant &, Writer &); }; +} // namespace Internals + +template +typename Internals::EnableIf::has_append, + size_t>::type +serializeJson(const TSource &source, TDestination &destination) { + Internals::JsonWriter writer(destination); + Internals::JsonSerializer >::serialize( + source, writer); + return writer.bytesWritten(); } + +#if ARDUINOJSON_ENABLE_STD_STREAM +template +std::ostream &serializeJson(const TSource &source, std::ostream &os) { + Internals::StreamPrintAdapter adapter(os); + serializeJson(source, adapter); + return os; } +#endif + +template +size_t serializeJson(const TSource &source, char *buffer, size_t bufferSize) { + Internals::StaticStringBuilder sb(buffer, bufferSize); + return serializeJson(source, sb); +} + +template +size_t serializeJson(const TSource &source, char (&buffer)[N]) { + return serializeJson(source, buffer, N); +} + +template +typename Internals::EnableIf::has_append, + size_t>::type +serializeJson(const TSource &source, TDestination &str) { + Internals::DynamicStringBuilder sb(str); + return serializeJson(source, sb); +} + +template +size_t serializeJsonPretty(const TSource &source, + Internals::IndentedPrint &print) { + Internals::Prettyfier p(print); + return serializeJson(source, p); +} + +template +size_t serializeJsonPretty(const TSource &source, char *buffer, + size_t bufferSize) { + Internals::StaticStringBuilder sb(buffer, bufferSize); + return serializeJsonPretty(source, sb); +} + +template +size_t serializeJsonPretty(const TSource &source, char (&buffer)[N]) { + return serializeJsonPretty(source, buffer, N); +} + +template +typename Internals::EnableIf::has_append, + size_t>::type +serializeJsonPretty(const TSource &source, TDestination &print) { + Internals::IndentedPrint indentedPrint(print); + return serializeJsonPretty(source, indentedPrint); +} + +template +typename Internals::EnableIf::has_append, + size_t>::type +serializeJsonPretty(const TSource &source, TDestination &str) { + Internals::DynamicStringBuilder sb(str); + return serializeJsonPretty(source, sb); +} + +template +size_t measureJson(const TSource &source) { + Internals::DummyPrint dp; + return serializeJson(source, dp); +} + +template +size_t measureJsonPretty(const TSource &source) { + Internals::DummyPrint dp; + return serializeJsonPretty(source, dp); +} + +#if ARDUINOJSON_ENABLE_STD_STREAM +inline std::ostream &operator<<(std::ostream &os, const JsonArray &source) { + serializeJson(source, os); + return os; +} +inline std::ostream &operator<<(std::ostream &os, const JsonObject &source) { + serializeJson(source, os); + return os; +} +inline std::ostream &operator<<(std::ostream &os, const JsonVariant &source) { + serializeJson(source, os); + return os; +} + +namespace Internals { +inline std::ostream &operator<<(std::ostream &os, + const JsonArraySubscript &source) { + serializeJson(source, os); + return os; +} + +template +inline std::ostream &operator<<(std::ostream &os, + const JsonObjectSubscript &source) { + serializeJson(source, os); + return os; +} +} // namespace Internals + +#endif + +} // namespace ArduinoJson diff --git a/src/ArduinoJson/Serialization/JsonWriter.hpp b/src/ArduinoJson/Serialization/JsonWriter.hpp index 146d51dc..d5682863 100644 --- a/src/ArduinoJson/Serialization/JsonWriter.hpp +++ b/src/ArduinoJson/Serialization/JsonWriter.hpp @@ -26,8 +26,6 @@ class JsonWriter { explicit JsonWriter(Print &sink) : _sink(sink), _length(0) {} // Returns the number of bytes sent to the Print implementation. - // This is very handy for implementations of printTo() that must return the - // number of bytes written. size_t bytesWritten() const { return _length; } @@ -151,5 +149,5 @@ class JsonWriter { private: JsonWriter &operator=(const JsonWriter &); // cannot be assigned }; -} -} +} // namespace Internals +} // namespace ArduinoJson diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 931a4612..578937a9 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -69,6 +69,7 @@ add_subdirectory(IntegrationTests) add_subdirectory(JsonArray) add_subdirectory(JsonObject) add_subdirectory(JsonParser) +add_subdirectory(JsonSerializer) add_subdirectory(JsonVariant) add_subdirectory(JsonWriter) add_subdirectory(Misc) diff --git a/test/DynamicJsonBuffer/alloc.cpp b/test/DynamicJsonBuffer/alloc.cpp index c45e6866..0d1994f6 100644 --- a/test/DynamicJsonBuffer/alloc.cpp +++ b/test/DynamicJsonBuffer/alloc.cpp @@ -18,7 +18,8 @@ std::stringstream allocatorLog; struct SpyingAllocator : DefaultAllocator { void* allocate(size_t n) { - allocatorLog << "A" << (n - DynamicJsonBuffer::EmptyBlockSize); + allocatorLog << static_cast("A") + << (n - DynamicJsonBuffer::EmptyBlockSize); return DefaultAllocator::allocate(n); } void deallocate(void* p) { diff --git a/test/IntegrationTests/round_trip.cpp b/test/IntegrationTests/round_trip.cpp index 9dd49f93..487dbb74 100644 --- a/test/IntegrationTests/round_trip.cpp +++ b/test/IntegrationTests/round_trip.cpp @@ -10,11 +10,11 @@ void check(std::string originalJson) { std::string prettyJson; deserializeJson(obj, originalJson); - obj.prettyPrintTo(prettyJson); + serializeJsonPretty(obj, prettyJson); std::string finalJson; deserializeJson(obj, originalJson); - obj.printTo(finalJson); + serializeJson(obj, finalJson); REQUIRE(originalJson == finalJson); } diff --git a/test/JsonArray/CMakeLists.txt b/test/JsonArray/CMakeLists.txt index ec0e5206..c0b13b60 100644 --- a/test/JsonArray/CMakeLists.txt +++ b/test/JsonArray/CMakeLists.txt @@ -9,8 +9,6 @@ add_executable(JsonArrayTests copyTo.cpp invalid.cpp iterator.cpp - prettyPrintTo.cpp - printTo.cpp remove.cpp set.cpp size.cpp diff --git a/test/JsonArray/copyFrom.cpp b/test/JsonArray/copyFrom.cpp index a881e53d..fa2045bc 100644 --- a/test/JsonArray/copyFrom.cpp +++ b/test/JsonArray/copyFrom.cpp @@ -14,7 +14,7 @@ TEST_CASE("JsonArray::copyFrom()") { bool ok = array.copyFrom(source); REQUIRE(ok); - array.printTo(json, sizeof(json)); + serializeJson(array, json, sizeof(json)); REQUIRE(std::string("[1,2,3]") == json); } @@ -27,7 +27,7 @@ TEST_CASE("JsonArray::copyFrom()") { bool ok = array.copyFrom(source); REQUIRE_FALSE(ok); - array.printTo(json, sizeof(json)); + serializeJson(array, json, sizeof(json)); REQUIRE(std::string("[1,2]") == json); } @@ -39,7 +39,7 @@ TEST_CASE("JsonArray::copyFrom()") { bool ok = array.copyFrom(source); REQUIRE(ok); - array.printTo(json, sizeof(json)); + serializeJson(array, json, sizeof(json)); REQUIRE(std::string("[[1,2,3],[4,5,6]]") == json); } @@ -53,7 +53,7 @@ TEST_CASE("JsonArray::copyFrom()") { bool ok = array.copyFrom(source); REQUIRE_FALSE(ok); - array.printTo(json, sizeof(json)); + serializeJson(array, json, sizeof(json)); REQUIRE(std::string("[[1,2,3],[4,5]]") == json); } } diff --git a/test/JsonArray/invalid.cpp b/test/JsonArray/invalid.cpp index 045517ba..f057c0a7 100644 --- a/test/JsonArray/invalid.cpp +++ b/test/JsonArray/invalid.cpp @@ -28,7 +28,7 @@ TEST_CASE("JsonArray::invalid()") { SECTION("PrintToWritesBrackets") { char buffer[32]; - JsonArray::invalid().printTo(buffer, sizeof(buffer)); + serializeJson(JsonArray::invalid(), buffer, sizeof(buffer)); REQUIRE_THAT(buffer, Equals("[]")); } } diff --git a/test/JsonObject/CMakeLists.txt b/test/JsonObject/CMakeLists.txt index 93e8f0de..b6de6d30 100644 --- a/test/JsonObject/CMakeLists.txt +++ b/test/JsonObject/CMakeLists.txt @@ -8,8 +8,6 @@ add_executable(JsonObjectTests get.cpp invalid.cpp iterator.cpp - prettyPrintTo.cpp - printTo.cpp remove.cpp set.cpp size.cpp diff --git a/test/JsonObject/invalid.cpp b/test/JsonObject/invalid.cpp index 019ced10..7dbfd868 100644 --- a/test/JsonObject/invalid.cpp +++ b/test/JsonObject/invalid.cpp @@ -29,7 +29,7 @@ TEST_CASE("JsonObject::invalid()") { SECTION("PrintToWritesBraces") { char buffer[32]; - obj.printTo(buffer, sizeof(buffer)); + serializeJson(obj, buffer, sizeof(buffer)); REQUIRE_THAT(buffer, Equals("{}")); } } diff --git a/test/JsonObject/remove.cpp b/test/JsonObject/remove.cpp index 28bf539b..4048ebd5 100644 --- a/test/JsonObject/remove.cpp +++ b/test/JsonObject/remove.cpp @@ -33,7 +33,7 @@ TEST_CASE("JsonObject::remove()") { } std::string result; - obj.printTo(result); + serializeJson(obj, result); REQUIRE("{\"a\":0,\"c\":2}" == result); } } diff --git a/test/JsonSerializer/CMakeLists.txt b/test/JsonSerializer/CMakeLists.txt new file mode 100644 index 00000000..f513ca1d --- /dev/null +++ b/test/JsonSerializer/CMakeLists.txt @@ -0,0 +1,14 @@ +# ArduinoJson - arduinojson.org +# Copyright Benoit Blanchon 2014-2018 +# MIT License + +add_executable(JsonSerializerTests + JsonArray.cpp + JsonArrayPretty.cpp + JsonObject.cpp + JsonObjectPretty.cpp + JsonVariant.cpp +) + +target_link_libraries(JsonSerializerTests catch) +add_test(JsonSerializer JsonSerializerTests) diff --git a/test/JsonArray/printTo.cpp b/test/JsonSerializer/JsonArray.cpp similarity index 94% rename from test/JsonArray/printTo.cpp rename to test/JsonSerializer/JsonArray.cpp index 82ebad9a..ced91238 100644 --- a/test/JsonArray/printTo.cpp +++ b/test/JsonSerializer/JsonArray.cpp @@ -7,14 +7,14 @@ static void check(JsonArray &array, std::string expected) { std::string actual; - size_t actualLen = array.printTo(actual); + size_t actualLen = serializeJson(array, actual); REQUIRE(expected == actual); REQUIRE(actualLen == expected.size()); - size_t measuredLen = array.measureLength(); + size_t measuredLen = measureJson(array); REQUIRE(measuredLen == expected.size()); } -TEST_CASE("JsonArray::printTo()") { +TEST_CASE("serializeJson(JsonArray)") { StaticJsonArray array; SECTION("Empty") { diff --git a/test/JsonArray/prettyPrintTo.cpp b/test/JsonSerializer/JsonArrayPretty.cpp similarity index 89% rename from test/JsonArray/prettyPrintTo.cpp rename to test/JsonSerializer/JsonArrayPretty.cpp index af36a320..4785d643 100644 --- a/test/JsonArray/prettyPrintTo.cpp +++ b/test/JsonSerializer/JsonArrayPretty.cpp @@ -7,14 +7,14 @@ static void check(JsonArray& array, std::string expected) { std::string actual; - size_t actualLen = array.prettyPrintTo(actual); - size_t measuredLen = array.measurePrettyLength(); + size_t actualLen = serializeJsonPretty(array, actual); + size_t measuredLen = measureJsonPretty(array); CHECK(actualLen == expected.size()); CHECK(measuredLen == expected.size()); REQUIRE(expected == actual); } -TEST_CASE("JsonArray::prettyPrintTo()") { +TEST_CASE("serializeJsonPretty(JsonArray)") { DynamicJsonArray array; SECTION("Empty") { diff --git a/test/JsonObject/printTo.cpp b/test/JsonSerializer/JsonObject.cpp similarity index 94% rename from test/JsonObject/printTo.cpp rename to test/JsonSerializer/JsonObject.cpp index f72bb2e1..25476928 100644 --- a/test/JsonObject/printTo.cpp +++ b/test/JsonSerializer/JsonObject.cpp @@ -8,14 +8,15 @@ void check(const JsonObject &obj, const std::string &expected) { char actual[256]; - size_t actualLen = obj.printTo(actual); - size_t measuredLen = obj.measureLength(); + size_t actualLen = serializeJson(obj, actual); + size_t measuredLen = measureJson(obj); REQUIRE(expected == actual); REQUIRE(expected.size() == actualLen); REQUIRE(expected.size() == measuredLen); } -TEST_CASE("JsonObject::printTo()") { + +TEST_CASE("serializeJson(JsonObject)") { DynamicJsonObject obj; SECTION("EmptyObject") { diff --git a/test/JsonObject/prettyPrintTo.cpp b/test/JsonSerializer/JsonObjectPretty.cpp similarity index 90% rename from test/JsonObject/prettyPrintTo.cpp rename to test/JsonSerializer/JsonObjectPretty.cpp index 885a6895..efcc947e 100644 --- a/test/JsonObject/prettyPrintTo.cpp +++ b/test/JsonSerializer/JsonObjectPretty.cpp @@ -9,15 +9,15 @@ void check(const JsonObject &obj, const std::string expected) { char json[256]; - size_t actualLen = obj.prettyPrintTo(json); - size_t measuredLen = obj.measurePrettyLength(); + size_t actualLen = serializeJsonPretty(obj, json); + size_t measuredLen = measureJsonPretty(obj); REQUIRE(json == expected); REQUIRE(expected.size() == actualLen); REQUIRE(expected.size() == measuredLen); } -TEST_CASE("JsonObject::prettyPrintTo()") { +TEST_CASE("serializeJsonPretty(JsonObject)") { DynamicJsonObject obj; SECTION("EmptyObject") { diff --git a/test/JsonVariant/printTo.cpp b/test/JsonSerializer/JsonVariant.cpp similarity index 91% rename from test/JsonVariant/printTo.cpp rename to test/JsonSerializer/JsonVariant.cpp index 3431eaee..10c5ddf3 100644 --- a/test/JsonVariant/printTo.cpp +++ b/test/JsonSerializer/JsonVariant.cpp @@ -8,12 +8,12 @@ void check(JsonVariant variant, const std::string &expected) { char buffer[256] = ""; - size_t returnValue = variant.printTo(buffer, sizeof(buffer)); + size_t returnValue = serializeJson(variant, buffer, sizeof(buffer)); REQUIRE(expected == buffer); REQUIRE(expected.size() == returnValue); } -TEST_CASE("JsonVariant::printTo()") { +TEST_CASE("serializeJson(JsonVariant)") { SECTION("Empty") { check(JsonVariant(), ""); } diff --git a/test/JsonVariant/CMakeLists.txt b/test/JsonVariant/CMakeLists.txt index 0a00a15a..2ceb9277 100644 --- a/test/JsonVariant/CMakeLists.txt +++ b/test/JsonVariant/CMakeLists.txt @@ -2,13 +2,12 @@ # Copyright Benoit Blanchon 2014-2018 # MIT License -add_executable(JsonVariantTests +add_executable(JsonVariantTests as.cpp compare.cpp copy.cpp is.cpp or.cpp - printTo.cpp set_get.cpp subscript.cpp success.cpp diff --git a/test/Misc/std_string.cpp b/test/Misc/std_string.cpp index e164f8bd..ea11cb35 100644 --- a/test/Misc/std_string.cpp +++ b/test/Misc/std_string.cpp @@ -49,19 +49,19 @@ TEST_CASE("std::string") { REQUIRE(std::string("world") == array[0]); } - SECTION("printTo()") { + SECTION("serializeJson()") { array.add(4); array.add(2); std::string json; - array.printTo(json); + serializeJson(array, json); REQUIRE(std::string("[4,2]") == json); } - SECTION("prettyPrintTo") { + SECTION("serializeJsonPretty()") { array.add(4); array.add(2); std::string json; - array.prettyPrintTo(json); + serializeJsonPretty(array, json); REQUIRE(std::string("[\r\n 4,\r\n 2\r\n]") == json); } } @@ -69,7 +69,7 @@ TEST_CASE("std::string") { SECTION("JsonObject") { DynamicJsonObject object; - SECTION("deserializeJson") { + SECTION("deserializeJson()") { std::string json("{\"hello\":\"world\"}"); bool success = deserializeJson(object, json); @@ -156,7 +156,7 @@ TEST_CASE("std::string") { char json[64]; object.createNestedObject(key); eraseString(key); - object.printTo(json, sizeof(json)); + serializeJson(object, json, sizeof(json)); REQUIRE(std::string("{\"key\":{}}") == json); } @@ -165,7 +165,7 @@ TEST_CASE("std::string") { char json[64]; object.createNestedArray(key); eraseString(key); - object.printTo(json, sizeof(json)); + serializeJson(object, json, sizeof(json)); REQUIRE(std::string("{\"key\":[]}") == json); } @@ -197,17 +197,17 @@ TEST_CASE("std::string") { REQUIRE(std::string("world") == object["hello"]); } - SECTION("printTo") { + SECTION("serializeJson()") { object["key"] = "value"; std::string json; - object.printTo(json); + serializeJson(object, json); REQUIRE(std::string("{\"key\":\"value\"}") == json); } - SECTION("prettyPrintTo") { + SECTION("serializeJsonPretty()") { object["key"] = "value"; std::string json; - object.prettyPrintTo(json); + serializeJsonPretty(object, json); REQUIRE(std::string("{\r\n \"key\": \"value\"\r\n}") == json); }