Don't store string literals by pointer anymore

Fixes #2189
This commit is contained in:
Benoit Blanchon
2025-08-28 10:04:11 +02:00
parent 509807d3c2
commit dddc4912c4
30 changed files with 174 additions and 331 deletions

View File

@@ -1,6 +1,25 @@
ArduinoJson: change log ArduinoJson: change log
======================= =======================
HEAD
----
* Don't store string literals by pointer anymore (issue #2189)
Version 7.3 introduced a new way to detect string literals, but it fails in some edge cases.
I could not find a way to fix it, so I chose to remove the optimization rather than keep it broken.
> ### BREAKING CHANGES
>
> Since version 7.3, you could pass a boolean to `JsonString`'s constructor to force the string to be stored by pointer.
> This optimization has been removed, and you'll get a deprecation warning if you use it.
> To fix the issue, you must remove the boolean argument from the constructor, or better yet, remove `JsonString` altogether.
>
> ```diff
> char name[] = "ArduinoJson";
> - doc["name"] = JsonString(name, true);
> + doc["name"] = name;
> ```
v7.4.2 (2025-06-20) v7.4.2 (2025-06-20)
------ ------

View File

@@ -54,7 +54,7 @@ TEST_CASE("BasicJsonDocument") {
doc["hello"] = "world"; doc["hello"] = "world";
auto copy = doc; auto copy = doc;
REQUIRE(copy.as<std::string>() == "{\"hello\":\"world\"}"); REQUIRE(copy.as<std::string>() == "{\"hello\":\"world\"}");
REQUIRE(allocatorLog == "AA"); REQUIRE(allocatorLog == "AAAAAA");
} }
SECTION("capacity") { SECTION("capacity") {

View File

@@ -56,6 +56,7 @@ TEST_CASE("JsonArray::add(T)") {
REQUIRE(array[0].is<int>() == false); REQUIRE(array[0].is<int>() == false);
REQUIRE(spy.log() == AllocatorLog{ REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()), Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
}); });
} }

View File

@@ -104,6 +104,8 @@ TEST_CASE("deserializeJson(MemberProxy)") {
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\",\"value\":[42]}"); REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\",\"value\":[42]}");
REQUIRE(spy.log() == AllocatorLog{}); REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("value")),
});
} }
} }

View File

@@ -31,6 +31,7 @@ TEST_CASE("ElementProxy::add()") {
REQUIRE(doc.as<std::string>() == "[[\"world\"]]"); REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
REQUIRE(spy.log() == AllocatorLog{ REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()), Allocate(sizeofPool()),
Allocate(sizeofString("world")),
}); });
} }

View File

@@ -25,6 +25,7 @@ TEST_CASE("MemberProxy::add()") {
REQUIRE(doc.as<std::string>() == "{\"hello\":[42]}"); REQUIRE(doc.as<std::string>() == "{\"hello\":[42]}");
REQUIRE(spy.log() == AllocatorLog{ REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()), Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
}); });
} }
@@ -34,6 +35,8 @@ TEST_CASE("MemberProxy::add()") {
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}"); REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
REQUIRE(spy.log() == AllocatorLog{ REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()), Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
Allocate(sizeofString("world")),
}); });
} }
@@ -44,6 +47,7 @@ TEST_CASE("MemberProxy::add()") {
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}"); REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
REQUIRE(spy.log() == AllocatorLog{ REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()), Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
Allocate(sizeofString("world")), Allocate(sizeofString("world")),
}); });
} }
@@ -55,6 +59,7 @@ TEST_CASE("MemberProxy::add()") {
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}"); REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
REQUIRE(spy.log() == AllocatorLog{ REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()), Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
Allocate(sizeofString("world")), Allocate(sizeofString("world")),
}); });
@@ -71,6 +76,7 @@ TEST_CASE("MemberProxy::add()") {
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}"); REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
REQUIRE(spy.log() == AllocatorLog{ REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()), Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
Allocate(sizeofString("world")), Allocate(sizeofString("world")),
}); });
} }

View File

@@ -32,6 +32,7 @@ TEST_CASE("JsonDocument::add(T)") {
REQUIRE(doc.as<std::string>() == "[\"hello\"]"); REQUIRE(doc.as<std::string>() == "[\"hello\"]");
REQUIRE(spy.log() == AllocatorLog{ REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()), Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
}); });
} }

View File

