JsValue Class Reference

A discriminated union type for JSON values. More...

Public Types

enum  Type {
  ObjectType, ArrayType, StringType, BoolType,
  IntType, RealType, NullType
}
 Type held by this JSON value. More...
 

Public Member Functions

JS_API JsValue ()
 Constructs a null value. More...
 
JS_API JsValue (const JsObject &value)
 Constructs a value holding the given object. More...
 
JS_API JsValue (JsObject &&value)
 Constructs a value holding the given object rvalue reference. More...
 
JS_API JsValue (const JsArray &value)
 Constructs a value holding the given array. More...
 
JS_API JsValue (JsArray &&value)
 Constructs a value holding the given array rvalue reference. More...
 
JS_API JsValue (const char *value)
 Constructs a value holding the given char array as a std::string. More...
 
JS_API JsValue (const std::string &value)
 Constructs a value holding the given std::string. More...
 
JS_API JsValue (std::string &&value)
 Constructs a value holding the given std::string rvalue reference. More...
 
JS_API JsValue (bool value)
 Constructs a value holding a bool. More...
 
JS_API JsValue (int value)
 Constructs a value holding a signed integer. More...
 
JS_API JsValue (int64_t value)
 Constructs a value holding a 64-bit signed integer. More...
 
JS_API JsValue (uint64_t value)
 Constructs a value holding a 64-bit unsigned integer. More...
 
JS_API JsValue (double value)
 Constructs a value holding a double. More...
 
JS_API const JsObject & GetJsObject () const
 Returns the object held by this value. More...
 
JS_API const JsArray & GetJsArray () const
 Returns the array held by this value. More...
 
JS_API const std::string & GetString () const
 Returns the string held by this value. More...
 
JS_API bool GetBool () const
 Returns the bool held by this value. More...
 
JS_API int GetInt () const
 Returns the integer held by this value. More...
 
JS_API int64_t GetInt64 () const
 Returns the 64-bit integer held by this value. More...
 
JS_API uint64_t GetUInt64 () const
 Returns the 64-bit unsigned integer held by this value. More...
 
JS_API double GetReal () const
 Returns the double held by this value. More...
 
template<typename T , typename ReturnType = typename std::conditional< std::is_same<T, JsObject>::value || std::is_same<T, JsArray>::value || std::is_same<T, std::string>::value, const T&, T>::type>
ReturnType Get () const
 Returns the value corresponding to the C++ type specified in the template parameter if it is holding such a value. More...
 
template<typename T >
std::vector< T > GetArrayOf () const
 Returns a vector holding the elements of this value's array that correspond to the C++ type specified as the template parameter. More...
 
JS_API Type GetType () const
 Returns the type of this value. More...
 
JS_API std::string GetTypeName () const
 Returns a display name for the type of this value. More...
 
JS_API bool IsObject () const
 Returns true if this value is holding an object type. More...
 
JS_API bool IsArray () const
 Returns true if this value is holding an array type. More...
 
JS_API bool IsString () const
 Returns true if this value is holding a string type. More...
 
JS_API bool IsBool () const
 Returns true if this value is holding a boolean type. More...
 
JS_API bool IsInt () const
 Returns true if this value is holding an integer type. More...
 
JS_API bool IsReal () const
 Returns true if this value is holding a real type. More...
 
JS_API bool IsUInt64 () const
 Returns true if this value is holding a 64-bit unsigned integer. More...
 
template<typename T >
bool Is () const
 Returns true if this value is holding a type that corresponds to the C++ type specified as the template parameter. More...
 
template<typename T >
bool IsArrayOf () const
 Returns true if this value is holding an array whose elements all correspond to the C++ type specified as the template parameter. More...
 
JS_API bool IsNull () const
 Returns true if this value is null, false otherwise. More...
 
JS_API operator bool () const
 Evaluates to true if this value is not null. More...
 
JS_API bool operator== (const JsValue &other) const
 Returns true of both values hold the same type and the underlying held values are equal. More...
 
JS_API bool operator!= (const JsValue &other) const
 Returns true if values are of different type, or the underlying held values are not equal. More...
 

Detailed Description

A discriminated union type for JSON values.

A JsValue may contain one of the following types:

  • JsObject, a dictionary type
  • JsArray, a vector type
  • std::string
  • bool
  • int64_t
  • uint64_t
  • double
  • null

Definition at line 61 of file value.h.

Member Enumeration Documentation

◆ Type

enum Type

Type held by this JSON value.

Definition at line 65 of file value.h.

Constructor & Destructor Documentation

◆ JsValue() [1/13]

JS_API JsValue ( )

Constructs a null value.

◆ JsValue() [2/13]

JS_API JsValue ( const JsObject &  value)

Constructs a value holding the given object.

◆ JsValue() [3/13]

JS_API JsValue ( JsObject &&  value)

Constructs a value holding the given object rvalue reference.

◆ JsValue() [4/13]

JS_API JsValue ( const JsArray &  value)

Constructs a value holding the given array.

◆ JsValue() [5/13]

JS_API JsValue ( JsArray &&  value)

Constructs a value holding the given array rvalue reference.

◆ JsValue() [6/13]

