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

@@ -13,8 +13,10 @@ class Writer<::String, void> {
static const size_t bufferCapacity = ARDUINOJSON_STRING_BUFFER_SIZE;
public:
explicit Writer(::String& str) : destination_(&str) {
size_ = 0;
explicit Writer(::String& str) : destination_(&str), 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() {

View File

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