Search code examples
c++jsonjsoncpp

What's the difference between clear and arrayValue in jsoncpp


To work with json using a library jsoncpp. Previously created an array of objects can be cleared by clear.

Json::Value value;
...
value.clear();

Also, it can be purified by arrayValue (or nullValue).

Json::Value value;
...
value = Json::arrayValue;

Question: What's the difference between clear and arrayValue?


Solution

  • null, empty array, and empty object are three different things in JsonCpp.

    value.clear() does the following:

    • null -> null
    • any array -> empty array
    • any object -> empty object
    • anything else -> throw std::runtime_error

    value = Json:arrayValue does the following:

    • anything -> empty array

    So the two constructs are not semantically equivalent.