9 #include <initializer_list>
14 #include <string_view>
15 #include <type_traits>
23 : std::runtime_error(key +
" is not present in Object") {}
28 : std::runtime_error(
"this->value was not double, uint64_t or int64_t") {}
31 template <
typename T,
typename U>
32 constexpr std::optional<T> losslessNarrow(U u) noexcept {
33 constexpr
const bool is_different_signedness =
34 (std::is_signed<T>::value != std::is_signed<U>::value);
36 const T t = gsl::narrow_cast<T>(u);
38 if (
static_cast<U
>(t) != u ||
39 (is_different_signedness && ((t < T{}) != (u < U{})))) {
46 template <
typename T,
typename U>
47 constexpr T losslessNarrowOrDefault(U u, T defaultValue) noexcept {
48 constexpr
const bool is_different_signedness =
49 (std::is_signed<T>::value != std::is_signed<U>::value);
51 const T t = gsl::narrow_cast<T>(u);
53 if (
static_cast<U
>(t) != u ||
54 (is_different_signedness && ((t < T{}) != (u < U{})))) {
72 using Null = std::nullptr_t;
87 using Object = std::map<std::string, JsonValue>;
92 using Array = std::vector<JsonValue>;
110 if (std::isnan(v) || std::isinf(v)) {
121 JsonValue(std::int8_t v) noexcept : value(
static_cast<std::int64_t
>(v)) {}
127 JsonValue(std::uint8_t v) noexcept : value(
static_cast<std::uint64_t
>(v)) {}
133 JsonValue(std::int16_t v) noexcept : value(
static_cast<std::int64_t
>(v)) {}
139 JsonValue(std::uint16_t v) noexcept : value(
static_cast<std::uint64_t
>(v)) {}
145 JsonValue(std::int32_t v) noexcept : value(
static_cast<std::int64_t
>(v)) {}
151 JsonValue(std::uint32_t v) noexcept : value(
static_cast<std::uint64_t
>(v)) {}
176 JsonValue(std::string&& v) noexcept : value(std::move(v)) {}
186 JsonValue(
const std::map<std::string, JsonValue>& v) : value(v) {}
191 JsonValue(std::map<std::string, JsonValue>&& v) : value(std::move(v)) {}
196 JsonValue(
const std::vector<JsonValue>& v) : value(v) {}
201 JsonValue(std::vector<JsonValue>&& v) noexcept : value(std::move(v)) {}
212 JsonValue(std::initializer_list<std::pair<const std::string, JsonValue>> v)
213 : value(std::map<std::string,
JsonValue>(v)) {}
216 getValuePtrForKey(
const std::string& key)
const;
217 [[nodiscard]]
JsonValue* getValuePtrForKey(
const std::string& key);
233 template <
typename T>
235 const JsonValue* pValue = this->getValuePtrForKey(key);
240 return std::get_if<T>(&pValue->
value);
258 JsonValue* pValue = this->getValuePtrForKey(key);
259 return std::get_if<T>(&pValue->
value);
283 typename std::enable_if<
284 std::is_integral<To>::value ||
285 std::is_floating_point<To>::value>::type* =
nullptr>
287 const Object& pObject = std::get<Object>(this->value);
288 const auto it = pObject.find(key);
289 if (it == pObject.end()) {
292 return it->second.getSafeNumber<To>();
314 typename std::enable_if<
315 std::is_integral<To>::value ||
316 std::is_floating_point<To>::value>::type* =
nullptr>
318 const std::string& key,
319 To defaultValue)
const {
320 const Object& pObject = std::get<Object>(this->value);
321 const auto it = pObject.find(key);
322 if (it == pObject.end()) {
325 return it->second.getSafeNumberOrDefault<To>(defaultValue);
335 [[nodiscard]]
inline bool hasKey(
const std::string& key)
const {
336 const Object* pObject = std::get_if<Object>(&this->value);
341 return pObject->find(key) != pObject->end();
355 typename std::enable_if<
356 std::is_integral<To>::value ||
357 std::is_floating_point<To>::value>::type* =
nullptr>
359 const std::uint64_t* uInt = std::get_if<std::uint64_t>(&this->value);
361 return gsl::narrow<To>(*uInt);
364 const std::int64_t* sInt = std::get_if<std::int64_t>(&this->value);
366 return gsl::narrow<To>(*sInt);
369 const double* real = std::get_if<double>(&this->value);
371 return gsl::narrow<To>(*real);
386 typename std::enable_if<
387 std::is_integral<To>::value ||
388 std::is_floating_point<To>::value>::type* =
nullptr>
390 const std::uint64_t* uInt = std::get_if<std::uint64_t>(&this->value);
392 return losslessNarrowOrDefault<To>(*uInt, defaultValue);
395 const std::int64_t* sInt = std::get_if<std::int64_t>(&this->value);
397 return losslessNarrowOrDefault<To>(*sInt, defaultValue);
400 const double* real = std::get_if<double>(&this->value);
402 return losslessNarrowOrDefault<To>(*real, defaultValue);
416 return std::get<JsonValue::Object>(this->value);
426 return std::get<String>(this->value);
436 return std::get<JsonValue::Array>(this->value);
447 [[nodiscard]] std::vector<std::string>
457 return std::get<bool>(this->value);
466 return std::get<double>(this->value);
476 return std::get<std::uint64_t>(this->value);
486 return std::get<std::int64_t>(this->value);
494 const auto* v = std::get_if<bool>(&this->value);
508 const auto* v = std::get_if<JsonValue::String>(&this->value);
520 [[nodiscard]]
inline double
522 const auto* v = std::get_if<double>(&this->value);
534 [[nodiscard]]
inline std::uint64_t
536 const auto* v = std::get_if<std::uint64_t>(&this->value);
548 [[nodiscard]]
inline std::int64_t
550 const auto* v = std::get_if<std::int64_t>(&this->value);
561 [[nodiscard]]
inline bool isNull() const noexcept {
562 return std::holds_alternative<Null>(this->value);
570 [[nodiscard]]
inline bool isNumber() const noexcept {
571 return isDouble() || isUint64() || isInt64();
577 [[nodiscard]]
inline bool isBool() const noexcept {
578 return std::holds_alternative<Bool>(this->value);
584 [[nodiscard]]
inline bool isString() const noexcept {
585 return std::holds_alternative<String>(this->value);
591 [[nodiscard]]
inline bool isObject() const noexcept {
592 return std::holds_alternative<Object>(this->value);
598 [[nodiscard]]
inline bool isArray() const noexcept {
599 return std::holds_alternative<Array>(this->value);
605 [[nodiscard]]
inline bool isDouble() const noexcept {
606 return std::holds_alternative<double>(this->value);
612 [[nodiscard]]
inline bool isUint64() const noexcept {
613 return std::holds_alternative<std::uint64_t>(this->value);
619 [[nodiscard]]
inline bool isInt64() const noexcept {
620 return std::holds_alternative<std::int64_t>(this->value);
A generic implementation of a value in a JSON structure.
To getSafeNumericalValueForKey(const std::string &key) const
Converts the numerical value corresponding to the given key to the provided numerical template type.
std::map< std::string, JsonValue > Object
The type to represent an Object JSON value.
JsonValue(std::int8_t v) noexcept
Creates a std::int64_t JSON value (Widening conversion from std::int8_t).
bool getBool() const
Gets the bool from the value.
JsonValue(const std::map< std::string, JsonValue > &v)
Creates an Object JSON value with the given properties.
std::vector< std::string > getArrayOfStrings(const std::string &defaultString) const
Gets an array of strings from the value.
bool isInt64() const noexcept
Returns whether this value is a std::int64_t value.
JsonValue(std::int16_t v) noexcept
Creates a std::int64_t JSON value (Widening conversion from std::int16_t).
bool isNumber() const noexcept
Returns whether this value is a double, std::uint64_t or std::int64_t. Use this function in conjuncti...
bool isDouble() const noexcept
Returns whether this value is a double value.
JsonValue(std::vector< JsonValue > &&v) noexcept
Creates an Array JSON value with the given elements.
std::vector< JsonValue > Array
The type to represent an Array JSON value.
double getDoubleOrDefault(double defaultValue) const noexcept
Gets the double from the value or returns defaultValue.
JsonValue(std::initializer_list< std::pair< const std::string, JsonValue >> v)
Creates an JSON value from the given initializer list.
JsonValue(std::initializer_list< JsonValue > v)
Creates an JSON value from the given initializer list.
JsonValue(std::uint16_t v) noexcept
Creates a std::uint64_t JSON value (Widening conversion from std::uint16_t).
std::variant< Null, double, std::uint64_t, std::int64_t, Bool, String, Object, Array > value
The actual value.
To getSafeNumber() const
Gets the numerical quantity from the value casted to the To type. This function should be used over g...
const T * getValuePtrForKey(const std::string &key) const
Gets a typed value corresponding to the given key in the object represented by this instance.
bool isBool() const noexcept
Returns whether this value is a Bool value.
const JsonValue::String getStringOrDefault(String defaultValue) const
Gets the string from the value or returns defaultValue.
JsonValue(std::nullptr_t) noexcept
Creates a null JSON value.
JsonValue(std::string &&v) noexcept
Creates a String JSON value.
const JsonValue::Object & getObject() const
Gets the object from the value.
bool Bool
The type to represent a Bool JSON value.
JsonValue(bool v) noexcept
Creates a Bool JSON value.
bool isNull() const noexcept
Returns whether this value is a null value.
std::uint64_t getUint64() const
Gets the std::uint64_t from the value.
std::int64_t getInt64OrDefault(std::int64_t defaultValue) const noexcept
Gets the int64_t from the value or returns defaultValue.
std::string String
The type to represent a String JSON value.
JsonValue(const char *v)
Creates a String JSON value.
const JsonValue::String & getString() const
Gets the string from the value.
bool isUint64() const noexcept
Returns whether this value is a std::uint64_t value.
JsonValue(std::int64_t v) noexcept
Creates a std::int64_t JSON value.
bool isArray() const noexcept
Returns whether this value is an Array value.
JsonValue() noexcept
Default constructor.
bool isString() const noexcept
Returns whether this value is a String value.
T * getValuePtrForKey(const std::string &key)
Gets a typed value corresponding to the given key in the object represented by this instance.
bool isObject() const noexcept
Returns whether this value is an Object value.
JsonValue(double v) noexcept
Creates a Number JSON value.
std::int64_t getInt64() const
Gets the std::int64_t from the value.
JsonValue(std::map< std::string, JsonValue > &&v)
Creates an Object JSON value with the given properties.
bool getBoolOrDefault(bool defaultValue) const noexcept
Gets the bool from the value or returns defaultValue.
To getSafeNumberOrDefault(To defaultValue) const noexcept
Gets the numerical quantity from the value casted to the To type or returns defaultValue if unable to...
double getDouble() const
Gets the double from the value.
JsonValue(const std::string &v)
Creates a String JSON value.
JsonValue(std::uint32_t v) noexcept
Creates a std::uint64_t JSON value (Widening conversion from std::uint32_t).
std::uint64_t getUint64OrDefault(std::uint64_t defaultValue) const noexcept
Gets the uint64_t from the value or returns defaultValue.
JsonValue(std::uint8_t v) noexcept
Creates a std::uint64_t JSON value (Widening conversion from std::uint8_t).
JsonValue(std::int32_t v) noexcept
Creates a std::int64_t JSON value (Widening conversion from std::int32_t).
JsonValue(std::uint64_t v) noexcept
Creates a std::uint64_t JSON value.
To getSafeNumericalValueOrDefaultForKey(const std::string &key, To defaultValue) const
Converts the numerical value corresponding to the given key to the provided numerical template type.
const JsonValue::Array & getArray() const
Gets the array from the value.
JsonValue(const std::vector< JsonValue > &v)
Creates an Array JSON value with the given elements.
std::nullptr_t Null
The type to represent a null JSON value.
bool hasKey(const std::string &key) const
Determines if this value is an Object and has the given key.
Utility classes for Cesium.