CASM  1.1.0
A Clusters Approach to Statistical Mechanics
CASM::jsonParser Class Reference

#include <jsonParser.hh>

+ Inheritance diagram for CASM::jsonParser:

Detailed Description

jsonParser allows for reading / writing JSON data

JSON consists of values, which can be of type: object, array, float, int, str, null, bool object: map/dict of <string, JSON value> array: vector/list of JSON values

value[string] -> value iff object value[int] -> value iff array value.get<type>() -> type must be correct type

Assumes functions exist for each type T to read/write: jsonParser& to_json( const T &value, jsonParser &json) void from_json( T &value, const jsonParser &json);

These functions exist for basic types (bool, int, double, std::string), and the containers: Eigen::MatrixXd, CASM::Array, CASM::Matrix, CASM::Vector, So if they also exist for the type being contained, you can read/write the entire object as a JSON array or nested JSON arrays

Simple Usage:

Reading data from a JSON file:

jsonParser json("myfile.json");

Three ways to get data of type T:

T t; from_json(t, json["path"]["to"]["object"]["data"]); <– use [std::string] for JSON values of any type t = json["path"]["to"]["object"]["data"].get<T>(); t = from_json<T>(json["path"]["to"]["array"][0]); <– use [int] for JSON arrays only

Check if 'name' in JSON object:

if( json.contains("other_data") ) from_json( array, json["other_data"]);

Writing data to a JSON file:

jsonParser json; json["mydata"].put(mydata); <– if ["mydata"] does not exist, it is created, else overwrites json["more_data"].put(more_data);

ofstream file("myfile.json"); json.write(file); file.close();

Definition at line 85 of file jsonParser.hh.

Classes

struct  as_array
 
struct  as_flattest
 

Public Types

typedef json_spirit::mObject::size_type size_type
 
typedef jsonParserIterator< false > iterator
 
typedef jsonParserIterator< true > const_iterator
 

Public Member Functions

 jsonParser ()
 Create a new empty jsonParser. More...
 
template<typename T >
 jsonParser (T &t)
 
template<typename T >
 jsonParser (const T &t)
 
bool read (std::istream &stream)
 Reads json from the stream. More...
 
bool read (const boost::filesystem::path &mypath)
 Reads json from a path. More...
 
void print (std::ostream &stream, unsigned int indent=2, unsigned int prec=12) const
 Print json to stream. More...
 
void write (const std::string &file_name, unsigned int indent=2, unsigned int prec=12) const
 Write json to file. More...
 
void write (const boost::filesystem::path &mypath, unsigned int indent=2, unsigned int prec=12) const
 Write json to file. More...
 
bool operator!= (const jsonParser &json) const
 
bool almost_equal (const jsonParser &B, double tol) const
 
bool is_null () const
 Check if null type. More...
 
bool is_bool () const
 Check if bool type. More...
 
bool is_int () const
 Check if int type. More...
 
bool is_float () const
 Check if number type (not including int) More...
 
bool is_number () const
 Check if number type (including int) More...
 
bool is_string () const
 Check if string. More...
 
bool is_obj () const
 Check if object type. More...
 
bool is_array () const
 Check if array type. More...
 
jsonParseroperator[] (const std::string &name)
 
const jsonParseroperator[] (const std::string &name) const
 
jsonParserat (const fs::path &path)
 
const jsonParserat (const fs::path &path) const
 
jsonParseroperator[] (const size_type &element)
 
const jsonParseroperator[] (const size_type &element) const
 
jsonParserat (const size_type &element)
 
const jsonParserat (const size_type &element) const
 
size_type size () const
 
iterator begin ()
 Returns const_iterator to beginning of JSON object or JSON array. More...
 
const_iterator begin () const
 Returns iterator to beginning of JSON object or JSON array. More...
 
iterator end ()
 Returns iterator to end of JSON object or JSON array. More...
 
const_iterator end () const
 Returns const_iterator to end of JSON object or JSON array. More...
 
const_iterator cbegin () const
 Returns const_iterator to beginning of JSON object or JSON array. More...
 
const_iterator cend () const
 Returns const_iterator to end of JSON object or JSON array. More...
 
iterator find (const std::string &name)
 Return iterator to JSON object value with 'name'. More...
 