@@ -64,6 +64,8 @@ TEST_CASE("JsonDocument constructor") {
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}"); REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
REQUIRE(spyingAllocator.log() == AllocatorLog{ REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofPool()), Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
Allocate(sizeofString("world")),
}); });
} }
@@ -87,6 +89,7 @@ TEST_CASE("JsonDocument constructor") {
REQUIRE(doc2.as<std::string>() == "[\"hello\"]"); REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
REQUIRE(spyingAllocator.log() == AllocatorLog{ REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofPool()), Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
}); });
} }

View File

@@ -37,7 +37,9 @@ TEST_CASE("JsonDocument::set()") {
doc.set("example"); doc.set("example");
REQUIRE(doc.as<const char*>() == "example"_s); REQUIRE(doc.as<const char*>() == "example"_s);
REQUIRE(spy.log() == AllocatorLog{}); REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("example")),
});
} }
SECTION("const char*") { SECTION("const char*") {

View File

@@ -69,17 +69,8 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
REQUIRE(spyingAllocator.log() == AllocatorLog{}); REQUIRE(spyingAllocator.log() == AllocatorLog{});
} }
SECTION("linked string") { SECTION("string") {
doc.set("hello"); doc.set("abcdefg");
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "hello");
REQUIRE(spyingAllocator.log() == AllocatorLog{});
}
SECTION("owned string") {
doc.set("abcdefg"_s);
REQUIRE(doc.as<std::string>() == "abcdefg"); REQUIRE(doc.as<std::string>() == "abcdefg");
doc.shrinkToFit(); doc.shrinkToFit();
@@ -101,20 +92,7 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
}); });
} }
SECTION("linked key") { SECTION("object key") {
doc["key"] = 42;
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "{\"key\":42}");
REQUIRE(spyingAllocator.log() ==
AllocatorLog{
Allocate(sizeofPool()),
Reallocate(sizeofPool(), sizeofObject(1)),
});
}
SECTION("owned key") {
doc["abcdefg"_s] = 42; doc["abcdefg"_s] = 42;
doc.shrinkToFit(); doc.shrinkToFit();
@@ -128,20 +106,7 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
}); });
} }
SECTION("linked string in array") { SECTION("string in array") {
doc.add("hello");
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "[\"hello\"]");
REQUIRE(spyingAllocator.log() ==
AllocatorLog{
Allocate(sizeofPool()),
Reallocate(sizeofPool(), sizeofArray(1)),
});
}
SECTION("owned string in array") {
doc.add("abcdefg"_s); doc.add("abcdefg"_s);
doc.shrinkToFit(); doc.shrinkToFit();
@@ -155,20 +120,7 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
}); });
} }
SECTION("linked string in object") { SECTION("string in object") {
doc["key"] = "hello";
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "{\"key\":\"hello\"}");
REQUIRE(spyingAllocator.log() ==
AllocatorLog{
Allocate(sizeofPool()),
Reallocate(sizeofPool(), sizeofObject(1)),
});
}
SECTION("owned string in object") {
doc["key"] = "abcdefg"_s; doc["key"] = "abcdefg"_s;
doc.shrinkToFit(); doc.shrinkToFit();

View File

@@ -114,6 +114,7 @@ TEST_CASE("JsonDocument::operator[] key storage") {
REQUIRE(doc.as<std::string>() == "{\"hello\":0}"); REQUIRE(doc.as<std::string>() == "{\"hello\":0}");
REQUIRE(spy.log() == AllocatorLog{ REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()), Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
}); });
} }

View File

