1 #ifndef CASM_JSONPARSER_HH
2 #define CASM_JSONPARSER_HH
4 #include "casm/external/json_spirit/json_spirit_reader_template.h"
5 #include "casm/external/json_spirit/json_spirit_writer_template.h"
16 template<
bool IsConst>
29 class jsonParser :
public json_spirit::mValue {
90 json_spirit::mValue(json_spirit::mObject()) {}
112 bool read(std::istream &stream);
119 void print(std::ostream &stream,
unsigned int indent = 2,
unsigned int prec = 12)
const;
122 void write(
const std::string &file_name,
unsigned int indent = 2,
unsigned int prec = 12)
const;
131 using json_spirit::mValue::set_force_column;
134 using json_spirit::mValue::set_force_row;
137 using json_spirit::mValue::set_scientific;
140 using json_spirit::mValue::set_remove_trailing_zeros;
143 using json_spirit::mValue::unset_force_column;
146 using json_spirit::mValue::unset_force_row;
149 using json_spirit::mValue::unset_scientific;
152 using json_spirit::mValue::unset_remove_trailing_zeros;
155 using json_spirit::mValue::operator==;
206 size_type
size()
const;
212 const_iterator
begin()
const;
218 const_iterator
end()
const;
221 const_iterator
cbegin()
const;
224 const_iterator
cend()
const;
228 iterator
find(
const std::string &name);
231 const_iterator
find(
const std::string &name)
const;
234 bool contains(
const std::string &name)
const;
237 size_type
erase(
const std::string &name);
242 template<
typename T,
typename...Args>
243 T
get(
Args... args)
const;
247 template<
typename T,
typename...Args>
248 void get(T &t,
Args... args)
const;
252 template<
typename T,
typename...Args>
253 bool get_if(T &t,
const std::string &key,
Args... args)
const;
257 template<
typename T,
typename...Args>
258 bool get_else(T &t,
const std::string &key,
const T &default_value,
Args... args)
const;
281 template<
typename Iterator>
286 return *
this =
array();
291 return *
this =
array(N);
299 template<
typename Iterator>
306 return *
this =
null();
313 std::stringstream ss;
331 return json = json_spirit::mValue(json_spirit::mObject());
335 template<
typename Iterator>
338 return json.
put_obj(begin, end);
344 return json = json_spirit::mValue(json_spirit::mArray());
350 return json = json_spirit::mValue(json_spirit::mArray(N));
361 template<
typename Iterator>
373 return json = json_spirit::mValue();
379 this->json_spirit::mValue::operator=(value);
386 std::ostream &
operator<<(std::ostream &stream,
const jsonParser &json);
387 std::istream &
operator>>(std::istream &stream, jsonParser &json);
390 jsonParser &
to_json(
bool value, jsonParser &json);
391 jsonParser &
to_json(
int value, jsonParser &json);
392 jsonParser &
to_json(
unsigned int value, jsonParser &json);
393 jsonParser &
to_json(
long int value, jsonParser &json);
394 jsonParser &
to_json(
unsigned long int value, jsonParser &json);
395 jsonParser &
to_json(
double value, jsonParser &json);
396 jsonParser &
to_json(
const std::string &value, jsonParser &json);
397 jsonParser &
to_json(
const char *value, jsonParser &json);
398 jsonParser &
to_json(
const jsonParser &value, jsonParser &json);
412 template<> std::string from_json<std::string>(
const jsonParser &json);
416 void from_json(
bool &value,
const jsonParser &json);
417 void from_json(
int &value,
const jsonParser &json);
418 void from_json(
unsigned int &value,
const jsonParser &json);
419 void from_json(
long int &value,
const jsonParser &json);
420 void from_json(
unsigned long int &value,
const jsonParser &json);
421 void from_json(
double &value,
const jsonParser &json);
422 void from_json(std::string &value,
const jsonParser &json);
423 void from_json(jsonParser &value,
const jsonParser &json);
424 void from_json(std::istream &stream,
const jsonParser &json);
429 if(!json.
read(stream)) {
430 throw std::runtime_error(
431 std::string(
"ERROR: Could not read JSON. Please check your formatting. "
432 "For instance, try http://www.jsoneditoronline.org."));
441 if(!json.
read(file_path)) {
442 throw std::runtime_error(
443 std::string(
"ERROR: Could not read JSON file: '") + file_path.string() +
444 "'.\n\nPlease check your formatting. For instance, try http://www.jsoneditoronline.org.");
452 json[
"real"] = value.real();
453 json[
"imag"] = value.imag();
461 value = std::complex<T>(json[
"real"].
get<T>(), json[
"imag"].get<T>());
471 template<
typename Key,
typename T>
472 jsonParser &
to_json(
const std::pair<Key, T> &value, jsonParser &json);
475 template<
typename Key,
typename T>
476 void from_json(std::pair<Key, T> &value,
const jsonParser &json);
485 template<
typename ReturnType>
490 return CASM::from_json<ReturnType>(json);
514 template<
bool IsConst>
515 class jsonParserIterator {
520 typedef typename std::conditional<IsConst, json_spirit::mObject::const_iterator, json_spirit::mObject::iterator>::type
object_iterator;
521 typedef typename std::conditional<IsConst, json_spirit::mArray::const_iterator, json_spirit::mArray::iterator>::type
array_iterator;
523 typedef typename std::conditional<IsConst, const jsonParser, jsonParser>::type
value_type;
552 if(
type == json_spirit::obj_type)
553 return (reference)
obj_iter->second;
554 else if(
type == json_spirit::array_type)
561 if(
type == json_spirit::obj_type)
563 else if(
type == json_spirit::array_type)
564 return (pointer) & (*array_iter);
574 if(
type == json_spirit::obj_type)
576 else if(
type == json_spirit::array_type)
583 return !(*
this == iter);
587 if(
type == json_spirit::obj_type) {
591 else if(
type == json_spirit::array_type) {
605 if(
type == json_spirit::obj_type) {
609 else if(
type == json_spirit::array_type) {
620 if(
type == json_spirit::obj_type) {
624 else if(
type == json_spirit::array_type) {
638 if(
type == json_spirit::obj_type) {
642 else if(
type == json_spirit::array_type) {
653 if(
type == json_spirit::obj_type)
655 else if(
type == json_spirit::array_type)
663 if(
type == json_spirit::obj_type)
666 throw std::runtime_error(
"Calling 'name' on non-object jsonParserIterator");
673 swap(a.type, b.type);
674 swap(a.obj_iter, b.obj_iter);
675 swap(a.array_iter, b.array_iter);
676 swap(a.val_iter, b.val_iter);
728 template<
typename T,
typename...Args>
733 template<
typename T,
typename...Args>
739 template<
typename T,
typename...Args>
748 template<
typename T,
typename...Args>
760 template <
typename T>
766 template<
typename Iterator>
770 for(
auto it = begin; it !=
end; ++it) {
771 to_json(it->second, (*
this)[it->first]);
780 for(
auto i = 0; i < N; ++i) {
787 template<
typename Iterator>
794 for(
auto it = begin; it !=
end; ++it) {
801 template <
typename T>
807 template<
typename Key,
typename T>
810 return json[value.first] = value.second;
814 template<
typename Key,
typename T>
816 auto it = json.
begin();
817 value = std::make_pair<Key, T>(it.name(), *it);
jsonParser & operator[](const std::string &name)
static jsonParser null()
Returns a null JSON value.
bool operator==(const jsonParserIterator &iter)
size_type size() const
Returns array size if *this is a JSON array, object size if *this is a JSON object, 1 otherwise.
jsonParser()
Create a new empty jsonParser.
bool is_int() const
Check if int type.
std::conditional< IsConst, json_spirit::mArray::const_iterator, json_spirit::mArray::iterator >::type array_iterator
jsonParserIterator operator--(int)
array_iterator array_iter
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.
jsonParserIterator & operator=(jsonParserIterator iter)
void write(const std::string &file_name, unsigned int indent=2, unsigned int prec=12) const
Write json to file.
bool get_if(T &t, const std::string &key, Args...args) const
std::conditional< IsConst, json_spirit::mObject::const_iterator, json_spirit::mObject::iterator >::type object_iterator
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
jsonParser & operator=(const json_spirit::mValue &value)
jsonParserIterator< true > const_iterator
jsonParser & at(const fs::path &path)
bool read(std::istream &stream)
Reads json from the stream.
jsonParserIterator(pointer j, const int &iter)
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.
static jsonParser parse(std::istream &stream)
Construct a jsonParser from a stream containing JSON data.
T get(Args...args) const
Get data from json, using one of several alternatives.
jsonParser & operator=(const T &value)
Puts data of any type T for which 'jsonParser& to_json( const T &value, jsonParser &json)' is defined...
static jsonParser object(Iterator begin, Iterator end)
Puts new JSON object, from iterators over a range of values of type std::pair
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.
jsonParserIterator(pointer j, const array_iterator &iter)
void swap(ConfigDoF &A, ConfigDoF &B)
jsonParserIterator(const jsonParserIterator &iter)
jsonParser & put_null()
Puts 'null' JSON value.
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)
bool operator==(const UnitCellCoord &A, const UnitCellCoord &B)
Compare UnitCellCoord.
json_spirit::mObject::size_type size_type
bool is_string() const
Check if string.
json_spirit::Value_type type
bool operator!=(const jsonParserIterator &iter)
bool get_else(T &t, const std::string &key, const T &default_value, Args...args) const
friend void swap(jsonParserIterator &a, jsonParserIterator &b)
static ReturnType from_json(const jsonParser &json)
Default from_json is equivalent to.
bool is_bool() const
Check if bool type.
jsonParserIterator & operator--()
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'.
Helper struct for constructing objects that need additional data.
static jsonParser array(Iterator begin, Iterator end, typename CASM_TMP::enable_if_iterator< Iterator >::type *=nullptr)
Puts new JSON array, from iterators.
jsonParser & put_obj()
Puts new empty JSON object.
std::forward_iterator_tag iterator_category
jsonParserIterator< false > iterator
static jsonParser parse(const std::string &str)
Construct a jsonParser from a string containing JSON data.
jsonParser & put(const T &value)
Puts data of any type T for which 'jsonParser& to_json( const T &value, jsonParser &json)' is defined (same as 'o...
double from_json< double >(const jsonParser &json)
bool is_number() const
Check if number type (not including int)
static jsonParser array(size_type N)
Returns an empty json array.
bool almost_equal(const jsonParser &B, double tol) const
jsonParser & push_back(const T &value)
Puts new valued element at end of array of any type T for which 'jsonParser& to_json( const T &value...
bool contains(const std::string &name) const
Return true if JSON object contains 'name'.
jsonParserIterator(pointer j, const object_iterator &iter)
std::enable_if< is_iterator< T >::type::value, void > enable_if_iterator
Template alias to enable a template function via SFINAE for any iterator.
std::string name()
When iterating over a JSON object, returns the 'name' of the 'name':value pair the iterator is pointi...
static jsonParser parse(const fs::path &path)
Construct a jsonParser from a file containing JSON data.
jsonParser & put_array(size_type N)
Puts new JSON array.
bool is_array() const
Check if array type.
jsonParserIterator & operator++()
static jsonParser object()
Returns an empty json object.
const_iterator cbegin() const
Returns const_iterator to beginning of JSON object or JSON array.
jsonParser from_json< jsonParser >(const jsonParser &json)
bool operator!=(const jsonParser &json) const
bool from_json< bool >(const jsonParser &json)
jsonParser & put_array()
Puts new empty JSON array.
static jsonParser array()
Returns an empty json array.
static jsonParser array(size_type N, const T &t)
Puts new JSON array, using the same value.
std::conditional< IsConst, const jsonParser, jsonParser >::type value_type
unsigned long int from_json< unsigned long int >(const jsonParser &json)
jsonParserIterator operator++(int)