diff --git a/include/rapidjson/pointer.h b/include/rapidjson/pointer.h index 6ee6abc..e470585 100644 --- a/include/rapidjson/pointer.h +++ b/include/rapidjson/pointer.h @@ -43,7 +43,7 @@ public: { } - GenericPointer(const Ch* source, Allocator* allocator = 0) + explicit GenericPointer(const Ch* source, Allocator* allocator = 0) : allocator_(allocator), ownAllocator_(), nameBuffer_(), @@ -156,22 +156,23 @@ public: v->SetArray(); } - switch (v->GetType()) { - case kObjectType: - { - typename ValueType::MemberIterator m = v->FindMember(GenericStringRef(t->name, t->length)); - if (m == v->MemberEnd()) { - v->AddMember(Value(t->name, t->length, allocator).Move(), Value().Move(), allocator); - v = &(--v->MemberEnd())->value; // Assumes AddMember() appends at the end - exist = false; - } - else - v = &m->value; + if (t->index == kPointerInvalidIndex) { + if (!v->IsObject()) + v->SetObject(); // Change to Object + + typename ValueType::MemberIterator m = v->FindMember(GenericStringRef(t->name, t->length)); + if (m == v->MemberEnd()) { + v->AddMember(Value(t->name, t->length, allocator).Move(), Value().Move(), allocator); + v = &(--v->MemberEnd())->value; // Assumes AddMember() appends at the end + exist = false; } - break; - case kArrayType: - if (t->index == kPointerInvalidIndex) + else + v = &m->value; + } + else { + if (!v->IsArray()) v->SetArray(); // Change to Array + if (t->index >= v->Size()) { v->Reserve(t->index + 1, allocator); while (t->index >= v->Size()) @@ -179,11 +180,6 @@ public: exist = false; } v = &((*v)[t->index]); - break; - default: - // Impossible. - RAPIDJSON_ASSERT(false); - break; } } @@ -237,6 +233,28 @@ public: return Create(root, allocator) = value; } + // Copy semantics, create parents if non-exist + ValueType& Set(ValueType& root, const ValueType& value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator).CopyFrom(value, allocator); + } + + ValueType& Set(ValueType& root, GenericStringRef value, typename ValueType::AllocatorType& allocator) const { + ValueType v(value); + return Create(root, allocator) = v; + } + + ValueType& Set(ValueType& root, const Ch* value, typename ValueType::AllocatorType& allocator) const { + ValueType v(value, allocator); + return Create(root, allocator) = v; + } + + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (ValueType&)) + Set(ValueType& root, T value, typename ValueType::AllocatorType& allocator) const { + ValueType v(value); + return Create(root, allocator) = v; + } + // Create parents if non-exist ValueType& Swap(ValueType& root, ValueType& value, typename ValueType::AllocatorType& allocator) const { return Create(root, allocator).Swap(value); @@ -340,7 +358,7 @@ typename T::ValueType& CreateValueByPointer(T& root, const GenericPointer typename T::ValueType& CreateValueByPointer(T& root, const CharType(&source)[N], typename T::AllocatorType& a) { const GenericPointer pointer(source, N - 1); - return pointer.Create(root, a); + return CreateValueByPointer(root, pointer, a); } template @@ -356,13 +374,13 @@ const typename T::ValueType* GetValueByPointer(const T& root, const GenericPoint template typename T::ValueType* GetValueByPointer(T& root, const CharType (&source)[N]) { const GenericPointer pointer(source, N - 1); - return pointer.Get(root); + return GetValueByPointer(root, pointer); } template const typename T::ValueType* GetValueByPointer(const T& root, const CharType(&source)[N]) { const GenericPointer pointer(source, N - 1); - return pointer.Get(root); + return GetValueByPointer(root, pointer); } template @@ -373,7 +391,7 @@ typename T::ValueType& GetValueByPointerWithDefault(T& root, const GenericPointe template typename T::ValueType& GetValueByPointerWithDefault(T& root, const CharType(&source)[N], const typename T::ValueType& defaultValue, typename T::AllocatorType& a) { const GenericPointer pointer(source, N - 1); - return pointer.GetWithDefault(root, defaultValue, a); + return GetValueByPointerWithDefault(root, pointer, defaultValue, a); } template @@ -381,10 +399,45 @@ typename T::ValueType& SetValueByPointer(T& root, const GenericPointer +typename T::ValueType& SetValueByPointer(T& root, const GenericPointer& pointer, GenericStringRef value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const GenericPointer& pointer, const typename T::Ch* value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename T::ValueType&)) +SetValueByPointer(T& root, const GenericPointer& pointer, T2 value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} + template typename T::ValueType& SetValueByPointer(T& root, const CharType(&source)[N], typename T::ValueType& value, typename T::AllocatorType& a) { const GenericPointer pointer(source, N - 1); - return pointer.Set(root, value , a); + return SetValueByPointer(root, pointer, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const CharType(&source)[N], GenericStringRef value, typename T::AllocatorType& a) { + const GenericPointer pointer(source, N - 1); + return SetValueByPointer(root, pointer, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const CharType(&source)[N], const typename T::Ch* value, typename T::AllocatorType& a) { + const GenericPointer pointer(source, N - 1); + return SetValueByPointer(root, pointer, value, a); +} + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename T::ValueType&)) +SetValueByPointer(T& root, const CharType(&source)[N], T2 value, typename T::AllocatorType& a) { + const GenericPointer pointer(source, N - 1); + return SetValueByPointer(root, pointer, value, a); } template @@ -395,7 +448,7 @@ typename T::ValueType& SwapValueByPointer(T& root, const GenericPointer typename T::ValueType& SwapValueByPointer(T& root, const CharType(&source)[N], typename T::ValueType& value, typename T::AllocatorType& a) { const GenericPointer pointer(source, N - 1); - return pointer.Swap(root, value, a); + return SwapValueByPointer(root, pointer, value, a); } typedef GenericPointer Pointer; diff --git a/test/unittest/pointertest.cpp b/test/unittest/pointertest.cpp index 98a006d..3efa0ec 100644 --- a/test/unittest/pointertest.cpp +++ b/test/unittest/pointertest.cpp @@ -293,11 +293,46 @@ TEST(Pointer, Set) { d.Parse(kJson); Document::AllocatorType& a = d.GetAllocator(); + // Value version Pointer("/foo/0").Set(d, Value(123).Move(), a); EXPECT_EQ(123, d["foo"][0].GetInt()); - Pointer("/foo/2").Set(d, Value(456).Move(), a); - EXPECT_EQ(456, d["foo"][2].GetInt()); + Pointer("/foo/null").Set(d, Value().Move(), a); + EXPECT_TRUE(GetValueByPointer(d, "/foo/null")->IsNull()); + + // Generic version + Pointer("/foo/int").Set(d, -1, a); + EXPECT_EQ(-1, GetValueByPointer(d, "/foo/int")->GetInt()); + + Pointer("/foo/uint").Set(d, 0x87654321, a); + EXPECT_EQ(0x87654321, GetValueByPointer(d, "/foo/uint")->GetUint()); + + const int64_t i64 = static_cast(RAPIDJSON_UINT64_C2(0x80000000, 0)); + Pointer("/foo/int64").Set(d, i64, a); + EXPECT_EQ(i64, GetValueByPointer(d, "/foo/int64")->GetInt64()); + + const uint64_t u64 = RAPIDJSON_UINT64_C2(0xFFFFFFFFF, 0xFFFFFFFFF); + Pointer("/foo/uint64").Set(d, u64, a); + EXPECT_EQ(u64, GetValueByPointer(d, "/foo/uint64")->GetUint64()); + + Pointer("/foo/true").Set(d, true, a); + EXPECT_TRUE(GetValueByPointer(d, "/foo/true")->IsTrue()); + + Pointer("/foo/false").Set(d, false, a); + EXPECT_TRUE(GetValueByPointer(d, "/foo/false")->IsFalse()); + + // StringRef version + Pointer("/foo/hello").Set(d, "Hello", a); + EXPECT_STREQ("Hello", GetValueByPointer(d, "/foo/hello")->GetString()); + + // Copy string version + { + char buffer[256]; + strcpy(buffer, "World"); + Pointer("/foo/world").Set(d, buffer, a); + memset(buffer, 0, sizeof(buffer)); + } + EXPECT_STREQ("World", GetValueByPointer(d, "/foo/world")->GetString()); } TEST(Pointer, Swap) { @@ -346,16 +381,98 @@ TEST(Pointer, GetValueByPointerWithDefault) { EXPECT_TRUE(Value("baz") == GetValueByPointerWithDefault(d, "/foo/1", v, a)); } -TEST(Pointer, SetValueByPointer) { +TEST(Pointer, SetValueByPointer_Pointer) { Document d; d.Parse(kJson); Document::AllocatorType& a = d.GetAllocator(); + // Value version SetValueByPointer(d, Pointer("/foo/0"), Value(123).Move(), a); EXPECT_EQ(123, d["foo"][0].GetInt()); - SetValueByPointer(d, "/foo/2", Value(456).Move(), a); - EXPECT_EQ(456, d["foo"][2].GetInt()); + SetValueByPointer(d, Pointer("/foo/null"), Value().Move(), a); + EXPECT_TRUE(GetValueByPointer(d, "/foo/null")->IsNull()); + + // Generic version + SetValueByPointer(d, Pointer("/foo/int"), -1, a); + EXPECT_EQ(-1, GetValueByPointer(d, "/foo/int")->GetInt()); + + SetValueByPointer(d, Pointer("/foo/uint"), 0x87654321, a); + EXPECT_EQ(0x87654321, GetValueByPointer(d, "/foo/uint")->GetUint()); + + const int64_t i64 = static_cast(RAPIDJSON_UINT64_C2(0x80000000, 0)); + SetValueByPointer(d, Pointer("/foo/int64"), i64, a); + EXPECT_EQ(i64, GetValueByPointer(d, "/foo/int64")->GetInt64()); + + const uint64_t u64 = RAPIDJSON_UINT64_C2(0xFFFFFFFFF, 0xFFFFFFFFF); + SetValueByPointer(d, Pointer("/foo/uint64"), u64, a); + EXPECT_EQ(u64, GetValueByPointer(d, "/foo/uint64")->GetUint64()); + + SetValueByPointer(d, Pointer("/foo/true"), true, a); + EXPECT_TRUE(GetValueByPointer(d, "/foo/true")->IsTrue()); + + SetValueByPointer(d, Pointer("/foo/false"), false, a); + EXPECT_TRUE(GetValueByPointer(d, "/foo/false")->IsFalse()); + + // StringRef version + SetValueByPointer(d, Pointer("/foo/hello"), "Hello", a); + EXPECT_STREQ("Hello", GetValueByPointer(d, "/foo/hello")->GetString()); + + // Copy string version + { + char buffer[256]; + strcpy(buffer, "World"); + SetValueByPointer(d, Pointer("/foo/world"), buffer, a); + memset(buffer, 0, sizeof(buffer)); + } + EXPECT_STREQ("World", GetValueByPointer(d, "/foo/world")->GetString()); +} + +TEST(Pointer, SetValueByPointer_String) { + Document d; + d.Parse(kJson); + Document::AllocatorType& a = d.GetAllocator(); + + // Value version + SetValueByPointer(d, "/foo/0", Value(123).Move(), a); + EXPECT_EQ(123, d["foo"][0].GetInt()); + + SetValueByPointer(d, "/foo/null", Value().Move(), a); + EXPECT_TRUE(GetValueByPointer(d, "/foo/null")->IsNull()); + + // Generic version + SetValueByPointer(d, "/foo/int", -1, a); + EXPECT_EQ(-1, GetValueByPointer(d, "/foo/int")->GetInt()); + + SetValueByPointer(d, "/foo/uint", 0x87654321, a); + EXPECT_EQ(0x87654321, GetValueByPointer(d, "/foo/uint")->GetUint()); + + const int64_t i64 = static_cast(RAPIDJSON_UINT64_C2(0x80000000, 0)); + SetValueByPointer(d, "/foo/int64", i64, a); + EXPECT_EQ(i64, GetValueByPointer(d, "/foo/int64")->GetInt64()); + + const uint64_t u64 = RAPIDJSON_UINT64_C2(0xFFFFFFFFF, 0xFFFFFFFFF); + SetValueByPointer(d, "/foo/uint64", u64, a); + EXPECT_EQ(u64, GetValueByPointer(d, "/foo/uint64")->GetUint64()); + + SetValueByPointer(d, "/foo/true", true, a); + EXPECT_TRUE(GetValueByPointer(d, "/foo/true")->IsTrue()); + + SetValueByPointer(d, "/foo/false", false, a); + EXPECT_TRUE(GetValueByPointer(d, "/foo/false")->IsFalse()); + + // StringRef version + SetValueByPointer(d, "/foo/hello", "Hello", a); + EXPECT_STREQ("Hello", GetValueByPointer(d, "/foo/hello")->GetString()); + + // Copy string version + { + char buffer[256]; + strcpy(buffer, "World"); + SetValueByPointer(d, "/foo/world", buffer, a); + memset(buffer, 0, sizeof(buffer)); + } + EXPECT_STREQ("World", GetValueByPointer(d, "/foo/world")->GetString()); } TEST(Pointer, SwapValueByPointer) {