C ++ rapidjson: GenericValue :: IsNull returns false anyway

I am still shocked after discovering a mysterious problem in our project.

We realized that calling HasMember ("string") performed an additional search. So, for performance reasons, we are changing it.

Main idea:

Instead of calling HasMember and subsequently chasing the link, for example:

rapidjson::Document d;
d.Parse<0>(json);

if(d.HasMember("foo"))
{
    const rapidjson::Value& fooValue = d["foo"];

    // do something with fooValue
}

Changed to:

rapidjson::Document d;
d.Parse<0>(json);

const rapidjson::Value& fooValue = d["foo"];
if( !fooValue.IsNull() )
{
    // do something with fooValue
}

It was very good, we save to execute two requests instead of one. However, there is a problem.

If you start watching how quickjson implements nullvalue (returns by default if the search fails), you will see the following code:

//! Get the value associated with the object name.
GenericValue & operator[](const Ch* name) {
    // Check
    if (Member * member = FindMember(name)) {
        return member->value;
    } else {
        // Nothing
        static GenericValue NullValue;
        return NullValue;
    }
}

// Finder
const GenericValue & operator[] (const Ch* name) const { 
    // Return
    return const_cast<GenericValue &> (* this)[name]; 
}

, , . , , .

, - NullValue. , IsNull ( ) , NullValue .

, ? , ?

, null , const, . const, const_cast ( , , ).

, . - quickjson, .

+4
1

-. operator[] , .

, API RapidJSON. operator[] . , ,

MemberIterator FindMember(const Ch* name);
ConstMemberIterator FindMember(const Ch* name) const;

MemberEnd(), , . .

, , RapidJSON GitHub. . , , . .

P.S. RapidJSON.

+6

All Articles