Skip to content

File include/brisk/core/Json.hpp


Json class

Json

JsonA structure representing a JSON value.

This structure encapsulates various types of JSON data and provides methods for manipulation and conversion between JSON and other data types.


JsonType enum

enum class JsonType

JsonTypeEnum representing the different types of JSON values.

This enum class defines the various data types that can be found in a JSON structure. Each value corresponds to a specific JSON data type.

Null enumerator (JsonType::Null)

Represents a JSON null value.

Array enumerator (JsonType::Array)

Represents a JSON array.

Object enumerator (JsonType::Object)

Represents a JSON object.

String enumerator (JsonType::String)

Represents a JSON string.

SignedInteger enumerator (JsonType::SignedInteger)

Represents a JSON signed integer (64-bit).

UnsignedInteger enumerator (JsonType::UnsignedInteger)

Represents a JSON unsigned integer (64-bit).

Float enumerator (JsonType::Float)

Represents a JSON floating-point number (64-bit).

Bool enumerator (JsonType::Bool)

Represents a JSON boolean value.

Last enumerator (JsonType::Last)

Represents a JSON boolean value.


Json class

Json

JsonA structure representing a JSON value.

This structure encapsulates various types of JSON data and provides methods for manipulation and conversion between JSON and other data types.


JsonConverter class

template <typename T> JsonConverter

IteratePath class

IteratePath

IteratePathA structure to iterate over a JSON path.

This structure provides functionality to parse and iterate over a specified path within a JSON document, enabling access to nested elements.

path variable (IteratePath::path)

std::string_view path

The path to iterate over.

Iterator class (IteratePath::Iterator)

Iterator

IteratorAn iterator for IteratePath.

This struct implements a forward iterator for traversing the components of a JSON path.

operator== function (IteratePath::Iterator::operator==)

bool operator==(const Iterator &it) const

Compares two iterators for equality.
Param it The iterator to compare against.
Returns True if both iterators point to the same element; otherwise, false.

operator!= function (IteratePath::Iterator::operator!=)

bool operator!=(const Iterator &it) const

Compares two iterators for inequality.
Param it The iterator to compare against.
Returns True if the iterators point to different elements; otherwise, false.

operator* function (IteratePath::Iterator::operator*)

std::string_view operator*() const

Dereferences the iterator to get the current path component.
Returns The current path component as a string view.

operator++ function (IteratePath::Iterator::operator++)

Iterator &operator++()

Advances the iterator to the next path component.
Returns A reference to this iterator.

Iterator operator++(int)

Advances the iterator to the next path component (postfix increment).
Returns A copy of the iterator before incrementing.

begin function (IteratePath::begin)

Iterator begin() const

Returns an iterator to the beginning of the path components.
Returns An Iterator pointing to the first component.

find_next function (IteratePath::find_next)

static size_t find_next(std::string_view path,
                        size_t offset)

Finds the next delimiter in the path.
Param path The path to search.
Param offset The starting position for the search.
Returns The position of the next delimiter.

end function (IteratePath::end)

Iterator end() const

Returns an iterator to the end of the path components.
Returns An Iterator pointing to the end.


iteratePath function

IteratePath iteratePath(std::string_view s)

Parses a string and returns an IteratePath structure.
Param s The string representing the JSON path.
Returns An IteratePath object representing the parsed path.


Json class

Json

JsonA structure representing a JSON value.

This structure encapsulates various types of JSON data and provides methods for manipulation and conversion between JSON and other data types.

Json function (Json::Json)

Json(JsonArray x) : JsonVariant(std::move(x))

Constructs a Json object from a JsonArray.

Json(JsonObject x) : JsonVariant(std::move(x))

Constructs a Json object from a JsonObject.

Json(JsonString x) : JsonVariant(std::move(x))

Constructs a Json object from a JsonString.

Json(std::nullptr_t x)
    : JsonVariant(static_cast<JsonNull>(x))

Constructs a Json object from a nullptr (JSON null).

Json(bool x) : JsonVariant(static_cast<JsonBool>(x))

Constructs a Json object from a boolean value.

Json(const char *x) : JsonVariant(JsonString(x))

Constructs a Json object from a C-style string.

Json(std::string_view x) : JsonVariant(JsonString(x))

Constructs a Json object from a string view.

Json(float x) : JsonVariant(static_cast<JsonFloat>(x))

Constructs a Json object from a float value.

Json(double x) : JsonVariant(static_cast<JsonFloat>(x))

Constructs a Json object from a double value.

Json(char x)
    : JsonVariant(
          static_cast<std::conditional_t<
              std::is_signed_v<char>, JsonSignedInteger,
              JsonUnsignedInteger>>(x))

Constructs a Json object from a character.

Json(signed char x)
    : JsonVariant(static_cast<JsonSignedInteger>(x))

