luco::node class

the class that holds a luco node which is either luco::object, luco::array or luco::value

Constructors, destructors, conversion operators

node() explicit
default constructor which creates luco::node with type luco::node_type::object
node(enum node_type type) explicit
constructor to allow setting the type of the luco::node

Public functions

auto as_array() const →  std::shared_ptr<luco::array>
access the luco::array the luco::node is holding, if it exists
auto as_boolean() const →  bool
cast a node into a bool if it is holding luco::value that is a luco boolean (bool)
auto as_double() const →  double
cast a node into a double if it is holding luco::value that is a luco number (double)
auto as_integer() const →  int64_t
cast a node into a int64_t if it is holding luco::value that is a luco number (int64_t)
auto as_null() const →  null_type
cast a node into a luco::null_type if it is holding luco::value that is a luco null (luco::null_type)
auto as_number() const →  double
cast a node into a double if it is holding luco::value that is a luco number (int64_t or double)
auto as_object() const →  std::shared_ptr<luco::object>
access the luco::object the luco::node is holding, if it exists
auto as_string() const →  std::string
cast a node into a string if it is holding luco::value that is a luco string (luco::null_type)
auto as_value() const →  std::shared_ptr<class value>
access the luco::value the luco::node is holding, if it exists
auto at(const std::string& object_key) const →  class node&
access the node at the specified object key
auto at(const size_t array_index) const →  class node&
access the node at the specified array index
auto contains(const std::string& key) const →  bool noexcept
checks if a key exists in a luco object
auto dump_to_file(const std::filesystem::path& path, const std::pair<char, size_t>& indent_conf = {' ', 4}) const →  expected<monostate, error>
write luco::node to a file
void dump_to_stdout(const std::pair<char, size_t>& indent_conf = {' ', 4}) const
write luco::node to stdout
auto dump_to_string(const std::pair<char, size_t>& indent_conf = {' ', 4}) const →  std::string
write luco::node to string
auto is_array() const →  bool noexcept
checks if luco::node is holding luco::array
auto is_boolean() const →  bool noexcept
checks if luco::node is holding luco::value that is holding luco boolean (bool)
auto is_double() const →  bool noexcept
checks if luco::node is holding luco::value that is holding luco number (double)
auto is_integer() const →  bool noexcept
checks if luco::node is holding luco::value that is holding luco number (int64_t)
auto is_null() const →  bool noexcept
checks if luco::node is holding luco::value that is holding luco null (luco::null_type)
auto is_number() const →  bool noexcept
checks if luco::node is holding luco::value that is holding luco number (double or int64_t)
auto is_object() const →  bool noexcept
checks if luco::node is holding luco::object
auto is_string() const →  bool noexcept
checks if luco::node is holding luco::value that is holding luco string (std::string)
auto is_value() const →  bool noexcept
checks if luco::node is holding luco::value
auto operator+(const node& other_node) →  class node
add two luco::node together. they must have the same type and be either object, array, string or number
template <typename container_or_node_type>
auto operator=(const container_or_node_type& node_value) →  class node& noexcept
asign a node with a container_or_node_type
template <typename container_or_node_type>
void set(const container_or_node_type& node_value) noexcept
set a node with a container_or_node_type
auto stringify() const →  std::string noexcept
stringify the luco (object, array or value) inside the luco::node
auto try_as_array() const →  expected<std::shared_ptr<luco::array>, error> noexcept
access the luco::array the luco::node is holding, if it exists
auto try_as_boolean() const →  expected<bool, error> noexcept
cast a node into a bool if it is holding luco::value that is a luco boolean (bool)
auto try_as_double() const →  expected<double, error> noexcept
cast a node into a double if it is holding luco::value that is a luco number (double)
auto try_as_integer() const →  expected<int64_t, error> noexcept
cast a node into a int64_t if it is holding luco::value that is a luco number (int64_t)
auto try_as_null() const →  expected<null_type, error> noexcept
cast a node into a luco::null_type if it is holding luco::value that is a luco null
auto try_as_number() const →  expected<double, error> noexcept
cast a node into a double if it is holding luco::value that is a luco number (double or int64_t)
auto try_as_object() const →  expected<std::shared_ptr<luco::object>, error> noexcept
access the luco::object the luco::node is holding, if it exists
auto try_as_string() const →  expected<std::string, error> noexcept
cast a node into a std::string if it is holding luco::value that is a luco string (std::string)
auto try_as_value() const →  expected<std::shared_ptr<class value>, error> noexcept
access the luco::value the luco::node is holding, if it exists
auto try_at(const std::string& object_key) const →  expected<std::reference_wrapper<luco::node>, luco::error> noexcept
access the node at the specified object key
auto try_at(const size_t array_index) const →  expected<std::reference_wrapper<luco::node>, luco::error> noexcept
access the node at the specified array index
auto type() const →  node_type noexcept
gets the luco::node_type of the internal node
auto type_name() const →  std::string noexcept
gets string representation of luco::node_type of the internal node
auto value_type_name() const →  std::string noexcept
gets string representation of luco::value_type of the internal node if it's holding luco::value
auto valuetype() const →  value_type noexcept
gets the luco::value_type of the node if it's holding luco::value. otherwise it returns value_type::none

