Wt  3.3.9
Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
Wt::Json::Value Class Reference

A JSON value. More...

Public Member Functions

 Value ()
 Default construtor. More...
 
 Value (const WString &value)
 Creates a value from a string. More...
 
 Value (bool value)
 Creates a value from a boolean. More...
 
 Value (int value)
 Creates a value from an integer. More...
 
 Value (long long value)
 Creates a value from a long. More...
 
 Value (double value)
 Creates a value from a double. More...
 
 Value (const char *value)
 Creates a value from a const char*. More...
 
 Value (const Array &value)
 Creates a value from a Json::Object. More...
 
 Value (const Object &value)
 Creates a value from a Json::Object. More...
 
 Value (Type type)
 Creates a value with a given type. More...
 
 Value (const Value &other)
 Copy constructor.
 
Valueoperator= (const Value &other)
 Assignment operator. More...
 
bool operator== (const Value &other) const
 Comparison operator. More...
 
bool operator!= (const Value &other) const
 Comparison operator. More...
 
Type type () const
 Returns the type. More...
 
bool isNull () const
 Returns whether the value is Null. More...
 
bool hasType (const std::type_info &type) const
 Returns whether the value is compatible with a given C++ type. More...
 
 operator const WString & () const
 Extracts the string value. More...
 
 operator std::string () const
 Extracts the string value (UTF-8 encoded). More...
 
 operator bool () const
 Extracts the boolean value. More...
 
 operator int () const
 Extracts the integer number value. More...
 
 operator long long () const
 Extracts the integer number value. More...
 
 operator double () const
 Extracts the floating point number value. More...
 
 operator const Array & () const
 Extracts the array value. More...
 
 operator const Object & () const
 Extracts the object value. More...
 
 operator Array & ()
 Accesses the array value. More...
 
 operator Object & ()
 Accesses the object value. More...
 
const WStringorIfNull (const WString &v) const
 Extracts the string value, using a fallback when null. More...
 
std::string orIfNull (const char *v) const
 Extracts the UTF-8 encoded string value, using a fallback when null. More...
 
std::string orIfNull (const std::string &v) const
 Extracts the UTF-8 encoded string value, using a fallback when null. More...
 
bool orIfNull (bool v) const
 Extracts the boolean value, using a fallback when null. More...
 
int orIfNull (int v) const
 Extracts the number value, using a fallback when null. More...
 
long long orIfNull (long long v) const
 Extracts the number value, using a fallback when null. More...
 
double orIfNull (double v) const
 Extracts the number value, using a fallback when null. More...
 
const ArrayorIfNull (const Array &v) const
 Extracts the array value, using a fallback when null. More...
 
const ObjectorIfNull (const Object &v) const
 Extracts the object value, using a fallback when null. More...
 
Value toString () const
 Converts the value to a string. More...
 
Value toBool () const
 Converts the value to a boolean. More...
 
Value toNumber () const
 Converts the value to a number. More...
 

Static Public Member Functions

static Type typeOf (const std::type_info &type)
 Returns the JSON type that corresponds to a C++ type. More...
 

Static Public Attributes

static const Value Null
 Null constant. More...
 
static const Value True
 True constant. More...
 
static const Value False
 False constant. More...
 

Detailed Description

A JSON value.

This class represents a JSON value, which may be:

Constructor & Destructor Documentation

Wt::Json::Value::Value ( )

Default construtor.

This creates a Null value.

Wt::Json::Value::Value ( const WString value)

Creates a value from a string.

This creates a Json::StringType value.

Wt::Json::Value::Value ( bool  value)

Creates a value from a boolean.

This creates a Json::BoolType value.

Wt::Json::Value::Value ( int  value)

Creates a value from an integer.

This creates a Json::NumberType value.

Wt::Json::Value::Value ( long long  value)

Creates a value from a long.

This creates a Json::NumberType value.

Wt::Json::Value::Value ( double  value)

Creates a value from a double.

This creates a Json::NumberType value.

Wt::Json::Value::Value ( const char *  value)

Creates a value from a const char*.

This creates a Json::StringType value.

This constructor first converts const char* to WString using WString(const char *)

Wt::Json::Value::Value ( const Array value)

Creates a value from a Json::Object.

This creates a Json::ArrayType value.

Wt::Json::Value::Value ( const Object value)

Creates a value from a Json::Object.

This creates a Json::ObjectType value.

Wt::Json::Value::Value ( Type  type)

Creates a value with a given type.

This creates a value of the given type, using a default constructed value of that type:

Member Function Documentation

bool Wt::Json::Value::hasType ( const std::type_info &  type) const

Returns whether the value is compatible with a given C++ type.

This returns whether the value type can be contained in the given C++ type, i.e. when a casting operation will not fail throwing a TypeException.

See also
typeOf()
bool Wt::Json::Value::isNull ( ) const