Constructs a Json object from a signed char.

Json(signed short x)
    : JsonVariant(static_cast<JsonSignedInteger>(x))

Constructs a Json object from a signed short.

Json(signed int x)
    : JsonVariant(static_cast<JsonSignedInteger>(x))

Constructs a Json object from a signed int.

Json(signed long x)
    : JsonVariant(static_cast<JsonSignedInteger>(x))

Constructs a Json object from a signed long.

Json(signed long long x)
    : JsonVariant(static_cast<JsonSignedInteger>(x))

Constructs a Json object from a signed long long.

Json(unsigned char x)
    : JsonVariant(static_cast<JsonUnsignedInteger>(x))

Constructs a Json object from an unsigned char.

Json(unsigned short x)
    : JsonVariant(static_cast<JsonUnsignedInteger>(x))

Constructs a Json object from an unsigned short.

Json(unsigned int x)
    : JsonVariant(static_cast<JsonUnsignedInteger>(x))

Constructs a Json object from an unsigned int.

Json(unsigned long x)
    : JsonVariant(static_cast<JsonUnsignedInteger>(x))

Constructs a Json object from an unsigned long.

Json(unsigned long long x)
    : JsonVariant(static_cast<JsonUnsignedInteger>(x))

Constructs a Json object from an unsigned long long.

Json(const Json &) = default

Copy constructor.

Json(Json &&) = default

Move constructor.

operator= function (Json::operator=)

Json &operator=(const Json &) = default

Copy assignment operator.

Json &operator=(Json &&) = default

Move assignment operator.

Json function (Json::Json)

template <typename U> Json(U val) : JsonVariant(nullptr)

Constructs a Json object from any value type.
Template param U The type of the value.
Param val The value to construct the Json object from.

is function (Json::is)

template <typename T> bool is() const

Checks if the current Json value is of a specific type.
Template param T The type to check against.
Returns True if the current value is of type T; otherwise, false.

type function (Json::type)

JsonType type() const

Returns the JsonType of the current value.
Returns The type of the current Json value.

access function (Json::access)

template <typename T> const T &access() const &

Accesses the value of type T from the Json object.
Template param T The type to access.
Returns A const reference to the value of type T.

template <typename T> T &access() &

Accesses the value of type T from the Json object.
Template param T The type to access.
Returns A reference to the value of type T.

template <typename T> T &&access() &&

Accesses the value of type T from the Json object.
Template param T The type to access.
Returns An rvalue reference to the value of type T.

to function (Json::to)

template <typename T> RefOpt<T> to()

Converts the current Json value to another type.
Template param T The target type.
Returns An optional reference to type T, if the conversion is successful.

template <typename T> ConstRefOpt<T> to() const

Converts the current Json value to another type.
Template param T The target type.
Returns An optional value of type T, if the conversion is successful.

template <typename T> bool to(T &val) const

Converts the current Json value to another type and stores it in the provided variable.
Template param T The target type.
Param val The variable to store the converted value.
Returns True if the conversion was successful; otherwise, false.

toJson function (Json::toJson)

std::string toJson(int indent = 0) const

Converts the current Json object to a JSON string representation.
Param indent The number of spaces for indentation (default is 0). If negative, tabs will be used instead of spaces.
Returns A string representing the JSON.

fromJson function (Json::fromJson)

static std::optional<Json> fromJson(const std::string &s)

Parses a JSON string and returns a Json object.
Param s The JSON string to parse.
Returns An optional Json object if parsing is successful.

toMsgPack function (Json::toMsgPack)

Bytes toMsgPack() const

Converts the current Json object to a MessagePack byte array.
Returns A vector of bytes representing the MessagePack format.

fromMsgPack function (Json::fromMsgPack)

static std::optional<Json> fromMsgPack(const BytesView &s)

Parses a MessagePack byte array and returns a Json object.
Param s The byte array to parse.
Returns An optional Json object if parsing is successful.

getItem function (Json::getItem)

template <typename T = Json>
ConstRefOpt<T> getItem(std::string_view key) const

Retrieves an item from a JsonObject by key.
Template param T The type of the item.
Param key The key of the item to retrieve.
Returns An optional reference to the item of type T if it exists.

getItemTo function (Json::getItemTo)

template <typename T = Json>
bool getItemTo(std::string_view key, T &to) const

Retrieves an item from a JsonObject by key and stores it in the provided variable.
Template param T The type of the item.
Param key The key of the item to retrieve.
Param to The variable to store the item.
Returns True if the item exists and was retrieved successfully; otherwise, false.

exists function (Json::exists)

bool exists(std::string_view key) const

Checks if an item exists in the JsonObject by key.
Param key The key of the item to check.
Returns True if the item exists; otherwise, false.

setItem function (Json::setItem)