const_iterator find (const std::string &name) const
 Return const_iterator to JSON object value with 'name'. More...
 
jsonParser::iterator find_at (const fs::path &path)
 Return iterator to sub-object or element, or 'end' if not found. More...
 
jsonParser::const_iterator find_at (const fs::path &path) const
 Return iterator to sub-object or element, or 'end' if not found. More...
 
bool contains (const std::string &name) const
 Return true if JSON object contains 'name'. More...
 
size_type erase (const std::string &name)
 Erase key:value pair from an object. More...
 
template<typename T , typename... Args>
get (Args &&... args) const
 Get data from json, using one of several alternatives. More...
 
template<typename T , typename... Args>
void get (T &t, Args &&... args) const
 
template<typename T , typename... Args>
bool get_if (T &t, const std::string &key, Args &&... args) const
 
template<typename T , typename... Args>
get_if_else (const std::string &key, const T &default_value, Args &&... args) const
 
template<typename T , typename... Args>
bool get_else (T &t, const std::string &key, const T &default_value, Args &&... args) const
 
template<typename T , typename... Args>
std::unique_ptr< T > make (Args &&... args) const
 Get data from json. More...
 
template<typename T , typename... Args>
void make (std::unique_ptr< T > &ptr, Args &&... args) const
 Get data from json. More...
 
template<typename T , typename... Args>
bool make_if (std::unique_ptr< T > &ptr, const std::string &key, Args &&... args) const
 Get data from json if key exists. More...
 
template<typename T , typename... Args>
std::unique_ptr< T > make_optional (const std::string &key, Args &&... args) const
 Get data from json if key exists, else return empty ptr. More...
 
template<typename T , typename... Args>
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'. More...
 
template<typename T , typename... Args>
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. More...
 
template<typename T >
jsonParseroperator= (const T &value)
 
template<typename T , typename... Args>
jsonParserpush_back (const T &value, Args &&... args)
 
template<typename T >
jsonParserput (const T &value)
 
jsonParserput_obj ()
 Puts new empty JSON object. More...
 
template<typename Iterator >
jsonParserput_obj (Iterator begin, Iterator end)
 
jsonParserput_array ()
 Puts new empty JSON array. More...
 
jsonParserput_array (size_type N)
 Puts new JSON array. More...
 
template<typename T >
jsonParserput_array (size_type N, const T &t)
 Puts new JSON array, using the same value. More...
 
template<typename Iterator , typename... Args, typename CASM_TMP::enable_if_iterator< Iterator >::type * = nullptr>
jsonParserput_array (Iterator begin, Iterator end, Args &&... args)
 Puts new JSON array, from iterators. More...
 
jsonParserput_null ()
 Puts 'null' JSON value. More...
 

Static Public Member Functions

static jsonParser parse (const std::string &str)
 Construct a jsonParser from a string containing JSON data. More...
 
static jsonParser parse (const fs::path &path)
 Construct a jsonParser from a file containing JSON data. More...
 
static jsonParser parse (std::istream &stream)
 Construct a jsonParser from a stream containing JSON data. More...
 
static jsonParser object ()
 Returns an empty json object. More...
 
template<typename Iterator >
static jsonParser object (Iterator begin, Iterator end)
 
static jsonParser array ()
 Returns an empty json array. More...
 
static jsonParser array (size_type N)
 Returns an empty json array. More...
 
template<typename T >
static jsonParser array (size_type N, const T &t)
 Puts new JSON array, using the same value. More...
 
template<typename Iterator >
static jsonParser array (Iterator begin, Iterator end, typename CASM_TMP::enable_if_iterator< Iterator >::type *=nullptr)
 Puts new JSON array, from iterators. More...
 
static jsonParser null ()
 Returns a null JSON value. More...
 

Private Member Functions

jsonParseroperator= (const json_spirit::mValue &value)
 

Member Typedef Documentation

◆ const_iterator

Definition at line 89 of file jsonParser.hh.

◆ iterator

Definition at line 88 of file jsonParser.hh.

◆ size_type

typedef json_spirit::mObject::size_type CASM::jsonParser::size_type

Definition at line 87 of file jsonParser.hh.

Constructor & Destructor Documentation

◆ jsonParser() [1/3]

CASM::jsonParser::jsonParser ( )
inline

Create a new empty jsonParser.

