cesium-native 0.43.0
Loading...
Searching...
No Matches
CesiumUtility::JsonValue Class Referencefinal

A generic implementation of a value in a JSON structure. More...

#include <CesiumUtility/JsonValue.h>

Public Types

using Null = std::nullptr_t
 The type to represent a null JSON value.
 
using Bool = bool
 The type to represent a Bool JSON value.
 
using String = std::string
 The type to represent a String JSON value.
 
using Object = std::map<std::string, JsonValue>
 The type to represent an Object JSON value.
 
using Array = std::vector<JsonValue>
 The type to represent an Array JSON value.
 

Public Member Functions

 JsonValue () noexcept
 Default constructor.
 
 JsonValue (std::nullptr_t) noexcept
 Creates a null JSON value.
 
 JsonValue (double v) noexcept
 Creates a Number JSON value.
 
 JsonValue (std::int8_t v) noexcept
 Creates a std::int64_t JSON value (Widening conversion from std::int8_t).
 
 JsonValue (std::uint8_t v) noexcept
 Creates a std::uint64_t JSON value (Widening conversion from std::uint8_t).
 
 JsonValue (std::int16_t v) noexcept
 Creates a std::int64_t JSON value (Widening conversion from std::int16_t).
 
 JsonValue (std::uint16_t v) noexcept
 Creates a std::uint64_t JSON value (Widening conversion from std::uint16_t).
 
 JsonValue (std::int32_t v) noexcept
 Creates a std::int64_t JSON value (Widening conversion from std::int32_t).
 
 JsonValue (std::uint32_t v) noexcept
 Creates a std::uint64_t JSON value (Widening conversion from std::uint32_t).
 
 JsonValue (std::int64_t v) noexcept
 Creates a std::int64_t JSON value.
 
 JsonValue (std::uint64_t v) noexcept
 Creates a std::uint64_t JSON value.
 
 JsonValue (bool v) noexcept
 Creates a Bool JSON value.
 
 JsonValue (const std::string &v)
 Creates a String JSON value.
 
 JsonValue (std::string &&v) noexcept
 Creates a String JSON value.
 
 JsonValue (const char *v)
 Creates a String JSON value.
 
 JsonValue (const std::map< std::string, JsonValue > &v)
 Creates an Object JSON value with the given properties.
 
 JsonValue (std::map< std::string, JsonValue > &&v)
 Creates an Object JSON value with the given properties.
 
 JsonValue (const std::vector< JsonValue > &v)
 Creates an Array JSON value with the given elements.
 
 JsonValue (std::vector< JsonValue > &&v) noexcept
 Creates an Array JSON value with the given elements.
 
 JsonValue (std::initializer_list< JsonValue > v)
 Creates an JSON value from the given initializer list.
 
 JsonValue (std::initializer_list< std::pair< const std::string, JsonValue > > v)
 Creates an JSON value from the given initializer list.
 
const JsonValuegetValuePtrForKey (const std::string &key) const
 Attempts to obtain a pointer to a JsonValue for the given key on this object.
 
JsonValuegetValuePtrForKey (const std::string &key)
 Attempts to obtain a pointer to a JsonValue for the given key on this object.
 
template<typename T >
const T * getValuePtrForKey (const std::string &key) const
 Gets a typed value corresponding to the given key in the object represented by this instance.
 
template<typename T >
T * getValuePtrForKey (const std::string &key)
 Gets a typed value corresponding to the given key in the object represented by this instance.
 
template<typename To , typename std::enable_if< std::is_integral< To >::value||std::is_floating_point< To >::value >::type * = nullptr>
std::optional< To > getSafeNumericalValueForKey (const std::string &key) const
 Converts the numerical value corresponding to the given key to the provided numerical template type.
 
template<typename To , typename std::enable_if< std::is_integral< To >::value||std::is_floating_point< To >::value >::type * = nullptr>
To getSafeNumericalValueOrDefaultForKey (const std::string &key, To defaultValue) const
 Converts the numerical value corresponding to the given key to the provided numerical template type.
 
bool hasKey (const std::string &key) const
 Determines if this value is an Object and has the given key.
 
