diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index d0f4bea..c390123 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -401,7 +401,7 @@ namespace internal { template struct TypeHelper { static bool Is(const ValueType&) { RAPIDJSON_ASSERT(false && "Unsupport type"); } - static T Get(const ValueType&) { RAPIDJSON_ASSERT(false && "Unsupport type"); } + static T Get(const ValueType&) { RAPIDJSON_ASSERT(false && "Unsupport type"); return T(); } static ValueType& Set(ValueType&, T) { RAPIDJSON_ASSERT(false && "Unsupport type"); } static ValueType& Set(ValueType&, T, typename ValueType::AllocatorType&) { RAPIDJSON_ASSERT(false && "Unsupport type"); } }; @@ -462,6 +462,15 @@ struct TypeHelper { static ValueType& Set(ValueType& v, float data, typename ValueType::AllocatorType&) { return v.SetFloat(data); } }; +template +struct TypeHelper { + typedef const typename ValueType::Ch* StringType; + static bool Is(const ValueType& v) { return v.IsString(); } + static StringType Get(const ValueType& v) { return v.GetString(); } + static ValueType& Set(ValueType& v, const StringType data) { return v.SetString(typename ValueType::StringRefType(data)); } + static ValueType& Set(ValueType& v, const StringType data, typename ValueType::AllocatorType& a) { return v.SetString(data, a); } +}; + #if RAPIDJSON_HAS_STDSTRING template struct TypeHelper > { @@ -1561,10 +1570,10 @@ public: RAPIDJSON_ASSERT((flags_ & kUint64Flag) != 0); return static_cast(data_.n.u64); // uint64_t -> double (may lose precision) } - //! Get the value as double type. + //! Get the value as float type. /*! \note If the value is 64-bit integer type, it may lose precision. Use \c IsLosslessFloat() to check whether the converison is lossless. */ - double GetFloat() const { + float GetFloat() const { RAPIDJSON_ASSERT(IsFloat()); return static_cast(GetDouble()); } diff --git a/test/unittest/valuetest.cpp b/test/unittest/valuetest.cpp index 7d1bb9e..a1912b3 100644 --- a/test/unittest/valuetest.cpp +++ b/test/unittest/valuetest.cpp @@ -768,6 +768,11 @@ TEST(Value, String) { EXPECT_STREQ("World", w.GetString()); EXPECT_EQ(5u, w.GetStringLength()); + // templated functions + EXPECT_TRUE(z.Is()); + EXPECT_STREQ(cstr, z.Get()); + EXPECT_STREQ("Apple", z.Set("Apple").Get()); + #if RAPIDJSON_HAS_STDSTRING { std::string str = "Hello World";