Definition at line 94 of file jsonParser.hh.

◆ jsonParser() [2/3]

template<typename T >
CASM::jsonParser::jsonParser ( T &  t)
inlineexplicit

Create a jsonParser from any other object for which 'to_json(t, json)' is defined

To parse a std::string as JSON, rather than store it verbatim, use jsonParser::parse

Definition at line 102 of file jsonParser.hh.

◆ jsonParser() [3/3]

template<typename T >
CASM::jsonParser::jsonParser ( const T &  t)
inlineexplicit

Create a jsonParser from any other object for which 'to_json(t, json)' is defined

To parse a std::string as JSON, rather than store it verbatim, use jsonParser::parse

Definition at line 112 of file jsonParser.hh.

Member Function Documentation

◆ almost_equal()

bool CASM::jsonParser::almost_equal ( const jsonParser B,
double  tol 
) const

Definition at line 218 of file jsonParser.cc.

◆ array() [1/4]

static jsonParser CASM::jsonParser::array ( )
inlinestatic

Returns an empty json array.

Definition at line 409 of file jsonParser.hh.

◆ array() [2/4]

template<typename Iterator >
static jsonParser CASM::jsonParser::array ( Iterator  begin,
Iterator  end,
typename CASM_TMP::enable_if_iterator< Iterator >::type *  = nullptr 
)
inlinestatic

Puts new JSON array, from iterators.

Definition at line 429 of file jsonParser.hh.

◆ array() [3/4]

static jsonParser CASM::jsonParser::array ( size_type  N)
inlinestatic

Returns an empty json array.

Definition at line 415 of file jsonParser.hh.

◆ array() [4/4]

template<typename T >
static jsonParser CASM::jsonParser::array ( size_type  N,
const T &  t 
)
inlinestatic

Puts new JSON array, using the same value.

Definition at line 422 of file jsonParser.hh.

◆ at() [1/4]

jsonParser & CASM::jsonParser::at ( const fs::path &  path)

Return a reference to the sub-jsonParser (JSON value) with specified relative path Will throw if the 'path' doesn't exist.

  • If 'path' is 'A/B/C', then json.at(path) is equivalent to json[A][B][C]
  • If any sub-jsonParser is an array, it will attempt to convert the filename to int

Return a reference to the sub-jsonParser (JSON value) with specified relative path Will throw if the 'path' doesn't exist.

  • If 'path' is 'A/B/C', then json[path] is equivalent to json[A][B][C]
  • If any sub-jsonParser is an array, it will attempt to convert the filename to int

Definition at line 316 of file jsonParser.cc.

◆ at() [2/4]

const jsonParser & CASM::jsonParser::at ( const fs::path &  path) const

Return a reference to the sub-jsonParser (JSON value) with specified relative path Will throw if the 'path' doesn't exist.

  • If 'path' is 'A/B/C', then json.at(path) is equivalent to json[A][B][C]
  • If any sub-jsonParser is an array, it will attempt to convert the filename to int

Return a reference to the sub-jsonParser (JSON value) with specified relative path Will throw if the 'path' doesn't exist.

  • If 'path' is 'A/B/C', then json[path] is equivalent to json[A][B][C]
  • If any sub-jsonParser is an array, it will attempt to convert the filename to int

Definition at line 328 of file jsonParser.cc.

◆ at() [3/4]

jsonParser & CASM::jsonParser::at ( const size_type element)

Return a reference to the sub-jsonParser (JSON value) from index 'element' iff jsonParser is a JSON array

Definition at line 397 of file jsonParser.cc.

◆ at() [4/4]

const jsonParser & CASM::jsonParser::at ( const size_type element) const

Return a const reference to the sub-jsonParser (JSON value) from index 'element' iff jsonParser is a JSON array

Definition at line 410 of file jsonParser.cc.

◆ begin() [1/2]

jsonParser::iterator CASM::jsonParser::begin ( )

Returns const_iterator to beginning of JSON object or JSON array.

Returns iterator to beginning of JSON object or JSON array.

Definition at line 497 of file jsonParser.cc.

◆ begin() [2/2]

jsonParser::const_iterator CASM::jsonParser::begin ( ) const

Returns iterator to beginning of JSON object or JSON array.

Returns const_iterator to beginning of JSON object or JSON array.

