mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-14 11:06:35 +02:00
serializeXxx()
sets std::string
and String
instead of appending
This commit is contained in:
@ -26,3 +26,4 @@ HEAD
|
||||
* Remove `JsonDocument::garbageCollect()`
|
||||
* Add `deserializeJson(JsonVariant, ...)` and `deserializeMsgPack(JsonVariant, ...)` (#1226)
|
||||
* Call `shrinkToFit()` in `deserializeJson()` and `deserializeMsgPack()`
|
||||
* `serializeJson()` and `serializeMsgPack()` replace the content of `std::string` and `String` instead of appending to it
|
||||
|
@ -9,8 +9,11 @@
|
||||
// Reproduces Arduino's String class
|
||||
class String {
|
||||
public:
|
||||
String() : maxCapacity_(1024) {}
|
||||
explicit String(const char* s) : str_(s), maxCapacity_(1024) {}
|
||||
String() = default;
|
||||
String(const char* s) {
|
||||
if (s)
|
||||
str_.assign(s);
|
||||
}
|
||||
|
||||
void limitCapacityTo(size_t maxCapacity) {
|
||||
maxCapacity_ = maxCapacity;
|
||||
@ -33,7 +36,10 @@ class String {
|
||||
}
|
||||
|
||||
String& operator=(const char* s) {
|
||||
if (s)
|
||||
str_.assign(s);
|
||||
else
|
||||
str_.clear();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -59,7 +65,7 @@ class String {
|
||||
|
||||
private:
|
||||
std::string str_;
|
||||
size_t maxCapacity_;
|
||||
size_t maxCapacity_ = 1024;
|
||||
};
|
||||
|
||||
class StringSumHelper : public ::String {};
|
||||
|
@ -12,14 +12,14 @@ TEST_CASE("serialize JsonArray to std::string") {
|
||||
array.add(2);
|
||||
|
||||
SECTION("serializeJson()") {
|
||||
std::string json;
|
||||
std::string json = "erase me";
|
||||
serializeJson(array, json);
|
||||
|
||||
REQUIRE("[4,2]" == json);
|
||||
}
|
||||
|
||||
SECTION("serializeJsonPretty") {
|
||||
std::string json;
|
||||
std::string json = "erase me";
|
||||
serializeJsonPretty(array, json);
|
||||
|
||||
REQUIRE("[\r\n 4,\r\n 2\r\n]" == json);
|
||||
@ -32,14 +32,14 @@ TEST_CASE("serialize JsonObject to std::string") {
|
||||
obj["key"] = "value";
|
||||
|
||||
SECTION("object") {
|
||||
std::string json;
|
||||
std::string json = "erase me";
|
||||
serializeJson(doc, json);
|
||||
|
||||
REQUIRE("{\"key\":\"value\"}" == json);
|
||||
}
|
||||
|
||||
SECTION("serializeJsonPretty") {
|
||||
std::string json;
|
||||
std::string json = "erase me";
|
||||
serializeJsonPretty(doc, json);
|
||||
|
||||
REQUIRE("{\r\n \"key\": \"value\"\r\n}" == json);
|
||||
@ -50,7 +50,7 @@ TEST_CASE("serialize an std::string containing a NUL") {
|
||||
JsonDocument doc;
|
||||
doc.set(std::string("hello\0world", 11));
|
||||
|
||||
std::string json;
|
||||
std::string json = "erase me";
|
||||
serializeJson(doc, json);
|
||||
CHECK("\"hello\\u0000world\"" == json);
|
||||
}
|
||||
|
@ -141,7 +141,7 @@ TEST_CASE("Writer<custom_string>") {
|
||||
TEST_CASE("serializeJson(doc, String)") {
|
||||
JsonDocument doc;
|
||||
doc["hello"] = "world";
|
||||
::String output;
|
||||
::String output = "erase me";
|
||||
|
||||
SECTION("sufficient capacity") {
|
||||
serializeJson(doc, output);
|
||||
|
@ -13,8 +13,10 @@ class Writer<::String, void> {
|
||||
static const size_t bufferCapacity = ARDUINOJSON_STRING_BUFFER_SIZE;
|
||||
|
||||
public:
|
||||
explicit Writer(::String& str) : destination_(&str) {
|
||||
size_ = 0;
|
||||
explicit Writer(::String& str) : destination_(&str), size_(0) {
|
||||
// clear the string but don't use "" to avoid useless allocation
|
||||
// https://cpp4arduino.com/2018/11/21/eight-tips-to-use-the-string-class-efficiently.html
|
||||
str = static_cast<const char*>(0);
|
||||
}
|
||||
|
||||
~Writer() {
|
||||
|
@ -24,7 +24,9 @@ template <typename TDestination>
|
||||
class Writer<TDestination,
|
||||
typename enable_if<is_std_string<TDestination>::value>::type> {
|
||||
public:
|
||||
Writer(TDestination& str) : str_(&str) {}
|
||||
Writer(TDestination& str) : str_(&str) {
|
||||
str.clear();
|
||||
}
|
||||
|
||||
size_t write(uint8_t c) {
|
||||
str_->push_back(static_cast<char>(c));
|
||||
|
Reference in New Issue
Block a user