template<typename To , typename std::enable_if< std::is_integral< To >::value||std::is_floating_point< To >::value >::type * = nullptr>
std::optional< To > getSafeNumber () const
 Gets the numerical quantity from the value casted to the To type. This function should be used over getDouble() / getUint64() / getInt64() if you plan on casting that type into another smaller type or different type.
 
template<typename To , typename std::enable_if< std::is_integral< To >::value||std::is_floating_point< To >::value >::type * = nullptr>
To getSafeNumberOrDefault (To defaultValue) const noexcept
 Gets the numerical quantity from the value casted to the To type or returns defaultValue if unable to do so.
 
const JsonValue::ObjectgetObject () const
 Gets the object from the value.
 
const JsonValue::StringgetString () const
 Gets the string from the value.
 
const JsonValue::ArraygetArray () const
 Gets the array from the value.
 
std::vector< std::string > getArrayOfStrings (const std::string &defaultString) const
 Gets an array of strings from the value.
 
bool getBool () const
 Gets the bool from the value.
 
double getDouble () const
 Gets the double from the value.
 
std::uint64_t getUint64 () const
 Gets the std::uint64_t from the value.
 
std::int64_t getInt64 () const
 Gets the std::int64_t from the value.
 
bool getBoolOrDefault (bool defaultValue) const noexcept
 Gets the bool from the value or returns defaultValue.
 
const JsonValue::String getStringOrDefault (String defaultValue) const
 Gets the string from the value or returns defaultValue.
 
double getDoubleOrDefault (double defaultValue) const noexcept
 Gets the double from the value or returns defaultValue.
 
std::uint64_t getUint64OrDefault (std::uint64_t defaultValue) const noexcept
 Gets the uint64_t from the value or returns defaultValue.
 
std::int64_t getInt64OrDefault (std::int64_t defaultValue) const noexcept
 Gets the int64_t from the value or returns defaultValue.
 
bool isNull () const noexcept
 Returns whether this value is a null value.
 
bool isNumber () const noexcept
 Returns whether this value is a double, std::uint64_t or std::int64_t. Use this function in conjunction with getNumber for safely casting to arbitrary types.
 
bool isBool () const noexcept
 Returns whether this value is a Bool value.
 
bool isString () const noexcept
 Returns whether this value is a String value.
 
bool isObject () const noexcept
 Returns whether this value is an Object value.
 
bool isArray () const noexcept
 Returns whether this value is an Array value.
 
bool isDouble () const noexcept
 Returns whether this value is a double value.
 
bool isUint64 () const noexcept
 Returns whether this value is a std::uint64_t value.
 
bool isInt64 () const noexcept
 Returns whether this value is a std::int64_t value.
 
bool operator== (const JsonValue &rhs) const noexcept
 Returns true if two values are equal.
 
int64_t getSizeBytes () const noexcept
 Returns the size in bytes of this JsonValue.
 

Public Attributes

std::variant< Null, double, std::uint64_t, std::int64_t, Bool, String, Object, Arrayvalue
 The actual value.
 

Detailed Description

A generic implementation of a value in a JSON structure.

Instances of this class are used to represent the common extras field of glTF elements that extend the the ExtensibleObject class.

Definition at line 75 of file JsonValue.h.

Member Typedef Documentation

◆ Array

The type to represent an Array JSON value.

Definition at line 100 of file JsonValue.h.

◆ Bool

The type to represent a Bool JSON value.

Definition at line 85 of file JsonValue.h.

◆ Null

using CesiumUtility::JsonValue::Null = std::nullptr_t

The type to represent a null JSON value.

Definition at line 80 of file JsonValue.h.

◆ Object

using CesiumUtility::JsonValue::Object = std::map<std::string, JsonValue>

The type to represent an Object JSON value.

Definition at line 95 of file JsonValue.h.

◆ String

using CesiumUtility::JsonValue::String = std::string

The type to represent a String JSON value.

Definition at line 90 of file JsonValue.h.

Constructor & Destructor Documentation

◆ JsonValue() [1/21]

CesiumUtility::JsonValue::JsonValue ( )
inlinenoexcept

Default constructor.

Definition at line 105 of file JsonValue.h.

◆ JsonValue() [2/21]

CesiumUtility::JsonValue::JsonValue ( std::nullptr_t )
inlinenoexcept

