Skip to content

Commit 806fa90

Browse files
committed
Always store serialized("string") by copy (#1915)
1 parent 95f5d9d commit 806fa90

File tree

10 files changed

+11
-54
lines changed

10 files changed

+11
-54
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ HEAD
1313
* Remove `JsonDocument::capacity()`
1414
* Store the strings in the heap
1515
* Reference-count shared strings
16+
* Always store `serialized("string")` by copy (#1915)

extras/tests/JsonArray/add.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ TEST_CASE("JsonArray::add()") {
115115
REQUIRE(expectedSize == doc.memoryUsage());
116116
}
117117

118-
SECTION("should not duplicate serialized(const char*)") {
118+
SECTION("should duplicate serialized(const char*)") {
119119
array.add(serialized("{}"));
120-
const size_t expectedSize = sizeofArray(1);
120+
const size_t expectedSize = sizeofArray(1) + sizeofString(2);
121121
REQUIRE(expectedSize == doc.memoryUsage());
122122
}
123123

extras/tests/JsonDocument/shrinkToFit.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,6 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
9797
<< AllocatorLog::Reallocate(4096, 0));
9898
}
9999

100-
SECTION("linked raw") {
101-
doc.set(serialized("[{},123]"));
102-
103-
doc.shrinkToFit();
104-
105-
REQUIRE(doc.as<std::string>() == "[{},123]");
106-
REQUIRE(spyingAllocator.log() == AllocatorLog()
107-
<< AllocatorLog::Allocate(4096)
108-
<< AllocatorLog::Reallocate(4096, 0));
109-
}
110-
111100
SECTION("owned raw") {
112101
doc.set(serialized(std::string("[{},12]")));
113102

extras/tests/JsonVariant/copy.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,16 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
8989
AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
9090
}
9191

92-
SECTION("stores Serialized<const char*> by reference") {
93-
var1.set(serialized("hello!!", 8));
92+
SECTION("stores Serialized<const char*> by copy") {
93+
var1.set(serialized("hello!!", 7));
9494
spyingAllocator.clearLog();
9595

9696
var2.set(var1);
9797

98-
REQUIRE(doc1.memoryUsage() == 0);
99-
REQUIRE(doc2.memoryUsage() == 0);
100-
REQUIRE(spyingAllocator.log() == AllocatorLog());
98+
REQUIRE(doc1.memoryUsage() == sizeofString(7));
99+
REQUIRE(doc2.memoryUsage() == sizeofString(7));
100+
REQUIRE(spyingAllocator.log() ==
101+
AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
101102
}
102103

103104
SECTION("stores Serialized<char*> by copy") {

extras/tests/JsonVariant/set.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,6 @@ TEST_CASE("JsonVariant::set() releases the previous value") {
205205

206206
SECTION("Serialized<const char*>") {
207207
v.set(serialized("[]"));
208-
REQUIRE(doc.memoryUsage() == sizeofObject(1));
208+
REQUIRE(doc.memoryUsage() == sizeofObject(1) + sizeofString(2));
209209
}
210210
}

src/ArduinoJson/Variant/ConverterImpl.hpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,22 +155,11 @@ convertToJson(const T& src, JsonVariant dst) {
155155
variantSetString(data, adaptString(src), pool);
156156
}
157157

158-
template <>
159-
struct Converter<SerializedValue<const char*>>
160-
: private detail::VariantAttorney {
161-
static void toJson(SerializedValue<const char*> src, JsonVariant dst) {
162-
variantSetLinkedRaw(getData(dst), src, getPool(dst));
163-
}
164-
};
165-
166158
// SerializedValue<std::string>
167159
// SerializedValue<String>
168160
// SerializedValue<const __FlashStringHelper*>
169161
template <typename T>
170-
struct Converter<
171-
SerializedValue<T>,
172-
typename detail::enable_if<!detail::is_same<const char*, T>::value>::type>
173-
: private detail::VariantAttorney {
162+
struct Converter<SerializedValue<T>> : private detail::VariantAttorney {
174163
static void toJson(SerializedValue<T> src, JsonVariant dst) {
175164
variantSetOwnedRaw(getData(dst), src, getPool(dst));
176165
}

src/ArduinoJson/Variant/VariantContent.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ enum {
1717

1818
OWNED_VALUE_BIT = 0x01,
1919
VALUE_IS_NULL = 0,
20-
VALUE_IS_LINKED_RAW = 0x02,
2120
VALUE_IS_OWNED_RAW = 0x03,
2221
VALUE_IS_LINKED_STRING = 0x04,
2322
VALUE_IS_OWNED_STRING = 0x05,

src/ArduinoJson/Variant/VariantData.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class VariantData {
4646
content_.asString.size);
4747

4848
case VALUE_IS_OWNED_RAW:
49-
case VALUE_IS_LINKED_RAW:
5049
return visitor.visitRawJson(content_.asString.data,
5150
content_.asString.size);
5251

@@ -158,12 +157,6 @@ class VariantData {
158157
content_.asFloat = value;
159158
}
160159

161-
void setLinkedRaw(const char* data, size_t n) {
162-
setType(VALUE_IS_LINKED_RAW);
163-
content_.asString.data = data;
164-
content_.asString.size = n;
165-
}
166-
167160
void setOwnedRaw(const char* data, size_t n) {
168161
setType(VALUE_IS_OWNED_RAW);
169162
content_.asString.data = data;

src/ArduinoJson/Variant/VariantFunctions.hpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,6 @@ inline void variantSetOwnedRaw(VariantData* var, SerializedValue<T> value,
135135
var->setNull();
136136
}
137137

138-
inline void variantSetLinkedRaw(VariantData* var,
139-
SerializedValue<const char*> value,
140-
MemoryPool* pool) {
141-
if (!var)
142-
return;
143-
variantRelease(var, pool);
144-
if (value.data())
145-
var->setLinkedRaw(value.data(), value.size());
146-
else
147-
var->setNull();
148-
}
149-
150138
inline size_t variantSize(const VariantData* var) {
151139
return var != 0 ? var->size() : 0;
152140
}

src/ArduinoJson/Variant/VariantImpl.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ inline JsonString VariantData::asString() const {
8585

8686
inline JsonString VariantData::asRaw() const {
8787
switch (type()) {
88-
case VALUE_IS_LINKED_RAW:
89-
return JsonString(content_.asString.data, content_.asString.size,
90-
JsonString::Linked);
9188
case VALUE_IS_OWNED_RAW:
9289
return JsonString(content_.asString.data, content_.asString.size,
9390
JsonString::Copied);

0 commit comments

Comments
 (0)