Wt: Wt::Json::Value Class Reference

Wt 4.10.4

Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
Wt::Json::Value Class Reference
JSON Library (Wt::Json)

A JSON value. More...

`#include <Value.h>`

## Public Member Functions
| Value ()
| Default construtor.
| Value (const WString &value)
| Creates a value from a string.
| Value (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 value)
| Creates a value from a long.
| Value (long long value)
| Creates a value from a long long.
| Value (double value)
| Creates a value from a double.
| Value (const char *value)
| Creates a value from a const char*.
| Value (const Array &value)
| Creates a value from a Json::Object.
| Value (Array &&value)
| Creates a value from a Json::Object.
| Value (const Object &value)
| Creates a value from a Json::Object.
| Value (Object &&value)
| Creates a value from a Json::Object.
| Value (Type type)
| Creates a value with a given type.
| Value (const Value &other)
| Copy constructor.
| Value (Value &&other)
| Move constructor.
Value & | operator= (const Value &other)
| Assignment operator.
Value & | operator= (Value &&other)
| Move Assignment operator.
Value & | operator= (Object &&other)
| Move assignment operator.
Value & | operator= (Array &&other)
| Move 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 () 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

## ◆ Value() [1/14]

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

Default construtor.

This creates a Null value.

## ◆ Value() [2/14]

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

Creates a value from a string.

This creates a Json::Type::String value.

## ◆ Value() [3/14]

Wt::Json::Value::Value | ( | WString && | value | ) |

Creates a value from a string.

This creates a Json::Type::String value.

## ◆ Value() [4/14]

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

Creates a value from a boolean.

This creates a Json::Type::Bool value.

## ◆ Value() [5/14]

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

Creates a value from an integer.

This creates a Json::Type::Number value.

## ◆ Value() [6/14]

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

Creates a value from a long.

This creates a Json::Type::Number value.

## ◆ Value() [7/14]

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

Creates a value from a long long.

This creates a Json::Type::Number value.

## ◆ Value() [8/14]

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

Creates a value from a double.

This creates a Json::Type::Number value.

## ◆ Value() [9/14]

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 *)

## ◆ Value() [10/14]

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

Creates a value from a Json::Object.

This creates a Json::ArrayType value.

## ◆ Value() [11/14]

Wt::Json::Value::Value | ( | Array && | value | ) |

Creates a value from a Json::Object.

This creates a Json::ArrayType value.

## ◆ Value() [12/14]

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

Creates a value from a Json::Object.

This creates a Json::ObjectType value.

## ◆ Value() [13/14]

Wt::Json::Value::Value | ( | Object && | value | ) |

Creates a value from a Json::Object.

This creates a Json::ObjectType value.

## ◆ Value() [14/14]

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::Type::Bool: false
- Json::Type::Number: 0
- Json::Type::String: ""
- Json::Type::Array: an empty array
- Json::Type::Object: an empty object

## Member Function Documentation

## ◆ hasType()

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()

## ◆ isNull()

bool Wt::Json::Value::isNull | ( |  | ) | const

Returns whether the value is Null.

This returns `true` when the type is Json::Type::Null.

## ◆ operator Array &()

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::Type::Array);
Json::Array& children = person.get("children");
// add children ...
Wt::Json::Array
A JSON array.
Definition Array.h:31
Wt::Json::Object
A JSON object.
Definition Object.h:35
Wt::Json::Object::get
const Value & get(const std::string &name) const
Returns the value for a member (or null if not defined).
Wt::Json::Value
A JSON value.
Definition Value.h:105
Wt::Json::Type::Array
@ Array
an Array

Exceptions

  TypeException | if the value type is not Json::Type::Array

## ◆ operator bool()

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);
Wt::Json::Value::toBool
Value toBool() const
Converts the value to a boolean.
Wt::Json::Value::orIfNull
const WString & orIfNull(const WString &v) const
Extracts the string value, using a fallback when null.

Exceptions

  TypeException | if the value type is not Json::Type::Bool

## ◆ operator const Array &()

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::Type::Array

## ◆ operator const Object &()

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::Type::Object

## ◆ operator const WString &()

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) {
...
}
Wt::WString
A value class which describes a locale-aware unicode string.
Definition WString.h:74

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"));
Wt::Json::Value::toString
Value toString() const
Converts the value to a string.

Exceptions

  TypeException | if the value type is not Json::Type::String

## ◆ operator double()

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);
Wt::Json::Value::toNumber
Value toNumber() const
Converts the value to a number.

Exceptions

  TypeException | if the value type is not Json::Type::Number

## ◆ operator int()

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::Type::Number

## ◆ operator long()

Wt::Json::Value::operator long | ( |  | ) | const

Extracts the integer number value.

This returns the value of a number JSON value.

For example:

const Json::Object& person = ...;
try {
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 cost = person.get("cost").toNumber().orIfNull(0L);
Wt::Key::L
@ L
'L' key

Exceptions

  TypeException | if the value type is not Json::Type::Number

## ◆ operator long long()

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::Type::Number

## ◆ operator Object &()

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::Type::Object));
Json::Object& child = children.back();
...
}
Wt::Json::Type::Object
@ Object
an Object

Exceptions

  TypeException | if the value type is not Json::Type::Object

## ◆ operator std::string()

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::Type::String

## ◆ operator!=()

bool Wt::Json::Value::operator!= | ( | const Value & | other | ) | const

Comparison operator.

Returns whether two values have a different type or value.

## ◆ operator=() [1/2]

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.

## ◆ operator=() [2/2]

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

Move Assignment operator.

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

## ◆ operator==()

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

Comparison operator.

Returns whether two values have the same type and value.

## ◆ orIfNull() [1/9]

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::Type::Bool

## ◆ orIfNull() [2/9]

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::Type::Array

## ◆ orIfNull() [3/9]

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::Type::String

## ◆ orIfNull() [4/9]

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::Type::Object

## ◆ orIfNull() [5/9]

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::Type::String

## ◆ orIfNull() [6/9]

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::Type::String

## ◆ orIfNull() [7/9]

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::Type::Number

## ◆ orIfNull() [8/9]

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::Type::Number

## ◆ orIfNull() [9/9]

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::Type::Number

## ◆ toBool()

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.

## ◆ toNumber()

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.

## ◆ toString()

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

  WException | if the Value is a number initialized to NaN

## ◆ type()

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

Returns the type.

Returns the type of this value.

## ◆ typeOf()

static 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

## ◆ False

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

False constant.

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

## ◆ Null

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

Null constant.

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

## ◆ True

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

True constant.

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