Search code examples
rapidjson

Can I distinguish Integer and Double type in rapidjson


When I ask type of rapidjson::Value using GetType() method, it returns only belows Type:

//! Type of JSON value
enum Type {
    kNullType = 0,      //!< null
    kFalseType = 1,     //!< false
    kTrueType = 2,      //!< true
    kObjectType = 3,    //!< object
    kArrayType = 4,     //!< array
    kStringType = 5,    //!< string
    kNumberType = 6     //!< number
};

As you can see, there are no such kIntType nor kDoubleType (even kUintType, kInt64Type) Therefore, I can't get actual value of rapidjson::Value.

For example:

if (value.GetType() == rapidjson::kNumberType)
{
    double v = value.GetDouble()        // this?
    unsigned long v = value.GetUInt64() // or this??
    int v = value.GetInt()              // or this?
}

Is there anyway to distinguish actual numeric type?

Thanks.


Solution

  • There are:

    1. bool Value::IsInt() const
    2. bool Value::IsUint() const
    3. bool Value::IsInt64() const
    4. bool Value::IsUint64() const
    5. bool Value::IsDouble() const

    Note that, 1-4 are not exclusive to each other. For example, the value 123 will make 1-4 return true but 5 will return false. And calling GetDouble() is always OK when IsNumber() or 1-5 is true, though precision loss is possible when the value is actually a 64-bit (unsigned) integer.

    http://miloyip.github.io/rapidjson/md_doc_tutorial.html#QueryNumber