10#include <nlohmann/json.hpp>
161 return m_js.toStyledString();
168 return m_js.contains(key);
170 return m_js.isMember(key);
179 return get( key, nlohmann::json::object(),
"empty object ");
181 return get( key, Json::ValueType::objectValue,
"empty object ");
191 std::stringstream default_str;
192 default_str <<
"value "<<value;
193 return get( key, value, default_str.str());
198 return get( idx, nlohmann::json::object(),
"empty array");
200 return get( idx, Json::ValueType::objectValue,
"empty array");
205 std::stringstream default_str;
206 default_str <<
"value "<<value;
207 return get( idx, value, default_str.str());
216 if( m_js.is_number())
220 return m_js.asDouble();
222 return type_error<double>( value,
"a Double");
225 unsigned asUInt(
unsigned value = 0)
const{
227 if( m_js.is_number())
231 return m_js.asUInt();
233 return type_error<unsigned>( value,
"an Unsigned");
225 unsigned asUInt(
unsigned value = 0)
const {
…}
238 if( m_js.is_number())
244 return type_error<int>( value,
"an Int");
249 if( m_js.is_boolean())
253 return m_js.asBool();
255 return type_error<bool>( value,
"a Bool");
258 std::string
asString( std::string value =
"")
const{
260 if( m_js.is_string())
265 return m_js.asString();
267 return type_error<std::string>( value,
"a String");
273 std::string access = m_access_str +
"\""+key+
"\": ";
274 std::stringstream message;
276 if( !m_js.is_object( ) || !m_js.contains(key))
278 if( !m_js.isObject( ) || !m_js.isMember(key))
281 message <<
"*** Key error: "<<access<<
" not found.";
282 raise_error( message.str(), default_str);
289 std::string access = m_access_str +
"["+std::to_string(idx)+
"] ";
291 if( !m_js.is_array() || !(idx < m_js.size()))
293 if( !m_js.isArray() || !m_js.isValidIndex(idx))
296 std::stringstream message;
300 if( m_access_str.empty())
301 message <<
"*** Index error: Index "<<idx<<
" not present.";
303 message <<
"*** Index error: Index "<<idx<<
" not present in "<<m_access_str<<
".";
304 raise_error( message.str(), default_str);
310 T type_error( T value, std::string type)
const
312 std::stringstream message, default_str;
313 default_str <<
"value "<<value;
314 message <<
"*** Type error: "<<m_access_str<<
" "<<m_js<<
" is not "<<type<<
".";
315 raise_error( message.str(), default_str.str());
318 void raise_error( std::string message, std::string default_str)
const
321 throw std::runtime_error( message);
323 std::cerr <<
"WARNING "<< message<<
" Using default "<<default_str<<
"\n";
329 std::string m_access_str =
"";
350 std::ifstream isI( filename);
353 std::string message =
"\nAn error occured while parsing "+filename+
"\n";
354 message +=
"*** File does not exist! *** \n\n";
356 throw std::runtime_error( message);
358 std::cerr <<
"WARNING: "<<message<<std::endl;
365 bool ignore_comments =
false, allow_exceptions =
false;
367 ignore_comments =
true;
369 allow_exceptions =
true;
371 js = nlohmann::json::parse( isI,
nullptr, allow_exceptions, ignore_comments);
374 std::string message =
"An error occured while parsing "+filename+
"\n";
375 std::cerr <<
"WARNING: "<<message<<std::endl;
378 Json::CharReaderBuilder parser;
380 Json::CharReaderBuilder::strictMode( &parser.settings_);
383 Json::CharReaderBuilder::strictMode( &parser.settings_);
387 parser.settings_[
"allowComments"].swap( js_true);
388 parser.settings_[
"collectComments"].swap(js_false);
391 Json::CharReaderBuilder::setDefaults( &parser.settings_);
394 if( !parseFromStream( parser, isI, &js, &errs) )
396 std::string message =
"An error occured while parsing "+filename+
"\n"+errs;
398 throw std::runtime_error( message);
400 std::cerr <<
"WARNING: "<<message<<std::endl;
433 bool ignore_comments =
false, allow_exceptions =
false;
435 ignore_comments =
true;
437 allow_exceptions =
true;
440 js = nlohmann::json::parse( input,
nullptr, allow_exceptions, ignore_comments);
443 std::string message =
"An error occured while parsing \n";
444 std::cerr <<
"WARNING: "<<message<<std::endl;
450 Json::CharReaderBuilder parser;
452 Json::CharReaderBuilder::strictMode( &parser.settings_);
455 Json::CharReaderBuilder::strictMode( &parser.settings_);
456 parser.settings_[
"allowComments"] =
true;
457 parser.settings_[
"collectComments"] =
false;
460 Json::CharReaderBuilder::setDefaults( &parser.settings_);
463 std::stringstream ss(input);
464 if( !parseFromStream( parser, ss, &js, &errs) )
467 throw std::runtime_error( errs);
469 std::cerr <<
"WARNING: "<<errs<<std::endl;
488template<
class ContainerType>
492 return nlohmann::json(shared);
495 for(
const auto& value : shared)
505 std::vector<T> cc( shared);
JsonType string2Json(std::string input, enum comments comm=file::comments::are_discarded, enum error err=file::error::is_throw)
Convenience wrapper to parse a string into a JsonType.
Definition json_wrapper.h:429
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:347
dg::file::JsonType vec2json(const ContainerType &shared)
convert a vector to a json arrray
Definition json_wrapper.h:489
Json::Value JsonType
Json Type to use in dg::file functions and classes.
Definition json_wrapper.h:37
error
Switch between how to handle errors in a Json utitlity functions.
Definition json_wrapper.h:42
comments
Switch how comments are treated in a json string or file.
Definition json_wrapper.h:49
@ is_warning
Handle the error by writing a warning to std::cerr.
@ is_silent
Ignore the error and silently continue execution.
@ is_throw
throw an error (std::runtime_error)
@ 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.
Definition easy_atts.h:15
Wrapped Access to Json values with error handling.
Definition json_wrapper.h:121
std::string asString(std::string value="") const
Wrap the corresponding JsonType function with error handling.
Definition json_wrapper.h:258
WrappedJsonValue()
Default constructor By default the error mode is error::is_throw.
Definition json_wrapper.h:124
WrappedJsonValue(JsonType js)
By default the error mode is error::is_throw.
Definition json_wrapper.h:130
bool isMember(std::string key) const
Return true if key is a Member of the json value.
Definition json_wrapper.h:166
WrappedJsonValue operator[](unsigned idx) const
Wrap the corresponding JsonType function with error handling.
Definition json_wrapper.h:196
WrappedJsonValue get(unsigned idx, const JsonType &value) const
Wrap the corresponding JsonType function with error handling.
Definition json_wrapper.h:204
bool asBool(bool value=false) const
Wrap the corresponding JsonType function with error handling.
Definition json_wrapper.h:247
unsigned asUInt(unsigned value=0) const
Wrap the corresponding JsonType function with error handling.
Definition json_wrapper.h:225
void set_mode(error new_mode)
Change the error mode.
Definition json_wrapper.h:137
double asDouble(double value=0) const
Wrap the corresponding JsonType function with error handling.
Definition json_wrapper.h:214
WrappedJsonValue get(std::string key, const JsonType &value) const
Wrap the corresponding JsonType function with error handling.
Definition json_wrapper.h:190
std::string toStyledString() const
The stored json object as a formatted string.
Definition json_wrapper.h:157
WrappedJsonValue(JsonType js, error mode)
Construct with Json value and error mode.
Definition json_wrapper.h:134
WrappedJsonValue(error mode)
Construct with error mode.
Definition json_wrapper.h:127
std::string access_string() const
The creation history of the object.
Definition json_wrapper.h:150
WrappedJsonValue operator[](std::string key) const
Definition json_wrapper.h:177
const JsonType & asJson() const
Read access to the raw Json value.
Definition json_wrapper.h:141
JsonType & asJson()
Write access to the raw Json value (if you know what you are doing)
Definition json_wrapper.h:143
unsigned size() const
Wrap the corresponding JsonType function with error handling.
Definition json_wrapper.h:210
int asInt(int value=0) const
Wrap the corresponding JsonType function with error handling.
Definition json_wrapper.h:236