7#include <initializer_list>
21using Array = std::vector<Value>;
27struct TransparentStringHash {
28 using is_transparent = void;
30 [[nodiscard]] std::size_t operator()(
const std::string_view sv)
const noexcept {
31 return std::hash<std::string_view>{}(sv);
33 [[nodiscard]] std::size_t operator()(
const std::string& s)
const noexcept {
34 return std::hash<std::string_view>{}(s);
36 [[nodiscard]] std::size_t operator()(
const char* s)
const noexcept {
37 return std::hash<std::string_view>{}(std::string_view{s});
44concept SignedIntLike =
45 std::is_integral_v<T> && std::is_signed_v<T> && !std::is_same_v<std::remove_cv_t<T>,
bool> &&
46 !std::is_same_v<std::remove_cv_t<T>,
char> && !std::is_same_v<std::remove_cv_t<T>,
wchar_t> &&
47 !std::is_same_v<std::remove_cv_t<T>,
char8_t> &&
48 !std::is_same_v<std::remove_cv_t<T>,
char16_t> &&
49 !std::is_same_v<std::remove_cv_t<T>,
char32_t>;
52concept UnsignedIntLike =
53 std::is_integral_v<T> && std::is_unsigned_v<T> && !std::is_same_v<std::remove_cv_t<T>,
bool> &&
54 !std::is_same_v<std::remove_cv_t<T>,
char> && !std::is_same_v<std::remove_cv_t<T>,
wchar_t> &&
55 !std::is_same_v<std::remove_cv_t<T>,
char8_t> &&
56 !std::is_same_v<std::remove_cv_t<T>,
char16_t> &&
57 !std::is_same_v<std::remove_cv_t<T>,
char32_t>;
60concept FloatLike = std::is_floating_point_v<T>;
82 std::unique_ptr<Object>>;
112 Value& operator=(std::initializer_list<
Value> il);
114 Value& operator=(std::initializer_list<std::pair<const std::
string,
Value>> il);
117 Value(std::nullptr_t) noexcept;
121 requires std::same_as<B,
bool>
125 template <detail::SignedIntLike T>
129 template <detail::UnsignedIntLike T>
135 template <detail::FloatLike T>
139 Value(std::
string s);
141 Value(std::string_view s);
143 Value(const
char* s);
157 Value(std::initializer_list<std::pair<const std::
string,
Value>> il);
166 [[nodiscard]]
bool is_null() const noexcept;
168 [[nodiscard]]
bool is_bool() const noexcept;
170 [[nodiscard]]
bool is_int() const noexcept;
172 [[nodiscard]]
bool is_uint() const noexcept;
174 [[nodiscard]]
bool is_float() const noexcept;
176 [[nodiscard]]
bool is_double() const noexcept;
178 [[nodiscard]]
bool is_number() const noexcept;
180 [[nodiscard]]
bool is_string() const noexcept;
182 [[nodiscard]]
bool is_array() const noexcept;
184 [[nodiscard]]
bool is_object() const noexcept;
189 [[nodiscard]]
bool as_bool() const;
191 [[nodiscard]] std::int64_t
as_int() const;
193 [[nodiscard]] std::uint64_t
as_uint() const;
195 [[nodiscard]]
float as_float() const;
202 [[nodiscard]] const std::
string&
as_string() const;
206 [[nodiscard]] const Array&
as_array() const;
217 [[nodiscard]] const
bool*
as_bool_if() const noexcept;
219 [[nodiscard]] std::int64_t*
as_int_if() noexcept;
221 [[nodiscard]] const std::int64_t*
as_int_if() const noexcept;
223 [[nodiscard]] std::uint64_t*
as_uint_if() noexcept;
225 [[nodiscard]] const std::uint64_t*
as_uint_if() const noexcept;
229 [[nodiscard]] const
float*
as_float_if() const noexcept;
233 [[nodiscard]] const
double*
as_double_if() const noexcept;
237 [[nodiscard]] const std::
string*
as_string_if() const noexcept;
241 [[nodiscard]] const Array*
as_array_if() const noexcept;
249 [[nodiscard]] T*
get_if() noexcept;
252 [[nodiscard]] const T*
get_if() const noexcept;
256 [[nodiscard]] T
value_or(T fallback) const;
264 [[nodiscard]] std::
size_t index() const noexcept;
267 friend
bool operator==(const
Value& a, const
Value& b) noexcept;
269 friend
bool operator!=(const
Value& a, const
Value& b) noexcept;
278[[nodiscard]]
Value null() noexcept;
280[[nodiscard]]
Value boolean(
bool b) noexcept;
284 requires(detail::SignedIntLike<T> || detail::UnsignedIntLike<T> || detail::FloatLike<T>)
285[[nodiscard]] inline
Value number(T v) noexcept {
290[[nodiscard]]
Value string(std::string s);
292[[nodiscard]]
Value string(std::string_view s);
294[[nodiscard]]
Value string(
const char* s);
297[[nodiscard]]
inline Array array() {
301[[nodiscard]]
inline Array array(
const std::initializer_list<Value> il) {
Ordered-by-insertion-time-ish string-keyed map of Values, with transparent string-view lookup and JSO...
Definition object.hpp:21
Discriminated union holding one of the JSON-like alternatives (null, bool, signed/unsigned integer,...
Definition value.hpp:67
std::string * as_string_if() noexcept
Return a pointer to the string, or nullptr if not held.
Definition object.hpp:539
bool is_int() const noexcept
True if the value holds a signed integer (std::int64_t).
Definition object.hpp:433
bool is_bool() const noexcept
True if the value holds a bool.
Definition object.hpp:430
bool is_uint() const noexcept
True if the value holds an unsigned integer (std::uint64_t).
Definition object.hpp:436
bool is_object() const noexcept
True if the value holds a nested Object.
Definition object.hpp:454
std::string & as_string()
Access the string alternative; throws on type mismatch.
Definition object.hpp:488
bool is_null() const noexcept
True if the value holds nullptr.
Definition object.hpp:427
std::int64_t * as_int_if() noexcept
Return a pointer to the signed integer, or nullptr if not held.
Definition object.hpp:514
double as_double() const
Return as double, widening from int64/uint64/float as needed.
Definition object.hpp:472
Value() noexcept
Construct a null-valued Value.
Definition object.hpp:351
bool is_double() const noexcept
True if the value holds a double.
Definition object.hpp:442
std::int64_t as_int() const
Return the signed integer; throws on type mismatch.
Definition object.hpp:461
std::size_t index() const noexcept
Return the zero-based index of the active alternative.
Definition object.hpp:611
float as_float() const
Return the float; strict — throws if the value isn't a float.
Definition object.hpp:468
bool is_float() const noexcept
True if the value holds a float.
Definition object.hpp:439
T value_or(T fallback) const
Return the held T by value, or fallback if a different alternative is active.
Definition object.hpp:597
bool is_array() const noexcept
True if the value holds an Array.
Definition object.hpp:451
std::uint64_t as_uint() const
Return the unsigned integer; throws on type mismatch.
Definition object.hpp:464
Object * as_object_if() noexcept
Return a pointer to the nested object, or nullptr if not held.
Definition object.hpp:552
std::variant< std::nullptr_t, bool, std::int64_t, std::uint64_t, float, double, std::string, Array, std::unique_ptr< Object > > variant_type
Underlying std::variant type that stores the active alternative.
Definition value.hpp:82
T * get_if() noexcept
Return a pointer to the alternative of type T, or nullptr if not held.
Definition object.hpp:588
Array & as_array()
Access the array alternative; throws on type mismatch.
Definition object.hpp:494
std::uint64_t * as_uint_if() noexcept
Return a pointer to the unsigned integer, or nullptr if not held.
Definition object.hpp:520
variant_type & raw() noexcept
Return the underlying std::variant for advanced access.
Definition object.hpp:604
bool is_number() const noexcept
True if the value holds any numeric alternative.
Definition object.hpp:445
bool is_string() const noexcept
True if the value holds a std::string.
Definition object.hpp:448
bool as_bool() const
Return the bool; throws std::bad_variant_access on type mismatch.
Definition object.hpp:458
Array * as_array_if() noexcept
Return a pointer to the array, or nullptr if not held.
Definition object.hpp:545
bool * as_bool_if() noexcept
Return a pointer to the bool, or nullptr if the value isn't a bool.
Definition object.hpp:508
double * as_double_if() noexcept
Return a pointer to the double, or nullptr if not held.
Definition object.hpp:532
Object & as_object()
Access the nested object; throws on type mismatch.
Definition object.hpp:501
float * as_float_if() noexcept
Return a pointer to the float, or nullptr if not held.
Definition object.hpp:526