@@ -16,44 +16,18 @@ TEST_CASE("JsonObject::set()") {
JsonObject obj1 = doc1.to<JsonObject>(); JsonObject obj1 = doc1.to<JsonObject>();
JsonObject obj2 = doc2.to<JsonObject>(); JsonObject obj2 = doc2.to<JsonObject>();
SECTION("doesn't copy static string in key or value") { SECTION("copy key and string value") {
obj1["hello"] = "world"; obj1["hello"] = "world";
spy.clearLog(); spy.clearLog();
bool success = obj2.set(obj1); bool success = obj2.set(obj1);
REQUIRE(success == true);
REQUIRE(obj2["hello"] == "world"_s);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
});
}
SECTION("copy local string value") {
obj1["hello"] = "world"_s;
spy.clearLog();
bool success = obj2.set(obj1);
REQUIRE(success == true);
REQUIRE(obj2["hello"] == "world"_s);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
});
}
SECTION("copy local key") {
obj1["hello"_s] = "world";
spy.clearLog();
bool success = obj2.set(obj1);
REQUIRE(success == true); REQUIRE(success == true);
REQUIRE(obj2["hello"] == "world"_s); REQUIRE(obj2["hello"] == "world"_s);
REQUIRE(spy.log() == AllocatorLog{ REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()), Allocate(sizeofPool()),
Allocate(sizeofString("hello")), Allocate(sizeofString("hello")),
Allocate(sizeofString("world")),
}); });
} }
@@ -110,7 +84,7 @@ TEST_CASE("JsonObject::set()") {
} }
SECTION("copy fails in the middle of an array") { SECTION("copy fails in the middle of an array") {
TimebombAllocator timebomb(1); TimebombAllocator timebomb(2);
JsonDocument doc3(&timebomb); JsonDocument doc3(&timebomb);
JsonObject obj3 = doc3.to<JsonObject>(); JsonObject obj3 = doc3.to<JsonObject>();

View File

@@ -101,30 +101,8 @@ TEST_CASE("JsonObject::operator[]") {
obj[key] = 42; obj[key] = 42;
REQUIRE(42 == obj[key]); REQUIRE(42 == obj[key]);
} }
SECTION("should duplicate key and value strings") {
SECTION("should not duplicate const char*") {
obj["hello"] = "world"; obj["hello"] = "world";
REQUIRE(spy.log() == AllocatorLog{Allocate(sizeofPool())});
}
SECTION("should duplicate char* value") {
obj["hello"] = const_cast<char*>("world");
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
});
}
SECTION("should duplicate char* key") {
obj[const_cast<char*>("hello")] = "world";
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
});
}
SECTION("should duplicate char* key&value") {
obj[const_cast<char*>("hello")] = const_cast<char*>("world");
REQUIRE(spy.log() == AllocatorLog{ REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()), Allocate(sizeofPool()),
Allocate(sizeofString("hello")), Allocate(sizeofString("hello")),
@@ -132,46 +110,6 @@ TEST_CASE("JsonObject::operator[]") {
}); });
} }
SECTION("should duplicate std::string value") {
obj["hello"] = "world"_s;
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
});
}
SECTION("should duplicate std::string key") {
obj["hello"_s] = "world";
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
});
}
SECTION("should duplicate std::string key&value") {
obj["hello"_s] = "world"_s;
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
Allocate(sizeofString("world")),
});
}
SECTION("should duplicate a non-static JsonString key") {
obj[JsonString("hello", false)] = "world";
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
});
}
SECTION("should not duplicate a static JsonString key") {
obj[JsonString("hello", true)] = "world";
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
});
}
SECTION("should ignore null key") { SECTION("should ignore null key") {
// object must have a value to make a call to strcmp() // object must have a value to make a call to strcmp()
obj["dummy"] = 42; obj["dummy"] = 42;

View File

@@ -185,7 +185,6 @@ TEST_CASE("JsonVariant::as()") {
REQUIRE(variant.as<long>() == 42L); REQUIRE(variant.as<long>() == 42L);
REQUIRE(variant.as<double>() == 42); REQUIRE(variant.as<double>() == 42);
REQUIRE(variant.as<JsonString>() == "42"); REQUIRE(variant.as<JsonString>() == "42");
REQUIRE(variant.as<JsonString>().isStatic() == true);
} }
SECTION("set(\"hello\")") { SECTION("set(\"hello\")") {
@@ -208,7 +207,6 @@ TEST_CASE("JsonVariant::as()") {
REQUIRE(variant.as<const char*>() == "4.2"_s); REQUIRE(variant.as<const char*>() == "4.2"_s);
REQUIRE(variant.as<std::string>() == "4.2"_s); REQUIRE(variant.as<std::string>() == "4.2"_s);
REQUIRE(variant.as<JsonString>() == "4.2"); REQUIRE(variant.as<JsonString>() == "4.2");
REQUIRE(variant.as<JsonString>().isStatic() == false);
} }
SECTION("set(std::string(\"123.45\"))") { SECTION("set(std::string(\"123.45\"))") {
@@ -220,7 +218,6 @@ TEST_CASE("JsonVariant::as()") {
REQUIRE(variant.as<const char*>() == "123.45"_s); REQUIRE(variant.as<const char*>() == "123.45"_s);
REQUIRE(variant.as<std::string>() == "123.45"_s); REQUIRE(variant.as<std::string>() == "123.45"_s);
REQUIRE(variant.as<JsonString>() == "123.45"); REQUIRE(variant.as<JsonString>() == "123.45");
REQUIRE(variant.as<JsonString>().isStatic() == false);
} }
SECTION("set(\"true\")") { SECTION("set(\"true\")") {

View File

@@ -38,13 +38,15 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
REQUIRE(var1.as<std::string>() == "{\"value\":[42]}"); REQUIRE(var1.as<std::string>() == "{\"value\":[42]}");
} }
SECTION("stores const char* by reference") { SECTION("stores string literals by copy") {
var1.set("hello!!"); var1.set("hello!!");
spyingAllocator.clearLog(); spyingAllocator.clearLog();
var2.set(var1); var2.set(var1);
REQUIRE(spyingAllocator.log() == AllocatorLog{}); REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofString("hello!!")),
});
} }
SECTION("stores char* by copy") { SECTION("stores char* by copy") {

View File

@@ -9,6 +9,7 @@
#include "Literals.hpp" #include "Literals.hpp"
using ArduinoJson::detail::sizeofObject; using ArduinoJson::detail::sizeofObject;
using ArduinoJson::detail::sizeofString;
enum ErrorCode { ERROR_01 = 1, ERROR_10 = 10 }; enum ErrorCode { ERROR_01 = 1, ERROR_10 = 10 };
@@ -21,9 +22,10 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
bool result = variant.set("hello\0world"); bool result = variant.set("hello\0world");
REQUIRE(result == true); REQUIRE(result == true);
CHECK(variant == REQUIRE(variant == "hello\0world"_s); // stores by copy
"hello"_s); // linked string cannot contain '\0' at the moment REQUIRE(spy.log() == AllocatorLog{
CHECK(spy.log() == AllocatorLog{}); Allocate(sizeofString(11)),
});
} }
SECTION("const char*") { SECTION("const char*") {
@@ -140,19 +142,7 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
}); });
} }
SECTION("static JsonString") { SECTION("JsonString") {
char str[16];
strcpy(str, "hello");
bool result = variant.set(JsonString(str, true));
strcpy(str, "world");
REQUIRE(result == true);
REQUIRE(variant == "world"); // stores by pointer
REQUIRE(spy.log() == AllocatorLog{});
}
SECTION("non-static JsonString") {
char str[16]; char str[16];
strcpy(str, "hello"); strcpy(str, "hello");

View File

@@ -13,7 +13,6 @@ TEST_CASE("JsonString") {
CHECK(s.isNull() == true); CHECK(s.isNull() == true);
CHECK(s.c_str() == 0); CHECK(s.c_str() == 0);
CHECK(s.isStatic() == true);
CHECK(s == JsonString()); CHECK(s == JsonString());
CHECK(s != ""); CHECK(s != "");
} }
@@ -96,7 +95,6 @@ TEST_CASE("JsonString") {
JsonString s("hello world", 5); JsonString s("hello world", 5);
CHECK(s.size() == 5); CHECK(s.size() == 5);
CHECK(s.isStatic() == false);
CHECK(s == "hello"); CHECK(s == "hello");
CHECK(s != "hello world"); CHECK(s != "hello world");
} }

