Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/miloyip/rapidjson.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKrystian Chmura <krystian.chmura@gmail.com>2021-01-05 16:20:57 +0300
committerKrystian Chmura <krystian.chmura@sabre.com>2021-01-05 16:20:57 +0300
commitcbf62de55d684b70df2ae072097568719454f321 (patch)
tree08257479afc21bb28b867824e5107927ff903627
parent3cdd3c8370a2cf5eed3fa4afbe4465a525f54e33 (diff)
Add implicit conversion from Object and Array to Value (#1404)
Allows resolution of JSON Pointer on Object and Array
-rw-r--r--include/rapidjson/document.h2
-rw-r--r--test/unittest/pointertest.cpp32
2 files changed, 34 insertions, 0 deletions
diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h
index 533a9ad2..028235ec 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 72c686f0..43718038 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<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;