Definition at line 507 of file jsonParser.cc.

◆ cbegin()

jsonParser::const_iterator CASM::jsonParser::cbegin ( ) const

Returns const_iterator to beginning of JSON object or JSON array.

Returns const iterator to beginning of const JSON object or JSON array.

Definition at line 510 of file jsonParser.cc.

◆ cend()

jsonParser::const_iterator CASM::jsonParser::cend ( ) const

Returns const_iterator to end of JSON object or JSON array.

Definition at line 533 of file jsonParser.cc.

◆ contains()

bool CASM::jsonParser::contains ( const std::string &  name) const

Return true if JSON object contains 'name'.

Definition at line 601 of file jsonParser.cc.

◆ end() [1/2]

jsonParser::iterator CASM::jsonParser::end ( )

Returns iterator to end of JSON object or JSON array.

Definition at line 520 of file jsonParser.cc.

◆ end() [2/2]

jsonParser::const_iterator CASM::jsonParser::end ( ) const

Returns const_iterator to end of JSON object or JSON array.

Returns iterator to end of JSON object or JSON array.

Definition at line 530 of file jsonParser.cc.

◆ erase()

json_spirit::mObject::size_type CASM::jsonParser::erase ( const std::string &  name)

Erase key:value pair from an object.

Erase key:value pair from an object Returns the number of elements erased, which will be 0 or 1

Definition at line 607 of file jsonParser.cc.

◆ find() [1/2]

jsonParser::iterator CASM::jsonParser::find ( const std::string &  name)

Return iterator to JSON object value with 'name'.

Definition at line 543 of file jsonParser.cc.

◆ find() [2/2]

jsonParser::const_iterator CASM::jsonParser::find ( const std::string &  name) const

Return const_iterator to JSON object value with 'name'.

Definition at line 548 of file jsonParser.cc.

◆ find_at() [1/2]

jsonParser::iterator CASM::jsonParser::find_at ( const fs::path &  path)

Return iterator to sub-object or element, or 'end' if not found.

Return iterator to sub-object or element, or 'end' if not found

  • If path.empty(), return iterator that dereferences to this, and one increment results in end

Definition at line 556 of file jsonParser.cc.

◆ find_at() [2/2]

jsonParser::const_iterator CASM::jsonParser::find_at ( const fs::path &  path) const

Return iterator to sub-object or element, or 'end' if not found.

Return iterator to sub-object or element, or 'end' if not found

  • If path.empty(), return iterator that dereferences to this, and one increment results in end

Definition at line 596 of file jsonParser.cc.

◆ is_array()

bool CASM::jsonParser::is_array ( ) const

Check if array type.

Definition at line 275 of file jsonParser.cc.

◆ is_bool()

bool CASM::jsonParser::is_bool ( ) const

Check if bool type.

Definition at line 257 of file jsonParser.cc.

◆ is_float()

bool CASM::jsonParser::is_float ( ) const

Check if number type (not including int)

Definition at line 263 of file jsonParser.cc.

◆ is_int()

bool CASM::jsonParser::is_int ( ) const

Check if int type.

Definition at line 260 of file jsonParser.cc.

◆ is_null()

bool CASM::jsonParser::is_null ( ) const

Check if null type.

Definition at line 254 of file jsonParser.cc.

◆ is_number()

bool CASM::jsonParser::is_number ( ) const

Check if number type (including int)

Definition at line 266 of file jsonParser.cc.

◆ is_obj()

bool CASM::jsonParser::is_obj ( ) const

Check if object type.

Definition at line 272 of file jsonParser.cc.

◆ is_string()

bool CASM::jsonParser::is_string ( ) const

Check if string.

Definition at line 269 of file jsonParser.cc.

◆ null()

static jsonParser CASM::jsonParser::null ( )
inlinestatic

Returns a null JSON value.

Definition at line 437 of file jsonParser.hh.

◆ object() [1/2]

static jsonParser CASM::jsonParser::object ( )
inlinestatic

Returns an empty json object.

Definition at line 395 of file jsonParser.hh.

◆ object() [2/2]

template<typename Iterator >
static jsonParser CASM::jsonParser::object ( Iterator  begin,
Iterator  end 
)
inlinestatic

