serializeXxx() sets std::string and String instead of appending

This commit is contained in:
Benoit Blanchon
2023-07-31 18:37:35 +02:00
parent 3003756adb
commit af6954c224
6 changed files with 24 additions and 13 deletions

View File

@ -26,3 +26,4 @@ HEAD
* Remove `JsonDocument::garbageCollect()` * Remove `JsonDocument::garbageCollect()`
* Add `deserializeJson(JsonVariant, ...)` and `deserializeMsgPack(JsonVariant, ...)` (#1226) * Add `deserializeJson(JsonVariant, ...)` and `deserializeMsgPack(JsonVariant, ...)` (#1226)
* Call `shrinkToFit()` in `deserializeJson()` and `deserializeMsgPack()` * Call `shrinkToFit()` in `deserializeJson()` and `deserializeMsgPack()`
* `serializeJson()` and `serializeMsgPack()` replace the content of `std::string` and `String` instead of appending to it

View File

@ -9,8 +9,11 @@
// Reproduces Arduino's String class // Reproduces Arduino's String class
class String { class String {
public: public:
String() : maxCapacity_(1024) {} String() = default;
explicit String(const char* s) : str_(s), maxCapacity_(1024) {} String(const char* s) {
if (s)
str_.assign(s);
}
void limitCapacityTo(size_t maxCapacity) { void limitCapacityTo(size_t maxCapacity) {
maxCapacity_ = maxCapacity; maxCapacity_ = maxCapacity;
@ -33,7 +36,10 @@ class String {
} }
String& operator=(const char* s) { String& operator=(const char* s) {
str_.assign(s); if (s)
str_.assign(s);
else
str_.clear();
return *this; return *this;
} }
@ -59,7 +65,7 @@ class String {
private: private:
std::string str_; std::string str_;
size_t maxCapacity_; size_t maxCapacity_ = 1024;
}; };
class StringSumHelper : public ::String {}; class StringSumHelper : public ::String {};

View File

@ -12,14 +12,14 @@ TEST_CASE("serialize JsonArray to std::string") {
array.add(2); array.add(2);
SECTION("serializeJson()") { SECTION("serializeJson()") {
std::string json; std::string json = "erase me";
serializeJson(array, json); serializeJson(array, json);
REQUIRE("[4,2]" == json); REQUIRE("[4,2]" == json);
} }
SECTION("serializeJsonPretty") { SECTION("serializeJsonPretty") {
std::string json; std::string json = "erase me";
serializeJsonPretty(array, json); serializeJsonPretty(array, json);
REQUIRE("[\r\n 4,\r\n 2\r\n]" == json); REQUIRE("[\r\n 4,\r\n 2\r\n]" == json);
@ -32,14 +32,14 @@ TEST_CASE("serialize JsonObject to std::string") {
obj["key"] = "value"; obj["key"] = "value";
SECTION("object") { SECTION("object") {
std::string json; std::string json = "erase me";
serializeJson(doc, json); serializeJson(doc, json);
REQUIRE("{\"key\":\"value\"}" == json); REQUIRE("{\"key\":\"value\"}" == json);
} }
SECTION("serializeJsonPretty") { SECTION("serializeJsonPretty") {
std::string json; std::string json = "erase me";
serializeJsonPretty(doc, json); serializeJsonPretty(doc, json);
REQUIRE("{\r\n \"key\": \"value\"\r\n}" == json); REQUIRE("{\r\n \"key\": \"value\"\r\n}" == json);
@ -50,7 +50,7 @@ TEST_CASE("serialize an std::string containing a NUL") {
JsonDocument doc; JsonDocument doc;
doc.set(std::string("hello\0world", 11)); doc.set(std::string("hello\0world", 11));
std::string json; std::string json = "erase me";
serializeJson(doc, json); serializeJson(doc, json);
CHECK("\"hello\\u0000world\"" == json); CHECK("\"hello\\u0000world\"" == json);
} }

View File

@ -141,7 +141,7 @@ TEST_CASE("Writer<custom_string>") {
TEST_CASE("serializeJson(doc, String)") { TEST_CASE("serializeJson(doc, String)") {
JsonDocument doc; JsonDocument doc;
doc["hello"] = "world"; doc["hello"] = "world";
::String output; ::String output = "erase me";
SECTION("sufficient capacity") { SECTION("sufficient capacity") {
serializeJson(doc, output); serializeJson(doc, output);

View File

@ -13,8 +13,10 @@ class Writer<::String, void> {
static const size_t bufferCapacity = ARDUINOJSON_STRING_BUFFER_SIZE; static const size_t bufferCapacity = ARDUINOJSON_STRING_BUFFER_SIZE;
public: public:
explicit Writer(::String& str) : destination_(&str) { explicit Writer(::String& str) : destination_(&str), size_(0) {
size_ = 0; // clear the string but don't use "" to avoid useless allocation
// https://cpp4arduino.com/2018/11/21/eight-tips-to-use-the-string-class-efficiently.html
str = static_cast<const char*>(0);
} }
~Writer() { ~Writer() {

View File

@ -24,7 +24,9 @@ template <typename TDestination>
class Writer<TDestination, class Writer<TDestination,
typename enable_if<is_std_string<TDestination>::value>::type> { typename enable_if<is_std_string<TDestination>::value>::type> {
public: public:
Writer(TDestination& str) : str_(&str) {} Writer(TDestination& str) : str_(&str) {
str.clear();
}
size_t write(uint8_t c) { size_t write(uint8_t c) {
str_->push_back(static_cast<char>(c)); str_->push_back(static_cast<char>(c));