Skip to content

Commit 5b1f588

Browse files
committed
Remove the overload of setString() for StringNode
1 parent cb1dcfa commit 5b1f588

File tree

9 files changed

+33
-23
lines changed

9 files changed

+33
-23
lines changed

extras/tests/Deprecated/BasicJsonDocument.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ TEST_CASE("BasicJsonDocument") {
4646
deserializeJson(doc, "{\"hello\":\"world\"}");
4747
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
4848
doc.clear();
49-
REQUIRE(allocatorLog == "ARAARDDD");
49+
REQUIRE(allocatorLog == "AARARDDD");
5050
}
5151

5252
SECTION("copy") {

extras/tests/JsonDeserializer/input_types.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ TEST_CASE("deserializeJson(char*)") {
2626
REQUIRE(spy.log() ==
2727
AllocatorLog{
2828
Allocate(sizeofStringBuffer()),
29-
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
3029
Allocate(sizeofPool()),
30+
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
3131
Allocate(sizeofStringBuffer()),
3232
Reallocate(sizeofStringBuffer(), sizeofString("world")),
3333
Reallocate(sizeofPool(), sizeofObject(1)),

extras/tests/JsonDeserializer/object.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,8 @@ TEST_CASE("deserialize JSON object") {
299299
REQUIRE(spy.log() ==
300300
AllocatorLog{
301301
Allocate(sizeofStringBuffer()),
302-
Reallocate(sizeofStringBuffer(), sizeofString("a")),
303302
Allocate(sizeofPool()),
303+
Reallocate(sizeofStringBuffer(), sizeofString("a")),
304304
Allocate(sizeofStringBuffer()),
305305
Reallocate(sizeofStringBuffer(), sizeofString("b")),
306306
Allocate(sizeofStringBuffer()),
@@ -378,7 +378,7 @@ TEST_CASE("deserialize JSON object under memory constraints") {
378378
}
379379

380380
SECTION("pool allocation fails") {
381-
timebomb.setCountdown(2);
381+
timebomb.setCountdown(1);
382382
char input[] = "{\"a\":1}";
383383

384384
DeserializationError err = deserializeJson(doc, input);

extras/tests/JsonDeserializer/string.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ TEST_CASE("Allocation of the key fails") {
133133
REQUIRE(spy.log() ==
134134
AllocatorLog{
135135
Allocate(sizeofStringBuffer()),
136-
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
137136
Allocate(sizeofPool()),
137+
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
138138
AllocateFail(sizeofStringBuffer()),
139139
ReallocateFail(sizeofPool(), sizeofObject(1)),
140140
});
@@ -155,8 +155,8 @@ TEST_CASE("Allocation of the key fails") {
155155
REQUIRE(spy.log() ==
156156
AllocatorLog{
157157
Allocate(sizeofStringBuffer()),
158-
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
159158
Allocate(sizeofPool()),
159+
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
160160
AllocateFail(sizeofStringBuffer()),
161161
ReallocateFail(sizeofPool(), sizeofObject(1)),
162162
});

src/ArduinoJson/Json/JsonDeserializer.hpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,11 @@ class JsonDeserializer {
275275
if (memberFilter.allow()) {
276276
auto member = object.getMember(adaptString(key), resources_);
277277
if (!member) {
278-
// Save key in memory pool.
279-
auto savedKey = stringBuilder_.save();
280-
281-
// Allocate slot in object
282-
member = object.addMember(savedKey, resources_);
283-
if (!member)
278+
auto keyVariant = object.addPair(&member, resources_);
279+
if (!keyVariant)
284280
return DeserializationError::NoMemory;
281+
282+
keyVariant->setOwnedString(stringBuilder_.save());
285283
} else {
286284
member->clear(resources_);
287285
}

src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,11 @@ class MsgPackDeserializer {
408408
if (memberFilter.allow()) {
409409
ARDUINOJSON_ASSERT(object != 0);
410410

411-
// Save key in memory pool.
412-
auto savedKey = stringBuffer_.save();
413-
414-
member = object->addMember(savedKey, resources_);
415-
if (!member)
411+
auto keyVariant = object->addPair(&member, resources_);
412+
if (!keyVariant)
416413
return DeserializationError::NoMemory;
414+
415+
keyVariant->setOwnedString(stringBuffer_.save());
417416
} else {
418417
member = 0;
419418
}

src/ArduinoJson/Object/ObjectData.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
1010

1111
class ObjectData : public CollectionData {
1212
public:
13-
template <typename TAdaptedString> // also works with StringNode*
13+
template <typename TAdaptedString>
1414
VariantData* addMember(TAdaptedString key, ResourceManager* resources);
1515

16+
VariantData* addPair(VariantData** value, ResourceManager* resources);
17+
1618
template <typename TAdaptedString>
1719
VariantData* getOrAddMember(TAdaptedString key, ResourceManager* resources);
1820

src/ArduinoJson/Object/ObjectImpl.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,22 @@ inline VariantData* ObjectData::addMember(TAdaptedString key,
6868
return valueSlot.ptr();
6969
}
7070

71+
inline VariantData* ObjectData::addPair(VariantData** value,
72+
ResourceManager* resources) {
73+
auto keySlot = resources->allocVariant();
74+
if (!keySlot)
75+
return nullptr;
76+
77+
auto valueSlot = resources->allocVariant();
78+
if (!valueSlot)
79+
return nullptr;
80+
*value = valueSlot.ptr();
81+
82+
CollectionData::appendPair(keySlot, valueSlot, resources);
83+
84+
return keySlot.ptr();
85+
}
86+
7187
// Returns the size (in bytes) of an object with n members.
7288
constexpr size_t sizeofObject(size_t n) {
7389
return 2 * n * ResourceManager::slotSize;

src/ArduinoJson/Variant/VariantData.hpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -488,11 +488,6 @@ class VariantData {
488488
template <typename TAdaptedString>
489489
bool setString(TAdaptedString value, ResourceManager* resources);
490490

491-
bool setString(StringNode* s, ResourceManager*) {
492-
setOwnedString(s);
493-
return true;
494-
}
495-
496491
template <typename TAdaptedString>
497492
static void setString(VariantData* var, TAdaptedString value,
498493
ResourceManager* resources) {

0 commit comments

Comments
 (0)