From 432476c98a511135c6a90c1a6bc40428cadf0799 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Sat, 10 Dec 2016 15:59:48 +0100 Subject: [PATCH] Added an `printTo(char[N])` and `prettyPrintTo(char[N])` (issue #292) --- CHANGELOG.md | 1 + include/ArduinoJson/Internals/JsonPrintable.hpp | 10 ++++++++++ test/JsonArray_PrettyPrintTo_Tests.cpp | 8 +++++--- test/JsonArray_PrintTo_Tests.cpp | 8 +++++--- test/JsonObject_PrettyPrintTo_Tests.cpp | 8 +++++--- test/JsonObject_PrintTo_Tests.cpp | 6 ++++-- test/JsonObject_Subscript_Tests.cpp | 2 +- 7 files changed, 31 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30139e10..bb3b43ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ArduinoJson: change log HEAD ---- +* Added an `printTo(char[N])` and `prettyPrintTo(char[N])` (issue #292) * Added ability to set a nested value like this: `root["A"]["B"] = "C"` (issue #352) * Renamed `*.ipp` to `*Impl.hpp` because they were ignored by Arduino IDE (issue #396) diff --git a/include/ArduinoJson/Internals/JsonPrintable.hpp b/include/ArduinoJson/Internals/JsonPrintable.hpp index 71c58206..dfecfdce 100644 --- a/include/ArduinoJson/Internals/JsonPrintable.hpp +++ b/include/ArduinoJson/Internals/JsonPrintable.hpp @@ -50,6 +50,11 @@ class JsonPrintable { return printTo(sb); } + template + size_t printTo(char (&buffer)[N]) const { + return printTo(buffer, N); + } + template typename TypeTraits::EnableIf::has_append, size_t>::type printTo(TString &str) const { @@ -67,6 +72,11 @@ class JsonPrintable { return prettyPrintTo(sb); } + template + size_t prettyPrintTo(char (&buffer)[N]) const { + return prettyPrintTo(buffer, N); + } + size_t prettyPrintTo(Print &print) const { IndentedPrint indentedPrint = IndentedPrint(print); return prettyPrintTo(indentedPrint); diff --git a/test/JsonArray_PrettyPrintTo_Tests.cpp b/test/JsonArray_PrettyPrintTo_Tests.cpp index 0ef47af0..6f858b74 100644 --- a/test/JsonArray_PrettyPrintTo_Tests.cpp +++ b/test/JsonArray_PrettyPrintTo_Tests.cpp @@ -5,8 +5,8 @@ // https://github.com/bblanchon/ArduinoJson // If you like this project, please add a star! -#include #include +#include class JsonArray_PrettyPrintTo_Tests : public testing::Test { public: @@ -19,7 +19,7 @@ class JsonArray_PrettyPrintTo_Tests : public testing::Test { void outputMustBe(const char* expected) { char actual[256]; - size_t actualLen = array.prettyPrintTo(actual, sizeof(actual)); + size_t actualLen = array.prettyPrintTo(actual); size_t measuredLen = array.measurePrettyLength(); EXPECT_STREQ(expected, actual); @@ -28,7 +28,9 @@ class JsonArray_PrettyPrintTo_Tests : public testing::Test { } }; -TEST_F(JsonArray_PrettyPrintTo_Tests, Empty) { outputMustBe("[]"); } +TEST_F(JsonArray_PrettyPrintTo_Tests, Empty) { + outputMustBe("[]"); +} TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement) { array.add(1); diff --git a/test/JsonArray_PrintTo_Tests.cpp b/test/JsonArray_PrintTo_Tests.cpp index bd8e4732..e75acd58 100644 --- a/test/JsonArray_PrintTo_Tests.cpp +++ b/test/JsonArray_PrintTo_Tests.cpp @@ -5,8 +5,8 @@ // https://github.com/bblanchon/ArduinoJson // If you like this project, please add a star! -#include #include +#include class JsonArray_PrintTo_Tests : public testing::Test { public: @@ -17,7 +17,7 @@ class JsonArray_PrintTo_Tests : public testing::Test { JsonArray &array; void outputMustBe(const char *expected) { - size_t actualLen = array.printTo(buffer, sizeof(buffer)); + size_t actualLen = array.printTo(buffer); size_t measuredLen = array.measureLength(); EXPECT_STREQ(expected, buffer); @@ -29,7 +29,9 @@ class JsonArray_PrintTo_Tests : public testing::Test { char buffer[256]; }; -TEST_F(JsonArray_PrintTo_Tests, Empty) { outputMustBe("[]"); } +TEST_F(JsonArray_PrintTo_Tests, Empty) { + outputMustBe("[]"); +} TEST_F(JsonArray_PrintTo_Tests, Null) { array.add(static_cast(0)); diff --git a/test/JsonObject_PrettyPrintTo_Tests.cpp b/test/JsonObject_PrettyPrintTo_Tests.cpp index 9f32d298..0e904415 100644 --- a/test/JsonObject_PrettyPrintTo_Tests.cpp +++ b/test/JsonObject_PrettyPrintTo_Tests.cpp @@ -5,8 +5,8 @@ // https://github.com/bblanchon/ArduinoJson // If you like this project, please add a star! -#include #include +#include class JsonObject_PrettyPrintTo_Tests : public testing::Test { public: @@ -19,7 +19,7 @@ class JsonObject_PrettyPrintTo_Tests : public testing::Test { void outputMustBe(const char *expected) { char buffer[256]; - size_t actualLen = _object.prettyPrintTo(buffer, sizeof(buffer)); + size_t actualLen = _object.prettyPrintTo(buffer); size_t measuredLen = _object.measurePrettyLength(); EXPECT_STREQ(expected, buffer); @@ -28,7 +28,9 @@ class JsonObject_PrettyPrintTo_Tests : public testing::Test { } }; -TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject) { outputMustBe("{}"); } +TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject) { + outputMustBe("{}"); +} TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember) { _object["key"] = "value"; diff --git a/test/JsonObject_PrintTo_Tests.cpp b/test/JsonObject_PrintTo_Tests.cpp index 90878982..bfb1c744 100644 --- a/test/JsonObject_PrintTo_Tests.cpp +++ b/test/JsonObject_PrintTo_Tests.cpp @@ -15,7 +15,7 @@ class JsonObject_PrintTo_Tests : public testing::Test { protected: void outputMustBe(const char *expected) { char actual[256]; - size_t actualLen = _object.printTo(actual, sizeof(actual)); + size_t actualLen = _object.printTo(actual); size_t measuredLen = _object.measureLength(); EXPECT_STREQ(expected, actual); @@ -27,7 +27,9 @@ class JsonObject_PrintTo_Tests : public testing::Test { JsonObject &_object; }; -TEST_F(JsonObject_PrintTo_Tests, EmptyObject) { outputMustBe("{}"); } +TEST_F(JsonObject_PrintTo_Tests, EmptyObject) { + outputMustBe("{}"); +} TEST_F(JsonObject_PrintTo_Tests, TwoStrings) { _object["key1"] = "value1"; diff --git a/test/JsonObject_Subscript_Tests.cpp b/test/JsonObject_Subscript_Tests.cpp index c16ad413..52f72d89 100644 --- a/test/JsonObject_Subscript_Tests.cpp +++ b/test/JsonObject_Subscript_Tests.cpp @@ -5,8 +5,8 @@ // https://github.com/bblanchon/ArduinoJson // If you like this project, please add a star! -#include #include +#include class JsonObject_Subscript_Tests : public ::testing::Test { public: