0

I am deserializing a json string into an object using rapidjson. When I encounter an issue, not with the structure of the json, but with the content, I want to report an error stating the offset of where the problem is.

Unfortunately, unless it is a parse error, I don't see where I can get the current offset of a Value within a Document. Anyone have any ways of accomplishing this?

For example:

Document doc;
doc.Parse<0>(json.c_str());
if( doc.HasMember( "Country" ) ) {
    const Value& country_node = doc["Country"];
    if( !isValid(country_node.GetString()) )
        cout << "Invalid country specified at position " << country_node.Offset()?????
}
user3072517
  • 513
  • 1
  • 7
  • 21

1 Answers1

1

Unfortunately, RapidJSON does not support this in the DOM API.

If you use the SAX API, when you encounter an invalid value, you can return false in the handler function, and the Reader will generate a kParseErrorTermination error with the offset.

The reason why this is not supported in DOM because this will incur memory overhead and may only be used rarely. Please drop an issue at GitHub if you would like to further discuss this feature with the community.

Milo Yip
  • 4,902
  • 2
  • 25
  • 27