Function documentation

luco::node::node(enum node_type type) explicit

constructor to allow setting the type of the luco::node

Parameters
type type of node from enum luco::node_type

std::shared_ptr<luco::array> luco::node::as_array() const

access the luco::array the luco::node is holding, if it exists

Returns std::shared_ptr<luco::array>
Exceptions
luco::error if it doesn't hold luco::array

bool luco::node::as_boolean() const

cast a node into a bool if it is holding luco::value that is a luco boolean (bool)

Returns luco boolean
Exceptions
luco::error if it doesn't hold a luco::value and bool

double luco::node::as_double() const

cast a node into a double if it is holding luco::value that is a luco number (double)

Returns luco number
Exceptions
luco::error if it doesn't hold a luco::value and double

int64_t luco::node::as_integer() const

cast a node into a int64_t if it is holding luco::value that is a luco number (int64_t)

Returns luco number
Exceptions
luco::error if it doesn't hold a luco::value and int64_t

null_type luco::node::as_null() const

cast a node into a luco::null_type if it is holding luco::value that is a luco null (luco::null_type)

Returns luco null
Exceptions
luco::error if it doesn't hold a luco::value and luco::null_type

double luco::node::as_number() const

cast a node into a double if it is holding luco::value that is a luco number (int64_t or double)

Returns luco number
Exceptions
luco::error if it doesn't hold a luco::value and (int64_t or double)

std::shared_ptr<luco::object> luco::node::as_object() const

access the luco::object the luco::node is holding, if it exists

Returns std::shared_ptr<luco::object>
Exceptions
luco::error if it doesn't hold luco::object

std::string luco::node::as_string() const

cast a node into a string if it is holding luco::value that is a luco string (luco::null_type)

Returns luco string
Exceptions
luco::error if it doesn't hold a luco::value and string

std::shared_ptr<class value> luco::node::as_value() const

access the luco::value the luco::node is holding, if it exists

Returns std::shared_ptr<luco::value>
Exceptions
luco::error if it doesn't hold luco::value

class node& luco::node::at(const std::string& object_key) const

access the node at the specified object key

Parameters
object_key luco key to access in an object
Returns luco::node& at the specified key

class node& luco::node::at(const size_t array_index) const

access the node at the specified array index

Parameters
array_index luco index to access in an array
Returns luco::node& at the specified index

bool luco::node::contains(const std::string& key) const noexcept

checks if a key exists in a luco object

Parameters
key key to lookup
Returns true if it does

expected<monostate, error> luco::node::dump_to_file(const std::filesystem::path& path, const std::pair<char, size_t>& indent_conf = {' ', 4}) const

write luco::node to a file

Parameters
path path to write to
indent_conf indentation config for writing {char, size}

void luco::node::dump_to_stdout(const std::pair<char, size_t>& indent_conf = {' ', 4}) const

write luco::node to stdout

Parameters
indent_conf indentation config for writing {char, size}

std::string luco::node::dump_to_string(const std::pair<char, size_t>& indent_conf = {' ', 4}) const

write luco::node to string

Parameters
indent_conf indentation config for writing {char, size}
Returns luco serialized

bool luco::node::is_array() const noexcept

checks if luco::node is holding luco::array

Returns true if it does

bool luco::node::is_boolean() const noexcept

checks if luco::node is holding luco::value that is holding luco boolean (bool)

Returns true if it does

bool luco::node::is_double() const noexcept

checks if luco::node is holding luco::value that is holding luco number (double)

Returns true if it does

bool luco::node::is_integer() const noexcept

checks if luco::node is holding luco::value that is holding luco number (int64_t)

Returns true if it does

bool luco::node::is_null() const noexcept

checks if luco::node is holding luco::value that is holding luco null (luco::null_type)

Returns true if it does

bool luco::node::is_number() const noexcept

checks if luco::node is holding luco::value that is holding luco number (double or int64_t)

Returns true if it does

bool luco::node::is_object() const noexcept

checks if luco::node is holding luco::object

Returns true if it does

bool luco::node::is_string() const noexcept

checks if luco::node is holding luco::value that is holding luco string (std::string)

Returns true if it does

bool luco::node::is_value() const noexcept

checks if luco::node is holding luco::value

Returns true if it does
luco::node node(luco::node_type::value);
if (node.is_value())
{
 // do something
}

class node luco::node::operator+(const node& other_node)

add two luco::node together. they must have the same type and be either object, array, string or number

Parameters
other_node the other node to add to the current node
Returns new node containing content of both nodes
Exceptions
luco::error if different types or not one of the required types
luco::node new_node = node1 + node2;