Creates a null JSON value.

Definition at line 110 of file JsonValue.h.

◆ JsonValue() [3/21]

CesiumUtility::JsonValue::JsonValue ( double v)
inlinenoexcept

Creates a Number JSON value.

NaN and ±Infinity are represented as JsonValue::Null.

Definition at line 117 of file JsonValue.h.

◆ JsonValue() [4/21]

CesiumUtility::JsonValue::JsonValue ( std::int8_t v)
inlinenoexcept

Creates a std::int64_t JSON value (Widening conversion from std::int8_t).

Definition at line 129 of file JsonValue.h.

◆ JsonValue() [5/21]

CesiumUtility::JsonValue::JsonValue ( std::uint8_t v)
inlinenoexcept

Creates a std::uint64_t JSON value (Widening conversion from std::uint8_t).

Definition at line 135 of file JsonValue.h.

◆ JsonValue() [6/21]

CesiumUtility::JsonValue::JsonValue ( std::int16_t v)
inlinenoexcept

Creates a std::int64_t JSON value (Widening conversion from std::int16_t).

Definition at line 141 of file JsonValue.h.

◆ JsonValue() [7/21]

CesiumUtility::JsonValue::JsonValue ( std::uint16_t v)
inlinenoexcept

Creates a std::uint64_t JSON value (Widening conversion from std::uint16_t).

Definition at line 147 of file JsonValue.h.

◆ JsonValue() [8/21]

CesiumUtility::JsonValue::JsonValue ( std::int32_t v)
inlinenoexcept

Creates a std::int64_t JSON value (Widening conversion from std::int32_t).

Definition at line 153 of file JsonValue.h.

◆ JsonValue() [9/21]

CesiumUtility::JsonValue::JsonValue ( std::uint32_t v)
inlinenoexcept

Creates a std::uint64_t JSON value (Widening conversion from std::uint32_t).

Definition at line 159 of file JsonValue.h.

◆ JsonValue() [10/21]

CesiumUtility::JsonValue::JsonValue ( std::int64_t v)
inlinenoexcept

Creates a std::int64_t JSON value.

Definition at line 164 of file JsonValue.h.

◆ JsonValue() [11/21]

CesiumUtility::JsonValue::JsonValue ( std::uint64_t v)
inlinenoexcept

Creates a std::uint64_t JSON value.

Definition at line 169 of file JsonValue.h.

◆ JsonValue() [12/21]

CesiumUtility::JsonValue::JsonValue ( bool v)
inlinenoexcept

Creates a Bool JSON value.

Definition at line 174 of file JsonValue.h.

◆ JsonValue() [13/21]

CesiumUtility::JsonValue::JsonValue ( const std::string & v)
inline

Creates a String JSON value.

Definition at line 179 of file JsonValue.h.

◆ JsonValue() [14/21]

CesiumUtility::JsonValue::JsonValue ( std::string && v)
inlinenoexcept

Creates a String JSON value.

Definition at line 184 of file JsonValue.h.

◆ JsonValue() [15/21]

CesiumUtility::JsonValue::JsonValue ( const char * v)
inline

Creates a String JSON value.

Definition at line 189 of file JsonValue.h.

◆ JsonValue() [16/21]

CesiumUtility::JsonValue::JsonValue ( const std::map< std::string, JsonValue > & v)
inline

Creates an Object JSON value with the given properties.

Definition at line 194 of file JsonValue.h.

◆ JsonValue() [17/21]

CesiumUtility::JsonValue::JsonValue ( std::map< std::string, JsonValue > && v)
inline

Creates an Object JSON value with the given properties.

Definition at line 199 of file JsonValue.h.

◆ JsonValue() [18/21]

CesiumUtility::JsonValue::JsonValue ( const std::vector< JsonValue > & v)
inline

Creates an Array JSON value with the given elements.

Definition at line 204 of file JsonValue.h.

◆ JsonValue() [19/21]

CesiumUtility::JsonValue::JsonValue ( std::vector< JsonValue > && v)
inlinenoexcept

Creates an Array JSON value with the given elements.

Definition at line 209 of file JsonValue.h.

◆ JsonValue() [20/21]

CesiumUtility::JsonValue::JsonValue ( std::initializer_list< JsonValue > v)
inline

