diff --git a/include/rapidjson/memorybuffer.h b/include/rapidjson/memorybuffer.h index 4e82036..b94d2df 100644 --- a/include/rapidjson/memorybuffer.h +++ b/include/rapidjson/memorybuffer.h @@ -49,6 +49,7 @@ struct GenericMemoryBuffer { void Flush() {} void Clear() { stack_.Clear(); } + void ShrinkToFit() { stack_.ShrinkToFit(); } Ch* Push(size_t count) { return stack_.template Push(count); } void Pop(size_t count) { stack_.template Pop(count); } diff --git a/include/rapidjson/stringbuffer.h b/include/rapidjson/stringbuffer.h index b8d7968..bccc247 100644 --- a/include/rapidjson/stringbuffer.h +++ b/include/rapidjson/stringbuffer.h @@ -42,6 +42,12 @@ struct GenericStringBuffer { void Flush() {} void Clear() { stack_.Clear(); } + void ShrinkToFit() { + // Push and pop a null terminator. This is safe. + *stack_.template Push() = '\0'; + stack_.ShrinkToFit(); + stack_.template Pop(1); + } Ch* Push(size_t count) { return stack_.template Push(count); } void Pop(size_t count) { stack_.template Pop(count); } diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp index e313c8b..c3e9d4e 100644 --- a/test/unittest/writertest.cpp +++ b/test/unittest/writertest.cpp @@ -31,6 +31,7 @@ TEST(Writer, Compact) { StringStream s("{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] } "); StringBuffer buffer; Writer writer(buffer); + buffer.ShrinkToFit(); Reader reader; reader.Parse<0>(s, writer); EXPECT_STREQ("{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3]}", buffer.GetString());