@luco node1_key = "node1_value" @eluco @luco node2_key = "node2_value" @eluco @luco node1_key = "node1_value" node2_key = "node2_value" @eluco

template <typename container_or_node_type>
class node& luco::node::operator=(const container_or_node_type& node_value) noexcept

asign a node with a container_or_node_type

Parameters
node_value value to be set
Returns the address of the node which can be used to modify the value

template <typename container_or_node_type>
void luco::node::set(const container_or_node_type& node_value) noexcept

set a node with a container_or_node_type

Parameters
node_value value to be set

std::string luco::node::stringify() const noexcept

stringify the luco (object, array or value) inside the luco::node

Returns serialized luco

@luco

key1 = "val1" key2 = "val2"

@eluco

@luco

"val1" "val2" "val3"

@eluco

@luco "value" @eluco

expected<std::shared_ptr<luco::array>, error> luco::node::try_as_array() const noexcept

access the luco::array the luco::node is holding, if it exists

Returns luco::array or luco::error if it doesn't hold a luco::array

expected<bool, error> luco::node::try_as_boolean() const noexcept

cast a node into a bool if it is holding luco::value that is a luco boolean (bool)

Returns bool or luco::error if it doesn't hold a string

expected<double, error> luco::node::try_as_double() const noexcept

cast a node into a double if it is holding luco::value that is a luco number (double)

Returns double or luco::error if it doesn't hold a string

expected<int64_t, error> luco::node::try_as_integer() const noexcept

cast a node into a int64_t if it is holding luco::value that is a luco number (int64_t)

Returns int64_t or luco::error if it doesn't hold a string

expected<null_type, error> luco::node::try_as_null() const noexcept

cast a node into a luco::null_type if it is holding luco::value that is a luco null

Returns luco::null_type or luco::error if it doesn't hold a null (luco::null_type)
luco::expected<null_type, error> null = node.try_as_null();
if (not null)
{
 // handle error
 std::println("{}", null.error().message());
}
else
{
 // success
 luco::null_type null_value = null.value();
}

expected<double, error> luco::node::try_as_number() const noexcept

cast a node into a double if it is holding luco::value that is a luco number (double or int64_t)

Returns double or luco::error if it doesn't hold a string

expected<std::shared_ptr<luco::object>, error> luco::node::try_as_object() const noexcept

access the luco::object the luco::node is holding, if it exists

Returns luco::object or luco::error if it doesn't hold a luco::object

expected<std::string, error> luco::node::try_as_string() const noexcept

cast a node into a std::string if it is holding luco::value that is a luco string (std::string)

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

expected<std::shared_ptr<class value>, error> luco::node::try_as_value() const noexcept

access the luco::value the luco::node is holding, if it exists

Returns luco::value or luco::error if it doesn't hold a luco::value

expected<std::reference_wrapper<luco::node>, luco::error> luco::node::try_at(const std::string& object_key) const noexcept

access the node at the specified object key

Parameters
object_key luco key to access in an object
Returns either std::reference_wrapper<luco::node> if the node was found or luco::error if not
luco::expected<std::reference_wrapper<luco::node>, luco::error> maybe_node =
object_node.try_at("key");
if (maybe_node)
{
 // notice the '&' is neccessary if this reference would be used to modify
 // the value inside
 luco::node& node_at_key = maybe_node.value().get();
 // .value() to get the expected type, .get() for the node reference

 // or you can set it like this to avoid forgetting the '&'
 maybe_node.value().get().set(std::string("new value"));
}
else
{
 std::println("{}", maybe_node.error().message());
}

expected<std::reference_wrapper<luco::node>, luco::error> luco::node::try_at(const size_t array_index) const noexcept

access the node at the specified array index

Parameters
array_index luco index to access in an array
Returns either std::reference_wrapper<luco::node> if the node was found or luco::error if not
luco::expected<std::reference_wrapper<luco::node>, luco::error> maybe_node =
array_node.try_at(0);
if (maybe_node)
{
 // notice the '&' is neccessary if this reference would be used to modify
 // the value inside
 luco::node& node_at_key = maybe_node.value().get();
 // .value() to get the expected type, .get() for the node reference

 // or you can set it like this to avoid forgetting the '&'
 maybe_node.value().get().set(true);
}
else
{
 std::println("{}", maybe_node.error().message());
}

node_type luco::node::type() const noexcept

gets the luco::node_type of the internal node

Returns node_type

std::string luco::node::type_name() const noexcept

gets string representation of luco::node_type of the internal node

Returns string name of the node_type

std::string luco::node::value_type_name() const noexcept

gets string representation of luco::value_type of the internal node if it's holding luco::value

Returns string name of the value_type

value_type luco::node::valuetype() const noexcept

gets the luco::value_type of the node if it's holding luco::value. otherwise it returns value_type::none

Returns value_type