xentara-utils v2.0.4
The Xentara Utility Library
|
A single value in a JSON document. More...
#include <xentara/utils/json/decoder/Value.hpp>
Public Types | |
enum class | Type { Object , Array , String , Number , Boolean , Null , Defunct , MovedAway } |
JSON types. More... | |
Public Member Functions | |
Value () noexcept=default | |
Constructor for a null value. | |
auto | skip () -> void |
Skips the value. | |
auto | document () const -> const Document * |
Gets the JSON document that the value belongs to. | |
auto | lineNumber () const -> std::uint_least32_t |
Gets the line number in the JSON document that the value is on. | |
auto | location () const -> Location |
Gets the location of the value within its JSON document. | |
template<> | |
auto | asString () -> String |
Accessing the value type | |
auto | getType () const noexcept -> Type |
gets the type of the object | |
auto | isObject () const noexcept -> bool |
Determines if the value is an object. | |
auto | isArray () const noexcept -> bool |
Determines if the value is an array. | |
auto | isString () const noexcept -> bool |
Determines if the value is a string. | |
auto | isNumber () const noexcept -> bool |
Determines if the value is a number. | |
auto | isBoolean () const noexcept -> bool |
Determines if the value is a Boolean value. | |
auto | isNull () const noexcept -> bool |
Determines if the value is a null value. | |
Getting the value as a standard JSON type | |
auto | asObject () -> Object |
Interprets the value as a JSON object. | |
auto | asArray () -> Array |
Interprets the value as a JSON array. | |
template<std::same_as< String > > | |
auto | asString () -> String |
Interprets the value as a string. | |
template<tools::StringType StdString> | |
auto | asString (const typename StdString::allocator_type &allocator=typename StdString::allocator_type()) -> StdString |
Reads the value as an std::basic_string. | |
template<tools::Arithmetic Number> | |
auto | asNumber () -> Number |
Reads the value as a number. | |
template<std::same_as< Number > > | |
auto | asNumber () -> Number |
Reads the value as a generic number. | |
auto | asBoolean () -> bool |
Reads the value as a Boolean value. | |
Getting the value as non-JSON type | |
auto | toUuid () -> core::Uuid |
Reads the value as a string containing a UUID. | |
auto | toDateTime () -> std::chrono::system_clock::time_point |
Reads the value as a string containing a date and time. | |
auto | toDate () -> std::chrono::sys_days |
Reads the value as a string containing a date. | |
auto | toTime () -> std::chrono::system_clock::duration |
Reads the value as a string containing a time with time zone information. | |
auto | toNakedTime () -> std::chrono::system_clock::duration |
Reads the value as a string containing a time without time zone information. | |
template<typename Duration = std::chrono::nanoseconds> | |
auto | toTimeDuration () -> Duration |
Reads the value as a string containing a duration. | |
template<typename Duration = std::chrono::nanoseconds> | |
auto | toTimeDuration (AllowLegacyMicrosecondsTag) -> Duration |
Reads the value as a string containing a duration, or as a number containing microseconds. | |
A single value in a JSON document.
This object lazily reads the data from the document when you call one of the conversion functions (asObject(), asArray(), asString(), asNumber(), or asBoolean()). This means that you must call one and only one of these function exactly once. The only exception are JSON null values. For null values, there is no conversion function, and you do not need to call any function.
If you are not interested in the value, you can also call skip() to skip the entire value. In that case, you must not call any of the conversion functions.
JSON values can be moved but not copied
|
strong |
JSON types.
|
defaultnoexcept |
Constructor for a null value.
auto xentara::utils::json::decoder::Value::asArray | ( | ) | -> Array |
Interprets the value as a JSON array.
This method does not return the actual array, but rather an accessor that allows reading the elements one by one.
NullValueError | The value is null |
TypeMismatchError | The value is not a JSON array, but another type |
SyntaxError | The stream does not contain a valid JSON element |
std::runtime_error | An error occurred reading from the stream |
auto xentara::utils::json::decoder::Value::asBoolean | ( | ) | -> bool |
Reads the value as a Boolean value.
NullValueError | The value is null |
TypeMismatchError | The value is not a JSON Boolean value, but another type |
SyntaxError | The stream does not contain a valid JSON element |
std::runtime_error | An error occurred reading from the stream |
auto xentara::utils::json::decoder::Value::asNumber | ( | ) | -> Number |
Reads the value as a number.
NullValueError | The value is null |
TypeMismatchError | The value is not a JSON number, but another type |
std::range_error | The number contained in the value cannot be represented by the given type |
SyntaxError | The stream does not contain a valid JSON element |
std::runtime_error | An error occurred reading from the stream |
template auto xentara::utils::json::decoder::Value::asNumber< Number > | ( | ) | -> Number |
Reads the value as a generic number.
NullValueError | The value is null |
TypeMismatchError | The value is not a JSON number, but another type |
std::range_error | The number contained in the value cannot be represented by the given type |
SyntaxError | The stream does not contain a valid JSON element |
std::runtime_error | An error occurred reading from the stream |
auto xentara::utils::json::decoder::Value::asObject | ( | ) | -> Object |
Interprets the value as a JSON object.
This method does not return the actual object, but rather an accessor that allows reading the members one by one.
NullValueError | The value is null |
TypeMismatchError | The value is not a JSON object, but another type |
SyntaxError | The stream does not contain a valid JSON element |
std::runtime_error | An error occurred reading from the stream |
auto xentara::utils::json::decoder::Value::asString | ( | ) | -> String |
Interprets the value as a string.
This method does not return the actual string, but rather an accessor that allows reading the characters one by one.
NullValueError | The value is null |
TypeMismatchError | The value is not a JSON string, but another type |
SyntaxError | The stream does not contain a valid JSON element |
std::runtime_error | An error occurred reading from the stream |
auto xentara::utils::json::decoder::Value::asString | ( | ) | -> String |
auto xentara::utils::json::decoder::Value::asString | ( | const typename StdString::allocator_type & | allocator = typename StdString::allocator_type() | ) | -> StdString |
Reads the value as an std::basic_string.
allocator | The allocator for the returned string |
NullValueError | The value is null |
TypeMismatchError | The value is not a JSON string, but another type |
SyntaxError | The stream does not contain a valid JSON element |
std::runtime_error | An error occurred reading from the stream |
auto xentara::utils::json::decoder::Value::document | ( | ) | const -> const Document * |
Gets the JSON document that the value belongs to.
|
noexcept |
gets the type of the object
|
noexcept |
Determines if the value is an array.
|
noexcept |
Determines if the value is a Boolean value.
|
noexcept |
Determines if the value is a null value.
|
noexcept |
Determines if the value is a number.
|
noexcept |
Determines if the value is an object.
|
noexcept |
Determines if the value is a string.
auto xentara::utils::json::decoder::Value::lineNumber | ( | ) | const -> std::uint_least32_t |
Gets the line number in the JSON document that the value is on.
auto xentara::utils::json::decoder::Value::location | ( | ) | const -> Location |
Gets the location of the value within its JSON document.
auto xentara::utils::json::decoder::Value::skip | ( | ) | -> void |
Skips the value.
SyntaxError | The stream does not contain a valid JSON element |
std::runtime_error | An error occurred reading from the stream |
auto xentara::utils::json::decoder::Value::toDate | ( | ) | -> std::chrono::sys_days |
Reads the value as a string containing a date.
The string must conform to the full-date profile described in section 5.6 of RFC3339.
NullValueError | The value is null |
TypeMismatchError | The value is not a JSON string, but another type |
std::runtime_error | The string contained in the value is not a valid RFC3339 full-date string. |
SyntaxError | The stream does not contain a valid JSON element |
std::runtime_error | An error occurred reading from the stream |
auto xentara::utils::json::decoder::Value::toDateTime | ( | ) | -> std::chrono::system_clock::time_point |
Reads the value as a string containing a date and time.
The string must conform to the date-time profile described in section 5.6 of RFC3339.
NullValueError | The value is null |
TypeMismatchError | The value is not a JSON string, but another type |
std::runtime_error | The string contained in the value is not a valid RFC3339 date-time string. |
SyntaxError | The stream does not contain a valid JSON element |
std::runtime_error | An error occurred reading from the stream |
auto xentara::utils::json::decoder::Value::toNakedTime | ( | ) | -> std::chrono::system_clock::duration |
Reads the value as a string containing a time without time zone information.
The string must conform to the partial-time profile described in section 5.6 of RFC3339.
NullValueError | The value is null |
TypeMismatchError | The value is not a JSON string, but another type |
std::runtime_error | The string contained in the value is not a valid RFC3339 partial-time string. |
SyntaxError | The stream does not contain a valid JSON element |
std::runtime_error | An error occurred reading from the stream |
auto xentara::utils::json::decoder::Value::toTime | ( | ) | -> std::chrono::system_clock::duration |
Reads the value as a string containing a time with time zone information.
The string must conform to the full-time profile described in section 5.6 of RFC3339.
NullValueError | The value is null |
TypeMismatchError | The value is not a JSON string, but another type |
std::runtime_error | The string contained in the value is not a valid RFC3339 full-time string. |
SyntaxError | The stream does not contain a valid JSON element |
std::runtime_error | An error occurred reading from the stream |
auto xentara::utils::json::decoder::Value::toTimeDuration | ( | ) | -> Duration |
Reads the value as a string containing a duration.
The string must contain a number, followed by the time units. The number can contain decimals.
The supported units are:
String | Units |
---|---|
ns | nanoseconds |
ms | milliseconds |
µs (or us) | microseconds |
s | second |
m | minutes |
h | hours |
A value of 50µs can be represented by the strings "50µs", "0.05ms", or "0.00005s", for example.
If the time span is 0, then the string need not contain units. A time stamp of 0 simply be represented by the string "0" (in quotes).
Duration | The type of duration to parse. Must be a specialization of std::chrono::duration. If Duration::rep is an integral type, then the duration must be a multiple of nanoseconds. |
NullValueError | The value is null |
TypeMismatchError | The value is not a JSON string, but another type |
std::runtime_error | The string contained in the value is not a valid duration string. |
SyntaxError | The stream does not contain a valid JSON element |
std::runtime_error | An error occurred reading from the stream |
auto xentara::utils::json::decoder::Value::toTimeDuration | ( | AllowLegacyMicrosecondsTag | ) | -> Duration |
Reads the value as a string containing a duration, or as a number containing microseconds.
This overload can be used to read a time duration in the format of the normal toTimeDuration(), but also allow a JSON number representing a number of microseconds for backward compatibility.
Duration | The type of duration to parse. Must be a specialization of std::chrono::duration. If Duration::rep is an integral type, then the duration must be a multiple of nanoseconds. |
NullValueError | The value is null |
TypeMismatchError | The value is not a JSON string or a JSON number, but another type |
std::runtime_error | The string contained in the value is not a valid duration string. |
SyntaxError | The stream does not contain a valid JSON element |
std::runtime_error | An error occurred reading from the stream |
auto xentara::utils::json::decoder::Value::toUuid | ( | ) | -> core::Uuid |
Reads the value as a string containing a UUID.
NullValueError | The value is null |
TypeMismatchError | The value is not a JSON string, but another type |
std::runtime_error | The string contained in the value is not a valid UUID text representation |
SyntaxError | The stream does not contain a valid JSON element |
std::runtime_error | An error occurred reading from the stream |