Creates an JSON value from the given initializer list.

Definition at line 214 of file JsonValue.h.

◆ JsonValue() [21/21]

CesiumUtility::JsonValue::JsonValue ( std::initializer_list< std::pair< const std::string, JsonValue > > v)
inline

Creates an JSON value from the given initializer list.

Definition at line 220 of file JsonValue.h.

Member Function Documentation

◆ getArray()

const JsonValue::Array & CesiumUtility::JsonValue::getArray ( ) const
inlinenodiscard

Gets the array from the value.

Returns
The array.
Exceptions
std::bad_variant_accessif the underlying type is not a JsonValue::Array

Definition at line 450 of file JsonValue.h.

◆ getArrayOfStrings()

std::vector< std::string > CesiumUtility::JsonValue::getArrayOfStrings ( const std::string & defaultString) const
nodiscard

Gets an array of strings from the value.

Parameters
defaultStringThe default string to include in the array for an element that is not a string.
Returns
The array of strings, or an empty array if this value is not an array at all.

◆ getBool()

bool CesiumUtility::JsonValue::getBool ( ) const
inlinenodiscard

Gets the bool from the value.

Returns
The bool.
Exceptions
std::bad_variant_accessif the underlying type is not a JsonValue::Bool

Definition at line 471 of file JsonValue.h.

◆ getBoolOrDefault()

bool CesiumUtility::JsonValue::getBoolOrDefault ( bool defaultValue) const
inlinenodiscardnoexcept

Gets the bool from the value or returns defaultValue.

Returns
The bool or defaultValue if this->value is not a bool.

Definition at line 508 of file JsonValue.h.

◆ getDouble()

double CesiumUtility::JsonValue::getDouble ( ) const
inlinenodiscard

Gets the double from the value.

Returns
The double.
Exceptions
std::bad_variant_accessif the underlying type is not a double

Definition at line 480 of file JsonValue.h.

◆ getDoubleOrDefault()

double CesiumUtility::JsonValue::getDoubleOrDefault ( double defaultValue) const
inlinenodiscardnoexcept

Gets the double from the value or returns defaultValue.

Returns
The double or defaultValue if this->value is not a double.

Definition at line 536 of file JsonValue.h.

◆ getInt64()

std::int64_t CesiumUtility::JsonValue::getInt64 ( ) const
inlinenodiscard

Gets the std::int64_t from the value.

Returns
The std::int64_t.
Exceptions
std::bad_variant_accessif the underlying type is not a std::int64_t

Definition at line 500 of file JsonValue.h.

◆ getInt64OrDefault()

std::int64_t CesiumUtility::JsonValue::getInt64OrDefault ( std::int64_t defaultValue) const
inlinenodiscardnoexcept

Gets the int64_t from the value or returns defaultValue.

Returns
The int64_t or defaultValue if this->value is not a int64_t.

Definition at line 564 of file JsonValue.h.

◆ getObject()

const JsonValue::Object & CesiumUtility::JsonValue::getObject ( ) const
inlinenodiscard

Gets the object from the value.

Returns
The object.
Exceptions
std::bad_variant_accessif the underlying type is not a JsonValue::Object

Definition at line 430 of file JsonValue.h.

◆ getSafeNumber()

template<typename To , typename std::enable_if< std::is_integral< To >::value||std::is_floating_point< To >::value >::type * = nullptr>
std::optional< To > CesiumUtility::JsonValue::getSafeNumber ( ) const
inlinenodiscard

Gets the numerical quantity from the value casted to the To type. This function should be used over getDouble() / getUint64() / getInt64() if you plan on casting that type into another smaller type or different type.

Returns
The converted type if it is numerical and it can be cast without precision loss; otherwise, std::nullopt.

Definition at line 373 of file JsonValue.h.

◆ getSafeNumberOrDefault()

template<typename To , typename std::enable_if< std::is_integral< To >::value||std::is_floating_point< To >::value >::type * = nullptr>
To CesiumUtility::JsonValue::getSafeNumberOrDefault ( To defaultValue) const
inlinenodiscardnoexcept

Gets the numerical quantity from the value casted to the To type or returns defaultValue if unable to do so.

