Remove the overload of setString() for StringNode

This commit is contained in:
Benoit Blanchon
2025-02-27 19:01:26 +01:00
parent e03d8ae885
commit b06cee8f4d
9 changed files with 33 additions and 23 deletions

View File

@ -46,7 +46,7 @@ TEST_CASE("BasicJsonDocument") {
deserializeJson(doc, "{\"hello\":\"world\"}"); deserializeJson(doc, "{\"hello\":\"world\"}");
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}"); REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
doc.clear(); doc.clear();
REQUIRE(allocatorLog == "ARAARDDD"); REQUIRE(allocatorLog == "AARARDDD");
} }
SECTION("copy") { SECTION("copy") {

View File

@ -26,8 +26,8 @@ TEST_CASE("deserializeJson(char*)") {
REQUIRE(spy.log() == REQUIRE(spy.log() ==
AllocatorLog{ AllocatorLog{
Allocate(sizeofStringBuffer()), Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
Allocate(sizeofPool()), Allocate(sizeofPool()),
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
Allocate(sizeofStringBuffer()), Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("world")), Reallocate(sizeofStringBuffer(), sizeofString("world")),
Reallocate(sizeofPool(), sizeofObject(1)), Reallocate(sizeofPool(), sizeofObject(1)),

View File

@ -299,8 +299,8 @@ TEST_CASE("deserialize JSON object") {
REQUIRE(spy.log() == REQUIRE(spy.log() ==
AllocatorLog{ AllocatorLog{
Allocate(sizeofStringBuffer()), Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("a")),
Allocate(sizeofPool()), Allocate(sizeofPool()),
Reallocate(sizeofStringBuffer(), sizeofString("a")),
Allocate(sizeofStringBuffer()), Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("b")), Reallocate(sizeofStringBuffer(), sizeofString("b")),
Allocate(sizeofStringBuffer()), Allocate(sizeofStringBuffer()),
@ -378,7 +378,7 @@ TEST_CASE("deserialize JSON object under memory constraints") {
} }
SECTION("pool allocation fails") { SECTION("pool allocation fails") {
timebomb.setCountdown(2); timebomb.setCountdown(1);
char input[] = "{\"a\":1}"; char input[] = "{\"a\":1}";
DeserializationError err = deserializeJson(doc, input); DeserializationError err = deserializeJson(doc, input);

View File

@ -133,8 +133,8 @@ TEST_CASE("Allocation of the key fails") {
REQUIRE(spy.log() == REQUIRE(spy.log() ==
AllocatorLog{ AllocatorLog{
Allocate(sizeofStringBuffer()), Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
Allocate(sizeofPool()), Allocate(sizeofPool()),
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
AllocateFail(sizeofStringBuffer()), AllocateFail(sizeofStringBuffer()),
ReallocateFail(sizeofPool(), sizeofObject(1)), ReallocateFail(sizeofPool(), sizeofObject(1)),
}); });
@ -155,8 +155,8 @@ TEST_CASE("Allocation of the key fails") {
REQUIRE(spy.log() == REQUIRE(spy.log() ==
AllocatorLog{ AllocatorLog{
Allocate(sizeofStringBuffer()), Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
Allocate(sizeofPool()), Allocate(sizeofPool()),
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
AllocateFail(sizeofStringBuffer()), AllocateFail(sizeofStringBuffer()),
ReallocateFail(sizeofPool(), sizeofObject(1)), ReallocateFail(sizeofPool(), sizeofObject(1)),
}); });

View File

@ -275,13 +275,11 @@ class JsonDeserializer {
if (memberFilter.allow()) { if (memberFilter.allow()) {
auto member = object.getMember(adaptString(key), resources_); auto member = object.getMember(adaptString(key), resources_);
if (!member) { if (!member) {
// Save key in memory pool. auto keyVariant = object.addPair(&member, resources_);
auto savedKey = stringBuilder_.save(); if (!keyVariant)
// Allocate slot in object
member = object.addMember(savedKey, resources_);
if (!member)
return DeserializationError::NoMemory; return DeserializationError::NoMemory;
keyVariant->setOwnedString(stringBuilder_.save());
} else { } else {
member->clear(resources_); member->clear(resources_);
} }

View File

@ -408,12 +408,11 @@ class MsgPackDeserializer {
if (memberFilter.allow()) { if (memberFilter.allow()) {
ARDUINOJSON_ASSERT(object != 0); ARDUINOJSON_ASSERT(object != 0);
// Save key in memory pool. auto keyVariant = object->addPair(&member, resources_);
auto savedKey = stringBuffer_.save(); if (!keyVariant)
member = object->addMember(savedKey, resources_);
if (!member)
return DeserializationError::NoMemory; return DeserializationError::NoMemory;
keyVariant->setOwnedString(stringBuffer_.save());
} else { } else {
member = 0; member = 0;
} }

View File

@ -10,9 +10,11 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
class ObjectData : public CollectionData { class ObjectData : public CollectionData {
public: public:
template <typename TAdaptedString> // also works with StringNode* template <typename TAdaptedString>
VariantData* addMember(TAdaptedString key, ResourceManager* resources); VariantData* addMember(TAdaptedString key, ResourceManager* resources);
VariantData* addPair(VariantData** value, ResourceManager* resources);
template <typename TAdaptedString> template <typename TAdaptedString>
VariantData* getOrAddMember(TAdaptedString key, ResourceManager* resources); VariantData* getOrAddMember(TAdaptedString key, ResourceManager* resources);

View File

@ -68,6 +68,22 @@ inline VariantData* ObjectData::addMember(TAdaptedString key,
return valueSlot.ptr(); return valueSlot.ptr();
} }
inline VariantData* ObjectData::addPair(VariantData** value,
ResourceManager* resources) {
auto keySlot = resources->allocVariant();
if (!keySlot)
return nullptr;
auto valueSlot = resources->allocVariant();
if (!valueSlot)
return nullptr;
*value = valueSlot.ptr();
CollectionData::appendPair(keySlot, valueSlot, resources);
return keySlot.ptr();
}
// Returns the size (in bytes) of an object with n members. // Returns the size (in bytes) of an object with n members.
constexpr size_t sizeofObject(size_t n) { constexpr size_t sizeofObject(size_t n) {
return 2 * n * ResourceManager::slotSize; return 2 * n * ResourceManager::slotSize;

View File

@ -488,11 +488,6 @@ class VariantData {
template <typename TAdaptedString> template <typename TAdaptedString>
bool setString(TAdaptedString value, ResourceManager* resources); bool setString(TAdaptedString value, ResourceManager* resources);
bool setString(StringNode* s, ResourceManager*) {
setOwnedString(s);
return true;
}
template <typename TAdaptedString> template <typename TAdaptedString>
static void setString(VariantData* var, TAdaptedString value, static void setString(VariantData* var, TAdaptedString value,
ResourceManager* resources) { ResourceManager* resources) {