Returns whether the value is Null.

This returns true when the type is Json::NullType.

Wt::Json::Value::operator Array & ( )

Accesses the array value.

This returns the value of a array JSON value.

Use this method to modify the contained array in-place.

For example:

Json::Object person;
person["children"] = Json::Value(Json::ArrayType);
Json::Array& children = person.get("children");
// add children ...
Exceptions
TypeExceptionif the value type is not Json::ArrayType
Wt::Json::Value::operator bool ( ) const

Extracts the boolean value.

This returns the value of a boolean JSON value.

For example:

const Json::Object& person = ...;
try {
bool happy = person.get("happy");
...
} catch (const std::exception& e) {
...
}

To coerce a value of another type to a boolean use toBool() first. To provide a fallback in case the value is null or could not be coerced to a boolean, use orIfNull().

For example, the following code does not throw exceptions:

const Json::Object& person = ...;
bool happy = person.get("happy").toBool().orIfNull(false);
Exceptions
TypeExceptionif the value type is not Json::BoolType
Wt::Json::Value::operator const Array & ( ) const

Extracts the array value.

This returns the value of a array JSON value.

For example:

const Json::Object& person = ...;
try {
const Array& children = person.get("children");
...
} catch (const std::exception& e) {
...
}

To provide a fallback in case the value is null, use orIfNull().

Exceptions
TypeExceptionif the value type is not Json::ArrayType
Wt::Json::Value::operator const Object & ( ) const

Extracts the object value.

This returns the value of a object JSON value.

For example:

const Json::Object& person = ...;
try {
const Object& employer = person.get("employer");
...
} catch (const std::exception& e) {
...
}

To provide a fallback in case the value is null, use orIfNull().

Exceptions
TypeExceptionif the value type is not Json::ObjectType
Wt::Json::Value::operator const WString & ( ) const

Extracts the string value.

This returns the value of a string JSON value.

For example:

const Json::Object& person = ...;
try {
const WString& occupation = person.get("occupation");
...
} catch (const std::exception& e) {
...
}

To coerce a value of another type to a string use toString() first. To provide a fallback in case the value is null or could not be coerced to a string, use orIfNull().

For example, the following code does not throw exceptions:

const Json::Object& person = ...;
const WString& occupation = person.get("occupation").toString().orIfNull(WString("manager"));
Exceptions
TypeExceptionif the value type is not Json::StringType
Wt::Json::Value::operator double ( ) const

Extracts the floating point number value.

This returns the value of a number JSON value.

For example:

const Json::Object& person = ...;
try {
double cost = person.get("cost");
...
} catch (const std::exception& e) {
...
}

To coerce a value of another type to a number use toNumber() first. To provide a fallback in case the value is null or could not be coerced to a number, use orIfNull().

For example, the following code does not throw exceptions:

const Json::Object& person = ...;
double cost = person.get("cost").toNumber().orIfNull(0.0);
Exceptions
TypeExceptionif the value type is not Json::NumberType
Wt::Json::Value::operator int ( ) const

Extracts the integer number value.

This returns the value of a number JSON value.

For example:

const Json::Object& person = ...;
try {
int cost = person.get("cost");
...
} catch (const std::exception& e) {
...
}

To coerce a value of another type to a number use toNumber() first. To provide a fallback in case the value is null or could not be coerced to a number, use orIfNull().

For example, the following code does not throw exceptions:

const Json::Object& person = ...;
int cost = person.get("cost").toNumber().orIfNull(0);
Exceptions
TypeExceptionif the value type is not Json::NumberType
Wt::Json::Value::operator long long ( ) const

Extracts the integer number value.

This returns the value of a number JSON value.

For example:

const Json::Object& person = ...;
try {
long long cost = person.get("cost");
...
} catch (const std::exception& e) {
...
}

To coerce a value of another type to a number use toNumber() first. To provide a fallback in case the value is null or could not be coerced to a number, use orIfNull().

For example, the following code does not throw exceptions:

const Json::Object& person = ...;
long long cost = person.get("cost").toNumber().orIfNull(0LL);
Exceptions
TypeExceptionif the value type is not Json::NumberType
Wt::Json::Value::operator Object & ( )

Accesses the object value.

This returns the value of a object JSON value.

Use this method to modify the contained object in-place.

For example:

Json::Array& children = ...;
for (unsigned i = 0; i < 3; ++i) {
children.push_back(Json::Value(Json::ObjectType));
Json::Object& child = children.back();
...
}
Exceptions
TypeExceptionif the value type is not Json::ObjectType
Wt::Json::Value::operator std::string ( ) const

Extracts the string value (UTF-8 encoded).

This returns the value of a string JSON value.

For example:

const Json::Object& person = ...;
try {
std::string occupation = person.get("occupation");
...
} catch (const std::exception& e) {
...
}

