From cbf62de55d684b70df2ae072097568719454f321 Mon Sep 17 00:00:00 2001 From: Krystian Chmura Date: Tue, 5 Jan 2021 14:20:57 +0100 Subject: [PATCH] Add implicit conversion from Object and Array to Value (#1404) Allows resolution of JSON Pointer on Object and Array --- include/rapidjson/document.h | 2 ++ test/unittest/pointertest.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 533a9ad..028235e 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -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(); } diff --git a/test/unittest/pointertest.cpp b/test/unittest/pointertest.cpp index 72c686f..4371803 100644 --- a/test/unittest/pointertest.cpp +++ b/test/unittest/pointertest.cpp @@ -1529,6 +1529,38 @@ TEST(Pointer, Ambiguity) { } } +TEST(Pointer, ResolveOnObject) { + Document d; + EXPECT_FALSE(d.Parse("{\"a\": 123}").HasParseError()); + + { + Value::ConstObject o = static_cast(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(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;