|
| | WrappedJsonValue () |
| | Default constructor By default the error mode is error::is_throw.
|
| |
| | WrappedJsonValue (error mode) |
| | Construct with error mode.
|
| |
| | WrappedJsonValue (JsonType js) |
| | By default the error mode is error::is_throw.
|
| |
| | WrappedJsonValue (JsonType js, error mode) |
| | Construct with Json value and error mode.
|
| |
| void | set_mode (error new_mode) |
| | Change the error mode.
|
| |
| const JsonType & | asJson () const |
| | Read access to the raw Json value.
|
| |
| JsonType & | 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.
|
| |
| std::string | toStyledString () const |
| | The stored json object as a formatted string.
|
| |
| bool | isMember (std::string key) const |
| | Return true if key is a Member of the json value.
|
| |
| WrappedJsonValue | operator[] (std::string key) const |
| |
| WrappedJsonValue | get (std::string key, const JsonType &value) const |
| | Wrap the corresponding JsonType function with error handling.
|
| |
| WrappedJsonValue | operator[] (unsigned idx) const |
| | Wrap the corresponding JsonType function with error handling.
|
| |
| WrappedJsonValue | get (unsigned idx, const JsonType &value) const |
| | Wrap the corresponding JsonType function with error handling.
|
| |
| unsigned | size () const |
| | Wrap the corresponding JsonType function with error handling.
|
| |
| double | asDouble (double value=0) const |
| | Wrap the corresponding JsonType function with error handling.
|
| |
| unsigned | asUInt (unsigned value=0) const |
| | Wrap the corresponding JsonType function with error handling.
|
| |
| int | asInt (int value=0) const |
| | Wrap the corresponding JsonType function with error handling.
|
| |
| bool | asBool (bool value=false) const |
| | Wrap the corresponding JsonType function with error handling.
|
| |
| std::string | asString (std::string value="") const |
| | Wrap the corresponding JsonType function with error handling.
|
| |
Wrapped Access to Json values with error handling.
The purpose of this class is to serve as an extremely pedantic access guard to a Json file, in the sense that it will raise exceptions at the slightest misstep, for example when a key is misspelled, missing or has the wrong type. It will then compose an error message that shows where exactly the access in the file went wrong and thus help a user quickly debug the input (file).
This is necessary if the cost of a faulty input file with silly mistakes like misspelling could lead to potentially large (computational) costs if uncaught.
The interface of WrappedJsonValue is modelled after jsoncpp's Json::Value:
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;
}
JsonType file2Json(std::string filename, 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 JsonType.
Definition json_wrapper.h:348
@ is_throw
throw an error (std::runtime_error)
Wrapped Access to Json values with error handling.
Definition json_wrapper.h:121
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 JsonType function with error handling.
Definition json_wrapper.h:225
The caveat of this class is that once a json value is wrapped it is somewhat awkward to change its value (because it is what the class wants to prevent)
- Attention
- This class is only for read access. If you must change a value do so on the raw
JsonType accesible with the asJson() method
-
Do not assign to a key like this:
const JsonType & asJson() const
Read access to the raw Json value.
Definition json_wrapper.h:141
- Note
- If the Marco
DG_USE_JSONHPP is defined, the #include <nlohmann/json.hpp> parser is used instead of #include <json/json.h> Since the former is header-only no additional linker options must be present at compilation.