10#include <nlohmann/json.hpp>
161 return m_js.toStyledString();
168 return m_js.contains(key);
170 return m_js.isMember(key);
176 return m_js.is_string();
178 return m_js.isString();
187 return get( key, nlohmann::json::object(),
"empty object ");
189 return get( key, Json::ValueType::objectValue,
"empty object ");
199 std::stringstream default_str;
200 default_str <<
"value "<<value;
201 return get( key, value, default_str.str());
206 return get( idx, nlohmann::json::object(),
"empty array");
208 return get( idx, Json::ValueType::objectValue,
"empty array");
213 std::stringstream default_str;
214 default_str <<
"value "<<value;
215 return get( idx, value, default_str.str());
219 return (
unsigned)m_js.size();
224 if( m_js.is_number())
228 return m_js.asDouble();
230 return type_error<double>( value,
"a Double");
233 unsigned asUInt(
unsigned value = 0)
const{
235 if( m_js.is_number())
239 return m_js.asUInt();
241 return type_error<unsigned>( value,
"an Unsigned");
246 if( m_js.is_number())
252 return type_error<int>( value,
"an Int");
257 if( m_js.is_boolean())
261 return m_js.asBool();
263 return type_error<bool>( value,
"a Bool");
266 std::string
asString( std::string value =
"")
const{
268 if( m_js.is_string())
273 return m_js.asString();
275 return type_error<std::string>( value,
"a String");
281 std::string access = m_access_str +
"\""+key+
"\": ";
282 std::stringstream message;
284 if( !m_js.is_object( ) || !m_js.contains(key))
286 if( !m_js.isObject( ) || !m_js.isMember(key))
289 message <<
"*** Key error: "<<access<<
" not found.";
290 raise_error( message.str(), default_str);
297 std::string access = m_access_str +
"["+std::to_string(idx)+
"] ";
299 if( !m_js.is_array() || !(idx < m_js.size()))
301 if( !m_js.isArray() || !m_js.isValidIndex(idx))
304 std::stringstream message;
308 if( m_access_str.empty())
309 message <<
"*** Index error: Index "<<idx<<
" not present.";
311 message <<
"*** Index error: Index "<<idx<<
" not present in "<<m_access_str<<
".";
312 raise_error( message.str(), default_str);
318 T type_error( T value, std::string type)
const
320 std::stringstream message, default_str;
321 default_str <<
"value "<<value;
322 message <<
"*** Type error: "<<m_access_str<<
" "<<m_js<<
" is not "<<type<<
".";
323 raise_error( message.str(), default_str.str());
326 void raise_error( std::string message, std::string default_str)
const
329 throw std::runtime_error( message);
331 std::cerr <<
"WARNING "<< message<<
" Using default "<<default_str<<
"\n";
338 std::string m_access_str =
"";
359 std::ifstream isI( filename);
362 std::string message =
"\nAn error occured while parsing "+filename+
"\n";
363 message +=
"*** File does not exist! *** \n\n";
365 throw std::runtime_error( message);
367 std::cerr <<
"WARNING: "<<message<<std::endl;
375 bool ignore_comments =
false, allow_exceptions =
false;
377 ignore_comments =
true;
379 allow_exceptions =
true;
381 js = nlohmann::json::parse( isI,
nullptr, allow_exceptions, ignore_comments);
384 std::string message =
"An error occured while parsing "+filename+
"\n";
385 std::cerr <<
"WARNING: "<<message<<std::endl;
388 Json::CharReaderBuilder parser;
390 Json::CharReaderBuilder::strictMode( &parser.settings_);
393 Json::CharReaderBuilder::strictMode( &parser.settings_);
397 parser.settings_[
"allowComments"].swap( js_true);
398 parser.settings_[
"collectComments"].swap(js_false);
401 Json::CharReaderBuilder::setDefaults( &parser.settings_);
404 if( !parseFromStream( parser, isI, &js, &errs) )
406 std::string message =
"An error occured while parsing "+filename+
"\n"+errs;
408 throw std::runtime_error( message);
410 std::cerr <<
"WARNING: "<<message<<std::endl;
444 bool ignore_comments =
false, allow_exceptions =
false;
446 ignore_comments =
true;
448 allow_exceptions =
true;
451 js = nlohmann::json::parse( input,
nullptr, allow_exceptions, ignore_comments);
454 std::string message =
"An error occured while parsing \n";
455 std::cerr <<
"WARNING: "<<message<<std::endl;
461 Json::CharReaderBuilder parser;
463 Json::CharReaderBuilder::strictMode( &parser.settings_);
466 Json::CharReaderBuilder::strictMode( &parser.settings_);
467 parser.settings_[
"allowComments"] =
true;
468 parser.settings_[
"collectComments"] =
false;
471 Json::CharReaderBuilder::setDefaults( &parser.settings_);
474 std::stringstream ss(input);
475 if( !parseFromStream( parser, ss, &js, &errs) )
478 throw std::runtime_error( errs);
480 std::cerr <<
"WARNING: "<<errs<<std::endl;
500template<
class ContainerType>
504 return nlohmann::json(shared);
507 for(
const auto& value : shared)
517 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:440
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:356
dg::file::JsonType vec2json(const ContainerType &shared)
convert a vector to a json arrray
Definition json_wrapper.h:501
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:266
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 isString() const
Call corresponding JsonType function.
Definition json_wrapper.h:174
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:204
WrappedJsonValue get(unsigned idx, const JsonType &value) const
Wrap the corresponding JsonType function with error handling.
Definition json_wrapper.h:212
bool asBool(bool value=false) const
Wrap the corresponding JsonType function with error handling.
Definition json_wrapper.h:255
unsigned asUInt(unsigned value=0) const
Wrap the corresponding JsonType function with error handling.
Definition json_wrapper.h:233
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:222
WrappedJsonValue get(std::string key, const JsonType &value) const
Wrap the corresponding JsonType function with error handling.
Definition json_wrapper.h:198
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:185
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:218
int asInt(int value=0) const
Wrap the corresponding JsonType function with error handling.
Definition json_wrapper.h:244