Puts new JSON object, from iterators over a range of values of type std::pair<std::string, T>

Definition at line 403 of file jsonParser.hh.

◆ operator!=()

bool CASM::jsonParser::operator!= ( const jsonParser json) const
inline

Definition at line 173 of file jsonParser.hh.

◆ operator=()

jsonParser& CASM::jsonParser::operator= ( const json_spirit::mValue &  value)
inlineprivate

Definition at line 443 of file jsonParser.hh.

◆ operator[]() [1/4]

jsonParser & CASM::jsonParser::operator[] ( const size_type element)

Return a reference to the sub-jsonParser (JSON value) from index 'element' iff jsonParser is a JSON array

Definition at line 385 of file jsonParser.cc.

◆ operator[]() [2/4]

const jsonParser & CASM::jsonParser::operator[] ( const size_type element) const

Return a const reference to the sub-jsonParser (JSON value) from index 'element' iff jsonParser is a JSON array

Definition at line 391 of file jsonParser.cc.

◆ operator[]() [3/4]

jsonParser & CASM::jsonParser::operator[] ( const std::string &  name)

Return a reference to the sub-jsonParser (JSON value) with 'name' if it exists If it does not exist, create it with an empty JSON object and return a reference to it

Return a reference to the sub-jsonParser (JSON value) with 'name' if it exists If it does not exist, create it with value == 'null' and return a reference

Definition at line 283 of file jsonParser.cc.

◆ operator[]() [4/4]

const jsonParser & CASM::jsonParser::operator[] ( const std::string &  name) const

Return a reference to the sub-jsonParser (JSON value) with 'name' if it exists. Will throw if the 'name' doesn't exist.

Definition at line 297 of file jsonParser.cc.

◆ parse() [1/3]

jsonParser CASM::jsonParser::parse ( const fs::path &  path)
static

Construct a jsonParser from a file containing JSON data.

Definition at line 614 of file jsonParser.cc.

◆ parse() [2/3]

static jsonParser CASM::jsonParser::parse ( const std::string &  str)
inlinestatic

Construct a jsonParser from a string containing JSON data.

Definition at line 382 of file jsonParser.hh.

◆ parse() [3/3]

static jsonParser CASM::jsonParser::parse ( std::istream &  stream)
inlinestatic

Construct a jsonParser from a stream containing JSON data.

Definition at line 392 of file jsonParser.hh.

◆ print()

void CASM::jsonParser::print ( std::ostream &  stream,
unsigned int  indent = 2,
unsigned int  prec = 12 
) const

Print json to stream.

Writes json to stream.

Definition at line 188 of file jsonParser.cc.

◆ put_array() [1/2]

jsonParser& CASM::jsonParser::put_array ( )
inline

Puts new empty JSON array.

Definition at line 362 of file jsonParser.hh.

◆ put_array() [2/2]

jsonParser& CASM::jsonParser::put_array ( size_type  N)
inline

Puts new JSON array.

Definition at line 365 of file jsonParser.hh.

◆ put_null()

jsonParser& CASM::jsonParser::put_null ( )
inline

Puts 'null' JSON value.

Definition at line 377 of file jsonParser.hh.

◆ put_obj()

jsonParser& CASM::jsonParser::put_obj ( )
inline

Puts new empty JSON object.

Definition at line 354 of file jsonParser.hh.

◆ read() [1/2]

bool CASM::jsonParser::read ( const boost::filesystem::path &  mypath)

Reads json from a path.

◆ read() [2/2]

bool CASM::jsonParser::read ( std::istream &  stream)

Reads json from the stream.

Definition at line 168 of file jsonParser.cc.

◆ size()

jsonParser::size_type CASM::jsonParser::size ( ) const

Returns array size if *this is a JSON array, object size if *this is a JSON object, 1 otherwise

Definition at line 487 of file jsonParser.cc.

◆ write() [1/2]

void CASM::jsonParser::write ( const boost::filesystem::path &  mypath,
unsigned int  indent = 2,
unsigned int  prec = 12 
) const

Write json to file.

◆ write() [2/2]

void CASM::jsonParser::write ( const std::string &  file_name,
unsigned int  indent = 2,
unsigned int  prec = 12 
) const

Write json to file.

Definition at line 196 of file jsonParser.cc.


The documentation for this class was generated from the following files: