110 const Json::Value&
asJson( )
const{
return m_js;}
124 return get( key, Json::ValueType::objectValue,
"empty object ");
128 std::stringstream default_str;
129 default_str <<
"value "<<value;
130 return get( key, value, default_str.str());
134 return get( idx, Json::ValueType::objectValue,
"empty array");
138 std::stringstream default_str;
139 default_str <<
"value "<<value;
140 return get( idx, value, default_str.str());
149 return m_js.asDouble();
150 return type_error<double>( value,
"a Double");
153 unsigned asUInt(
unsigned value = 0)
const{
155 return m_js.asUInt();
156 return type_error<unsigned>( value,
"an Unsigned");
162 return type_error<int>( value,
"an Int");
167 return m_js.asBool();
168 return type_error<bool>( value,
"a Bool");
171 std::string
asString( std::string value =
"")
const{
174 return m_js.asString();
175 return type_error<std::string>( value,
"a String");
178 WrappedJsonValue(Json::Value js,
error mode, std::string access):m_js(js), m_mode( mode), m_access_str(access) {}
179 WrappedJsonValue get( std::string key,
const Json::Value& value, std::string default_str)
const
181 std::string access = m_access_str +
"\""+key+
"\": ";
182 std::stringstream message;
183 if( !m_js.isObject( ) || !m_js.isMember(key))
185 message <<
"*** Key error: "<<access<<
" not found.";
186 raise_error( message.str(), default_str);
191 WrappedJsonValue get(
unsigned idx,
const Json::Value& value, std::string default_str)
const
193 std::string access = m_access_str +
"["+std::to_string(idx)+
"] ";
194 if( !m_js.isArray() || !m_js.isValidIndex(idx))
196 std::stringstream message;
200 if( m_access_str.empty())
201 message <<
"*** Index error: Index "<<idx<<
" not present.";
203 message <<
"*** Index error: Index "<<idx<<
" not present in "<<m_access_str<<
".";
204 raise_error( message.str(), default_str);
210 T type_error( T value, std::string type)
const
212 std::stringstream message, default_str;
213 default_str <<
"value "<<value;
214 message <<
"*** Type error: "<<m_access_str<<
" "<<m_js<<
" is not "<<type<<
".";
215 raise_error( message.str(), default_str.str());
218 void raise_error( std::string message, std::string default_str)
const
221 throw std::runtime_error( message);
223 std::cerr <<
"WARNING "<< message<<
" Using default "<<default_str<<
"\n";
229 std::string m_access_str =
"";
246 Json::CharReaderBuilder parser;
248 Json::CharReaderBuilder::strictMode( &parser.settings_);
251 Json::CharReaderBuilder::strictMode( &parser.settings_);
253 Json::Value js_true (
true);
254 Json::Value js_false (
false);
255 parser.settings_[
"allowComments"].swap( js_true);
256 parser.settings_[
"collectComments"].swap(js_false);
259 Json::CharReaderBuilder::setDefaults( &parser.settings_);
261 std::ifstream isI( filename);
264 std::string message =
"\nAn error occured while parsing "+filename+
"\n";
265 message +=
"*** File does not exist! *** \n\n";
267 throw std::runtime_error( message);
269 std::cerr <<
"WARNING: "<<message<<std::endl;
275 if( !parseFromStream( parser, isI, &js, &errs) )
277 std::string message =
"An error occured while parsing "+filename+
"\n"+errs;
279 throw std::runtime_error( message);
281 std::cerr <<
"WARNING: "<<message<<std::endl;
303 Json::CharReaderBuilder parser;
305 Json::CharReaderBuilder::strictMode( &parser.settings_);
308 Json::CharReaderBuilder::strictMode( &parser.settings_);
309 parser.settings_[
"allowComments"] =
true;
310 parser.settings_[
"collectComments"] =
false;
313 Json::CharReaderBuilder::setDefaults( &parser.settings_);
316 std::stringstream ss(input);
317 if( !parseFromStream( parser, ss, &js, &errs) )
320 throw std::runtime_error( errs);
322 std::cerr <<
"WARNING: "<<errs<<std::endl;
static void string2Json(std::string input, Json::Value &js, enum comments comm=file::comments::are_discarded, enum error err=file::error::is_throw)
Convenience wrapper to parse a string into a Json::Value.
Definition: json_utilities.h:301
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
error
Switch between how to handle errors in a Json utitlity functions.
Definition: json_utilities.h:30
comments
Switch how comments are treated in a json string or file.
Definition: json_utilities.h:37
@ is_warning
Handle the error by writing a warning to std::cerr.
@ is_silent
Ignore the error and silently continue execution.
@ are_discarded
Allow comments but discard them in the Json value.
@ are_kept
Keep comments in the Json value.
@ are_forbidden
Treat comments as invalid Json.
Wrapped Access to Json values with error handling.
Definition: json_utilities.h:90
std::string asString(std::string value="") const
Wrap the corresponding Json::Value function with error handling.
Definition: json_utilities.h:171
WrappedJsonValue()
Default constructor By default the error mode is error::is_throw.
Definition: json_utilities.h:93
const Json::Value & asJson() const
Read access to the raw Json value.
Definition: json_utilities.h:110
WrappedJsonValue get(std::string key, const Json::Value &value) const
Wrap the corresponding Json::Value function with error handling.
Definition: json_utilities.h:127
WrappedJsonValue get(unsigned idx, const Json::Value &value) const
Wrap the corresponding Json::Value function with error handling.
Definition: json_utilities.h:137
WrappedJsonValue operator[](unsigned idx) const
Wrap the corresponding Json::Value function with error handling.
Definition: json_utilities.h:133
bool asBool(bool value=false) const
Wrap the corresponding Json::Value function with error handling.
Definition: json_utilities.h:165
WrappedJsonValue(Json::Value js)
By default the error mode is error::is_throw.
Definition: json_utilities.h:99
unsigned asUInt(unsigned value=0) const
Wrap the corresponding Json::Value function with error handling.
Definition: json_utilities.h:153
void set_mode(error new_mode)
Change the error mode.
Definition: json_utilities.h:106
double asDouble(double value=0) const
Wrap the corresponding Json::Value function with error handling.
Definition: json_utilities.h:147
WrappedJsonValue(error mode)
Construct with error mode.
Definition: json_utilities.h:96
WrappedJsonValue(Json::Value js, error mode)
Construct with Json value and error mode.
Definition: json_utilities.h:103
std::string access_string() const
The creation history of the object.
Definition: json_utilities.h:119
WrappedJsonValue operator[](std::string key) const
Wrap the corresponding Json::Value function with error handling.
Definition: json_utilities.h:123
Json::Value & asJson()
Write access to the raw Json value (if you know what you are doing)
Definition: json_utilities.h:112
unsigned size() const
Wrap the corresponding Json::Value function with error handling.
Definition: json_utilities.h:143
int asInt(int value=0) const
Wrap the corresponding Json::Value function with error handling.
Definition: json_utilities.h:159