diff --git a/include/rapidjson/prettywriter.h b/include/rapidjson/prettywriter.h index b9d7510..eb778b3 100644 --- a/include/rapidjson/prettywriter.h +++ b/include/rapidjson/prettywriter.h @@ -174,7 +174,6 @@ protected: level->valueCount++; } else { - RAPIDJSON_ASSERT(type == kObjectType || type == kArrayType); RAPIDJSON_ASSERT(!Base::hasRoot_); // Should only has one and only one root. Base::hasRoot_ = true; } diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index 9fdfde2..7927a0c 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -321,7 +321,6 @@ protected: level->valueCount++; } else { - RAPIDJSON_ASSERT(type == kObjectType || type == kArrayType); RAPIDJSON_ASSERT(!hasRoot_); // Should only has one and only one root. hasRoot_ = true; } diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp index 2d83418..9529ad9 100644 --- a/test/unittest/writertest.cpp +++ b/test/unittest/writertest.cpp @@ -155,12 +155,12 @@ TEST(Writer, OStreamWrapper) { EXPECT_STREQ("{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3]}", actual.c_str()); } -TEST(Writer, AssertRootMustBeArrayOrObject) { +TEST(Writer, AssertRootMayBeAnyValue) { #define T(x)\ {\ StringBuffer buffer;\ Writer writer(buffer);\ - ASSERT_THROW(x, AssertException);\ + EXPECT_TRUE(x);\ } T(writer.Bool(false)); T(writer.Bool(true)); @@ -228,9 +228,23 @@ TEST(Writer, AssertObjectKeyNotString) { TEST(Writer, AssertMultipleRoot) { StringBuffer buffer; Writer writer(buffer); + writer.StartObject(); writer.EndObject(); ASSERT_THROW(writer.StartObject(), AssertException); + + writer.Reset(buffer); + writer.Null(); + ASSERT_THROW(writer.Int(0), AssertException); + + writer.Reset(buffer); + writer.String("foo"); + ASSERT_THROW(writer.StartArray(), AssertException); + + writer.Reset(buffer); + writer.StartArray(); + writer.EndArray(); + ASSERT_THROW(writer.Double(3.14), AssertException); } TEST(Writer, RootObjectIsComplete) { @@ -260,3 +274,24 @@ TEST(Writer, RootArrayIsComplete) { writer.EndArray(); EXPECT_TRUE(writer.IsComplete()); } + +TEST(Writer, RootValueIsComplete) { +#define T(x)\ + {\ + StringBuffer buffer;\ + Writer writer(buffer);\ + EXPECT_FALSE(writer.IsComplete()); \ + x; \ + EXPECT_TRUE(writer.IsComplete()); \ + } + T(writer.Null()); + T(writer.Bool(true)); + T(writer.Bool(false)); + T(writer.Int(0)); + T(writer.Uint(0)); + T(writer.Int64(0)); + T(writer.Uint64(0)); + T(writer.Double(0)); + T(writer.String("")); +#undef T +}