|
| WrappedJsonValue () |
| Default constructor By default the error mode is error::is_throw .
|
|
| WrappedJsonValue (error mode) |
| Construct with error mode. More...
|
|
| WrappedJsonValue (Json::Value js) |
| By default the error mode is error::is_throw . More...
|
|
| WrappedJsonValue (Json::Value js, error mode) |
| Construct with Json value and error mode. More...
|
|
void | set_mode (error new_mode) |
| Change the error mode. More...
|
|
const Json::Value & | asJson () const |
| Read access to the raw Json value.
|
|
Json::Value & | asJson () |
| Write access to the raw Json value (if you know what you are doing)
|
|
std::string | access_string () const |
| The creation history of the object. More...
|
|
WrappedJsonValue | operator[] (std::string key) const |
| Wrap the corresponding Json::Value function with error handling.
|
|
WrappedJsonValue | get (std::string key, const Json::Value &value) const |
| Wrap the corresponding Json::Value function with error handling.
|
|
WrappedJsonValue | operator[] (unsigned idx) const |
| Wrap the corresponding Json::Value function with error handling.
|
|
WrappedJsonValue | get (unsigned idx, const Json::Value &value) const |
| Wrap the corresponding Json::Value function with error handling.
|
|
unsigned | size () const |
| Wrap the corresponding Json::Value function with error handling.
|
|
double | asDouble (double value=0) const |
| Wrap the corresponding Json::Value function with error handling.
|
|
unsigned | asUInt (unsigned value=0) const |
| Wrap the corresponding Json::Value function with error handling.
|
|
int | asInt (int value=0) const |
| Wrap the corresponding Json::Value function with error handling.
|
|
bool | asBool (bool value=false) const |
| Wrap the corresponding Json::Value function with error handling.
|
|
std::string | asString (std::string value="") const |
| Wrap the corresponding Json::Value function with error handling.
|
|
Wrapped Access to Json values with error handling.
The purpose of this class is to wrap the access to a Json::Value with guards that raise exceptions or display warnings in case an error occurs, for example when a key is misspelled, missing or has the wrong type. The goal is the composition of a good error message that helps a user quickly debug the input (file).
The Wrapper is necessary because Jsoncpp by default silently generates a new key in case it is not present which in our scenario is an invitation for stupid mistakes.
You can use the WrappedJsonValue
like a Json::Value
with read-only access:
Json::Value js;
try{
std::string hello = ws.get( "hello", "").asString();
int idx0 = ws[ "array" ][out_of_bounds_index].asInt();
} catch ( std::exception& e){
std::cerr << "Error in file test.json\n";
std::cerr << e.what()<<std::endl;
}
static void file2Json(std::string filename, Json::Value &js, enum comments comm=file::comments::are_discarded, enum error err=file::error::is_throw)
Convenience wrapper to open a file and parse it into a Json::Value.
Definition: json_utilities.h:244
Wrapped Access to Json values with error handling.
Definition: json_utilities.h:90
A feature of the class is that it keeps track of how a value is called. For example
{
int value = ws[
"some_non_existent_key"].
asUInt();
}
try{
some_function( js["nested"]);
} catch ( std::exception& e){ std::cerr << e.what()<<std::endl; }
unsigned asUInt(unsigned value=0) const
Wrap the corresponding Json::Value function with error handling.
Definition: json_utilities.h:153