forked from bblanchon/ArduinoJson
Remove the zero-copy mode
After removing the string size from `VariantContent`, `deserializeJson()` and `deserializeMsgPack()` could not support NUL in strings in the zero-copy mode anymore. Instead of adding a complicated warning in the documentation, I thought it was better to remove the zero-copy mode entirely. The zero-copy mode has always been a source of bugs because many users used it without realizing it. Also, the memory savings are smaller now that we deduplicate strings, so this feature should not be missed much.
This commit is contained in:
@ -14,3 +14,4 @@ HEAD
|
|||||||
* Store the strings in the heap
|
* Store the strings in the heap
|
||||||
* Reference-count shared strings
|
* Reference-count shared strings
|
||||||
* Always store `serialized("string")` by copy (#1915)
|
* Always store `serialized("string")` by copy (#1915)
|
||||||
|
* Remove the zero-copy mode of `deserializeJson()` and `deserializeMsgPack()`
|
||||||
|
@ -10,20 +10,17 @@
|
|||||||
#include "CustomReader.hpp"
|
#include "CustomReader.hpp"
|
||||||
|
|
||||||
using ArduinoJson::detail::sizeofObject;
|
using ArduinoJson::detail::sizeofObject;
|
||||||
|
using ArduinoJson::detail::sizeofString;
|
||||||
|
|
||||||
TEST_CASE("deserializeJson(char*)") {
|
TEST_CASE("deserializeJson(char*)") {
|
||||||
JsonDocument doc(1024);
|
JsonDocument doc(1024);
|
||||||
|
|
||||||
SECTION("should not duplicate strings") {
|
char input[] = "{\"hello\":\"world\"}";
|
||||||
char input[] = "{\"hello\":\"world\"}";
|
|
||||||
|
|
||||||
DeserializationError err = deserializeJson(doc, input);
|
DeserializationError err = deserializeJson(doc, input);
|
||||||
|
|
||||||
REQUIRE(err == DeserializationError::Ok);
|
REQUIRE(err == DeserializationError::Ok);
|
||||||
CHECK(doc.memoryUsage() == sizeofObject(1));
|
CHECK(doc.memoryUsage() == sizeofObject(1) + 2 * sizeofString(5));
|
||||||
CHECK(doc.as<JsonVariant>().memoryUsage() ==
|
|
||||||
sizeofObject(1)); // issue #1318
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("deserializeJson(unsigned char*, unsigned int)") { // issue #1897
|
TEST_CASE("deserializeJson(unsigned char*, unsigned int)") { // issue #1897
|
||||||
|
@ -154,12 +154,6 @@ TEST_CASE("String allocation fails") {
|
|||||||
REQUIRE(spyingAllocator.log() ==
|
REQUIRE(spyingAllocator.log() ==
|
||||||
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString(31)));
|
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString(31)));
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Input is const char*") {
|
|
||||||
char hello[] = "\"hello\"";
|
|
||||||
REQUIRE(deserializeJson(doc, hello) == DeserializationError::Ok);
|
|
||||||
REQUIRE(spyingAllocator.log() == AllocatorLog());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Deduplicate values") {
|
TEST_CASE("Deduplicate values") {
|
||||||
|
@ -1,47 +0,0 @@
|
|||||||
// ArduinoJson - https://arduinojson.org
|
|
||||||
// Copyright © 2014-2023, Benoit BLANCHON
|
|
||||||
// MIT License
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <ArduinoJson/Namespace.hpp>
|
|
||||||
#include <ArduinoJson/Strings/JsonString.hpp>
|
|
||||||
|
|
||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
|
||||||
|
|
||||||
class StringMover {
|
|
||||||
public:
|
|
||||||
StringMover(char* ptr) : writePtr_(ptr) {}
|
|
||||||
|
|
||||||
void startString() {
|
|
||||||
startPtr_ = writePtr_;
|
|
||||||
}
|
|
||||||
|
|
||||||
FORCE_INLINE const char* save() {
|
|
||||||
*writePtr_++ = 0; // terminator
|
|
||||||
return startPtr_;
|
|
||||||
}
|
|
||||||
|
|
||||||
void append(char c) {
|
|
||||||
*writePtr_++ = c;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isValid() const {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonString str() const {
|
|
||||||
writePtr_[0] = 0; // terminator
|
|
||||||
return JsonString(startPtr_, size(), JsonString::Linked);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t size() const {
|
|
||||||
return size_t(writePtr_ - startPtr_);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
char* writePtr_;
|
|
||||||
char* startPtr_;
|
|
||||||
};
|
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
|
@ -5,7 +5,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <ArduinoJson/StringStorage/StringCopier.hpp>
|
#include <ArduinoJson/StringStorage/StringCopier.hpp>
|
||||||
#include <ArduinoJson/StringStorage/StringMover.hpp>
|
|
||||||
|
|
||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
|
|
||||||
@ -15,10 +14,4 @@ StringCopier makeStringStorage(TInput&, MemoryPool* pool) {
|
|||||||
return StringCopier(pool);
|
return StringCopier(pool);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TChar>
|
|
||||||
StringMover makeStringStorage(
|
|
||||||
TChar* input, MemoryPool*,
|
|
||||||
typename enable_if<!is_const<TChar>::value>::type* = 0) {
|
|
||||||
return StringMover(reinterpret_cast<char*>(input));
|
|
||||||
}
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
Reference in New Issue
Block a user