JS_API JsValue ( const char *  value)
explicit

Constructs a value holding the given char array as a std::string.

◆ JsValue() [7/13]

JS_API JsValue ( const std::string &  value)
explicit

Constructs a value holding the given std::string.

◆ JsValue() [8/13]

JS_API JsValue ( std::string &&  value)
explicit

Constructs a value holding the given std::string rvalue reference.

◆ JsValue() [9/13]

JS_API JsValue ( bool  value)
explicit

Constructs a value holding a bool.

◆ JsValue() [10/13]

JS_API JsValue ( int  value)
explicit

Constructs a value holding a signed integer.

◆ JsValue() [11/13]

JS_API JsValue ( int64_t  value)
explicit

Constructs a value holding a 64-bit signed integer.

◆ JsValue() [12/13]

JS_API JsValue ( uint64_t  value)
explicit

Constructs a value holding a 64-bit unsigned integer.

◆ JsValue() [13/13]

JS_API JsValue ( double  value)
explicit

Constructs a value holding a double.

Member Function Documentation

◆ Get()

ReturnType Get ( ) const
inline

Returns the value corresponding to the C++ type specified in the template parameter if it is holding such a value.

Calling this function with C++ type T is equivalent to calling the specific Get function above that returns a value or reference to a type T.

If a value corresponding to the C++ type is not being held, this method raises a coding error. See Get functions above for default value returned in this case.

Definition at line 167 of file value.h.

◆ GetArrayOf()

std::vector< T > GetArrayOf ( ) const
inline

Returns a vector holding the elements of this value's array that correspond to the C++ type specified as the template parameter.

If this value is not holding an array, an empty vector is returned. If any of the array's elements does not correspond to the C++ type, it is replaced with the default value used by the Get functions above. In both cases, a coding error will be raised.

Definition at line 274 of file value.h.

◆ GetBool()

JS_API bool GetBool ( ) const

Returns the bool held by this value.

If this value is not holding a bool, this method raises a coding error and false is returned.

◆ GetInt()

JS_API int GetInt ( ) const

Returns the integer held by this value.

If this value is not holding an int, this method raises a coding error and zero is returned. If the value is holding a 64-bit integer larger than the platform int may hold, the value is truncated.

◆ GetInt64()

JS_API int64_t GetInt64 ( ) const

Returns the 64-bit integer held by this value.

If this value is not holding a 64-bit integer, this method raises a coding error and zero is returned.

◆ GetJsArray()

JS_API const JsArray& GetJsArray ( ) const

Returns the array held by this value.

If this value is not holding an array, this method raises a coding error and an empty array is returned.

◆ GetJsObject()

JS_API const JsObject& GetJsObject ( ) const

Returns the object held by this value.

If this value is not holding an object, this method raises a coding error and an empty object is returned.

◆ GetReal()

JS_API double GetReal ( ) const

Returns the double held by this value.

If this value is not holding a double, this method raises a coding error and zero is returned.

◆ GetString()

JS_API const std::string& GetString ( ) const

Returns the string held by this value.

If this value is not holding a string, this method raises a coding error and an empty string is returned.

◆ GetType()

JS_API Type GetType ( ) const

Returns the type of this value.

◆ GetTypeName()

JS_API std::string GetTypeName ( ) const

Returns a display name for the type of this value.

◆ GetUInt64()

JS_API uint64_t GetUInt64 ( ) const

Returns the 64-bit unsigned integer held by this value.

If this value is not holding a 64-bit unsigned integer, this method raises a coding error and zero is returned.

◆ Is()

bool Is ( ) const
inline

Returns true if this value is holding a type that corresponds to the C++ type specified as the template parameter.

Definition at line 210 of file value.h.

◆ IsArray()

JS_API bool IsArray ( ) const

Returns true if this value is holding an array type.

◆ IsArrayOf()

bool IsArrayOf ( ) const
inline

Returns true if this value is holding an array whose elements all correspond to the C++ type specified as the template parameter.

Definition at line 284 of file value.h.

◆ IsBool()

JS_API bool IsBool ( ) const

Returns true if this value is holding a boolean type.

◆ IsInt()

JS_API bool IsInt ( ) const

Returns true if this value is holding an integer type.

◆ IsNull()

JS_API bool IsNull ( ) const

Returns true if this value is null, false otherwise.

◆ IsObject()

JS_API bool IsObject ( ) const

Returns true if this value is holding an object type.

◆ IsReal()

JS_API bool IsReal ( ) const

Returns true if this value is holding a real type.

◆ IsString()

JS_API bool IsString ( ) const

Returns true if this value is holding a string type.

◆ IsUInt64()

JS_API bool IsUInt64 ( ) const

Returns true if this value is holding a 64-bit unsigned integer.

◆ operator bool()

JS_API operator bool ( ) const
explicit

Evaluates to true if this value is not null.

◆ operator!=()

JS_API bool operator!= ( const JsValue &  other) const

Returns true if values are of different type, or the underlying held values are not equal.

◆ operator==()

JS_API bool operator== ( const JsValue &  other) const

Returns true of both values hold the same type and the underlying held values are equal.


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