Returns
The converted type if it can be cast without precision loss or defaultValue if it cannot be converted safely.

Definition at line 404 of file JsonValue.h.

◆ getSafeNumericalValueForKey()

template<typename To , typename std::enable_if< std::is_integral< To >::value||std::is_floating_point< To >::value >::type * = nullptr>
std::optional< To > CesiumUtility::JsonValue::getSafeNumericalValueForKey ( const std::string & key) const
inlinenodiscard

Converts the numerical value corresponding to the given key to the provided numerical template type.

If this instance is not a JsonValue::Object, the key does not exist in this object, the named value does not have a numerical type, or if the named value cannot be converted from double / std::uint64_t / std::int64_t without precision loss, returns std::nullopt.

Template Parameters
ToThe expected type of the value.
Parameters
keyThe key for which to retrieve the value from this object.
Returns
The converted value, or std::nullopt if it cannot be converted for any of the previously-mentioned reasons.
Remarks
Compilation will fail if type 'To' is not an integral / float / double type.

Definition at line 300 of file JsonValue.h.

◆ getSafeNumericalValueOrDefaultForKey()

template<typename To , typename std::enable_if< std::is_integral< To >::value||std::is_floating_point< To >::value >::type * = nullptr>
To CesiumUtility::JsonValue::getSafeNumericalValueOrDefaultForKey ( const std::string & key,
To defaultValue ) const
inlinenodiscard

Converts the numerical value corresponding to the given key to the provided numerical template type.

If this instance is not a JsonValue::Object, the key does not exist in this object, or the named value does not have a numerical type that can be represented as T without precision loss, then the default value is returned.

Template Parameters
ToThe expected type of the value.
Parameters
keyThe key for which to retrieve the value from this object.
defaultValueThe value that will be returned if a numerical value can't be obtained.
Returns
The converted value.
Exceptions
Ifunable to convert the converted value for one of the aforementioned reasons.
Remarks
Compilation will fail if type 'To' is not an integral / float / double type.

Definition at line 333 of file JsonValue.h.

◆ getSizeBytes()

int64_t CesiumUtility::JsonValue::getSizeBytes ( ) const
inlinenoexcept

Returns the size in bytes of this JsonValue.

Definition at line 648 of file JsonValue.h.

◆ getString()

const JsonValue::String & CesiumUtility::JsonValue::getString ( ) const
inlinenodiscard

Gets the string from the value.

Returns
The string.
Exceptions
std::bad_variant_accessif the underlying type is not a JsonValue::String

Definition at line 440 of file JsonValue.h.

◆ getStringOrDefault()

const JsonValue::String CesiumUtility::JsonValue::getStringOrDefault ( String defaultValue) const
inlinenodiscard

Gets the string from the value or returns defaultValue.

Returns
The string or defaultValue if this->value is not a string.

Definition at line 522 of file JsonValue.h.

◆ getUint64()

std::uint64_t CesiumUtility::JsonValue::getUint64 ( ) const
inlinenodiscard

Gets the std::uint64_t from the value.

Returns
The std::uint64_t.
Exceptions
std::bad_variant_accessif the underlying type is not a std::uint64_t

Definition at line 490 of file JsonValue.h.

◆ getUint64OrDefault()

std::uint64_t CesiumUtility::JsonValue::getUint64OrDefault ( std::uint64_t defaultValue) const
inlinenodiscardnoexcept

Gets the uint64_t from the value or returns defaultValue.

Returns
The uint64_t or defaultValue if this->value is not a uint64_t.

Definition at line 550 of file JsonValue.h.

◆ getValuePtrForKey() [1/4]

JsonValue * CesiumUtility::JsonValue::getValuePtrForKey ( const std::string & key)
nodiscard

Attempts to obtain a pointer to a JsonValue for the given key on this object.

Parameters
keyThe key to lookup.
Returns
A pointer to the JsonValue for the provided key, or nullptr.

◆ getValuePtrForKey() [2/4]

template<typename T >
T * CesiumUtility::JsonValue::getValuePtrForKey ( const std::string & key)
inline

Gets a typed value corresponding to the given key in the object represented by this instance.

If this instance is not a JsonValue::Object, returns nullptr. If the key does not exist in this object, returns nullptr. If the named value does not have the type T, returns nullptr.