View File

@@ -22,7 +22,6 @@ TEST_CASE("adaptString()") {
CHECK(s.isNull() == false); CHECK(s.isNull() == false);
CHECK(s.size() == 11); CHECK(s.size() == 11);
CHECK(s.isStatic() == true);
} }
SECTION("null const char*") { SECTION("null const char*") {
@@ -38,7 +37,6 @@ TEST_CASE("adaptString()") {
CHECK(s.isNull() == false); CHECK(s.isNull() == false);
CHECK(s.size() == 5); CHECK(s.size() == 5);
CHECK(s.isStatic() == false);
CHECK(s.data() == p); CHECK(s.data() == p);
} }
@@ -46,7 +44,6 @@ TEST_CASE("adaptString()") {
auto s = adaptString(static_cast<const char*>(0), 10); auto s = adaptString(static_cast<const char*>(0), 10);
CHECK(s.isNull() == true); CHECK(s.isNull() == true);
CHECK(s.isStatic() == false);
} }
SECTION("non-null const char* + size") { SECTION("non-null const char* + size") {
@@ -54,7 +51,6 @@ TEST_CASE("adaptString()") {
CHECK(s.isNull() == false); CHECK(s.isNull() == false);
CHECK(s.size() == 5); CHECK(s.size() == 5);
CHECK(s.isStatic() == false);
} }
SECTION("null Flash string") { SECTION("null Flash string") {
@@ -62,7 +58,6 @@ TEST_CASE("adaptString()") {
CHECK(s.isNull() == true); CHECK(s.isNull() == true);
CHECK(s.size() == 0); CHECK(s.size() == 0);
CHECK(s.isStatic() == false);
} }
SECTION("non-null Flash string") { SECTION("non-null Flash string") {
@@ -70,7 +65,6 @@ TEST_CASE("adaptString()") {
CHECK(s.isNull() == false); CHECK(s.isNull() == false);
CHECK(s.size() == 5); CHECK(s.size() == 5);
CHECK(s.isStatic() == false);
} }
SECTION("std::string") { SECTION("std::string") {
@@ -79,7 +73,6 @@ TEST_CASE("adaptString()") {
CHECK(s.isNull() == false); CHECK(s.isNull() == false);
CHECK(s.size() == 5); CHECK(s.size() == 5);
CHECK(s.isStatic() == false);
} }
SECTION("Arduino String") { SECTION("Arduino String") {
@@ -88,7 +81,6 @@ TEST_CASE("adaptString()") {
CHECK(s.isNull() == false); CHECK(s.isNull() == false);
CHECK(s.size() == 5); CHECK(s.size() == 5);
CHECK(s.isStatic() == false);
} }
SECTION("custom_string") { SECTION("custom_string") {
@@ -97,25 +89,14 @@ TEST_CASE("adaptString()") {
CHECK(s.isNull() == false); CHECK(s.isNull() == false);
CHECK(s.size() == 5); CHECK(s.size() == 5);
CHECK(s.isStatic() == false);
} }
SECTION("JsonString linked") { SECTION("JsonString") {
JsonString orig("hello", true); JsonString orig("hello");
auto s = adaptString(orig); auto s = adaptString(orig);
CHECK(s.isNull() == false); CHECK(s.isNull() == false);
CHECK(s.size() == 5); CHECK(s.size() == 5);
CHECK(s.isStatic() == true);
}
SECTION("JsonString copied") {
JsonString orig("hello", false);
auto s = adaptString(orig);
CHECK(s.isNull() == false);
CHECK(s.size() == 5);
CHECK(s.isStatic() == false);
} }
} }

View File

@@ -89,9 +89,8 @@ TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 4") {
REQUIRE(err != DeserializationError::Ok); REQUIRE(err != DeserializationError::Ok);
} }
}
SECTION("bin 32 deserialization") { SECTION("bin 32") {
auto str = std::string(65536, '?'); auto str = std::string(65536, '?');
auto input = "\xc6\x00\x01\x00\x00"_s + str; auto input = "\xc6\x00\x01\x00\x00"_s + str;
@@ -106,17 +105,6 @@ TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 4") {
binary.size()) == str); binary.size()) == str);
} }
SECTION("bin 32 serialization") {
auto str = std::string(65536, '?');
doc.set(MsgPackBinary(str.data(), str.size()));
std::string output;
auto result = serializeMsgPack(doc, output);
REQUIRE(result == 5 + str.size());
REQUIRE(output == "\xc6\x00\x01\x00\x00"_s + str);
}
SECTION("ext 32 deserialization") { SECTION("ext 32 deserialization") {
auto str = std::string(65536, '?'); auto str = std::string(65536, '?');
auto input = "\xc9\x00\x01\x00\x00\x2a"_s + str; auto input = "\xc9\x00\x01\x00\x00\x2a"_s + str;
@@ -132,6 +120,19 @@ TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 4") {
REQUIRE(std::string(reinterpret_cast<const char*>(value.data()), REQUIRE(std::string(reinterpret_cast<const char*>(value.data()),
value.size()) == str); value.size()) == str);
} }
}
SECTION("serializeMsgPack()") {
SECTION("bin 32 serialization") {
auto str = std::string(65536, '?');
doc.set(MsgPackBinary(str.data(), str.size()));
std::string output;
auto result = serializeMsgPack(doc, output);
REQUIRE(result == 5 + str.size());
REQUIRE(output == "\xc6\x00\x01\x00\x00"_s + str);
}
SECTION("ext 32 serialization") { SECTION("ext 32 serialization") {
auto str = std::string(65536, '?'); auto str = std::string(65536, '?');
@@ -143,4 +144,16 @@ TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 4") {
REQUIRE(result == 6 + str.size()); REQUIRE(result == 6 + str.size());
REQUIRE(output == "\xc9\x00\x01\x00\x00\x2a"_s + str); REQUIRE(output == "\xc9\x00\x01\x00\x00\x2a"_s + str);
} }
SECTION("str 32 serialization") {
auto str = std::string(65536, '?');
doc.set(str);
std::string output;
auto result = serializeMsgPack(doc, output);
REQUIRE(result == 5 + str.size());
REQUIRE(output == "\xDB\x00\x01\x00\x00"_s + str);
}
}
} }