template <typename T>
bool setItem(std::string_view key, T &&val)

Sets an item in a JsonObject by key.
Template param T The type of the value to set.
Param key The key of the item to set.
Param val The value to assign to the item.
Returns True if the item was set successfully; otherwise, false.

itemByPath function (Json::itemByPath)

template <typename T = Json>
ConstRefOpt<T> itemByPath(std::string_view path) const

Retrieves an item from the Json object by path.
Template param T The type of the item.
Param path The path to the item.
Returns An optional reference to the item of type T if it exists.

setItemByPath function (Json::setItemByPath)

template <typename T>
bool setItemByPath(std::string_view path, T &&val)

Sets an item in the Json object by path.
Template param T The type of the value to set.
Param path The path to the item.
Param val The value to assign to the item.
Returns True if the item was set successfully; otherwise, false.

size function (Json::size)

size_t size() const

Returns the number of elements in the Json object or array.

Returns 0 if the Json value is neither an object nor an array
Returns The size of the Json object.

toObject function (Json::toObject)

Json &toObject()

Ensures that the current Json value is an object. If not, initializes it as an empty JsonObject.
Returns A reference to the current Json object.


JsonConverter class

template <typename T> JsonConverter

JsonConverter

JsonConverter

JsonConverter

template <typename T> JsonConverter

packArray function

template <typename... T>
inline bool packArray(Json &b, T &&...args)

Packs a variadic list of arguments into a JsonArray.

This function takes a variadic list of elements and constructs a JsonArray containing those elements. The elements are perfectly forwarded to ensure that their types are preserved (e.g., lvalues remain lvalues).
Template param T The types of the arguments to pack.
Param b The Json object to pack into, which will be assigned a new JsonArray.
Param args The variadic list of arguments to pack into the JsonArray.
Returns True, indicating that packing was successful.


unpackArray function

template <typename... T>
inline bool unpackArray(const Json &b, T &...args)

Unpacks the elements of a Json array into a variadic list of arguments.

This function attempts to extract elements from a given Json object (expected to be an array) and assign them to the provided arguments. The number of elements in the JsonArray must be at least equal to the number of provided arguments.
Template param T Types of the arguments to unpack into.
Param b The Json object to unpack from.
Param args The variadic list of arguments to unpack into.
Returns True if unpacking was successful; otherwise, false.


packArray function

template <typename T, size_t N>
inline bool packArray(Json &b, const T (&args)[N])

Packs a fixed-size array into a JsonArray.

This function takes a fixed-size array and assigns its elements to a Json object represented as a JsonArray.
Template param T The type of the elements in the array.
Template param N The number of elements in the array.
Param b The Json object to pack into.
Param args The fixed-size array of elements to pack.
Returns True, indicating that packing was successful.


unpackArray function

template <typename T, size_t N>
inline bool unpackArray(const Json &b, T (&args)[N])

Unpacks the elements of a JsonArray into a fixed-size array.

This function extracts elements from a given Json object (expected to be an array) and assigns them to a fixed-size array. The size of the JsonArray must be at least equal to the size of the provided array.
Template param T The type of the elements in the array.
Template param N The number of elements in the array.
Param b The Json object to unpack from.
Param args The fixed-size array to unpack into.
Returns True if unpacking was successful; otherwise, false.


JsonConverter class

template <HasReflection T> JsonConverter

Specialization of JsonConverter for types that have reflection metadata.

Provides functions to serialize and deserialize objects with reflection metadata to/from JSON.
Template param T The type of the object being converted to/from JSON.

toJson function (JsonConverter::toJson)

static bool toJson(Json &j, const T &val)

Serializes an object to a JSON object using reflection.
Param j The JSON object to populate.
Param val The object to serialize.
Returns true if serialization succeeds.

fromJson function (JsonConverter::fromJson)

static bool fromJson(const Json &j, T &val)

Deserializes an object from a JSON object using reflection.
Param j The JSON object to read from.
Param val The object to populate with data from the JSON object.
Returns true if deserialization succeeds.


JsonConverter class

template <typename T> JsonConverter

Specialization of JsonConverter for empty types.

Provides functions to serialize and deserialize empty types to/from JSON objects.
Template param T The empty type being converted to/from JSON.

toJson function (JsonConverter::toJson)

static bool toJson(Json &j, const T &val)

Serializes an empty object to a JSON object.
Param j The JSON object to populate (empty object).
Param val The object to serialize (empty, not used).
Returns Always returns true.

fromJson function (JsonConverter::fromJson)

static bool fromJson(const Json &j, T &val)

Deserializes an empty object from a JSON object.
Param j The JSON object to read from.
Param val The object to populate (empty, not modified).
Returns Always returns true.


Auto-generated from sources, Revision , https://github.com/brisklib/brisk/blob//include/brisk/