cesium-native  0.41.0
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. More...
 
 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
 
JsonValuegetValuePtrForKey (const std::string &key)
 
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. More...
 
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. More...
 
template<typename To , typename std::enable_if< std::is_integral< To >::value||std::is_floating_point< To >::value >::type * = nullptr>
To getSafeNumericalValueForKey (const std::string &key) const
 Converts the numerical value corresponding to the given key to the provided numerical template type. More...
 
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. More...
 
bool hasKey (const std::string &key) const
 Determines if this value is an Object and has the given key. More...
 
template<typename To , typename std::enable_if< std::is_integral< To >::value||std::is_floating_point< To >::value >::type * = nullptr>
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. More...
 
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. More...
 
const JsonValue::ObjectgetObject () const
 Gets the object from the value. More...
 
const JsonValue::StringgetString () const
 Gets the string from the value. More...
 
const JsonValue::ArraygetArray () const
 Gets the array from the value. More...
 
std::vector< std::string > getArrayOfStrings (const std::string &defaultString) const
 Gets an array of strings from the value. More...
 
bool getBool () const
 Gets the bool from the value. More...
 
double getDouble () const
 Gets the double from the value. More...
 
std::uint64_t getUint64 () const
 Gets the std::uint64_t from the value. More...
 
std::int64_t getInt64 () const
 Gets the std::int64_t from the value. More...
 
bool getBoolOrDefault (bool defaultValue) const noexcept
 Gets the bool from the value or returns defaultValue. More...
 
const JsonValue::String getStringOrDefault (String defaultValue) const
 Gets the string from the value or returns defaultValue. More...
 
double getDoubleOrDefault (double defaultValue) const noexcept
 Gets the double from the value or returns defaultValue. More...
 
std::uint64_t getUint64OrDefault (std::uint64_t defaultValue) const noexcept
 Gets the uint64_t from the value or returns defaultValue. More...
 
std::int64_t getInt64OrDefault (std::int64_t defaultValue) const noexcept
 Gets the int64_t from the value or returns defaultValue. More...
 
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.
 

Public Attributes

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

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 67 of file JsonValue.h.

Constructor & Destructor Documentation

◆ JsonValue()

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

Creates a Number JSON value.

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

Definition at line 109 of file JsonValue.h.

Member Function Documentation

◆ getArray()

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

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 435 of file JsonValue.h.

◆ getArrayOfStrings()

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

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
inline

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 456 of file JsonValue.h.

◆ getBoolOrDefault()

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

Gets the bool from the value or returns defaultValue.

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

Definition at line 493 of file JsonValue.h.

◆ getDouble()

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

Gets the double from the value.

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

Definition at line 465 of file JsonValue.h.

◆ getDoubleOrDefault()

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

Gets the double from the value or returns defaultValue.

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

Definition at line 521 of file JsonValue.h.

◆ getInt64()

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

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 485 of file JsonValue.h.

◆ getInt64OrDefault()

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

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 549 of file JsonValue.h.

◆ getObject()

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

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 415 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>
To CesiumUtility::JsonValue::getSafeNumber ( ) const
inline

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 can be cast without precision loss.
Exceptions
Ifthe underlying value is not a numerical type or it cannot be converted without precision loss.

Definition at line 358 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
inlinenoexcept

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 389 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>
To CesiumUtility::JsonValue::getSafeNumericalValueForKey ( const std::string &  key) const
inline

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

If this instance is not a JsonValue::Object, throws std::bad_variant_access. If the key does not exist in this object, throws JsonValueMissingKey. If the named value does not have a numerical type T, throws JsonValueNotRealValue, if the named value cannot be converted from double / std::uint64_t / std::int64_t without precision loss, throws gsl::narrowing_error

Template Parameters
ToThe expected type of the value.
Parameters
keyThe key for which to retrieve the value from this object.
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 286 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
inline

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.
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 317 of file JsonValue.h.

◆ getString()

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

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 425 of file JsonValue.h.

◆ getStringOrDefault()

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

Gets the string from the value or returns defaultValue.

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

Definition at line 507 of file JsonValue.h.

◆ getUint64()

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

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 475 of file JsonValue.h.

◆ getUint64OrDefault()

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

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 535 of file JsonValue.h.

◆ getValuePtrForKey() [1/2]

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 257 of file JsonValue.h.

◆ getValuePtrForKey() [2/2]

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 234 of file JsonValue.h.

◆ hasKey()

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

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 335 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 644 of file JsonValue.h.


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