3#include <CesiumUtility/Library.h>
7#include <initializer_list>
28template <
typename TTo,
typename TFrom>
42template <
typename TTo,
typename TFrom>
58 using Null = std::nullptr_t;
73 using Object = std::map<std::string, JsonValue>;
78 using Array = std::vector<JsonValue>;
88 JsonValue(std::nullptr_t) noexcept : value(
nullptr) {}
96 if (std::isnan(v) || std::isinf(v)) {
107 JsonValue(std::int8_t v) noexcept : value(
static_cast<std::int64_t
>(v)) {}
113 JsonValue(std::uint8_t v) noexcept : value(
static_cast<std::uint64_t
>(v)) {}
119 JsonValue(std::int16_t v) noexcept : value(
static_cast<std::int64_t
>(v)) {}
125 JsonValue(std::uint16_t v) noexcept : value(
static_cast<std::uint64_t
>(v)) {}
131 JsonValue(std::int32_t v) noexcept : value(
static_cast<std::int64_t
>(v)) {}
137 JsonValue(std::uint32_t v) noexcept : value(
static_cast<std::uint64_t
>(v)) {}
162 JsonValue(std::string&& v) noexcept : value(std::move(v)) {}
172 JsonValue(
const std::map<std::string, JsonValue>& v) : value(v) {}
177 JsonValue(std::map<std::string, JsonValue>&& v) : value(
std::move(v)) {}
182 JsonValue(
const std::vector<JsonValue>& v) : value(v) {}
187 JsonValue(std::vector<JsonValue>&& v) noexcept : value(std::move(v)) {}
198 JsonValue(std::initializer_list<std::pair<const std::string, JsonValue>> v)
228 template <
typename T>
230 const JsonValue* pValue = this->getValuePtrForKey(key);
235 return std::get_if<T>(&pValue->
value);
253 JsonValue* pValue = this->getValuePtrForKey(key);
254 return std::get_if<T>(&pValue->
value);
274 typename std::enable_if<
275 std::is_integral<To>::value ||
276 std::is_floating_point<To>::value>::type* =
nullptr>
277 [[nodiscard]] std::optional<To>
279 const Object& pObject = std::get<Object>(this->value);
280 const auto it = pObject.find(key);
281 if (it == pObject.end()) {
284 return it->second.getSafeNumber<To>();
308 typename std::enable_if<
309 std::is_integral<To>::value ||
310 std::is_floating_point<To>::value>::type* =
nullptr>
312 const std::string& key,
313 To defaultValue)
const {
314 const Object& pObject = std::get<Object>(this->value);
315 const auto it = pObject.find(key);
316 if (it == pObject.end()) {
319 return it->second.getSafeNumberOrDefault<To>(defaultValue);
329 [[nodiscard]]
inline bool hasKey(
const std::string& key)
const {
330 const Object* pObject = std::get_if<Object>(&this->value);
335 return pObject->find(key) != pObject->end();
348 typename std::enable_if<
349 std::is_integral<To>::value ||
350 std::is_floating_point<To>::value>::type* =
nullptr>
352 const std::uint64_t* uInt = std::get_if<std::uint64_t>(&this->value);
354 return losslessNarrow<To>(*uInt);
357 const std::int64_t* sInt = std::get_if<std::int64_t>(&this->value);
359 return losslessNarrow<To>(*sInt);
362 const double* real = std::get_if<double>(&this->value);
364 return losslessNarrow<To>(*real);
379 typename std::enable_if<
380 std::is_integral<To>::value ||
381 std::is_floating_point<To>::value>::type* =
nullptr>
383 const std::uint64_t* uInt = std::get_if<std::uint64_t>(&this->value);
385 return losslessNarrowOrDefault<To>(*uInt, defaultValue);
388 const std::int64_t* sInt = std::get_if<std::int64_t>(&this->value);
390 return losslessNarrowOrDefault<To>(*sInt, defaultValue);
393 const double* real = std::get_if<double>(&this->value);
395 return losslessNarrowOrDefault<To>(*real, defaultValue);
409 return std::get<JsonValue::Object>(this->value);
419 return std::get<String>(this->value);
429 return std::get<JsonValue::Array>(this->value);
440 [[nodiscard]] std::vector<std::string>
450 return std::get<bool>(this->value);
459 return std::get<double>(this->value);
469 return std::get<std::uint64_t>(this->value);
479 return std::get<std::int64_t>(this->value);
487 const auto* v = std::get_if<bool>(&this->value);
501 const auto* v = std::get_if<JsonValue::String>(&this->value);
513 [[nodiscard]]
inline double
515 const auto* v = std::get_if<double>(&this->value);
527 [[nodiscard]]
inline std::uint64_t
529 const auto* v = std::get_if<std::uint64_t>(&this->value);
541 [[nodiscard]]
inline std::int64_t
543 const auto* v = std::get_if<std::int64_t>(&this->value);
554 [[nodiscard]]
inline bool isNull() const noexcept {
555 return std::holds_alternative<Null>(this->value);
563 [[nodiscard]]
inline bool isNumber() const noexcept {
564 return isDouble() || isUint64() || isInt64();
570 [[nodiscard]]
inline bool isBool() const noexcept {
571 return std::holds_alternative<Bool>(this->value);
577 [[nodiscard]]
inline bool isString() const noexcept {
578 return std::holds_alternative<String>(this->value);
584 [[nodiscard]]
inline bool isObject() const noexcept {
585 return std::holds_alternative<Object>(this->value);
591 [[nodiscard]]
inline bool isArray() const noexcept {
592 return std::holds_alternative<Array>(this->value);
598 [[nodiscard]]
inline bool isDouble() const noexcept {
599 return std::holds_alternative<double>(this->value);
605 [[nodiscard]]
inline bool isUint64() const noexcept {
606 return std::holds_alternative<std::uint64_t>(this->value);
612 [[nodiscard]]
inline bool isInt64() const noexcept {
613 return std::holds_alternative<std::int64_t>(this->value);
620 return this->value == rhs.value;
628 int64_t operator()([[maybe_unused]]
const Null& ) {
return 0; }
629 int64_t operator()([[maybe_unused]]
const double& ) {
632 int64_t operator()([[maybe_unused]]
const std::uint64_t& ) {
635 int64_t operator()([[maybe_unused]]
const std::int64_t& ) {
638 int64_t operator()([[maybe_unused]]
const Bool& ) {
return 0; }
639 int64_t operator()(
const String& inside) {
640 return int64_t(inside.capacity() *
sizeof(
char));
642 int64_t operator()(
const Object& inside) {
645 int64_t(inside.size() * (
sizeof(std::string) +
sizeof(
JsonValue)));
646 for (
const auto& [k, v] : inside) {
647 accum += int64_t(k.capacity() *
sizeof(
char) -
sizeof(std::string));
648 accum += v.getSizeBytes() - int64_t(
sizeof(
JsonValue));
653 int64_t operator()(
const Array& inside) {
655 accum += int64_t(
sizeof(
JsonValue) * inside.capacity());
657 accum += v.getSizeBytes() - int64_t(
sizeof(
JsonValue));
663 return static_cast<int64_t
>(
sizeof(
JsonValue)) +
664 std::visit(Operation{}, this->value);
A generic implementation of a value in a JSON structure.
std::vector< std::string > getArrayOfStrings(const std::string &defaultString) const
Gets an array of strings from the value.
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).
std::optional< To > getSafeNumericalValueForKey(const std::string &key) const
Converts the numerical value corresponding to the given key to the provided numerical template type.
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.
bool isInt64() const noexcept
Returns whether this value is a std::int64_t value.
const JsonValue::String & getString() const
Gets the string from the 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 * getValuePtrForKey(const std::string &key)
Attempts to obtain a pointer to a JsonValue for the given key on this object.
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.
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.
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.
const T * getValuePtrForKey(const std::string &key) const
Gets a typed value corresponding to the given key in the object represented by this instance.
std::uint64_t getUint64() const
Gets the std::uint64_t from the value.
bool operator==(const JsonValue &rhs) const noexcept
Returns true if two values are equal.
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.
bool isUint64() const noexcept
Returns whether this value is a std::uint64_t value.
T * getValuePtrForKey(const std::string &key)
Gets a typed value corresponding to the given key in the object represented by this instance.
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.
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).
const JsonValue::Object & getObject() const
Gets the object from the value.
const JsonValue * getValuePtrForKey(const std::string &key) const
Attempts to obtain a pointer to a JsonValue for the given key on this object.
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).
int64_t getSizeBytes() const noexcept
Returns the size in bytes of this JsonValue.
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.
std::optional< To > getSafeNumber() const
Gets the numerical quantity from the value casted to the To type. This function should be used over g...
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.
JsonValue(std::initializer_list< std::pair< const std::string, JsonValue > > v)
Creates an JSON value from the given initializer list.
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.
constexpr TTo losslessNarrowOrDefault(TFrom from, TTo defaultValue) noexcept
Attempts a narrowing conversion of TFrom into TTo without losing information. If a lossless conversio...
std::optional< TTo > losslessNarrow(TFrom from) noexcept
Attempts a narrowing conversion of TFrom into TTo without losing information. If a lossless conversio...