Extension: Json and NetCDF utilities
#include "dg/file/file.h" (includes both Json and NetCDF utilities)
dg::file::WrappedJsonValue Struct Reference

Wrapped Access to Json values with error handling. More...

Public Member Functions

 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.
 

Detailed Description

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;
dg::file::file2Json( "test.json", js);
try{
std::string hello = ws.get( "hello", "").asString();
// the following access will throw
int idx0 = ws[ "array" ][out_of_bounds_index].asInt();
} catch ( std::exception& e){
std::cerr << "Error in file test.json\n";
//the what string knows that the out of bounds error occured in the array
//called "array"
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
@ is_throw
throw an error
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

void some_function( dg::file::WrappedJsonValue ws)
{
int value = ws[ "some_non_existent_key"].asUInt();
}
try{
some_function( js["nested"]);
} catch ( std::exception& e){ std::cerr << e.what()<<std::endl; }
//The what string knows that "some_non_existent_key" is expected to be
//contained in the "nested" key.
unsigned asUInt(unsigned value=0) const
Wrap the corresponding Json::Value function with error handling.
Definition: json_utilities.h:153

Constructor & Destructor Documentation

◆ WrappedJsonValue() [1/3]

dg::file::WrappedJsonValue::WrappedJsonValue ( error  mode)
inline

Construct with error mode.

Parameters
modeThe error mode

◆ WrappedJsonValue() [2/3]

dg::file::WrappedJsonValue::WrappedJsonValue ( Json::Value  js)
inline

By default the error mode is error::is_throw.

Parameters
jsThe Json value that will be guarded

◆ WrappedJsonValue() [3/3]

dg::file::WrappedJsonValue::WrappedJsonValue ( Json::Value  js,
error  mode 
)
inline

Construct with Json value and error mode.

Parameters
jsThe Json value that will be guarded
modeThe error mode

Member Function Documentation

◆ access_string()

std::string dg::file::WrappedJsonValue::access_string ( ) const
inline

The creation history of the object.

Useful to print when debugging parameter files

Returns
A string containing object history

◆ set_mode()

void dg::file::WrappedJsonValue::set_mode ( error  new_mode)
inline

Change the error mode.

Parameters
new_modeThe new error mode

The documentation for this struct was generated from the following file: