CppCMS
Classes | Public Member Functions | Friends
cppcms::json::value Class Reference

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

#include <cppcms/json.h>

List of all members.

Classes

struct  copyable

Public Member Functions

json_type type () const
bool is_undefined () const
bool is_null () const
bool const & boolean () const
double const & number () const
std::string const & str () const
json::object const & object () const
json::array const & array () const
bool & boolean ()
double & number ()
std::string & str ()
json::objectobject ()
json::arrayarray ()
void undefined ()
void null ()
void boolean (bool)
void number (double)
void str (std::string const &)
void object (json::object const &)
void array (json::array const &)
template<typename T >
get_value () const
template<typename T >
void set_value (T const &v)
value const & find (std::string const &path) const
value const & find (char const *path) const
value const & at (std::string const &path) const
value const & at (char const *path) const
valueat (std::string const &path)
valueat (char const *path)
void at (std::string const &path, value const &v)
void at (char const *path, value const &v)
template<typename T >
 value (T const &v)
json_type type (std::string const &path) const
json_type type (char const *path) const
template<typename T >
void set (std::string const &path, T const &v)
template<typename T >
void set (char const *path, T const &v)
std::string get (std::string const &path, char const *def) const
std::string get (char const *path, char const *def) const
template<typename T >
get (std::string const &path) const
template<typename T >
get (char const *path) const
template<typename T >
get (char const *path, T const &def) const
template<typename T >
get (std::string const &path, T const &def) const
valueoperator[] (std::string const &name)
value const & operator[] (std::string const &name) const
valueoperator[] (size_t n)
value const & operator[] (size_t n) const
std::string save (int how=compact) const
void save (std::ostream &out, int how=compact) const
bool load (std::istream &in, bool full, int *line_number=0)
bool operator== (value const &other) const
bool operator!= (value const &other) const
 value (value const &other)
value const & operator= (value const &other)
 value ()
 ~value ()
void swap (value &other)

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


Constructor & Destructor Documentation

template<typename T >
cppcms::json::value::value ( T const &  v) [inline]

Creates a value from and object v of type T

cppcms::json::value::value ( value const &  other) [inline]

Copy constructor

Default value - creates a value of undefined type

Destructor


Member Function Documentation

Convert value to json::array, throws bad_value_cast if value's type is not array

Get reference to array variable that represents this value, throws bad_value_cast if type is invalid

Set value to array type and assign it

value const& cppcms::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& cppcms::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& cppcms::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& cppcms::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

void cppcms::json::value::at ( std::string const &  path,
value const &  v 
)

Sets the value v at the path path, if the path invalid, creates it.

void cppcms::json::value::at ( char const *  path,
value const &  v 
)

Sets the value v at the path path, if the path invalid, creates it.

bool const& cppcms::json::value::boolean ( ) const

Convert value to boolean, throws bad_value_cast if value's type is not boolean

Get reference to bool variable that represents this value, throws bad_value_cast if type is invalid

Set value to boolean type and assign it

value const& cppcms::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& cppcms::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.

std::string cppcms::json::value::get ( std::string const &  path,
char const *  def 
) const [inline]

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

References get_value(), and is_undefined().

std::string cppcms::json::value::get ( char const *  path,
char const *  def 
) const [inline]

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

References get_value(), and is_undefined().

template<typename T >
T cppcms::json::value::get ( std::string const &  path) const [inline]

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 >
T cppcms::json::value::get ( char const *  path) const [inline]

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 >
T cppcms::json::value::get ( char const *  path,
T const &  def 
) const [inline]

Get an object of type T from the path path. Returns def if such path does not exists of conversion can't be done

References get_value(), and is_undefined().

template<typename T >
T cppcms::json::value::get ( std::string const &  path,
T const &  def 
) const [inline]

Get an object of type T from the path path. Returns def if such path does not exists of conversion can't be done

References get_value(), and is_undefined().

template<typename T >
T cppcms::json::value::get_value ( ) const [inline]

Convert the value to type T, using json::traits, throws bad_value_cast if conversion is not possible

Referenced by get().

Returns true if type()==json::is_null

Returns true if type()==json::is_undefined

Referenced by get().

bool cppcms::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.

Set value to null type

double const& cppcms::json::value::number ( ) const

Convert value to double, throws bad_value_cast if value's type is not number

Get reference to double variable that represents this value, throws bad_value_cast if type is invalid

void cppcms::json::value::number ( double  )

Set value to numeric type and assign it

Convert value to json::object, throws bad_value_cast if value's type is not object

Get reference to object variable that represents this value, throws bad_value_cast if type is invalid

Set value to object type and assign it

bool cppcms::json::value::operator!= ( value const &  other) const

Compare two values objects, return false if they are same

value const& cppcms::json::value::operator= ( value const &  other) [inline]

Assignment operator

bool cppcms::json::value::operator== ( value const &  other) const

Compare two values objects, return true if they are same

value& cppcms::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& cppcms::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

value& cppcms::json::value::operator[] ( 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& cppcms::json::value::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 cppcms::json::value::save ( int  how = compact) const

Convert a value to std::string, if how has value readable it is converted with indentation

void cppcms::json::value::save ( std::ostream &  out,
int  how = compact 
) const

Write a value to std::ostream, if how has value readable it is converted with indentation

template<typename T >
void cppcms::json::value::set ( std::string const &  path,
T const &  v 
) [inline]

Convert an object v of type T to a value at specific path, same as at(path,value(v))

template<typename T >
void cppcms::json::value::set ( char const *  path,
T const &  v 
) [inline]

Convert an object v of type T to a value at specific path, same as at(path,value(v))

template<typename T >
void cppcms::json::value::set_value ( T const &  v) [inline]

Convert the object v of type T to the value

std::string const& cppcms::json::value::str ( ) const

Convert value to strng, throws bad_value_cast if value's type is not string

std::string& cppcms::json::value::str ( )

Get reference to string variable that represents this value, throws bad_value_cast if type is invalid

void cppcms::json::value::str ( std::string const &  )

Set value to string type and assign it

void cppcms::json::value::swap ( value other) [inline]

Swaps two values, does not throw.

Get the type of the value

json_type cppcms::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()

json_type cppcms::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()

Set value to undefined type


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