View File

@@ -104,6 +104,8 @@ TEST_CASE("deserializeMsgPack(MemberProxy)") {
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\",\"value\":[42]}"); REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\",\"value\":[42]}");
REQUIRE(spy.log() == AllocatorLog{}); REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("value")),
});
} }
} }

View File

@@ -137,11 +137,12 @@ TEST_CASE("serialize MsgPack value") {
checkVariant(longest.c_str(), "\xDA\xFF\xFF"_s + longest); checkVariant(longest.c_str(), "\xDA\xFF\xFF"_s + longest);
} }
#if ARDUINOJSON_STRING_LENGTH_SIZE > 2
SECTION("str 32") { SECTION("str 32") {
std::string shortest(65536, '?'); std::string shortest(65536, '?');
checkVariant(JsonString(shortest.c_str(), true), // force store by pointer checkVariant(shortest.c_str(), "\xDB\x00\x01\x00\x00"_s + shortest);
"\xDB\x00\x01\x00\x00"_s + shortest);
} }
#endif
SECTION("serialized(const char*)") { SECTION("serialized(const char*)") {
checkVariant(serialized("\xDA\xFF\xFF"), "\xDA\xFF\xFF"); checkVariant(serialized("\xDA\xFF\xFF"), "\xDA\xFF\xFF");

View File

@@ -30,7 +30,7 @@ TEST_CASE("StringBuffer") {
memcpy(ptr, "a\0b", 3); memcpy(ptr, "a\0b", 3);
sb.save(&variant); sb.save(&variant);
REQUIRE(variant.type() == VariantType::OwnedString); REQUIRE(variant.type() == VariantType::LongString);
auto str = variant.asString(); auto str = variant.asString();
REQUIRE(str.size() == 3); REQUIRE(str.size() == 3);
@@ -44,7 +44,7 @@ TEST_CASE("StringBuffer") {
strcpy(ptr, "alfa"); strcpy(ptr, "alfa");
sb.save(&variant); sb.save(&variant);
REQUIRE(variant.type() == VariantType::OwnedString); REQUIRE(variant.type() == VariantType::LongString);
REQUIRE(variant.asString() == "alfa"); REQUIRE(variant.asString() == "alfa");
} }
} }

