DLPrimitives
Classes | Public Member Functions | Friends | List of all members
dlprim::json::value Class Reference

This class is central representation of json objects. More...

#include <include/dlprim/json.hpp>

Public Member Functions

json_type type () const
 Get the type of the value.
 
bool is_undefined () const
 Returns true if type()==json::is_undefined.
 
bool is_null () const
 Returns true if type()==json::is_null.
 
bool const & boolean () const
 Convert value to boolean, throws bad_value_cast if value's type is not boolean.
 
double const & number () const
 Convert value to double, throws bad_value_cast if value's type is not number.
 
std::string const & str () const
 Convert value to strng, throws bad_value_cast if value's type is not string.
 
json::object const & object () const
 Convert value to json::object, throws bad_value_cast if value's type is not object.
 
json::array const & array () const
 Convert value to json::array, throws bad_value_cast if value's type is not array.
 
bool & boolean ()
 Get reference to bool variable that represents this value, throws bad_value_cast if type is invalid.
 
double & number ()
 Get reference to double variable that represents this value, throws bad_value_cast if type is invalid.
 
std::string & str ()
 Get reference to string variable that represents this value, throws bad_value_cast if type is invalid.
 
json::objectobject ()
 Get reference to object variable that represents this value, throws bad_value_cast if type is invalid.
 
json::arrayarray ()
 Get reference to array variable that represents this value, throws bad_value_cast if type is invalid.
 
void undefined ()
 Set value to undefined type.
 
void null ()
 Set value to null type.
 
void boolean (bool)
 Set value to boolean type and assign it.
 
void number (double)
 Set value to numeric type and assign it.
 
void str (std::string const &)
 Set value to string type and assign it.
 
void object (json::object const &)
 Set value to object type and assign it.
 
void array (json::array const &)
 Set value to array type and assign it.
 
template<typename T >
get_value () const
 Convert the value to type T, using json::traits, throws bad_value_cast if conversion is not possible.
 
template<typename T >
void set_value (T const &v)
 Convert the object v of type T to the value.
 
value const & find (std::string const &path) const
 Searches a value in the path path. More...
 
value const & find (char const *path) const
 Searches a value in the path path. More...
 
value const & at (std::string const &path) const
 Searches a value in the path path, if not found throw bad_value_cast. More...
 
value const & at (char const *path) const
 Searches a value in the path path, if not found throw bad_value_cast. More...
 
valueat (std::string const &path)
 Searches a value in the path path, if not found throw bad_value_cast. More...
 
valueat (char const *path)
 Searches a value in the path path, if not found throw bad_value_cast. More...
 
void at (std::string const &path, value const &v)
 Sets the value v at the path path, if the path invalid, creates it.
 
void at (char const *path, value const &v)
 Sets the value v at the path path, if the path invalid, creates it.
 
template<typename T >
 value (T const &v)
 Creates a value from and object v of type T.
 
json_type type (std::string const &path) const
 Returns the type of variable in path, if not found returns undefined. More...
 
json_type type (char const *path) const
 Returns the type of variable in path, if not found returns undefined. More...
 
template<typename T >
void set (std::string const &path, T const &v)
 Convert an object v of type T to a value at specific path, same as at(path,value(v))
 
template<typename T >
void set (char const *path, T const &v)
 Convert an object v of type T to a value at specific path, same as at(path,value(v))
 
std::string get (std::string const &path, char const *def) const
 Get a string value from a path path. If the path is not invalid or the object is not of type string at this path, returns def instead.
 
std::string get (char const *path, char const *def) const
 Get a string value from a path path. If the path is not invalid or the object is not of type string at this path, returns def instead.
 
template<typename T >
get (std::string const &path) const
 Get an object of type T from the path path. Throws bad_value_cast if such path does not exists of conversion can't be done.
 
template<typename T >
get (char const *path) const
 Get an object of type T from the path path. Throws bad_value_cast if such path does not exists of conversion can't be done.
 
template<typename T >
get (char const *path, T const &def) const
 Get an object of type T from the path path. Returns def if such path does not exists of conversion can't be done.
 
template<typename T >
get (std::string const &path, T const &def) const
 Get an object of type T from the path path. Returns def if such path does not exists of conversion can't be done.
 
valueoperator[] (std::string const &name)
 Returns a reference to the node name of the value. For value = {"x",10} and name == "x" return a value that holds 10. More...
 
value const & operator[] (std::string const &name) const
 Returns reference to the node name of the value. For value = {"x",10} and name == "x" return a value that holds 10. More...
 
valueoperator[] (size_t n)
 Returns a reference to n 'th entry of the array. If the value is not an array it is reset to array, of the array is too small it is resized to size of at least n+1.
 
value const & operator[] (size_t n) const
 Returns a reference to n 'th entry of array, if the value is not array or n is too big, throws bad_value_cast.
 
