Wt: Wt::Json::Value Class Reference

Wt 3.3.0

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes

- Class List
- Class Hierarchy
- Class Members

Public Member Functions | Static Public Member Functions | Static Public Attributes
Wt::Json::Value Class Reference
JSON Library (Wt::Json)

A JSON value. More...

List of all members.

## Public Member Functions
| Value ()
| Default construtor.
| Value (const WString &value)
| Creates a value from a string.
| Value (bool value)
| Creates a value from a boolean.
| Value (int value)
| Creates a value from an integer.
| Value (long long value)
| Creates a value from a long.
| Value (double value)
| Creates a value from a double.
| Value (Type type)
| Creates a value with a given type.
| Value (const Value &other)
| Copy constructor.
Value & | operator= (const Value &other)
| Assignment operator.
bool | operator== (const Value &other) const
| Comparison operator.
bool | operator!= (const Value &other) const
| Comparison operator.
Type | type () const
| Returns the type.
bool | isNull () const
| Returns whether the value is Null.
bool | hasType (const std::type_info &type) const
| Returns whether the value is compatible with a given C++ type.
| operator const WString & () const
| Extracts the string value.
| operator std::string () const
| Extracts the string value (UTF-8 encoded).
| operator bool () const
| Extracts the boolean value.
| operator int () const
| Extracts the integer number value.
| operator long long () const
| Extracts the integer number value.
| operator double () const
| Extracts the floating point number value.
| operator const Array & () const
| Extracts the array value.
| operator const Object & () const
| Extracts the object value.
| operator Array & ()
| Accesses the array value.
| operator Object & ()
| Accesses the object value.
const WString & | orIfNull (const WString &v) const
| Extracts the string value, using a fallback when null.
std::string | orIfNull (const char *v) const
| Extracts the UTF-8 encoded string value, using a fallback when null.
std::string | orIfNull (const std::string &v) const
| Extracts the UTF-8 encoded string value, using a fallback when null.
bool | orIfNull (bool v) const
| Extracts the boolean value, using a fallback when null.
int | orIfNull (int v) const
| Extracts the number value, using a fallback when null.
long long | orIfNull (long long v) const
| Extracts the number value, using a fallback when null.
double | orIfNull (double v) const
| Extracts the number value, using a fallback when null.
const Array & | orIfNull (const Array &v) const
| Extracts the array value, using a fallback when null.
const Object & | orIfNull (const Object &v) const
| Extracts the object value, using a fallback when null.
Value | toString () const
| Converts the value to a string.
Value | toBool () const
| Converts the value to a boolean.
Value | toNumber () const
| Converts the value to a number.
## Static Public Member Functions
static Type | typeOf (const std::type_info &type)
| Returns the JSON type that corresponds to a C++ type.
## Static Public Attributes
static const Value | Null
| Null constant.
static const Value | True
| True constant.
static const Value | False
| False constant.

## Detailed Description

A JSON value.

This class represents a JSON value, which may be:

- a simple type (boolean, number, string)
- a composed type (Array or Object)
- Null: this represents values which are represented as "null" in JSON notations, but also the values returned for missing members in an Object, or values which are the result of a failed type coercion.

## 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 | ( | 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:

- Json::BoolType: false
- Json::NumberType: 0
- Json::StringType: ""
- Json::ArrayType: an empty array
- Json::ObjectType: an empty object

## 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:

  TypeException | if 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:

  TypeException | if 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:

  TypeException | if 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:

  TypeException | if 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:

  TypeException | if 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:

  TypeException | if 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:

  TypeException | if 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:

  TypeException | if 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:

  TypeException | if 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:

  TypeException | if 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:

  TypeException | if 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:

  TypeException | if 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:

  TypeException | if 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:

  TypeException | if 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:

  TypeException | if 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:

  TypeException | if 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:

  TypeException | if 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:

  TypeException | if 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:

  TypeException | if 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.

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)`.

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator