Don't call shrinkToFit() for deserializeXxx(JsonVariant)

This commit is contained in:
Benoit Blanchon
2023-08-01 18:39:35 +02:00
parent af6954c224
commit 57454cf97b
3 changed files with 31 additions and 45 deletions

View File

@ -43,11 +43,9 @@ TEST_CASE("deserializeJson(JsonVariant)") {
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "[[42]]");
REQUIRE(spy.log() ==
AllocatorLog{
Deallocate(sizeofString("hello")),
Reallocate(sizeofPool(), sizeofArray(1) + sizeofArray(1)),
});
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("hello")),
});
}
SECTION("variant is unbound") {
@ -70,11 +68,9 @@ TEST_CASE("deserializeJson(ElementProxy)") {
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "[[42]]");
REQUIRE(spy.log() ==
AllocatorLog{
Deallocate(sizeofString("hello")),
Reallocate(sizeofPool(), sizeofArray(1) + sizeofArray(1)),
});
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("hello")),
});
}
SECTION("element must be created") {
@ -82,10 +78,7 @@ TEST_CASE("deserializeJson(ElementProxy)") {
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "[\"hello\",[42]]");
REQUIRE(spy.log() ==
AllocatorLog{
Reallocate(sizeofPool(), sizeofArray(2) + sizeofArray(1)),
});
REQUIRE(spy.log() == AllocatorLog{});
}
}
@ -100,11 +93,9 @@ TEST_CASE("deserializeJson(MemberProxy)") {
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "{\"hello\":[42]}");
REQUIRE(spy.log() ==
AllocatorLog{
Deallocate(sizeofString("world")),
Reallocate(sizeofPool(), sizeofObject(1) + sizeofArray(1)),
});
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("world")),
});
}
SECTION("member must be created exists") {
@ -112,9 +103,6 @@ TEST_CASE("deserializeJson(MemberProxy)") {
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\",\"value\":[42]}");
REQUIRE(spy.log() ==
AllocatorLog{
Reallocate(sizeofPool(), sizeofObject(2) + sizeofArray(1)),
});
REQUIRE(spy.log() == AllocatorLog{});
}
}

View File

@ -43,11 +43,9 @@ TEST_CASE("deserializeMsgPack(JsonVariant)") {
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "[[42]]");
REQUIRE(spy.log() ==
AllocatorLog{
Deallocate(sizeofString("hello")),
Reallocate(sizeofPool(), sizeofArray(1) + sizeofArray(1)),
});
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("hello")),
});
}
SECTION("variant is unbound") {
@ -70,11 +68,9 @@ TEST_CASE("deserializeMsgPack(ElementProxy)") {
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "[[42]]");
REQUIRE(spy.log() ==
AllocatorLog{
Deallocate(sizeofString("hello")),
Reallocate(sizeofPool(), sizeofArray(1) + sizeofArray(1)),
});
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("hello")),
});
}
SECTION("element must be created exists") {
@ -82,10 +78,7 @@ TEST_CASE("deserializeMsgPack(ElementProxy)") {
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "[\"hello\",[42]]");
REQUIRE(spy.log() ==
AllocatorLog{
Reallocate(sizeofPool(), sizeofArray(2) + sizeofArray(1)),
});
REQUIRE(spy.log() == AllocatorLog{});
}
}
@ -102,7 +95,6 @@ TEST_CASE("deserializeMsgPack(MemberProxy)") {
REQUIRE(doc.as<std::string>() == "{\"hello\":[42]}");
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("world")),
Reallocate(sizeofPool(), sizeofObject(2)),
});
}
@ -111,9 +103,6 @@ TEST_CASE("deserializeMsgPack(MemberProxy)") {
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\",\"value\":[42]}");
REQUIRE(spy.log() ==
AllocatorLog{
Reallocate(sizeofPool(), sizeofObject(2) + sizeofArray(1)),
});
REQUIRE(spy.log() == AllocatorLog{});
}
}

View File

@ -34,6 +34,17 @@ struct is_deserialize_destination<
ResourceManager*>::value>::type> : true_type {
};
template <typename TDestination>
inline void shrinkJsonDocument(TDestination&) {
// no-op by default
}
#if ARDUINOJSON_AUTO_SHRINK
inline void shrinkJsonDocument(JsonDocument& doc) {
doc.shrinkToFit();
}
#endif
template <template <typename> class TDeserializer, typename TDestination,
typename TReader, typename TOptions>
DeserializationError doDeserialize(TDestination&& dst, TReader reader,
@ -45,9 +56,7 @@ DeserializationError doDeserialize(TDestination&& dst, TReader reader,
dst.clear();
auto err = TDeserializer<TReader>(resources, reader)
.parse(*data, options.filter, options.nestingLimit);
#if ARDUINOJSON_AUTO_SHRINK
resources->shrinkToFit();
#endif
shrinkJsonDocument(dst);
return err;
}