9 *((json_spirit::mValue *) &json) = json_spirit::mValue(value);
14 *((json_spirit::mValue *) &json) = json_spirit::mValue(value);
19 *((json_spirit::mValue *) &json) = json_spirit::mValue(boost::uint64_t(value));
24 *((json_spirit::mValue *) &json) = json_spirit::mValue(boost::int64_t(value));
29 *((json_spirit::mValue *) &json) = json_spirit::mValue(boost::uint64_t(value));
37 else if(value == 1.0 / 0.0) {
40 else if(value == -1.0 / 0.0) {
44 *((json_spirit::mValue *) &json) = json_spirit::mValue(value);
51 *((json_spirit::mValue *) &json) = json_spirit::mValue(value);
56 *((json_spirit::mValue *) &json) = json_spirit::mValue(value);
67 return json.get_bool();
72 return json.get_int();
77 return (
unsigned int) json.get_int();
82 return (
long int) json.get_int64();
87 return (
unsigned long int) json.get_uint64();
98 std::string from_json<std::string>(
const jsonParser &json) {
99 return json.get_str();
109 value = json.get_bool();
113 value = json.get_int();
117 value = json.get_int();
121 value = json.get_int64();
125 value = json.get_uint64();
130 std::string str = json.get_str();
134 else if(str ==
"inf") {
137 else if(str ==
"-inf") {
141 throw std::runtime_error(
"Expected json real, received string other than 'nan', 'inf', or '-inf': '" + str +
"'");
145 value = json.get_real();
150 value = json.get_str();
166 return json_spirit::read_stream(stream, (json_spirit::mValue &) *
this);
170 boost::filesystem::ifstream stream(file_path);
175 if(!json.
read(stream)) {
176 std::cerr <<
"ERROR: Unable to successfully parse JSON file. File parsed as:\n"
178 <<
"\nPlease correct input file and try again. Exiting...\n";
186 json_spirit::write_stream((json_spirit::mValue &) *
this, stream, indent, prec,
187 json_spirit::pretty_print | json_spirit::single_line_arrays);
191 void jsonParser::write(
const std::string &file_name,
unsigned int indent,
unsigned int prec)
const {
192 std::ofstream file(file_name.c_str());
193 print(file, indent, prec);
200 boost::filesystem::ofstream file(file_path);
201 print(file, indent, prec);
212 if(type() != B.type()) {
229 auto B_it = B.
begin();
230 for(; A_it != A_end; ++A_it, ++B_it) {
231 if(A_it.name() != B_it.name() || !A_it->almost_equal(*B_it, tol)) {
242 bool res = (*
this == B);
252 return type() == json_spirit::null_type;
257 return type() == json_spirit::bool_type;
262 return type() == json_spirit::int_type;
267 return type() == json_spirit::real_type;
272 return type() == json_spirit::str_type;
277 return type() == json_spirit::obj_type;
282 return type() == json_spirit::array_type;
292 json_spirit::mObject &obj = get_obj();
293 json_spirit::mObject::iterator it = obj.find(name);
296 if(it == obj.end()) {
297 obj[name] = json_spirit::mValue(json_spirit::mObject());
306 const json_spirit::mObject &obj = get_obj();
307 json_spirit::mObject::const_iterator it = obj.find(name);
310 if(it == obj.end()) {
311 throw std::runtime_error(
"Const operator[] access, but " + name +
" does not exist");
331 if(!path.is_relative()) {
332 throw std::invalid_argument(
333 "Error in jsonParser::operator[](const fs::path &path): path must be relative");
336 for(
auto it = path.begin(); it != path.end(); ++it) {
338 int index = std::stoi(it->string());
339 if(curr->
size() > index) {
340 curr = &((*curr)[index]);
343 std::string msg =
"Error in jsonParser::at: attempted to access element outside of array range";
344 std::cerr <<
"path: " << path << std::endl;
345 std::cerr <<
"index: " << index << std::endl;
346 std::cerr <<
"curr->size(): " << curr->
size() << std::endl;
347 throw std::invalid_argument(msg);
351 auto res = curr->
find(it->string());
352 if(res != curr->
end()) {
353 curr = &((*curr)[it->string()]);
356 std::string msg =
"Error in jsonParser::at: key not found";
357 std::cerr <<
"path: " << path << std::endl;
358 std::cerr <<
"key: " << it->string() << std::endl;
359 throw std::invalid_argument(msg);
376 return (
const jsonParser &) get_array()[element];
383 while(A_it != A.
cend()) {
386 return find_diff(*A_it, *B_it, diff / A_it.name());
389 std::stringstream ss;
390 ss << std::distance(A.
cbegin(), A_it);
391 return find_diff(*A_it, *B_it, diff / ss.str());
405 while(A_it != A.
cend()) {
406 if(!A_it->almost_equal(*B_it, tol)) {
411 return find_diff(*A_it, *B_it, tol, diff / A_it.name());
417 std::stringstream ss;
418 ss << std::distance(A.
cbegin(), A_it);
419 return find_diff(*A_it, *B_it, tol, diff / ss.str());
432 return get_obj().size();
434 return get_array().size();
507 return get_obj().erase(name);
jsonParser & operator[](const std::string &name)
size_type size() const
Returns array size if *this is a JSON array, object size if *this is a JSON object, 1 otherwise.
bool is_int() const
Check if int type.
void from_json(ClexDescription &desc, const jsonParser &json)
iterator end()
Returns iterator to end of JSON object or JSON array.
boost::filesystem::path find_diff(const jsonParser &A, const jsonParser &B, boost::filesystem::path diff=boost::filesystem::path())
Return the location at which jsonParser 'A' != 'B' as a boost::filesystem::path.
void write(const std::string &file_name, unsigned int indent=2, unsigned int prec=12) const
Write json to file.
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
jsonParserIterator< true > const_iterator
jsonParser & at(const fs::path &path)
bool read(std::istream &stream)
Reads json from the stream.
iterator begin()
Returns const_iterator to beginning of JSON object or JSON array.
const_iterator cend() const
Returns const_iterator to end of JSON object or JSON array.
T get(Args...args) const
Get data from json, using one of several alternatives.
bool is_obj() const
Check if object type.
void print(std::ostream &stream, unsigned int indent=2, unsigned int prec=12) const
Print json to stream.
long int from_json< long int >(const jsonParser &json)
unsigned int from_json< unsigned int >(const jsonParser &json)
bool is_null() const
Check if null type.
size_type erase(const std::string &name)
Erase key:value pair from an object.
std::ostream & operator<<(std::ostream &_stream, const FormattedPrintable &_formatted)
json_spirit::mObject::size_type size_type
bool is_string() const
Check if string.
bool is_bool() const
Check if bool type.
std::istream & operator>>(std::istream &_in, std::vector< T > &vec)
int from_json< int >(const jsonParser &json)
iterator find(const std::string &name)
Return iterator to JSON object value with 'name'.
jsonParserIterator< false > iterator
double from_json< double >(const jsonParser &json)
bool is_number() const
Check if number type (not including int)
bool almost_equal(const jsonParser &B, double tol) const
bool contains(const std::string &name) const
Return true if JSON object contains 'name'.
bool is_array() const
Check if array type.
const_iterator cbegin() const
Returns const_iterator to beginning of JSON object or JSON array.
jsonParser from_json< jsonParser >(const jsonParser &json)
bool from_json< bool >(const jsonParser &json)
bool almost_equal(const GenericCluster< CoordType > &LHS, const GenericCluster< CoordType > &RHS, double tol)
unsigned long int from_json< unsigned long int >(const jsonParser &json)