forked from bblanchon/ArduinoJson
Added StaticJsonDocument::garbageCollect()
This commit is contained in:
@ -13,6 +13,7 @@ HEAD
|
||||
* Fixed incorrect string comparison on some platforms (issue #1198)
|
||||
* Added move-constructor and move-assignment to `BasicJsonDocument`
|
||||
* Added `BasicJsonDocument::garbageCollect()` (issue #1195)
|
||||
* Added `StaticJsonDocument::garbageCollect()`
|
||||
* Changed copy-constructor of `BasicJsonDocument` to preserve the capacity of the source.
|
||||
|
||||
> ### BREAKING CHANGES
|
||||
|
@ -209,4 +209,16 @@ TEST_CASE("StaticJsonDocument") {
|
||||
REQUIRE_JSON(doc2, "42");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("garbageCollect()") {
|
||||
StaticJsonDocument<256> doc;
|
||||
doc[std::string("example")] = std::string("example");
|
||||
doc.remove("example");
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(1) + 16);
|
||||
|
||||
doc.garbageCollect();
|
||||
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(0));
|
||||
REQUIRE_JSON(doc, "{}");
|
||||
}
|
||||
}
|
||||
|
@ -120,7 +120,7 @@ class BasicJsonDocument : AllocatorOwner<TAllocator>, public JsonDocument {
|
||||
|
||||
bool garbageCollect() {
|
||||
// make a temporary clone and move assign
|
||||
BasicJsonDocument<TAllocator> tmp(*this);
|
||||
BasicJsonDocument tmp(*this);
|
||||
if (!tmp.capacity())
|
||||
return false;
|
||||
tmp.set(*this);
|
||||
|
@ -44,6 +44,11 @@ class StaticJsonDocument : public JsonDocument {
|
||||
return *this;
|
||||
}
|
||||
|
||||
void garbageCollect() {
|
||||
StaticJsonDocument tmp(*this);
|
||||
set(tmp);
|
||||
}
|
||||
|
||||
private:
|
||||
char _buffer[_capacity];
|
||||
};
|
||||
|
Reference in New Issue
Block a user