View File

@@ -43,7 +43,7 @@ class StringBuffer {
if (isTinyString(s, size_)) if (isTinyString(s, size_))
data->setTinyString(adaptString(s, size_)); data->setTinyString(adaptString(s, size_));
else else
data->setOwnedString(commitStringNode()); data->setLongString(commitStringNode());
} }
void saveRaw(VariantData* data) { void saveRaw(VariantData* data) {

View File

@@ -45,7 +45,7 @@ class StringBuilder {
} else { } else {
node->references++; node->references++;
} }
variant->setOwnedString(node); variant->setLongString(node);
} }
void append(const char* s) { void append(const char* s) {

View File

@@ -63,10 +63,6 @@ class FlashString {
::memcpy_P(p, s.str_, n); ::memcpy_P(p, s.str_, n);
} }
bool isStatic() const {
return false;
}
private: private:
const char* str_; const char* str_;
size_t size_; size_t size_;

View File

@@ -20,14 +20,8 @@ struct IsChar
class RamString { class RamString {
public: public:
static const size_t typeSortKey = 2; static const size_t typeSortKey = 2;
#if ARDUINOJSON_SIZEOF_POINTER <= 2
static constexpr size_t sizeMask = size_t(-1) >> 1;
#else
static constexpr size_t sizeMask = size_t(-1);
#endif
RamString(const char* str, size_t sz, bool isStatic = false) RamString(const char* str, size_t sz) : str_(str), size_(sz) {
: str_(str), size_(sz & sizeMask), static_(isStatic) {
ARDUINOJSON_ASSERT(size_ == sz); ARDUINOJSON_ASSERT(size_ == sz);
} }
@@ -49,21 +43,9 @@ class RamString {
return str_; return str_;
} }
bool isStatic() const {
return static_;
}
protected: protected:
const char* str_; const char* str_;
#if ARDUINOJSON_SIZEOF_POINTER <= 2
// Use a bitfield only on 8-bit microcontrollers
size_t size_ : sizeof(size_t) * 8 - 1;
bool static_ : 1;
#else
size_t size_; size_t size_;
bool static_;
#endif
}; };
template <typename TChar> template <typename TChar>
@@ -91,7 +73,7 @@ struct StringAdapter<const char (&)[N]> {
using AdaptedString = RamString; using AdaptedString = RamString;
static AdaptedString adapt(const char (&p)[N]) { static AdaptedString adapt(const char (&p)[N]) {
return RamString(p, N - 1, true); return RamString(p, N - 1);
} }
}; };

View File

