1 #ifndef CASM_JSONPARSER_HH
2 #define CASM_JSONPARSER_HH
7 #include "casm/external/json_spirit/json_spirit_reader_template.h"
8 #include "casm/external/json_spirit/json_spirit_writer_template.h"
12 namespace filesystem {
21 template <
bool IsConst>
22 class jsonParserIterator;
94 jsonParser() : json_spirit::mValue(json_spirit::mObject()) {}
101 template <
typename T>
111 template <
typename T>
128 bool read(std::istream &stream);
131 bool read(
const boost::filesystem::path &mypath);
134 void print(std::ostream &stream,
unsigned int indent = 2,
135 unsigned int prec = 12)
const;
138 void write(
const std::string &file_name,
unsigned int indent = 2,
139 unsigned int prec = 12)
const;
142 void write(
const boost::filesystem::path &mypath,
unsigned int indent = 2,
143 unsigned int prec = 12)
const;
148 using json_spirit::mValue::set_force_column;
151 using json_spirit::mValue::set_force_row;
154 using json_spirit::mValue::set_scientific;
157 using json_spirit::mValue::set_remove_trailing_zeros;
160 using json_spirit::mValue::unset_force_column;
163 using json_spirit::mValue::unset_force_row;
166 using json_spirit::mValue::unset_scientific;
169 using json_spirit::mValue::unset_remove_trailing_zeros;
171 using json_spirit::mValue::operator==;
280 template <
typename T,
typename... Args>
281 T
get(Args &&... args)
const;
286 template <
typename T,
typename... Args>
287 void get(T &t, Args &&... args)
const;
291 template <
typename T,
typename... Args>
292 bool get_if(T &t,
const std::string &key, Args &&... args)
const;
296 template <
typename T,
typename... Args>
297 T
get_if_else(
const std::string &key,
const T &default_value,
298 Args &&... args)
const;
302 template <
typename T,
typename... Args>
303 bool get_else(T &t,
const std::string &key,
const T &default_value,
304 Args &&... args)
const;
307 template <
typename T,
typename... Args>
308 std::unique_ptr<T>
make(Args &&... args)
const;
311 template <
typename T,
typename... Args>
312 void make(std::unique_ptr<T> &ptr, Args &&... args)
const;
315 template <
typename T,
typename... Args>
316 bool make_if(std::unique_ptr<T> &ptr,
const std::string &key,
317 Args &&... args)
const;
320 template <
typename T,
typename... Args>
322 Args &&... args)
const;
325 template <
typename T,
typename... Args>
327 std::unique_ptr<T> default_value,
328 Args &&... args)
const;
331 template <
typename T,
typename... Args>
332 bool make_else(std::unique_ptr<T> &ptr,
const std::string &key,
333 std::unique_ptr<T> default_value, Args &&... args)
const;
340 template <
typename T>
345 template <
typename T,
typename... Args>
350 template <
typename T>
358 template <
typename Iterator>
368 template <
typename T>
372 template <
typename Iterator,
typename... Args,
383 std::stringstream ss;
397 return json = json_spirit::mValue(json_spirit::mObject());
402 template <
typename Iterator>
411 return json = json_spirit::mValue(json_spirit::mArray());
417 return json = json_spirit::mValue(json_spirit::mArray(N));
421 template <
typename T>
428 template <
typename Iterator>
439 return json = json_spirit::mValue();
444 this->json_spirit::mValue::operator=(value);
449 std::ostream &
operator<<(std::ostream &stream,
const jsonParser &json);
450 std::istream &
operator>>(std::istream &stream, jsonParser &json);
453 jsonParser &
to_json(
bool value, jsonParser &json);
454 jsonParser &
to_json(
int value, jsonParser &json);
455 jsonParser &
to_json(
unsigned int value, jsonParser &json);
456 jsonParser &
to_json(
long int value, jsonParser &json);
457 jsonParser &
to_json(
unsigned long int value, jsonParser &json);
458 jsonParser &
to_json(
double value, jsonParser &json);
459 jsonParser &
to_json(
const std::string &value, jsonParser &json);
460 jsonParser &
to_json(
const char *value, jsonParser &json);
461 jsonParser &
to_json(
const jsonParser &value, jsonParser &json);
464 template <
typename T>
468 template <
typename T>
484 std::string from_json<std::string>(
const jsonParser &json);
489 void from_json(
bool &value,
const jsonParser &json);
490 void from_json(
int &value,
const jsonParser &json);
491 void from_json(
unsigned int &value,
const jsonParser &json);
492 void from_json(
long int &value,
const jsonParser &json);
493 void from_json(
unsigned long int &value,
const jsonParser &json);
494 void from_json(
double &value,
const jsonParser &json);
495 void from_json(std::string &value,
const jsonParser &json);
496 void from_json(jsonParser &value,
const jsonParser &json);
502 if (!json.
read(stream)) {
503 throw std::runtime_error(
504 std::string(
"ERROR: Could not read JSON. Please check your formatting. "
505 "For instance, try http://www.jsoneditoronline.org."));
510 void to_json(fs::path file_path, jsonParser &json);
513 template <
typename T>
516 json[
"real"] = value.real();
517 json[
"imag"] = value.imag();
522 template <
typename T>
525 value = std::complex<T>(json[
"real"].get<T>(), json[
"imag"].get<T>());
533 template <
typename Key,
typename T>
534 jsonParser &
to_json(
const std::pair<Key, T> &value, jsonParser &json);
537 template <
typename Key,
typename T>
538 void from_json(std::pair<Key, T> &value,
const jsonParser &json);
547 template <
typename ReturnType>
552 return CASM::from_json<ReturnType>(json);
563 template <
typename ValueType>
568 return CASM::make_from_json<ValueType>(json);
573 template <
typename T>
581 template <
typename T>
583 return std::unique_ptr<T>(
new T(from_json<T>(json)));
593 template <
typename T,
typename... Args>
601 fs::path
find_diff(
const jsonParser &A,
const jsonParser &B);
605 fs::path
find_diff(
const jsonParser &A,
const jsonParser &B,
double tol);
611 template <
bool IsConst>
616 typename std::conditional<IsConst, json_spirit::mObject::const_iterator,
617 json_spirit::mObject::iterator>
::type
620 typename std::conditional<IsConst, json_spirit::mArray::const_iterator,
621 json_spirit::mArray::iterator>
::type
624 typedef typename std::conditional<IsConst, const jsonParser, jsonParser>::type
663 std::string
name()
const;
665 template <
bool _IsConst>
683 template <
typename T,
typename... Args>
715 template <
typename T,
typename... Args>
727 template <
typename T,
typename... Args>
729 from_json(t, *
this, std::forward<Args>(args)...);
739 template <
typename T,
typename... Args>
743 it->get(t, std::forward<Args>(args)...);
756 template <
typename T,
typename... Args>
758 Args &&... args)
const {
761 return it->get<T>(std::forward<Args>(args)...);
763 return default_value;
774 template <
typename T,
typename... Args>
776 Args &&... args)
const {
779 it->get(t, std::forward<Args>(args)...);
805 template <
typename T,
typename... Args>
817 template <
typename T,
typename... Args>
829 template <
typename T,
typename... Args>
831 Args &&... args)
const {
834 it->make(ptr, std::forward<Args>(args)...);
847 template <
typename T,
typename... Args>
849 Args &&... args)
const {
852 return it->make(std::forward<Args>(args)...);
854 return std::unique_ptr<T>();
864 template <
typename T,
typename... Args>
866 std::unique_ptr<T> default_value,
867 Args &&... args)
const {
870 return it->make(std::forward<Args>(args)...);
872 return std::move(default_value);
882 template <
typename T,
typename... Args>
884 std::unique_ptr<T> default_value,
885 Args &&... args)
const {
888 it->make(ptr, std::forward<Args>(args)...);
892 ptr = std::move(default_value);
898 template <
typename T>
905 template <
typename Iterator>
908 for (
auto it =
begin; it !=
end; ++it) {
909 to_json(it->second, (*
this)[it->first]);
915 template <
typename T>
918 for (
auto i = 0; i < N; ++i) {
925 template <
typename Iterator,
typename... Args,
930 for (
auto it =
begin; it !=
end; ++it) {
931 push_back(*it, std::forward<Args>(args)...);
938 template <
typename T>
944 template <
typename Key,
typename T>
947 return json[value.first] = value.second;
951 template <
typename Key,
typename T>
953 auto it = json.
begin();
954 value = std::make_pair<Key, T>(it.name(), *it);
958 template <
typename T>
bool is_string() const
Check if string.
jsonParser()
Create a new empty jsonParser.
static jsonParser object()
Returns an empty json object.
iterator begin()
Returns const_iterator to beginning of JSON object or JSON array.
bool contains(const std::string &name) const
Return true if JSON object contains 'name'.
iterator end()
Returns iterator to end of JSON object or JSON array.
bool is_array() const
Check if array type.
static jsonParser array()
Returns an empty json array.
jsonParser & put_array(size_type N)
Puts new JSON array.
jsonParserIterator< false > iterator
json_spirit::mObject::size_type size_type
static jsonParser array(size_type N)
Returns an empty json array.
static jsonParser array(Iterator begin, Iterator end, typename CASM_TMP::enable_if_iterator< Iterator >::type *=nullptr)
Puts new JSON array, from iterators.
bool read(std::istream &stream)
Reads json from the stream.
jsonParser & operator=(const json_spirit::mValue &value)
const_iterator cend() const
Returns const_iterator to end of JSON object or JSON array.
jsonParser & put_obj()
Puts new empty JSON object.
bool read(const boost::filesystem::path &mypath)
Reads json from a path.
jsonParser & at(const fs::path &path)
static jsonParser array(size_type N, const T &t)
Puts new JSON array, using the same value.
iterator find(const std::string &name)
Return iterator to JSON object value with 'name'.
jsonParser & operator[](const std::string &name)
void write(const std::string &file_name, unsigned int indent=2, unsigned int prec=12) const
Write json to file.
bool is_float() const
Check if number type (not including int)
jsonParser & put_array()
Puts new empty JSON array.
jsonParserIterator< true > const_iterator
bool is_int() const
Check if int type.
void write(const boost::filesystem::path &mypath, unsigned int indent=2, unsigned int prec=12) const
Write json to file.
bool is_null() const
Check if null type.
bool is_number() const
Check if number type (including int)
jsonParser & put_null()
Puts 'null' JSON value.
bool is_bool() const
Check if bool type.
size_type erase(const std::string &name)
Erase key:value pair from an object.
static jsonParser parse(const std::string &str)
Construct a jsonParser from a string containing JSON data.
const_iterator cbegin() const
Returns const_iterator to beginning of JSON object or JSON array.
jsonParser::iterator find_at(const fs::path &path)
Return iterator to sub-object or element, or 'end' if not found.
static jsonParser parse(std::istream &stream)
Construct a jsonParser from a stream containing JSON data.
static jsonParser object(Iterator begin, Iterator end)
bool almost_equal(const jsonParser &B, double tol) const
bool is_obj() const
Check if object type.
bool operator!=(const jsonParser &json) const
void print(std::ostream &stream, unsigned int indent=2, unsigned int prec=12) const
Print json to stream.
friend void swap(jsonParserIterator< _IsConst > &a, jsonParserIterator< _IsConst > &b)
jsonParserIterator(pointer j, const object_iterator &iter)
std::conditional< IsConst, json_spirit::mObject::const_iterator, json_spirit::mObject::iterator >::type object_iterator
pointer operator->() const
array_iterator array_iter
jsonParserIterator(pointer j, const int &iter)
jsonParserIterator & operator--()
jsonParserIterator & operator++()
std::conditional< IsConst, json_spirit::mArray::const_iterator, json_spirit::mArray::iterator >::type array_iterator
std::forward_iterator_tag iterator_category
json_spirit::Value_type type
std::conditional< IsConst, const jsonParser, jsonParser >::type value_type
bool operator!=(const jsonParserIterator &iter) const
jsonParserIterator(pointer j, const array_iterator &iter)
bool operator==(const jsonParserIterator &iter) const
reference operator*() const
jsonParserIterator & operator=(jsonParserIterator iter)
std::unique_ptr< T > make_if_else(const std::string &key, std::unique_ptr< T > default_value, Args &&... args) const
Get data from json if 'this' contains 'key', else return 'default_value'.
double from_json< double >(const jsonParser &json)
bool make_if(std::unique_ptr< T > &ptr, const std::string &key, Args &&... args) const
Get data from json if key exists.
jsonParser json_pair(const std::string &key, const T &value)
Create pair/value json object without intermediate temporary.
std::unique_ptr< T > make_from_json(const jsonParser &json)
Make from JSON for basic types.
bool get_if(T &t, const std::string &key, Args &&... args) const
jsonParser & put(const T &value)
bool get_else(T &t, const std::string &key, const T &default_value, Args &&... args) const
bool make_else(std::unique_ptr< T > &ptr, const std::string &key, std::unique_ptr< T > default_value, Args &&... args) const
Get data from json if key exists, else assign default_value.
fs::path find_diff(const jsonParser &A, const jsonParser &B)
Return the location at which jsonParser 'A' != 'B' as a fs::path.
bool from_json< bool >(const jsonParser &json)
T get_if_else(const std::string &key, const T &default_value, Args &&... args) const
std::unique_ptr< T > make_optional(const std::string &key, Args &&... args) const
Get data from json if key exists, else return empty ptr.
unsigned int from_json< unsigned int >(const jsonParser &json)
jsonParser & push_back(const T &value, Args &&... args)
jsonParser from_json< jsonParser >(const jsonParser &json)
int from_json< int >(const jsonParser &json)
jsonParser & operator=(const T &value)
unsigned long int from_json< unsigned long int >(const jsonParser &json)
std::unique_ptr< T > make(Args &&... args) const
Get data from json.
long int from_json< long int >(const jsonParser &json)
T get(Args &&... args) const
Get data from json, using one of several alternatives.
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::ostream & operator<<(std::ostream &_stream, const FormattedPrintable &_formatted)
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
std::istream & operator>>(std::istream &_in, std::vector< T > &vec)
GenericDatumFormatter< std::string, DataObject > name()
void from_json(ClexDescription &desc, const jsonParser &json)
bool operator==(DoFSet const &A, DoFSet const &B)
Helper struct for constructing objects that need additional data.
static ReturnType from_json(const jsonParser &json)
Default from_json is equivalent to.
Helper struct for constructing objects that need additional data.
static std::unique_ptr< ValueType > make_from_json(const jsonParser &json)
Default make_from_json is equivalent to.