ljson::value class

holds a json value such as <std::string, double, int64_t, bool, null_type, monostate>

Constructors, destructors, conversion operators

template <is_allowed_value_type val_type>
value(const val_type& val) noexcept
constructor for ljson::value
value(const value& other)
copy constructor for ljson::value
value(const value&& other)
move constructor for ljson::value
value() noexcept
constructor which sets an empty ljson::value

Public functions

auto as_boolean() →  bool
cast ljson::value into a boolean if it is holding a json boolean (bool)
auto as_double() →  double
cast ljson::value into a number if it is holding a json number (double)
auto as_integer() →  int64_t
cast ljson::value into a number if it is holding a json number (int64_t)
auto as_null() →  null_type
cast ljson::value into a null if it is holding a json null (ljson::null_type)
auto as_number() →  double
cast ljson::value into a number if it is holding a json number (double or int64_t)
auto as_string() →  std::string
cast ljson::value into a string if it is holding a json string (std::string)
auto is_boolean() const →  bool noexcept
checks if ljson::value is holding json boolean (bool)
auto is_double() const →  bool noexcept
checks if ljson::value is holding json number (double)
auto is_empty() const →  bool noexcept
checks if ljson::value is not holding json value (ljson::monostate)
auto is_integer() const →  bool noexcept
checks if ljson::value is holding json number (int64_t)
auto is_null() const →  bool noexcept
checks if ljson::value is holding json null (ljson::null_type)
auto is_number() const →  bool noexcept
checks if ljson::value is holding json number (double or int64_t)
auto is_string() const →  bool noexcept
checks if ljson::value is holding json string (std::string)
auto operator=(const value& other) →  value&
copy assignment for ljson::value
auto operator=(const value&& other) →  value&
move assignment for ljson::value
template <is_allowed_value_type val_type>
void set_value_type(const val_type& val) noexcept
sets the value and type of ljson::value
auto set_value_type(const std::string& val, value_type type) →  expected<monostate, error>
sets the value and type of ljson::value
auto stringify() const →  std::string noexcept
cast the json value into a std::string
auto try_as_boolean() →  expected<bool, error> noexcept
cast ljson::value into a bool if it is holding a json boolean (bool)
auto try_as_double() →  expected<double, error> noexcept
cast ljson::value into a double if it is holding a json number (double)
auto try_as_integer() →  expected<int64_t, error> noexcept
cast ljson::value into a int64_t if it is holding a json number (int64_t)
auto try_as_null() →  expected<null_type, error> noexcept
cast ljson::value into a ljson::null_type if it is holding a json null (ljson::null_type)
auto try_as_number() →  expected<double, error> noexcept
cast ljson::value into a double if it is holding a json number (double or int64_t)
auto try_as_string() →  expected<std::string, error> noexcept
cast ljson::value into a std::string if it is holding a json string (std::string)
auto type() const →  ljson::value_type noexcept
gets the type of the stored json value
auto type_name() const →  std::string noexcept
gets string representation of ljson::value_type of the internal value

Function documentation

template <is_allowed_value_type val_type>
ljson::value::value(const val_type& val) noexcept

constructor for ljson::value

Parameters
val json value to be set

ljson::value::value(const value& other)

copy constructor for ljson::value

Parameters
other ljson::value to be copied

ljson::value::value(const value&& other)

move constructor for ljson::value

Parameters
other ljson::value to be moved

bool ljson::value::as_boolean()

cast ljson::value into a boolean if it is holding a json boolean (bool)

Returns json boolean
Exceptions
ljson::error if it doesn't hold json boolean

double ljson::value::as_double()

cast ljson::value into a number if it is holding a json number (double)

Returns json number
Exceptions
ljson::error if it doesn't hold json number

int64_t ljson::value::as_integer()

cast ljson::value into a number if it is holding a json number (int64_t)

Returns json number
Exceptions
ljson::error if it doesn't hold json number

null_type ljson::value::as_null()

cast ljson::value into a null if it is holding a json null (ljson::null_type)

Returns json null
Exceptions
ljson::error if it doesn't hold json null

double ljson::value::as_number()

cast ljson::value into a number if it is holding a json number (double or int64_t)

Returns json number
Exceptions
ljson::error if it doesn't hold json number

std::string ljson::value::as_string()

cast ljson::value into a string if it is holding a json string (std::string)

Returns json string
Exceptions
ljson::error if it doesn't json string

bool ljson::value::is_boolean() const noexcept

checks if ljson::value is holding json boolean (bool)

Returns true if it does

bool ljson::value::is_double() const noexcept

checks if ljson::value is holding json number (double)

Returns true if it does

bool ljson::value::is_empty() const noexcept

checks if ljson::value is not holding json value (ljson::monostate)

Returns true if it does

bool ljson::value::is_integer() const noexcept

checks if ljson::value is holding json number (int64_t)

Returns true if it does

bool ljson::value::is_null() const noexcept

checks if ljson::value is holding json null (ljson::null_type)

Returns true if it does

bool ljson::value::is_number() const noexcept

checks if ljson::value is holding json number (double or int64_t)

Returns true if it does

bool ljson::value::is_string() const noexcept

checks if ljson::value is holding json string (std::string)

Returns true if it does

value& ljson::value::operator=(const value& other)

copy assignment for ljson::value

Parameters
other ljson::value to be copied
Returns the address of the modified ljson::value

value& ljson::value::operator=(const value&& other)

move assignment for ljson::value

Parameters
other ljson::value to be moved
Returns the address of the modified ljson::value

template <is_allowed_value_type val_type>
void ljson::value::set_value_type(const val_type& val) noexcept

sets the value and type of ljson::value

Parameters
val json value to be set

expected<monostate, error> ljson::value::set_value_type(const std::string& val, value_type type)

sets the value and type of ljson::value

Parameters
val json value to be set
type json type (ljson::value_type) to be set
Returns ljson::monostate or ljson::error if the value wasn't set (wrong type was provided)

std::string ljson::value::stringify() const noexcept

cast the json value into a std::string

Returns string representation of the value

expected<bool, error> ljson::value::try_as_boolean() noexcept

cast ljson::value into a bool if it is holding a json boolean (bool)

Returns bool or ljson::error if it doesn't hold a boolean

expected<double, error> ljson::value::try_as_double() noexcept

cast ljson::value into a double if it is holding a json number (double)

Returns double or ljson::error if it doesn't hold a number

expected<int64_t, error> ljson::value::try_as_integer() noexcept

cast ljson::value into a int64_t if it is holding a json number (int64_t)

Returns int64_t or ljson::error if it doesn't hold a number

expected<null_type, error> ljson::value::try_as_null() noexcept

cast ljson::value into a ljson::null_type if it is holding a json null (ljson::null_type)

Returns ljson::null_type or ljson::error if it doesn't hold a null

expected<double, error> ljson::value::try_as_number() noexcept

cast ljson::value into a double if it is holding a json number (double or int64_t)

Returns double or ljson::error if it doesn't hold a number

expected<std::string, error> ljson::value::try_as_string() noexcept

cast ljson::value into a std::string if it is holding a json string (std::string)

Returns std::string or ljson::error if it doesn't hold a string
ljson::expected<std::string, error> string = value.try_as_string();
if (not string)
{
 // handle error
 std::println("{}", string.error().message());
}
else
{
 // success
 std::string string_value = string.value();
}

ljson::value_type ljson::value::type() const noexcept

gets the type of the stored json value

Returns ljson::value_type of the stored json value

std::string ljson::value::type_name() const noexcept

gets string representation of ljson::value_type of the internal value

Returns string name of the value_type