Added an printTo(char[N]) and prettyPrintTo(char[N]) (issue #292)

This commit is contained in:
Benoit Blanchon
2016-12-10 15:59:48 +01:00
parent 1f3e227a8b
commit 432476c98a
7 changed files with 31 additions and 12 deletions

View File

@ -4,6 +4,7 @@ ArduinoJson: change log
HEAD 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) * 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) * Renamed `*.ipp` to `*Impl.hpp` because they were ignored by Arduino IDE (issue #396)

View File

@ -50,6 +50,11 @@ class JsonPrintable {
return printTo(sb); return printTo(sb);
} }
template <size_t N>
size_t printTo(char (&buffer)[N]) const {
return printTo(buffer, N);
}
template <typename TString> template <typename TString>
typename TypeTraits::EnableIf<StringFuncs<TString>::has_append, size_t>::type typename TypeTraits::EnableIf<StringFuncs<TString>::has_append, size_t>::type
printTo(TString &str) const { printTo(TString &str) const {
@ -67,6 +72,11 @@ class JsonPrintable {
return prettyPrintTo(sb); return prettyPrintTo(sb);
} }
template <size_t N>
size_t prettyPrintTo(char (&buffer)[N]) const {
return prettyPrintTo(buffer, N);
}
size_t prettyPrintTo(Print &print) const { size_t prettyPrintTo(Print &print) const {
IndentedPrint indentedPrint = IndentedPrint(print); IndentedPrint indentedPrint = IndentedPrint(print);
return prettyPrintTo(indentedPrint); return prettyPrintTo(indentedPrint);

View File

@ -5,8 +5,8 @@
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star! // If you like this project, please add a star!
#include <gtest/gtest.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h>
class JsonArray_PrettyPrintTo_Tests : public testing::Test { class JsonArray_PrettyPrintTo_Tests : public testing::Test {
public: public:
@ -19,7 +19,7 @@ class JsonArray_PrettyPrintTo_Tests : public testing::Test {
void outputMustBe(const char* expected) { void outputMustBe(const char* expected) {
char actual[256]; char actual[256];
size_t actualLen = array.prettyPrintTo(actual, sizeof(actual)); size_t actualLen = array.prettyPrintTo(actual);
size_t measuredLen = array.measurePrettyLength(); size_t measuredLen = array.measurePrettyLength();
EXPECT_STREQ(expected, actual); 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) { TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement) {
array.add(1); array.add(1);

View File

@ -5,8 +5,8 @@
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star! // If you like this project, please add a star!
#include <gtest/gtest.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h>
class JsonArray_PrintTo_Tests : public testing::Test { class JsonArray_PrintTo_Tests : public testing::Test {
public: public:
@ -17,7 +17,7 @@ class JsonArray_PrintTo_Tests : public testing::Test {
JsonArray &array; JsonArray &array;
void outputMustBe(const char *expected) { void outputMustBe(const char *expected) {
size_t actualLen = array.printTo(buffer, sizeof(buffer)); size_t actualLen = array.printTo(buffer);
size_t measuredLen = array.measureLength(); size_t measuredLen = array.measureLength();
EXPECT_STREQ(expected, buffer); EXPECT_STREQ(expected, buffer);
@ -29,7 +29,9 @@ class JsonArray_PrintTo_Tests : public testing::Test {
char buffer[256]; char buffer[256];
}; };
TEST_F(JsonArray_PrintTo_Tests, Empty) { outputMustBe("[]"); } TEST_F(JsonArray_PrintTo_Tests, Empty) {
outputMustBe("[]");
}
TEST_F(JsonArray_PrintTo_Tests, Null) { TEST_F(JsonArray_PrintTo_Tests, Null) {
array.add(static_cast<char *>(0)); array.add(static_cast<char *>(0));

View File

@ -5,8 +5,8 @@
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star! // If you like this project, please add a star!
#include <gtest/gtest.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h>
class JsonObject_PrettyPrintTo_Tests : public testing::Test { class JsonObject_PrettyPrintTo_Tests : public testing::Test {
public: public:
@ -19,7 +19,7 @@ class JsonObject_PrettyPrintTo_Tests : public testing::Test {
void outputMustBe(const char *expected) { void outputMustBe(const char *expected) {
char buffer[256]; char buffer[256];
size_t actualLen = _object.prettyPrintTo(buffer, sizeof(buffer)); size_t actualLen = _object.prettyPrintTo(buffer);
size_t measuredLen = _object.measurePrettyLength(); size_t measuredLen = _object.measurePrettyLength();
EXPECT_STREQ(expected, buffer); 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) { TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember) {
_object["key"] = "value"; _object["key"] = "value";

View File

@ -15,7 +15,7 @@ class JsonObject_PrintTo_Tests : public testing::Test {
protected: protected:
void outputMustBe(const char *expected) { void outputMustBe(const char *expected) {
char actual[256]; char actual[256];
size_t actualLen = _object.printTo(actual, sizeof(actual)); size_t actualLen = _object.printTo(actual);
size_t measuredLen = _object.measureLength(); size_t measuredLen = _object.measureLength();
EXPECT_STREQ(expected, actual); EXPECT_STREQ(expected, actual);
@ -27,7 +27,9 @@ class JsonObject_PrintTo_Tests : public testing::Test {
JsonObject &_object; JsonObject &_object;
}; };
TEST_F(JsonObject_PrintTo_Tests, EmptyObject) { outputMustBe("{}"); } TEST_F(JsonObject_PrintTo_Tests, EmptyObject) {
outputMustBe("{}");
}
TEST_F(JsonObject_PrintTo_Tests, TwoStrings) { TEST_F(JsonObject_PrintTo_Tests, TwoStrings) {
_object["key1"] = "value1"; _object["key1"] = "value1";

View File

@ -5,8 +5,8 @@
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star! // If you like this project, please add a star!
#include <gtest/gtest.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h>
class JsonObject_Subscript_Tests : public ::testing::Test { class JsonObject_Subscript_Tests : public ::testing::Test {
public: public: