Commit 24563b28 authored by Milo Yip's avatar Milo Yip

Correct Value(kStringType) and more assertions

parent 29fa1558
...@@ -457,11 +457,15 @@ public: ...@@ -457,11 +457,15 @@ public:
*/ */
explicit GenericValue(Type type) RAPIDJSON_NOEXCEPT : data_(), flags_() { explicit GenericValue(Type type) RAPIDJSON_NOEXCEPT : data_(), flags_() {
static const unsigned defaultFlags[7] = { static const unsigned defaultFlags[7] = {
kNullFlag, kFalseFlag, kTrueFlag, kObjectFlag, kArrayFlag, kConstStringFlag, kNullFlag, kFalseFlag, kTrueFlag, kObjectFlag, kArrayFlag, kShortStringFlag,
kNumberAnyFlag kNumberAnyFlag
}; };
RAPIDJSON_ASSERT(type <= kNumberType); RAPIDJSON_ASSERT(type <= kNumberType);
flags_ = defaultFlags[type]; flags_ = defaultFlags[type];
// Use ShortString to store empty string.
if (type == kStringType)
data_.ss.SetLength(0);
} }
//! Explicit copy constructor (with allocator) //! Explicit copy constructor (with allocator)
...@@ -1399,6 +1403,7 @@ public: ...@@ -1399,6 +1403,7 @@ public:
if (!handler.StartObject()) if (!handler.StartObject())
return false; return false;
for (ConstMemberIterator m = MemberBegin(); m != MemberEnd(); ++m) { for (ConstMemberIterator m = MemberBegin(); m != MemberEnd(); ++m) {
RAPIDJSON_ASSERT(m->name.IsString()); // User may change the type of name by MemberIterator.
if (!handler.Key(m->name.GetString(), m->name.GetStringLength(), (m->name.flags_ & kCopyFlag) != 0)) if (!handler.Key(m->name.GetString(), m->name.GetStringLength(), (m->name.flags_ & kCopyFlag) != 0))
return false; return false;
if (!m->value.Accept(handler)) if (!m->value.Accept(handler))
......
...@@ -212,6 +212,18 @@ TEST(Document, AcceptWriter) { ...@@ -212,6 +212,18 @@ TEST(Document, AcceptWriter) {
EXPECT_EQ("{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3,4]}", os.str()); EXPECT_EQ("{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3,4]}", os.str());
} }
// Issue 226: Value of string type should not point to NULL
TEST(Document, AssertAcceptInvalidNameType) {
Document doc;
doc.SetObject();
doc.AddMember("a", 0, doc.GetAllocator());
doc.FindMember("a")->name.SetNull(); // Change name to non-string type.
OutputStringStream os;
Writer<OutputStringStream> writer(os);
ASSERT_THROW(doc.Accept(writer), AssertException);
}
// Issue 44: SetStringRaw doesn't work with wchar_t // Issue 44: SetStringRaw doesn't work with wchar_t
TEST(Document, UTF16_Document) { TEST(Document, UTF16_Document) {
GenericDocument< UTF16<> > json; GenericDocument< UTF16<> > json;
......
...@@ -600,7 +600,7 @@ TEST(Value, String) { ...@@ -600,7 +600,7 @@ TEST(Value, String) {
// Constructor with type // Constructor with type
Value y(kStringType); Value y(kStringType);
EXPECT_TRUE(y.IsString()); EXPECT_TRUE(y.IsString());
EXPECT_EQ(0, y.GetString()); EXPECT_STREQ("", y.GetString()); // Empty string should be "" instead of 0 (issue 226)
EXPECT_EQ(0u, y.GetStringLength()); EXPECT_EQ(0u, y.GetStringLength());
// SetConsttring() // SetConsttring()
...@@ -677,6 +677,12 @@ TEST(Value, String) { ...@@ -677,6 +677,12 @@ TEST(Value, String) {
#endif // RAPIDJSON_HAS_STDSTRING #endif // RAPIDJSON_HAS_STDSTRING
} }
// Issue 226: Value of string type should not point to NULL
TEST(Value, SetStringNullException) {
Value v;
EXPECT_THROW(v.SetString(0, 0), AssertException);
}
TEST(Value, Array) { TEST(Value, Array) {
Value x(kArrayType); Value x(kArrayType);
const Value& y = x; const Value& y = x;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment