libdonut  2.3.2
Application framework for cross-platform game development in C++20
Public Member Functions | Static Public Member Functions | List of all members
donut::json::Value Class Reference

JSON value type. More...

#include <donut/json.hpp>

Inheritance diagram for donut::json::Value:
donut::Variant< Null, Boolean, String, Number, Object, Array >

Public Member Functions

 Value () noexcept=default
 Construct a Null value. More...
 
 Value (Null) noexcept
 Construct a Null value. More...
 
 Value (std::nullptr_t) noexcept
 Construct a Null value. More...
 
 Value (Boolean value) noexcept
 Construct a Boolean value with the given underlying value. More...
 
 Value (const String &value)
 Construct a String value with the given underlying value. More...
 
 Value (String &&value) noexcept
 Construct a String value from the given underlying value. More...
 
 Value (const char *value)
 Construct a String value with the given underlying value. More...
 
 Value (std::string_view value)
 Construct a String value with the given underlying value. More...
 
 Value (const char8_t *value)
 Construct a String value with the given underlying value. More...
 
 Value (std::u8string_view value)
 Construct a String value with the given underlying value. More...
 
 Value (detail::number auto value) noexcept
 Construct a Number value with the given underlying value. More...
 
 Value (const Object &value)
 Construct an Object value with the given underlying value. More...
 
 Value (Object &&value) noexcept
 Construct an Object value from the given underlying value. More...
 
 Value (const Array &value)
 Construct an Array value with the given underlying value. More...
 
 Value (Array &&value) noexcept
 Construct an Array value from the given underlying value. More...
 
std::string toString (const SerializationOptions &options={}) const
 Get a JSON string representation of the value. More...
 
bool operator== (const Value &other) const
 Compare this value to another for equality. More...
 
std::partial_ordering operator<=> (const Value &other) const
 Compare this value to another. More...
 
- Public Member Functions inherited from donut::Variant< Null, Boolean, String, Number, Object, Array >
constexpr Variant () noexcept(std::is_nothrow_default_constructible_v< variant_alternative_t< 0, Variant >>) requires(HAS_DEFAULT_CONSTRUCTOR)
 Default-construct a variant with the first variant alternative, if it is default-constructible. More...
 
