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

A JSON value. More...

#include <Value.h>

Public Member Functions

 Value ()
 Default construtor. More...
 
 Value (const WString &value)
 Creates a value from a string. More...
 
 Value (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 value)
 Creates a value from a long. More...
 
 Value (long long value)
 Creates a value from a long 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 (Array &&value)
 Creates a value from a Json::Object. More...
 
 Value (const Object &value)
 Creates a value from a Json::Object. More...
 
 Value (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.
 
 Value (Value &&other)
 Move constructor.
 
Valueoperator= (const Value &other)
 Assignment operator. More...
 
Valueoperator= (Value &&other)
 Move Assignment operator. More...
 
Valueoperator= (Object &&other)
 Move assignment operator.
 
Valueoperator= (Array &&other)
 Move assignment operator.
 
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 () 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

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

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 ...
@ Array
an Array
Exceptions
TypeExceptionif 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);
Exceptions
TypeExceptionif 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
TypeExceptionif 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) {
...
}
@ Object
an Object

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

Exceptions
TypeExceptionif 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) {
...
}

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::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);
Exceptions
TypeExceptionif 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
TypeExceptionif 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);
Exceptions
TypeExceptionif 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
TypeExceptionif 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();
...
}
Exceptions
TypeExceptionif 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
TypeExceptionif 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
TypeExceptionif 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
TypeExceptionif 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
TypeExceptionif 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
TypeExceptionif 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
TypeExceptionif 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
TypeExceptionif 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
TypeExceptionif 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
TypeExceptionif 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
TypeExceptionif 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
WExceptionif 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()

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