@@ -19,17 +19,25 @@ class JsonString {
friend struct detail::StringAdapter<JsonString>; friend struct detail::StringAdapter<JsonString>;
public: public:
JsonString() : str_(nullptr, 0, true) {} JsonString() : str_(nullptr, 0) {}
JsonString(const char* data, bool isStatic = false) JsonString(const char* data) : str_(data, data ? ::strlen(data) : 0) {}
: str_(data, data ? ::strlen(data) : 0, isStatic) {}
ARDUINOJSON_DEPRECATED(
"ArduinoJson doesn't differentiate between static and dynamic strings "
"anymore. Remove the second argument to fix this warning.")
JsonString(const char* data, bool) : JsonString(data) {}
template <typename TSize, template <typename TSize,
detail::enable_if_t<detail::is_integral<TSize>::value && detail::enable_if_t<detail::is_integral<TSize>::value &&
!detail::is_same<TSize, bool>::value, !detail::is_same<TSize, bool>::value,
int> = 0> int> = 0>
JsonString(const char* data, TSize sz, bool isStatic = false) JsonString(const char* data, TSize sz) : str_(data, size_t(sz)) {}
: str_(data, size_t(sz), isStatic) {}
ARDUINOJSON_DEPRECATED(
"ArduinoJson doesn't differentiate between static and dynamic strings "
"anymore. Remove the third argument to fix this warning.")
JsonString(const char* data, size_t sz, bool) : JsonString(data, sz) {}
// Returns a pointer to the characters. // Returns a pointer to the characters.
const char* c_str() const { const char* c_str() const {
@@ -41,10 +49,10 @@ class JsonString {
return str_.isNull(); return str_.isNull();
} }
// Returns true if the string is stored by address. // Deprecated: always returns false.
// Returns false if the string is stored by copy. ARDUINOJSON_DEPRECATED("The isStatic() was removed in v7.5")
bool isStatic() const { bool isStatic() const {
return str_.isStatic(); return false;
} }
// Returns length of the string. // Returns length of the string.

View File

@@ -26,8 +26,7 @@ enum class VariantType : uint8_t {
Null = 0, // 0000 0000 Null = 0, // 0000 0000
TinyString = 0x02, // 0000 0010 TinyString = 0x02, // 0000 0010
RawString = 0x03, // 0000 0011 RawString = 0x03, // 0000 0011
LinkedString = 0x04, // 0000 0100 LongString = 0x05, // 0000 0101
OwnedString = 0x05, // 0000 0101
Boolean = 0x06, // 0000 0110 Boolean = 0x06, // 0000 0110
Uint32 = 0x0A, // 0000 1010 Uint32 = 0x0A, // 0000 1010
Int32 = 0x0C, // 0000 1100 Int32 = 0x0C, // 0000 1100
@@ -62,8 +61,7 @@ union VariantContent {
ArrayData asArray; ArrayData asArray;
ObjectData asObject; ObjectData asObject;
CollectionData asCollection; CollectionData asCollection;
const char* asLinkedString; struct StringNode* asStringNode;
struct StringNode* asOwnedString;
char asTinyString[tinyStringMaxLength + 1]; char asTinyString[tinyStringMaxLength + 1];
}; };

View File

@@ -76,16 +76,13 @@ class VariantData {
case VariantType::TinyString: case VariantType::TinyString:
return visit.visit(JsonString(content_.asTinyString)); return visit.visit(JsonString(content_.asTinyString));
case VariantType::LinkedString: case VariantType::LongString:
return visit.visit(JsonString(content_.asLinkedString, true)); return visit.visit(JsonString(content_.asStringNode->data,
content_.asStringNode->length));
case VariantType::OwnedString:
return visit.visit(JsonString(content_.asOwnedString->data,
content_.asOwnedString->length));
case VariantType::RawString: case VariantType::RawString:
return visit.visit(RawString(content_.asOwnedString->data, return visit.visit(RawString(content_.asStringNode->data,
content_.asOwnedString->length)); content_.asStringNode->length));
case VariantType::Int32: case VariantType::Int32:
return visit.visit(static_cast<JsonInteger>(content_.asInt32)); return visit.visit(static_cast<JsonInteger>(content_.asInt32));
@@ -215,11 +212,8 @@ class VariantData {
case VariantType::TinyString: case VariantType::TinyString:
str = content_.asTinyString; str = content_.asTinyString;
break; break;
case VariantType::LinkedString: case VariantType::LongString:
str = content_.asLinkedString; str = content_.asStringNode->data;
break;
case VariantType::OwnedString:
str = content_.asOwnedString->data;
break; break;
case VariantType::Float: case VariantType::Float:
return static_cast<T>(content_.asFloat); return static_cast<T>(content_.asFloat);
@@ -260,11 +254,8 @@ class VariantData {
case VariantType::TinyString: case VariantType::TinyString:
str = content_.asTinyString; str = content_.asTinyString;
break; break;
case VariantType::LinkedString: case VariantType::LongString:
str = content_.asLinkedString; str = content_.asStringNode->data;
break;
case VariantType::OwnedString:
str = content_.asOwnedString->data;
break; break;
case VariantType::Float: case VariantType::Float:
return convertNumber<T>(content_.asFloat); return convertNumber<T>(content_.asFloat);
@@ -291,8 +282,8 @@ class VariantData {
JsonString asRawString() const { JsonString asRawString() const {
switch (type_) { switch (type_) {
case VariantType::RawString: case VariantType::RawString:
return JsonString(content_.asOwnedString->data, return JsonString(content_.asStringNode->data,
content_.asOwnedString->length); content_.asStringNode->length);
default: default:
return JsonString(); return JsonString();
} }
@@ -302,11 +293,9 @@ class VariantData {
switch (type_) { switch (type_) {
case VariantType::TinyString: case VariantType::TinyString:
return JsonString(content_.asTinyString); return JsonString(content_.asTinyString);
case VariantType::LinkedString: case VariantType::LongString:
return JsonString(content_.asLinkedString, true); return JsonString(content_.asStringNode->data,
case VariantType::OwnedString: content_.asStringNode->length);
return JsonString(content_.asOwnedString->data,
content_.asOwnedString->length);
default: default:
return JsonString(); return JsonString();
} }
@@ -415,9 +404,7 @@ class VariantData {
} }
bool isString() const { bool isString() const {
return type_ == VariantType::LinkedString || return type_ == VariantType::LongString || type_ == VariantType::TinyString;
type_ == VariantType::OwnedString ||
type_ == VariantType::TinyString;
} }
size_t nesting(const ResourceManager* resources) const { size_t nesting(const ResourceManager* resources) const {
@@ -492,7 +479,7 @@ class VariantData {
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
ARDUINOJSON_ASSERT(s); ARDUINOJSON_ASSERT(s);
type_ = VariantType::RawString; type_ = VariantType::RawString;
content_.asOwnedString = s; content_.asStringNode = s;
} }
template <typename T> template <typename T>
@@ -519,13 +506,6 @@ class VariantData {
var->setString(value, resources); var->setString(value, resources);
} }
void setLinkedString(const char* s) {
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
ARDUINOJSON_ASSERT(s);
type_ = VariantType::LinkedString;
content_.asLinkedString = s;
}
template <typename TAdaptedString> template <typename TAdaptedString>
void setTinyString(const TAdaptedString& s) { void setTinyString(const TAdaptedString& s) {
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
@@ -543,11 +523,11 @@ class VariantData {
content_.asTinyString[n] = 0; content_.asTinyString[n] = 0;
} }
void setOwnedString(StringNode* s) { void setLongString(StringNode* s) {
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
ARDUINOJSON_ASSERT(s); ARDUINOJSON_ASSERT(s);
type_ = VariantType::OwnedString; type_ = VariantType::LongString;
content_.asOwnedString = s; content_.asStringNode = s;
} }
size_t size(const ResourceManager* resources) const { size_t size(const ResourceManager* resources) const {

View File

@@ -26,11 +26,6 @@ inline bool VariantData::setString(TAdaptedString value,
if (value.isNull()) if (value.isNull())
return false; return false;
if (value.isStatic()) {
setLinkedString(value.data());
return true;
}
if (isTinyString(value, value.size())) { if (isTinyString(value, value.size())) {
setTinyString(value); setTinyString(value);
return true; return true;
@@ -38,7 +33,7 @@ inline bool VariantData::setString(TAdaptedString value,
auto dup = resources->saveString(value); auto dup = resources->saveString(value);
if (dup) { if (dup) {
setOwnedString(dup); setLongString(dup);
return true; return true;
} }
@@ -47,7 +42,7 @@ inline bool VariantData::setString(TAdaptedString value,
inline void VariantData::clear(ResourceManager* resources) { inline void VariantData::clear(ResourceManager* resources) {
if (type_ & VariantTypeBits::OwnedStringBit) if (type_ & VariantTypeBits::OwnedStringBit)
resources->dereferenceString(content_.asOwnedString->data); resources->dereferenceString(content_.asStringNode->data);
#if ARDUINOJSON_USE_EXTENSIONS #if ARDUINOJSON_USE_EXTENSIONS
if (type_ & VariantTypeBits::ExtensionBit) if (type_ & VariantTypeBits::ExtensionBit)