Merge pull request #1821 from slsyy/master

Add implicit conversion from Object and Array to Value (#1404)
This commit is contained in:
Milo Yip 2021-01-06 13:43:21 +08:00 committed by GitHub
commit 585042c02b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View File

@ -2612,6 +2612,7 @@ public:
GenericArray& operator=(const GenericArray& rhs) { value_ = rhs.value_; return *this; }
~GenericArray() {}
operator ValueType&() const { return value_; }
SizeType Size() const { return value_.Size(); }
SizeType Capacity() const { return value_.Capacity(); }
bool Empty() const { return value_.Empty(); }
@ -2667,6 +2668,7 @@ public:
GenericObject& operator=(const GenericObject& rhs) { value_ = rhs.value_; return *this; }
~GenericObject() {}
operator ValueType&() const { return value_; }
SizeType MemberCount() const { return value_.MemberCount(); }
SizeType MemberCapacity() const { return value_.MemberCapacity(); }
bool ObjectEmpty() const { return value_.ObjectEmpty(); }

View File

@ -1529,6 +1529,38 @@ TEST(Pointer, Ambiguity) {
}
}
TEST(Pointer, ResolveOnObject) {
Document d;
EXPECT_FALSE(d.Parse("{\"a\": 123}").HasParseError());
{
Value::ConstObject o = static_cast<const Document&>(d).GetObject();
EXPECT_EQ(123, Pointer("/a").Get(o)->GetInt());
}
{
Value::Object o = d.GetObject();
Pointer("/a").Set(o, 456, d.GetAllocator());
EXPECT_EQ(456, Pointer("/a").Get(o)->GetInt());
}
}
TEST(Pointer, ResolveOnArray) {
Document d;
EXPECT_FALSE(d.Parse("[1, 2, 3]").HasParseError());
{
Value::ConstArray a = static_cast<const Document&>(d).GetArray();
EXPECT_EQ(2, Pointer("/1").Get(a)->GetInt());
}
{
Value::Array a = d.GetArray();
Pointer("/1").Set(a, 123, d.GetAllocator());
EXPECT_EQ(123, Pointer("/1").Get(a)->GetInt());
}
}
TEST(Pointer, LessThan) {
static const struct {
const char *str;