ljson::node class

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

Constructors, destructors, conversion operators

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

Public functions

auto as_array() const →  std::shared_ptr<ljson::array>
access the ljson::array the ljson::node is holding, if it exists
auto as_boolean() const →  bool
cast a node into a bool if it is holding ljson::value that is a json boolean (bool)
auto as_double() const →  double
cast a node into a double if it is holding ljson::value that is a json number (double)
auto as_integer() const →  int64_t
cast a node into a int64_t if it is holding ljson::value that is a json number (int64_t)
auto as_null() const →  null_type
cast a node into a ljson::null_type if it is holding ljson::value that is a json null (ljson::null_type)
auto as_number() const →  double
cast a node into a double if it is holding ljson::value that is a json number (int64_t or double)
auto as_object() const →  std::shared_ptr<ljson::object>
access the ljson::object the ljson::node is holding, if it exists
auto as_string() const →  std::string
cast a node into a string if it is holding ljson::value that is a json string (ljson::null_type)
auto as_value() const →  std::shared_ptr<class value>
access the ljson::value the ljson::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 json object
auto dump_to_file(const std::filesystem::path& path, const std::pair<char, int>& indent_conf = {' ', 4}) const →  expected<monostate, error>
write ljson::node to a file
void dump_to_stdout(const std::pair<char, int>& indent_conf = {' ', 4}) const
write ljson::node to stdout
auto dump_to_string(const std::pair<char, int>& indent_conf = {' ', 4}) const →  std::string
write ljson::node to string
auto is_array() const →  bool noexcept
checks if ljson::node is holding ljson::array
auto is_boolean() const →  bool noexcept
checks if ljson::node is holding ljson::value that is holding json boolean (bool)
auto is_double() const →  bool noexcept
checks if ljson::node is holding ljson::value that is holding json number (double)
auto is_integer() const →  bool noexcept
checks if ljson::node is holding ljson::value that is holding json number (int64_t)
auto is_null() const →  bool noexcept
checks if ljson::node is holding ljson::value that is holding json null (ljson::null_type)
auto is_number() const →  bool noexcept
checks if ljson::node is holding ljson::value that is holding json number (double or int64_t)
auto is_object() const →  bool noexcept
checks if ljson::node is holding ljson::object
auto is_string() const →  bool noexcept
checks if ljson::node is holding ljson::value that is holding json string (std::string)
auto is_value() const →  bool noexcept
checks if ljson::node is holding ljson::value
auto operator+(const node& other_node) →  class node
add two ljson::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 json (object, array or value) inside the ljson::node
auto try_as_array() const →  expected<std::shared_ptr<ljson::array>, error> noexcept
access the ljson::array the ljson::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 ljson::value that is a json boolean (bool)
auto try_as_double() const →  expected<double, error> noexcept
cast a node into a double if it is holding ljson::value that is a json number (double)
auto try_as_integer() const →  expected<int64_t, error> noexcept
cast a node into a int64_t if it is holding ljson::value that is a json number (int64_t)
auto try_as_null() const →  expected<null_type, error> noexcept
cast a node into a ljson::null_type if it is holding ljson::value that is a json null
auto try_as_number() const →  expected<double, error> noexcept
cast a node into a double if it is holding ljson::value that is a json number (double or int64_t)
auto try_as_object() const →  expected<std::shared_ptr<ljson::object>, error> noexcept
access the ljson::object the ljson::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 ljson::value that is a json string (std::string)
auto try_as_value() const →  expected<std::shared_ptr<class value>, error> noexcept
access the ljson::value the ljson::node is holding, if it exists
auto try_at(const std::string& object_key) const →  expected<std::reference_wrapper<ljson::node>, ljson::error> noexcept
access the node at the specified object key
auto try_at(const size_t array_index) const →  expected<std::reference_wrapper<ljson::node>, ljson::error> noexcept
access the node at the specified array index
auto type() const →  node_type noexcept
gets the ljson::node_type of the internal node
auto type_name() const →  std::string noexcept
gets string representation of ljson::node_type of the internal node
auto value_type_name() const →  std::string noexcept
gets string representation of ljson::value_type of the internal node if it's holding ljson::value
auto valuetype() const →  value_type noexcept
gets the ljson::value_type of the node if it's holding ljson::value. otherwise it returns value_type::none

Function documentation

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

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

Parameters
type type of node from enum ljson::node_type

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

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

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

bool ljson::node::as_boolean() const

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

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

double ljson::node::as_double() const

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

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

int64_t ljson::node::as_integer() const

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

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

null_type ljson::node::as_null() const

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

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

double ljson::node::as_number() const

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

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

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

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

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

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

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

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

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

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

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

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

access the node at the specified object key

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

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

access the node at the specified array index

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

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

checks if a key exists in a json object

Parameters
key key to lookup
Returns true if it does

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

write ljson::node to a file

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

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

write ljson::node to stdout

Parameters
indent_conf indentation config for writing {char, size}

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

write ljson::node to string

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

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

checks if ljson::node is holding ljson::array

Returns true if it does

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

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

Returns true if it does

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

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

Returns true if it does

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

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

Returns true if it does

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

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

Returns true if it does

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

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

Returns true if it does

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

checks if ljson::node is holding ljson::object

Returns true if it does

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

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

Returns true if it does

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

checks if ljson::node is holding ljson::value

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

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

add two ljson::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
ljson::error if different types or not one of the required types
ljson::node new_node = node1 + node2;
{"node1_key": "node1_value"}
{"node2_key": "node2_value"}
{"node1_key": "node1_value", "node2_key": "node2_value"}

template <typename container_or_node_type>
class node& ljson::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 ljson::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 ljson::node::stringify() const noexcept

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

Returns serialized json
{
  "key1": "val1",
  "key2": "val2"
}
[
  "val1",
  "val2",
  "val3"
]
value

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Returns std::string or ljson::error if it doesn't hold a string
ljson::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> ljson::node::try_as_value() const noexcept

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

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

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

access the node at the specified object key

Parameters
object_key json key to access in an object
Returns either std::reference_wrapper<ljson::node> if the node was found or ljson::error if not
ljson::expected<std::reference_wrapper<ljson::node>, ljson::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
 ljson::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<ljson::node>, ljson::error> ljson::node::try_at(const size_t array_index) const noexcept

access the node at the specified array index

Parameters
array_index json index to access in an array
Returns either std::reference_wrapper<ljson::node> if the node was found or ljson::error if not
ljson::expected<std::reference_wrapper<ljson::node>, ljson::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
 ljson::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 ljson::node::type() const noexcept

gets the ljson::node_type of the internal node

Returns node_type

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

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

Returns string name of the node_type

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

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

Returns string name of the value_type

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

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

Returns value_type