std::string save (int how=compact) const
 Convert a value to std::string, if how has value readable it is converted with indentation.
 
void save (std::ostream &out, int how=compact) const
 Write a value to std::ostream, if how has value readable it is converted with indentation.
 
bool load (std::istream &in, bool full, int *line_number=0)
 Read a value from std::istream. More...
 
bool load (char const *&begin, char const *end, bool full, int *line_number=0)
 Read a value from character range. More...
 
bool operator== (value const &other) const
 Compare two values objects, return true if they are same.
 
bool operator!= (value const &other) const
 Compare two values objects, return false if they are same.
 
valueoperator= (value &&other)
 Move assignment.
 
 value (value &&other)
 Move constructor.
 
 value (value const &other)
 Copy constructor.
 
value const & operator= (value const &other)
 Assignment operator.
 
 value ()
 Default value - creates a value of undefined type.
 
 ~value ()
 Destructor.
 
void swap (value &other)
 Swaps two values, does not throw.
 

Friends

struct copyable
 

Detailed Description

This class is central representation of json objects.

It can be a value from any type including scalar, object, array and undefined

Member Function Documentation

value const& dlprim::json::value::at ( std::string const &  path) const

Searches a value in the path path, if not found throw bad_value_cast.

For example if the json::value represents { "x" : { "y" : 10 } }, then find("x.y") would return a reference to value that hold a number 10, find("x") returns a reference to a value that holds an object { "y" : 10 } and find("foo") throws

value const& dlprim::json::value::at ( char const *  path) const

Searches a value in the path path, if not found throw bad_value_cast.

For example if the json::value represents { "x" : { "y" : 10 } }, then find("x.y") would return a reference to value that hold a number 10, find("x") returns a reference to a value that holds an object { "y" : 10 } and find("foo") throws

value& dlprim::json::value::at ( std::string const &  path)

Searches a value in the path path, if not found throw bad_value_cast.

For example if the json::value represents { "x" : { "y" : 10 } }, then find("x.y") would return a reference to value that hold a number 10, find("x") returns a reference to a value that holds an object { "y" : 10 } and find("foo") throws

value& dlprim::json::value::at ( char const *  path)

Searches a value in the path path, if not found throw bad_value_cast.

For example if the json::value represents { "x" : { "y" : 10 } }, then find("x.y") would return a reference to value that hold a number 10, find("x") returns a reference to a value that holds an object { "y" : 10 } and find("foo") throws

value const& dlprim::json::value::find ( std::string const &  path) const

Searches a value in the path path.

For example if the json::value represents { "x" : { "y" : 10 } }, then find("x.y") would return a reference to value that hold a number 10, find("x") returns a reference to a value that holds an object { "y" : 10 } and find("foo") would return value of undefined type.

value const& dlprim::json::value::find ( char const *  path) const

Searches a value in the path path.

For example if the json::value represents { "x" : { "y" : 10 } }, then find("x.y") would return a reference to value that hold a number 10, find("x") returns a reference to a value that holds an object { "y" : 10 } and find("foo") would return value of undefined type.

bool dlprim::json::value::load ( std::istream &  in,
bool  full,
int *  line_number = 0 
)

Read a value from std::istream.

Note: only JSON object and JSON array are considered valid values

Parameters
inthe std::istream used to read the data
fullrequire EOF once the object is read, otherwise consider it as syntax error
line_numberreturn a number of the line where syntax error occurred
Returns
returns true if the value was read successfully, otherwise returns false to indicate a syntax error.
bool dlprim::json::value::load ( char const *&  begin,
char const *  end,
bool  full,
int *  line_number = 0 
)

Read a value from character range.

Note: only JSON object and JSON array are considered valid values

Parameters
begin- begin of the text range, at the end points to the end of parsed range
end- end of the text range
fullrequire EOF once the object is read, otherwise consider it as syntax error
line_numberreturn a number of the line where syntax error occurred
Returns
returns true if the value was read successfully, otherwise returns false to indicate a syntax error.
value& dlprim::json::value::operator[] ( std::string const &  name)

Returns a reference to the node name of the value. For value = {"x",10} and name == "x" return a value that holds 10.

If value is not object it's type set to object. If such node does not exits, it is created with undefined value

value const& dlprim::json::value::operator[] ( std::string const &  name) const

Returns reference to the node name of the value. For value = {"x",10} and name == "x" return a value that holds 10.

If value is not object or such node does not exits, throws bad_value_cast

json_type dlprim::json::value::type ( std::string const &  path) const
inline

Returns the type of variable in path, if not found returns undefined.

Same as find(path).type()

References type().

json_type dlprim::json::value::type ( char const *  path) const
inline

Returns the type of variable in path, if not found returns undefined.

Same as find(path).type()

References type().


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