constexpr Variant (U &&value) noexcept(std::is_nothrow_constructible_v< decltype(F(std::forward< U >(value)))>) requires(!std
 Converting constructor. More...
 
constexpr Variant (std::in_place_type_t< T > type, Args &&... args) requires(variant_has_alternative_v< T
 Construct a variant alternative in-place given its type. More...
 
constexpr Variant (std::in_place_type_t< T > type, std::initializer_list< U > ilist, Args &&... args) requires(variant_has_alternative_v< T
 Construct a variant alternative in-place given its type, with an initializer list as the first constructor argument. More...
 
constexpr Variant (std::in_place_index_t< Index > index, Args &&... args) requires(Index< sizeof...(Ts) &&std
 Construct a variant alternative in-place given its index. More...
 
constexpr Variant (std::in_place_index_t< Index > index, std::initializer_list< U > ilist, Args &&... args) requires(Index< sizeof...(Ts) &&std
 Construct a variant alternative in-place given its index, with an initializer list as the first constructor argument. More...
 
constexpr Variant (const Variant &other) requires(!HAS_COPY_CONSTRUCTOR)=delete
 Deleted copy constructor. More...
 
constexpr Variant (const Variant &other) requires(HAS_COPY_CONSTRUCTOR &&HAS_TRIVIAL_COPY_CONSTRUCTOR)=default
 Trivial copy constructor. More...
 
constexpr Variant (const Variant &other) requires(HAS_COPY_CONSTRUCTOR &&!HAS_TRIVIAL_COPY_CONSTRUCTOR)
 Copy constructor. More...
 
constexpr Variant (Variant &&other) noexcept requires(HAS_MOVE_CONSTRUCTOR &&HAS_TRIVIAL_MOVE_CONSTRUCTOR)=default
 Trivial move constructor. More...
 
constexpr Variant (Variant &&other) noexcept((std::is_nothrow_move_constructible_v< Ts > &&...)) requires(HAS_MOVE_CONSTRUCTOR &&!HAS_TRIVIAL_MOVE_CONSTRUCTOR)
 Move constructor. More...
 
constexpr Variant std::forward (args)...)
 
constexpr Variant std::forward (args)...)
 
constexpr ~Variant ()
 Destructor. More...
 
constexpr ~Variant () requires(HAS_TRIVIAL_DESTRUCTOR)=default
 Trivial destructor. More...
 
constexpr Variantoperator= (const Variant &other) requires(!HAS_COPY_ASSIGNMENT)=delete
 Deleted copy assignment. More...
 
constexpr Variantoperator= (const Variant &other) requires(HAS_COPY_ASSIGNMENT &&HAS_TRIVIAL_COPY_ASSIGNMENT)=default
 Trivial copy assignment. More...
 
constexpr Variantoperator= (const Variant &other) requires(HAS_COPY_ASSIGNMENT &&!HAS_TRIVIAL_COPY_ASSIGNMENT)
 Copy assignment. More...
 
constexpr Variantoperator= (Variant &&other) noexcept requires(HAS_MOVE_ASSIGNMENT &&HAS_TRIVIAL_MOVE_ASSIGNMENT)=default
 Trivial move assignment. More...
 
constexpr Variantoperator= (Variant &&other) noexcept(((std::is_nothrow_move_constructible_v< Ts > &&//NOLINT(performance-noexcept-move-constructor, cppcoreguidelines-noexcept-move-operations) std::is_nothrow_move_assignable_v< Ts >)&&...)) requires(HAS_MOVE_ASSIGNMENT &&!HAS_TRIVIAL_MOVE_ASSIGNMENT)
 Move assignment. More...
 
constexpr Variantoperator= (U &&value) noexcept(std::is_nothrow_assignable_v< decltype(F(std::forward< U >(value)))&, U > &&std::is_nothrow_constructible_v< decltype(F(std::forward< U >(value))), U >) requires(!std
 Converting assignment. More...
 
constexpr T & emplace (Args &&... args) requires(variant_has_alternative_v< T
 Construct an alternative given its type, destroying the old value. More...
 
constexpr T & emplace (std::initializer_list< U > ilist, Args &&... args) requires(variant_has_alternative_v< T
 Construct an alternative given its type, with an initializer list as the first constructor argument, destroying the old value. More...
 
constexpr variant_alternative_t< Index, Variant > & emplace (Args &&... args) requires(std
 Construct an alternative given its index, destroying the old value. More...
 
constexpr variant_alternative_t< Index, Variant > & emplace (std::initializer_list< U > ilist, Args &&... args) requires(std
 Construct an alternative given its index, with an initializer list as the first constructor argument, destroying the old value. More...
 
constexpr void swap (Variant &other) noexcept(((std::is_nothrow_move_constructible_v< Ts > &&std::is_nothrow_swappable_v< Ts >)&&...))
 Swap this variant's value with that of another. More...
 
constexpr index_type index () const noexcept
 Get the alternative index of the currently active value held by the variant. More...
 
constexpr bool valueless_by_exception () const noexcept
 Check if the variant is in the valueless by exception state. More...
 
constexpr bool is () const noexcept requires(variant_has_alternative_v< T
 Check if the variant currently holds the alternative with the given type. More...
 
constexpr bool is () const noexcept
 Check if the variant currently holds the alternative with the given index. More...
 
constexpr T & as () &noexcept requires(variant_has_alternative_v< T
 Access the underlying value with the given type without a safety check. More...
 
return as ()
 
constexpr const T & as () const &noexcept requires(variant_has_alternative_v< T
 Access the underlying value with the given type without a safety check. More...
 
return as ()
 
constexpr T && as () &&noexcept requires(variant_has_alternative_v< T
 Access the underlying value with the given type without a safety check. More...
 
constexpr const T && as () const &&noexcept requires(variant_has_alternative_v< T
 Access the underlying value with the given type without a safety check. More...
 
constexpr variant_alternative_t< Index, Variant > & as () &noexcept
 Access the underlying value with the given index without a safety check. More...
 
constexpr const variant_alternative_t< Index, Variant > & as () const &noexcept
 Access the underlying value with the given index without a safety check. More...
 
constexpr variant_alternative_t< Index, Variant > && as () &&noexcept
 Access the underlying value with the given index without a safety check. More...
 
constexpr const variant_alternative_t< Index, Variant > && as () const &&noexcept
 Access the underlying value with the given index without a safety check. More...
 
return std::move (as< variant_index_v< T, Variant >>())
 
return std::move (as< variant_index_v< T, Variant >>())
 
constexpr T & get () &requires(variant_has_alternative_v< T
 Access the underlying value with the given type. More...
 
constexpr const T & get () const &requires(variant_has_alternative_v< T
 Access the underlying value with the given type. More...
 
constexpr T && get () &&requires(variant_has_alternative_v< T
 Access the underlying value with the given type. More...
 
constexpr const T && get () const &&requires(variant_has_alternative_v< T
 Access the underlying value with the given type. More...
 
constexpr variant_alternative_t< Index, Variant > & get () &
 Access the underlying value with the given index. More...
 
constexpr const variant_alternative_t< Index, Variant > & get () const &
 Access the underlying value with the given index. More...
 
constexpr variant_alternative_t< Index, Variant > && get () &&
 Access the underlying value with the given index. More...
 
constexpr const variant_alternative_t< Index, Variant > && get () const &&
 Access the underlying value with the given index. More...
 
constexpr T * get_if () noexcept requires(variant_has_alternative_v< T
 Access the underlying value with the given type if it is the currently active alternative. More...
 
constexpr const T * get_if () const noexcept requires(variant_has_alternative_v< T
 Access the underlying value with the given type if it is the currently active alternative. More...
 
constexpr variant_alternative_t< Index, Variant > * get_if () noexcept
 Access the underlying value with the given index if it is the currently active alternative. More...
 
constexpr const variant_alternative_t< Index, Variant > * get_if () const noexcept
 Access the underlying value with the given index if it is the currently active alternative. More...
 

Static Public Member Functions

static Value parse (std::u8string_view jsonString)
 Parse a value of any JSON type from a UTF-8 JSON string. More...
 
static Value parse (std::string_view jsonString)
 Parse a value of any JSON type from a JSON string of bytes, interpreted as UTF-8. More...
 
- Static Public Member Functions inherited from donut::Variant< Null, Boolean, String, Number, Object, Array >
static constexpr decltype(auto) visit (Visitor &&visitor, V &&variant)
 Call a visitor functor with the currently active underlying value of a variant. More...
 

Additional Inherited Members

- Public Types inherited from donut::Variant< Null, Boolean, String, Number, Object, Array >
using index_type = std::conditional_t< sizeof...(Ts)< 255ull, std::uint8_t, std::conditional_t< sizeof...(Ts)< 65535ull, std::uint16_t, std::conditional_t< sizeof...(Ts)< 4294967295ull, std::uint32_t, std::uint64_t > >>
 Index type used to encode the active alternative type. More...
 
- Public Attributes inherited from donut::Variant< Null, Boolean, String, Number, Object, Array >
constexpr Variant Variant
 
constexpr bool Variant
 
constexpr T Variant
 
constexpr const T Variant
 
constexpr Variant ilist
 
- Static Public Attributes inherited from donut::Variant< Null, Boolean, String, Number, Object, Array >
static constexpr index_type npos
 Invalid alternative index, representing the valueless by exception state. More...
 

Detailed Description

JSON value type.

Holds a value of one of the following types:

Constructor & Destructor Documentation

◆ Value() [1/15]

donut::json::Value::Value ( )
defaultnoexcept

Construct a Null value.

◆ Value() [2/15]

donut::json::Value::Value ( Null  )
inlinenoexcept

Construct a Null value.

◆ Value() [3/15]

donut::json::Value::Value ( std::nullptr_t  )
inlinenoexcept

Construct a Null value.

◆ Value() [4/15]

donut::json::Value::Value ( Boolean  value)
inlinenoexcept

Construct a Boolean value with the given underlying value.

Parameters
valuevalue to copy.

◆ Value() [5/15]

donut::json::Value::Value ( const String value)
inline

Construct a String value with the given underlying value.

Parameters
valuevalue to copy.
Exceptions
std::bad_allocon allocation failure.

◆ Value() [6/15]

donut::json::Value::Value ( String &&  value)
inlinenoexcept

Construct a String value from the given underlying value.

Parameters
valuevalue to take.

◆ Value() [7/15]

donut::json::Value::Value ( const char *  value)
inline

Construct a String value with the given underlying value.

Parameters
valueread-only pointer to the null-terminated string to copy the contents of.
Exceptions
std::bad_allocon allocation failure.

◆ Value() [8/15]

donut::json::Value::Value ( std::string_view  value)
inline

Construct a String value with the given underlying value.

Parameters
valueread-only view over the string to copy the contents of.
Exceptions
std::bad_allocon allocation failure.

◆ Value() [9/15]

donut::json::Value::Value ( const char8_t *  value)
inline

Construct a String value with the given underlying value.

Parameters
valueread-only pointer to the null-terminated UTF-8 string to copy the contents of.
Exceptions
std::bad_allocon allocation failure.

◆ Value() [10/15]

donut::json::Value::Value ( std::u8string_view  value)
inline

Construct a String value with the given underlying value.

Parameters
valueread-only view over the UTF-8 string to copy the contents of.
Exceptions
std::bad_allocon allocation failure.

◆ Value() [11/15]

donut::json::Value::Value ( detail::number auto  value)
inlinenoexcept

Construct a Number value with the given underlying value.

Parameters
valuevalue to copy. May be of any fundamental arithmetic type except for bool and character types.

◆ Value() [12/15]

donut::json::Value::Value ( const Object value)
inline

Construct an Object value with the given underlying value.

Parameters
valuevalue to copy.
Exceptions
std::bad_allocon allocation failure.

◆ Value() [13/15]

donut::json::Value::Value ( Object &&  value)
inlinenoexcept

Construct an Object value from the given underlying value.

Parameters
valuevalue to take.

◆ Value() [14/15]

donut::json::Value::Value ( const Array value)
inline

Construct an Array value with the given underlying value.

Parameters
valuevalue to copy.
Exceptions
std::bad_allocon allocation failure.

◆ Value() [15/15]

donut::json::Value::Value ( Array &&  value)
inlinenoexcept

Construct an Array value from the given underlying value.

Parameters
valuevalue to take.

Member Function Documentation

◆ parse() [1/2]

Value donut::json::Value::parse ( std::u8string_view  jsonString)
inlinestatic

Parse a value of any JSON type from a UTF-8 JSON string.

The parser supports JSON5 features such as comments, unquoted identifiers and trailing commas.

Parameters
jsonStringread-only view over the JSON string to parse a value from.
Returns
the parsed value.
Exceptions
Erroron failure to parse a JSON value.
std::bad_allocon allocation failure.

◆ parse() [2/2]

Value donut::json::Value::parse ( std::string_view  jsonString)
inlinestatic

Parse a value of any JSON type from a JSON string of bytes, interpreted as UTF-8.

The parser supports JSON5 features such as comments, unquoted identifiers and trailing commas.

Parameters
jsonStringread-only view over the JSON string to parse a value from.
Returns
the parsed value.
Exceptions
Erroron failure to parse a JSON value.
std::bad_allocon allocation failure.

◆ toString()

std::string donut::json::Value::toString ( const SerializationOptions options = {}) const
inline

Get a JSON string representation of the value.

Parameters
optionsoptions for JSON serialization, see SerializationOptions.
Returns
a JSON string containing a representation of the value as it would be if it had been serialized to an output stream with the given options.
Exceptions
Erroron failure to serialize the value.
std::bad_allocon allocation failure.

◆ operator==()

bool donut::json::Value::operator== ( const Value other) const
inline

Compare this value to another for equality.

Parameters
otherthe value to compare this value to.
Returns
true if the values are equal, false otherwise.

◆ operator<=>()

std::partial_ordering donut::json::Value::operator<=> ( const Value other) const
inline

Compare this value to another.

Parameters
otherthe value to compare this value to.
Returns
a partial ordering between the two values.

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