Template Parameters
TThe expected type of the value.
Parameters
keyThe key for which to retrieve the value from this object.
Returns
A pointer to the requested value, or nullptr if the value cannot be obtained as requested.

Definition at line 274 of file JsonValue.h.

◆ getValuePtrForKey() [3/4]

const JsonValue * CesiumUtility::JsonValue::getValuePtrForKey ( const std::string & key) const
nodiscard

Attempts to obtain a pointer to a JsonValue for the given key on this object.

Parameters
keyThe key to lookup.
Returns
A pointer to the JsonValue for the provided key, or nullptr.

◆ getValuePtrForKey() [4/4]

template<typename T >
const T * CesiumUtility::JsonValue::getValuePtrForKey ( const std::string & key) const
inline

Gets a typed value corresponding to the given key in the object represented by this instance.

If this instance is not a JsonValue::Object, returns nullptr. If the key does not exist in this object, returns nullptr. If the named value does not have the type T, returns nullptr.

Template Parameters
TThe expected type of the value.
Parameters
keyThe key for which to retrieve the value from this object.
Returns
A pointer to the requested value, or nullptr if the value cannot be obtained as requested.

Definition at line 251 of file JsonValue.h.

◆ hasKey()

bool CesiumUtility::JsonValue::hasKey ( const std::string & key) const
inlinenodiscard

Determines if this value is an Object and has the given key.

Parameters
keyThe key.
Returns
true if this value contains the key. false if it is not an object or does not contain the given key.

Definition at line 351 of file JsonValue.h.

◆ isArray()

bool CesiumUtility::JsonValue::isArray ( ) const
inlinenodiscardnoexcept

Returns whether this value is an Array value.

Definition at line 613 of file JsonValue.h.

◆ isBool()

bool CesiumUtility::JsonValue::isBool ( ) const
inlinenodiscardnoexcept

Returns whether this value is a Bool value.

Definition at line 592 of file JsonValue.h.

◆ isDouble()

bool CesiumUtility::JsonValue::isDouble ( ) const
inlinenodiscardnoexcept

Returns whether this value is a double value.

Definition at line 620 of file JsonValue.h.

◆ isInt64()

bool CesiumUtility::JsonValue::isInt64 ( ) const
inlinenodiscardnoexcept

Returns whether this value is a std::int64_t value.

Definition at line 634 of file JsonValue.h.

◆ isNull()

bool CesiumUtility::JsonValue::isNull ( ) const
inlinenodiscardnoexcept

Returns whether this value is a null value.

Definition at line 576 of file JsonValue.h.

◆ isNumber()

bool CesiumUtility::JsonValue::isNumber ( ) const
inlinenodiscardnoexcept

Returns whether this value is a double, std::uint64_t or std::int64_t. Use this function in conjunction with getNumber for safely casting to arbitrary types.

Definition at line 585 of file JsonValue.h.

◆ isObject()

bool CesiumUtility::JsonValue::isObject ( ) const
inlinenodiscardnoexcept

Returns whether this value is an Object value.

Definition at line 606 of file JsonValue.h.

◆ isString()

bool CesiumUtility::JsonValue::isString ( ) const
inlinenodiscardnoexcept

Returns whether this value is a String value.

Definition at line 599 of file JsonValue.h.

◆ isUint64()

bool CesiumUtility::JsonValue::isUint64 ( ) const
inlinenodiscardnoexcept

Returns whether this value is a std::uint64_t value.

Definition at line 627 of file JsonValue.h.

◆ operator==()

bool CesiumUtility::JsonValue::operator== ( const JsonValue & rhs) const
inlinenoexcept

Returns true if two values are equal.

Definition at line 641 of file JsonValue.h.

Member Data Documentation

◆ value

std::variant< Null, double, std::uint64_t, std::int64_t, Bool, String, Object, Array> CesiumUtility::JsonValue::value

The actual value.

The type of the value may be queried with the isNull, isDouble, isBool, isString, isObject, isUint64, isInt64, isNumber, and isArray functions.

The actual value may be obtained with the getNumber, getBool, and getString functions for the respective types. For Object values, the properties may be accessed with the getValueForKey functions.

Definition at line 710 of file JsonValue.h.


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