|
FlatImage
A configurable Linux containerization system
|
A type-safe wrapper around nlohmann::json for database operations. More...
#include <db.hpp>
Public Member Functions | |
| Db () noexcept | |
| Constructs a new Db object with an empty JSON object. | |
| template<typename T> requires std::same_as<std::remove_cvref_t<T>, json_t> | |
| Db (T &&json) noexcept | |
| Constructs a new Db object from a JSON object (move or copy) | |
| template<typename T> requires std::same_as<std::remove_cvref_t<T>, json_t> | |
| Db (std::reference_wrapper< T > const &json) noexcept | |
| Constructs a new Db object from a reference to an existing JSON object. | |
| std::vector< std::string > | keys () const noexcept |
| Retrieves all keys from the current JSON object or array. | |
| std::vector< std::pair< std::string, Db > > | items () const noexcept |
| Retrieves key-value pairs as Db objects from the current JSON. | |
| template<typename V = Db> | |
| Value< V > | value () noexcept |
| Converts the current JSON entry to a specified type. | |
| template<typename F, IsString Ks> | |
| decltype(auto) | apply (F &&f, Ks &&ks) |
| Value< std::string > | dump () |
| Serializes the current JSON data to a formatted string. | |
| bool | empty () const noexcept |
| Checks whether the JSON data is empty. | |
| template<IsString T> | |
| bool | contains (T &&t) const noexcept |
| Checks if the JSON contains a specific key. | |
| KeyType | type () const noexcept |
| Retrieves the type of the current JSON element. | |
| template<IsString T> | |
| bool | erase (T &&t) |
| Removes a key from the JSON structure. | |
| void | clear () |
| Resets the JSON data to an empty object. | |
| json_t & | data () |
| Retrieves a mutable reference to the underlying JSON data. | |
| json_t const & | data () const |
| Retrieves a const reference to the underlying JSON data. | |
| template<typename T> | |
| Db & | operator= (T &&t) |
| Assigns a new value to the underlying JSON data. | |
| Db | operator() (std::string const &t) |
| Accesses or creates a nested JSON entry by key. | |
Friends | |
| std::ostream & | operator<< (std::ostream &os, Db const &db) |
| Outputs the JSON data to an output stream. | |
A type-safe wrapper around nlohmann::json for database operations.
The Db class provides a user-friendly interface to nlohmann::json objects with support for both owned and referenced JSON data. It can either own its JSON data directly or wrap a reference to external JSON, enabling flexible use cases. The class supports element access, serialization, deserialization, and common database operations like clearing, erasing, and type checking. All operations are designed to be exception-safe through the Value<T> error handling pattern.
|
inlinenoexcept |
Constructs a new Db object with an empty JSON object.
Initializes the database with an empty nlohmann::json object as the underlying storage. The variant m_json is set to hold a json_t::object() by default.
Definition at line 118 of file db.hpp.
|
inlineexplicitnoexcept |
Constructs a new Db object from a JSON object (move or copy)
Creates a database by taking ownership of or copying a nlohmann::json object. The variant m_json directly stores the json_t value, supporting both move and copy construction depending on the value category of the passed argument.
| T | Type that is same as json_t (after removing cv and ref qualifiers) |
| json | The nlohmann::json object to store in the database (forwarded) |
|
inlineexplicitnoexcept |
Constructs a new Db object from a reference to an existing JSON object.
Creates a database wrapper around an existing nlohmann::json object passed by reference_wrapper. This allows the Db to operate on external JSON data without copying. The variant m_json stores the reference_wrapper, enabling modification of the original JSON object through the Db interface.
| T | Type that is same as json_t (after removing cv and ref qualifiers) |
| json | Reference wrapper containing the nlohmann::json object to wrap |
|
nodiscard |
|
inline |
Resets the JSON data to an empty object.
Clears all existing content and reinitializes the underlying JSON data as an empty object ({}). This operation discards all keys, values, and nested structures.
Definition at line 370 of file db.hpp.
|
nodiscardnoexcept |
Checks if the JSON contains a specific key.
Verifies whether the JSON object or array contains the specified key. Only valid for structured JSON types (objects and arrays); returns false for primitives. For objects, checks if the key exists as a property name. For arrays, checks if the key is a valid index.
| T | Type that satisfies the IsString concept (convertible to std::string) |
| key | The key to search for in the JSON structure |
Definition at line 309 of file db.hpp.
|
inline |
Retrieves a mutable reference to the underlying JSON data.
Accesses the JSON object stored in the variant m_json. If the variant holds a reference_wrapper, it extracts and returns the wrapped reference. Otherwise, it returns the directly stored json_t object. This allows uniform access regardless of whether the Db owns the JSON data or references external data.
Definition at line 163 of file db.hpp.
|
inline |
Retrieves a const reference to the underlying JSON data.
Provides const access to the JSON object by delegating to the non-const data() method through const_cast. This maintains the same access logic for both const and non-const contexts without code duplication.
Definition at line 182 of file db.hpp.
|
inlinenodiscard |
Serializes the current JSON data to a formatted string.
Converts the underlying nlohmann::json object to a string representation with 2-space indentation for readability. Uses the Try macro to safely handle any potential serialization errors and wrap them in the Value<std::string> result.
Definition at line 277 of file db.hpp.
|
inlinenodiscardnoexcept |
Checks whether the JSON data is empty.
Delegates to nlohmann::json's empty() method to determine if the underlying JSON structure contains no elements. For objects, this means no key-value pairs; for arrays, no elements; for strings, zero length.
Definition at line 291 of file db.hpp.
|
nodiscard |
Removes a key from the JSON structure.
Attempts to erase the specified key from the JSON data. Behavior depends on the JSON type:
| T | Type that satisfies the IsString concept (convertible to std::string) |
| key | The identifier of the key to remove |
Definition at line 343 of file db.hpp.
|
inlinenodiscardnoexcept |
Retrieves key-value pairs as Db objects from the current JSON.
Iterates over all entries in a structured JSON (object or array) and creates a pair containing the key as a string and the value wrapped in a new Db object. Returns an empty vector if the JSON is not structured. Each value is wrapped in its own Db instance, allowing nested database operations.
Definition at line 216 of file db.hpp.
|
inlinenodiscardnoexcept |
Retrieves all keys from the current JSON object or array.
Extracts the keys from a structured JSON element (object or array). For JSON objects, returns the property names. For arrays, returns string representations of indices. Returns an empty vector if the JSON is not structured. Uses C++20 ranges to transform the items view into a vector of strings.
Definition at line 197 of file db.hpp.
|
inlinenodiscard |
Accesses or creates a nested JSON entry by key.
Provides subscript-style access to JSON object properties. If the current JSON is an object or empty, accesses (or creates) the specified key and returns a new Db wrapping a reference to that nested element. If the JSON is not an object or empty, returns a copy of the current Db (no operation). Enables nested access like db("key1")("key2").
| key | The key to access or create in the JSON object |
Definition at line 404 of file db.hpp.
| Db & ns_db::Db::operator= | ( | T && | value | ) |
Assigns a new value to the underlying JSON data.
Replaces the current JSON content with the provided value using perfect forwarding. The value is converted to a JSON-compatible type by nlohmann::json's assignment operator. Returns a reference to this Db object to enable method chaining.
| T | Type of the value to assign (automatically deduced) |
| value | The value to assign to the JSON data |
Definition at line 387 of file db.hpp.
|
inlinenodiscardnoexcept |
Retrieves the type of the current JSON element.
Returns an enumeration value from nlohmann::json::value_t that indicates the JSON element's type (null, boolean, number_integer, number_unsigned, number_float, object, array, string, binary, or discarded).
Definition at line 324 of file db.hpp.
|
nodiscardnoexcept |
Converts the current JSON entry to a specified type.
Performs type-safe conversion of the JSON entry to the requested type V. Supports:
| V | The target type to convert to (default: Db) |
Definition at line 238 of file db.hpp.
|
friend |
Outputs the JSON data to an output stream.
Serializes the underlying nlohmann::json object and writes it to the provided output stream. This enables standard stream operations like std::cout << db. Uses nlohmann::json's built-in stream insertion operator for formatting.
| os | The target output stream |
| db | The database object to print |