mirror of
https://github.com/Tencent/rapidjson.git
synced 2025-03-10 03:29:59 +01:00
Add GetValueByPointerWithDefault() overloads
This commit is contained in:
parent
32b45f6e6d
commit
d0d18847c5
@ -445,12 +445,47 @@ typename T::ValueType& GetValueByPointerWithDefault(T& root, const GenericPointe
|
||||
return pointer.GetWithDefault(root, defaultValue, a);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename T::ValueType& GetValueByPointerWithDefault(T& root, const GenericPointer<typename T::ValueType>& pointer, GenericStringRef<typename T::Ch> defaultValue, typename T::AllocatorType& a) {
|
||||
return pointer.GetWithDefault(root, defaultValue, a);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename T::ValueType& GetValueByPointerWithDefault(T& root, const GenericPointer<typename T::ValueType>& pointer, const typename T::Ch* defaultValue, typename T::AllocatorType& a) {
|
||||
return pointer.GetWithDefault(root, defaultValue, a);
|
||||
}
|
||||
|
||||
template <typename T, typename T2>
|
||||
RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T2>, internal::IsGenericValue<T2> >), (typename T::ValueType&))
|
||||
GetValueByPointerWithDefault(T& root, const GenericPointer<typename T::ValueType>& pointer, T2 defaultValue, typename T::AllocatorType& a) {
|
||||
return pointer.GetWithDefault(root, defaultValue, a);
|
||||
}
|
||||
|
||||
template <typename T, typename CharType, size_t N>
|
||||
typename T::ValueType& GetValueByPointerWithDefault(T& root, const CharType(&source)[N], const typename T::ValueType& defaultValue, typename T::AllocatorType& a) {
|
||||
const GenericPointer<typename T::ValueType> pointer(source, N - 1);
|
||||
return GetValueByPointerWithDefault(root, pointer, defaultValue, a);
|
||||
}
|
||||
|
||||
template <typename T, typename CharType, size_t N>
|
||||
typename T::ValueType& GetValueByPointerWithDefault(T& root, const CharType(&source)[N], GenericStringRef<typename T::Ch> defaultValue, typename T::AllocatorType& a) {
|
||||
const GenericPointer<typename T::ValueType> pointer(source, N - 1);
|
||||
return GetValueByPointerWithDefault(root, pointer, defaultValue, a);
|
||||
}
|
||||
|
||||
template <typename T, typename CharType, size_t N>
|
||||
typename T::ValueType& GetValueByPointerWithDefault(T& root, const CharType(&source)[N], const typename T::Ch* defaultValue, typename T::AllocatorType& a) {
|
||||
const GenericPointer<typename T::ValueType> pointer(source, N - 1);
|
||||
return GetValueByPointerWithDefault(root, pointer, defaultValue, a);
|
||||
}
|
||||
|
||||
template <typename T, typename CharType, size_t N, typename T2>
|
||||
RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T2>, internal::IsGenericValue<T2> >), (typename T::ValueType&))
|
||||
GetValueByPointerWithDefault(T& root, const CharType(&source)[N], T2 defaultValue, typename T::AllocatorType& a) {
|
||||
const GenericPointer<typename T::ValueType> pointer(source, N - 1);
|
||||
return GetValueByPointerWithDefault(root, pointer, defaultValue, a);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename T::ValueType& SetValueByPointer(T& root, const GenericPointer<typename T::ValueType>& pointer, typename T::ValueType& value, typename T::AllocatorType& a) {
|
||||
return pointer.Set(root, value, a);
|
||||
|
@ -444,14 +444,102 @@ TEST(Pointer, GetValueByPointer) {
|
||||
EXPECT_EQ(&d["foo"][0], GetValueByPointer(v, "/foo/0"));
|
||||
}
|
||||
|
||||
TEST(Pointer, GetValueByPointerWithDefault) {
|
||||
TEST(Pointer, GetValueByPointerWithDefault_Pointer) {
|
||||
Document d;
|
||||
d.Parse(kJson);
|
||||
|
||||
Document::AllocatorType& a = d.GetAllocator();
|
||||
const Value v("qux");
|
||||
EXPECT_TRUE(Value("bar") == GetValueByPointerWithDefault(d, Pointer("/foo/0"), v, a));
|
||||
EXPECT_TRUE(Value("bar") == GetValueByPointerWithDefault(d, Pointer("/foo/0"), v, a));
|
||||
EXPECT_TRUE(Value("baz") == GetValueByPointerWithDefault(d, Pointer("/foo/1"), v, a));
|
||||
EXPECT_TRUE(Value("qux") == GetValueByPointerWithDefault(d, Pointer("/foo/2"), v, a));
|
||||
EXPECT_TRUE(Value("last") == GetValueByPointerWithDefault(d, Pointer("/foo/-"), Value("last").Move(), a));
|
||||
EXPECT_STREQ("last", d["foo"][3].GetString());
|
||||
|
||||
EXPECT_TRUE(GetValueByPointerWithDefault(d, Pointer("/foo/null"), Value().Move(), a).IsNull());
|
||||
EXPECT_TRUE(GetValueByPointerWithDefault(d, Pointer("/foo/null"), "x", a).IsNull());
|
||||
|
||||
// Generic version
|
||||
EXPECT_EQ(-1, GetValueByPointerWithDefault(d, Pointer("/foo/int"), -1, a).GetInt());
|
||||
EXPECT_EQ(-1, GetValueByPointerWithDefault(d, Pointer("/foo/int"), -2, a).GetInt());
|
||||
EXPECT_EQ(0x87654321, GetValueByPointerWithDefault(d, Pointer("/foo/uint"), 0x87654321, a).GetUint());
|
||||
EXPECT_EQ(0x87654321, GetValueByPointerWithDefault(d, Pointer("/foo/uint"), 0x12345678, a).GetUint());
|
||||
|
||||
const int64_t i64 = static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x80000000, 0));
|
||||
EXPECT_EQ(i64, GetValueByPointerWithDefault(d, Pointer("/foo/int64"), i64, a).GetInt64());
|
||||
EXPECT_EQ(i64, GetValueByPointerWithDefault(d, Pointer("/foo/int64"), i64 + 1, a).GetInt64());
|
||||
|
||||
const uint64_t u64 = RAPIDJSON_UINT64_C2(0xFFFFFFFFF, 0xFFFFFFFFF);
|
||||
EXPECT_EQ(u64, GetValueByPointerWithDefault(d, Pointer("/foo/uint64"), u64, a).GetUint64());
|
||||
EXPECT_EQ(u64, GetValueByPointerWithDefault(d, Pointer("/foo/uint64"), u64 - 1, a).GetUint64());
|
||||
|
||||
EXPECT_TRUE(GetValueByPointerWithDefault(d, Pointer("/foo/true"), true, a).IsTrue());
|
||||
EXPECT_TRUE(GetValueByPointerWithDefault(d, Pointer("/foo/true"), false, a).IsTrue());
|
||||
|
||||
EXPECT_TRUE(GetValueByPointerWithDefault(d, Pointer("/foo/false"), false, a).IsFalse());
|
||||
EXPECT_TRUE(GetValueByPointerWithDefault(d, Pointer("/foo/false"), true, a).IsFalse());
|
||||
|
||||
// StringRef version
|
||||
EXPECT_STREQ("Hello", GetValueByPointerWithDefault(d, Pointer("/foo/hello"), "Hello", a).GetString());
|
||||
|
||||
// Copy string version
|
||||
{
|
||||
char buffer[256];
|
||||
strcpy(buffer, "World");
|
||||
EXPECT_STREQ("World", GetValueByPointerWithDefault(d, Pointer("/foo/world"), buffer, a).GetString());
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
}
|
||||
EXPECT_STREQ("World", GetValueByPointer(d, Pointer("/foo/world"))->GetString());
|
||||
}
|
||||
|
||||
TEST(Pointer, GetValueByPointerWithDefault_String) {
|
||||
Document d;
|
||||
d.Parse(kJson);
|
||||
|
||||
Document::AllocatorType& a = d.GetAllocator();
|
||||
const Value v("qux");
|
||||
EXPECT_TRUE(Value("bar") == GetValueByPointerWithDefault(d, "/foo/0", v, a));
|
||||
EXPECT_TRUE(Value("bar") == GetValueByPointerWithDefault(d, "/foo/0", v, a));
|
||||
EXPECT_TRUE(Value("baz") == GetValueByPointerWithDefault(d, "/foo/1", v, a));
|
||||
EXPECT_TRUE(Value("qux") == GetValueByPointerWithDefault(d, "/foo/2", v, a));
|
||||
EXPECT_TRUE(Value("last") == GetValueByPointerWithDefault(d, "/foo/-", Value("last").Move(), a));
|
||||
EXPECT_STREQ("last", d["foo"][3].GetString());
|
||||
|
||||
EXPECT_TRUE(GetValueByPointerWithDefault(d, "/foo/null", Value().Move(), a).IsNull());
|
||||
EXPECT_TRUE(GetValueByPointerWithDefault(d, "/foo/null", "x", a).IsNull());
|
||||
|
||||
// Generic version
|
||||
EXPECT_EQ(-1, GetValueByPointerWithDefault(d, "/foo/int", -1, a).GetInt());
|
||||
EXPECT_EQ(-1, GetValueByPointerWithDefault(d, "/foo/int", -2, a).GetInt());
|
||||
EXPECT_EQ(0x87654321, GetValueByPointerWithDefault(d, "/foo/uint", 0x87654321, a).GetUint());
|
||||
EXPECT_EQ(0x87654321, GetValueByPointerWithDefault(d, "/foo/uint", 0x12345678, a).GetUint());
|
||||
|
||||
const int64_t i64 = static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x80000000, 0));
|
||||
EXPECT_EQ(i64, GetValueByPointerWithDefault(d, "/foo/int64", i64, a).GetInt64());
|
||||
EXPECT_EQ(i64, GetValueByPointerWithDefault(d, "/foo/int64", i64 + 1, a).GetInt64());
|
||||
|
||||
const uint64_t u64 = RAPIDJSON_UINT64_C2(0xFFFFFFFFF, 0xFFFFFFFFF);
|
||||
EXPECT_EQ(u64, GetValueByPointerWithDefault(d, "/foo/uint64", u64, a).GetUint64());
|
||||
EXPECT_EQ(u64, GetValueByPointerWithDefault(d, "/foo/uint64", u64 - 1, a).GetUint64());
|
||||
|
||||
EXPECT_TRUE(GetValueByPointerWithDefault(d, "/foo/true", true, a).IsTrue());
|
||||
EXPECT_TRUE(GetValueByPointerWithDefault(d, "/foo/true", false, a).IsTrue());
|
||||
|
||||
EXPECT_TRUE(GetValueByPointerWithDefault(d, "/foo/false", false, a).IsFalse());
|
||||
EXPECT_TRUE(GetValueByPointerWithDefault(d, "/foo/false", true, a).IsFalse());
|
||||
|
||||
// StringRef version
|
||||
EXPECT_STREQ("Hello", GetValueByPointerWithDefault(d, "/foo/hello", "Hello", a).GetString());
|
||||
|
||||
// Copy string version
|
||||
{
|
||||
char buffer[256];
|
||||
strcpy(buffer, "World");
|
||||
EXPECT_STREQ("World", GetValueByPointerWithDefault(d, "/foo/world", buffer, a).GetString());
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
}
|
||||
EXPECT_STREQ("World", GetValueByPointer(d, "/foo/world")->GetString());
|
||||
}
|
||||
|
||||
TEST(Pointer, SetValueByPointer_Pointer) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user