To coerce a value of another type to a string use toString() first. To provide a fallback in case the value is null or could not be coerced to a string, use orIfNull().

For example, the following code does not throw exceptions:

const Json::Object& person = ...;
const std::string occupation = person.get("occupation").toString().orIfNull("manager");
Exceptions
TypeExceptionif the value type is not Json::StringType
bool Wt::Json::Value::operator!= ( const Value other) const

Comparison operator.

Returns whether two values have a different type or value.

Value & Wt::Json::Value::operator= ( const Value other)

Assignment operator.

As a result of an assignment, both value and type are set to the value and type of the other value.

bool Wt::Json::Value::operator== ( const Value other) const

Comparison operator.

Returns whether two values have the same type and value.

const WString & Wt::Json::Value::orIfNull ( const WString v) const

Extracts the string value, using a fallback when null.

This is similar to the string cast operator, but this method returns a fallback when the value is null instead of throwing an exception.

Exceptions
TypeExceptionif the value is not null and has a type other than Json::StringType
std::string Wt::Json::Value::orIfNull ( const char *  v) const

Extracts the UTF-8 encoded string value, using a fallback when null.

This is similar to the string cast operator, but this method returns a fallback when the value is null instead of throwing an exception.

Exceptions
TypeExceptionif the value is not null and has a type other than Json::StringType
std::string Wt::Json::Value::orIfNull ( const std::string &  v) const

Extracts the UTF-8 encoded string value, using a fallback when null.

This is similar to the string cast operator, but this method returns a fallback when the value is null instead of throwing an exception.

Exceptions
TypeExceptionif the value is not null and has a type other than Json::StringType
bool Wt::Json::Value::orIfNull ( bool  v) const

Extracts the boolean value, using a fallback when null.

This is similar to the boolean cast operator, but this method returns a fallback when the value is null instead of throwing an exception.

Exceptions
TypeExceptionif the value is not null and has a type other than Json::BoolType
int Wt::Json::Value::orIfNull ( int  v) const

Extracts the number value, using a fallback when null.

This is similar to the int cast operator, but this method returns a fallback when the value is null instead of throwing an exception.

Exceptions
TypeExceptionif the value is not null and has a type other than Json::NumberType
long long Wt::Json::Value::orIfNull ( long long  v) const

Extracts the number value, using a fallback when null.

This is similar to the long long cast operator, but this method returns a fallback when the value is null instead of throwing an exception.

Exceptions
TypeExceptionif the value is not null and has a type other than Json::NumberType
double Wt::Json::Value::orIfNull ( double  v) const

Extracts the number value, using a fallback when null.

This is similar to the double cast operator, but this method returns a fallback when the value is null instead of throwing an exception.

Exceptions
TypeExceptionif the value is not null and has a type other than Json::NumberType
const Array & Wt::Json::Value::orIfNull ( const Array v) const

Extracts the array value, using a fallback when null.

This is similar to the Array cast operator, but this method returns a fallback when the value is null instead of throwing an exception.

Exceptions
TypeExceptionif the value is not null and has a type other than Json::ArrayType
const Object & Wt::Json::Value::orIfNull ( const Object v) const

Extracts the object value, using a fallback when null.

This is similar to the Object cast operator, but this method returns a fallback when the value is null instead of throwing an exception.

Exceptions
TypeExceptionif the value is not null and has a type other than Json::ObjectType
Value Wt::Json::Value::toBool ( ) const

Converts the value to a boolean.

A string value of "true" or "false" is interpreted as a boolean. Otherwise, Null is returned.

Value Wt::Json::Value::toNumber ( ) const

Converts the value to a number.

A string value is lexically casted to a number. If this fails, or for a boolean, array or object type, Null is returned.

Value Wt::Json::Value::toString ( ) const

Converts the value to a string.

The value is lexically casted to a string. For an object or array value, this coercion is not defined and Null is returned.

Exceptions
WExceptionif the Value is a number initialized to NaN
Type Wt::Json::Value::type ( ) const

Returns the type.

Returns the type of this value.

Type Wt::Json::Value::typeOf ( const std::type_info &  type)
static

Returns the JSON type that corresponds to a C++ type.

This is a utility method for converting between C++ types and JSON types.

Member Data Documentation

const Value Wt::Json::Value::False
static

False constant.

A constant value of type Json::BoolType with value false, i.e. as constructed by Json::Value(false)

const Value Wt::Json::Value::Null
static

Null constant.

A constant value with type Json::NullType, i.e. as constructed by Json::Value().

const Value Wt::Json::Value::True
static

True constant.

A constant value of type Json::BoolType with value true, i.e. as constructed by Json::Value(true).


Generated on Tue Nov 21 2017 for the C++ Web Toolkit (Wt) by doxygen 1.8.11