Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Support NUL in serializeJson() 5570/6730
  • Loading branch information
bblanchon committed Oct 29, 2021
1 parent 21873cf commit c7d0063
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 14 deletions.
2 changes: 1 addition & 1 deletion extras/tests/JsonDocument/DynamicJsonDocument.cpp
Expand Up @@ -169,7 +169,7 @@ TEST_CASE("DynamicJsonDocument assignment") {

doc2 = doc1;

REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
REQUIRE_JSON(doc1, "{\"hello\":\"world\"}");
}

SECTION("Assign from JsonObject") {
Expand Down
4 changes: 4 additions & 0 deletions extras/tests/JsonSerializer/JsonVariant.cpp
Expand Up @@ -63,6 +63,10 @@ TEST_CASE("serializeJson(JsonVariant)") {
SECTION("Escape tab") {
check(std::string("hello\tworld"), "\"hello\\tworld\"");
}

SECTION("NUL char") {
check(std::string("hello\0world", 11), "\"hello\\u0000world\"");
}
}

SECTION("SerializedValue<const char*>") {
Expand Down
3 changes: 2 additions & 1 deletion src/ArduinoJson/Json/JsonDeserializer.hpp
Expand Up @@ -346,7 +346,8 @@ class JsonDeserializer {
if (!parseQuotedString())
return false;
const char *value = _stringStorage.save();
variant.setStringPointer(value, typename TStringStorage::storage_policy());
variant.setStringPointer(value, _stringStorage.size() - 1,
typename TStringStorage::storage_policy());
return true;
}

Expand Down
4 changes: 3 additions & 1 deletion src/ArduinoJson/Json/TextFormatter.hpp
Expand Up @@ -53,8 +53,10 @@ class TextFormatter {
if (specialChar) {
writeRaw('\\');
writeRaw(specialChar);
} else {
} else if (c) {
writeRaw(c);
} else {
writeRaw("\\u0000");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp
Expand Up @@ -331,7 +331,7 @@ class MsgPackDeserializer {
bool readString(VariantData *variant, size_t n) {
if (!readString(n))
return false;
variant->setStringPointer(_stringStorage.save(),
variant->setStringPointer(_stringStorage.save(), _stringStorage.size() - 1,
typename TStringStorage::storage_policy());
return true;
}
Expand Down
8 changes: 6 additions & 2 deletions src/ArduinoJson/StringStorage/StringCopier.hpp
Expand Up @@ -43,14 +43,18 @@ class StringCopier {
_ptr[_size++] = c;
}

bool isValid() {
bool isValid() const {
return _ptr != 0;
}

const char* c_str() {
const char* c_str() const {
return _ptr;
}

size_t size() const {
return _size;
}

typedef storage_policies::store_by_copy storage_policy;

private:
Expand Down
4 changes: 4 additions & 0 deletions src/ArduinoJson/StringStorage/StringMover.hpp
Expand Up @@ -33,6 +33,10 @@ class StringMover {
return _startPtr;
}

size_t size() const {
return size_t(_writePtr - _startPtr);
}

typedef storage_policies::store_by_address storage_policy;

private:
Expand Down
4 changes: 2 additions & 2 deletions src/ArduinoJson/Variant/ConverterImpl.hpp
Expand Up @@ -262,13 +262,13 @@ inline void convertToJson(const ::Printable& src, VariantRef dst) {
if (!pool || !data)
return;
MemoryPoolPrint print(pool);
src.printTo(print);
size_t n = src.printTo(print);
if (print.overflowed()) {
pool->markAsOverflowed();
data->setNull();
return;
}
data->setStringPointer(print.c_str(), storage_policies::store_by_copy());
data->setStringPointer(print.c_str(), n, storage_policies::store_by_copy());
}

#endif
Expand Down
17 changes: 11 additions & 6 deletions src/ArduinoJson/Variant/VariantData.hpp
Expand Up @@ -228,18 +228,22 @@ class VariantData {
setType(VALUE_IS_NULL);
}

void setStringPointer(const char *s, storage_policies::store_by_copy) {
// TODO: why not use a JsonString for both functions?
void setStringPointer(const char *s, size_t n,
storage_policies::store_by_copy) {
ARDUINOJSON_ASSERT(s != 0);
setType(VALUE_IS_OWNED_STRING);
_content.asString.data = s;
_content.asString.size = strlen(s); // TODO
_content.asString.size = n;
}

void setStringPointer(const char *s, storage_policies::store_by_address) {
// TODO: why not use a JsonString for both functions?
void setStringPointer(const char *s, size_t n,
storage_policies::store_by_address) {
ARDUINOJSON_ASSERT(s != 0);
setType(VALUE_IS_LINKED_STRING);
_content.asString.data = s;
_content.asString.size = strlen(s); // TODO
_content.asString.size = n;
}

template <typename TAdaptedString>
Expand Down Expand Up @@ -348,7 +352,8 @@ class VariantData {
if (value.isNull())
setNull();
else
setStringPointer(value.data(), storage_policies::store_by_address());
setStringPointer(value.data(), value.size(),
storage_policies::store_by_address());
return true;
}

Expand All @@ -364,7 +369,7 @@ class VariantData {
setNull();
return false;
}
setStringPointer(copy, storage_policies::store_by_copy());
setStringPointer(copy, value.size(), storage_policies::store_by_copy());
return true;
}
};
Expand Down

0 comments on commit c7d0063

Please sign in to comment.