|
nfx-json 1.5.2
Modern C++20 JSON library with schema validation and generation
|
Low-level JSON value storage type. More...
#include <nfx/json/Document.h>
Classes | |
| class | ObjectIterator |
| Object iterator wrapper that provides key() and value(). More... | |
| class | KeysView |
| Range adapter for object keys. More... | |
| class | ValuesView |
| Range adapter for object values. More... | |
| class | MutableValuesView |
| Mutable values view. More... | |
| class | PathView |
| Path iterator for traversing all paths in a JSON document. More... | |
Public Member Functions | |
| Document () | |
| Default constructor. | |
| Document (bool value) noexcept | |
| Boolean constructor. | |
| Document (int value) noexcept | |
| Integer constructor. | |
| Document (unsigned int value) noexcept | |
| Unsigned integer constructor. | |
| Document (int64_t value) noexcept | |
| 64-bit integer constructor | |
| Document (uint64_t value) noexcept | |
| Unsigned integer constructor. | |
| Document (double value) noexcept | |
| Double constructor. | |
| Document (std::string value) | |
| String constructor. | |
| Document (std::string_view value) | |
| String view constructor. | |
| Document (const char *value) | |
| C-string constructor. | |
| template<size_t N> | |
| Document (const char(&value)[N]) | |
| String literal constructor (non-explicit for convenience). | |
| Document (std::nullptr_t) noexcept | |
| nullptr constructor (creates null JSON value) | |
| Document (Array value) | |
| Array constructor. | |
| Document (Object value) | |
| Object constructor. | |
| template<typename T> | |
| Document (T value, std::enable_if_t< std::is_same_v< T, long > &&!std::is_same_v< long, int > &&!std::is_same_v< long, int64_t >, int >=0) noexcept | |
| Long integer constructor (SFINAE overload for platforms where long != int && long != int64_t). | |
| template<typename T> | |
| Document (T value, std::enable_if_t< std::is_same_v< T, unsigned long > &&!std::is_same_v< unsigned long, unsigned int > &&!std::is_same_v< unsigned long, uint64_t >, int >=0) noexcept | |
| Unsigned long integer constructor (SFINAE overload for platforms where unsigned long != unsigned int && unsigned long != uint64_t). | |
| template<typename T> | |
| Document (T value, std::enable_if_t< std::is_same_v< T, long long > &&!std::is_same_v< long long, int64_t >, int >=0) noexcept | |
| Long long integer constructor (SFINAE overload for platforms where long long != int64_t). | |
| template<typename T> | |
| Document (T value, std::enable_if_t< std::is_same_v< T, unsigned long long > &&!std::is_same_v< unsigned long long, uint64_t >, int >=0) noexcept | |
| Unsigned long long integer constructor (SFINAE overload for platforms where unsigned long long != uint64_t). | |
| Document (const Document &other)=default | |
| Copy constructor. | |
| Document (Document &&other) noexcept=default | |
| Move constructor. | |
| ~Document ()=default | |
| Destructor. | |
| Document & | operator= (const Document &other)=default |
| Copy assignment. | |
| Document & | operator= (Document &&other) noexcept=default |
| Move assignment. | |
| Document & | operator= (std::string value) |
| Assignment from string. | |
| Document & | operator= (bool value) noexcept |
| Assignment from bool. | |
| Document & | operator= (int64_t value) noexcept |
| Assignment from int64_t. | |
| Document & | operator= (uint64_t value) noexcept |
| Assignment from uint64_t. | |
| Document & | operator= (double value) noexcept |
| Assignment from double. | |
| Document & | operator= (std::nullptr_t) noexcept |
| Assignment from nullptr (null). | |
| bool | operator== (const Document &other) const noexcept |
| Equality comparison. | |
| std::strong_ordering | operator<=> (const Document &other) const noexcept |
| Three-way comparison. | |
| Type | type () const noexcept |
| Get the JSON type. | |
| template<Value T> | |
| std::optional< T > | root () const |
| Get the root value of this document (safe, returns optional). | |
| template<Value T> | |
| bool | root (T &out) const |
| Get the root value of this document (output parameter version). | |
| template<Value T> | |
| std::optional< std::reference_wrapper< const T > > | rootRef () const |
| Get a reference to the root value (safe, no copy for large objects). | |
| template<Value T> | |
| std::optional< std::reference_wrapper< T > > | rootRef () |
| Get a mutable reference to the root value (safe, no copy). | |
| template<Checkable T> | |
| bool | isRoot () const |
| Check if the root value is of a specific type. | |
| template<typename Visitor> | |
| decltype(auto) | visit (Visitor &&visitor) const |
| Visit the root value with a visitor (const version). | |
| template<typename Visitor> | |
| decltype(auto) | visit (Visitor &&visitor) |
| Visit the root value with a visitor (mutable version). | |
| Document * | find (std::string_view key) noexcept |
| Get object member by key (mutable, returns nullptr if not found). | |
| const Document * | find (std::string_view key) const noexcept |
| Get object member by key (const version). | |
| void | set (std::string key, Document value) |
| Insert or assign object member. | |
| bool | contains (std::string_view key) const noexcept |
| Check if object contains key. | |
| Document & | at (std::string_view key) |
| Access object member by key (with bounds checking). | |
| const Document & | at (std::string_view key) const |
| Access object member by key (const version). | |
| Document & | at (size_t index) |
| Access array element with bounds checking. | |
| const Document & | at (size_t index) const |
| Access array element with bounds checking (const version). | |
| Document & | operator[] (std::string_view key) |
| Access or create object field. | |
| const Document & | operator[] (std::string_view key) const |
| Access object field (const). | |
| Document & | operator[] (size_t index) |
| Access or create array element. | |
| const Document & | operator[] (size_t index) const |
| Access array element (const). | |
| void | clear () |
| Clear object or array. | |
| size_t | erase (std::string_view key) |
| Erase key from object. | |
| size_t | erase (const char *key) |
| Erase key from object (overload for const char*). | |
| size_t | erase (const std::string &key) |
| Erase key from object (overload for std::string). | |
| template<typename iterator, typename = std::enable_if_t<!std::is_convertible_v<iterator, std::string_view>>> | |
| iterator | erase (iterator it) |
| Erase element from array by iterator. | |
| template<typename iterator> | |
| iterator | insert (iterator pos, Document value) |
| Insert element into array at iterator position. | |
| size_t | size () const noexcept |
| Get array size. | |
| bool | empty () const noexcept |
| Check if container is empty. | |
| void | push_back (Document value) |
| Append to array. | |
| void | reserve (size_t capacity) |
| Reserve capacity for array or object. | |
| size_t | capacity () const noexcept |
| Get capacity of array or object. | |
| Document & | front () |
| Get first element of array. | |
| const Document & | front () const |
| Get first element of array (const). | |
| Document & | back () |
| Get last element of array. | |
| const Document & | back () const |
| Get last element of array (const). | |
| auto | begin () |
| Begin iterator for arrays. | |
| auto | end () |
| End iterator for arrays. | |
| auto | begin () const |
| Begin iterator for arrays (const). | |
| auto | end () const |
| End iterator for arrays (const). | |
| ObjectIterator | objectBegin () const |
| Begin iterator for objects (returns ObjectIterator with key/value). | |
| ObjectIterator | objectEnd () const |
| End iterator for objects. | |
| KeysView | keys () const |
| Get a range view of object keys. | |
| ValuesView | values () const |
| Get a range view of object values (const). | |
| MutableValuesView | values () |
| Get a range view of object values (mutable). | |
| std::string | toString (int indent=0, size_t bufferSize=0) const |
| Convert to JSON string. | |
| std::vector< uint8_t > | toBytes () const |
| Convert to JSON bytes. | |
| void | merge (const Document &other, bool overwriteArrays=true) |
| Merge another Document into this one. | |
| void | update (std::string_view path, const Document &value) |
| Update value at specific path. | |
| template<Value T> | |
| std::optional< T > | get (std::string_view path) const |
| Get typed value at specified path. | |
| template<Value T> | |
| bool | get (std::string_view path, T &value) const |
| Get typed value at specified path into output parameter. | |
| template<Value T> | |
| std::optional< std::reference_wrapper< const T > > | getRef (std::string_view path) const |
| Get a const reference to value at specified path. | |
| template<Value T> | |
| std::optional< std::reference_wrapper< T > > | getRef (std::string_view path) |
| Get a mutable reference to value at specified path (zero-copy). | |
| template<Value T> | |
| void | set (std::string_view path, const T &value) |
| Set typed value at specified path (copy version). | |
| template<Value T> | |
| void | set (std::string_view path, T &&value) |
| Set typed value at specified path (move version). | |
| template<Container T> | |
| void | set (std::string_view path) |
| Create empty container at specified path. | |
| void | setNull (std::string_view path) |
| Set null value at specified path. | |
| template<Checkable T> | |
| bool | is (std::string_view path) const |
| Check if value at path is of specified type. | |
| bool | isNull (std::string_view path) const |
| Check if value at path is null. | |
| bool | isValid () const |
| Check if Document is in valid state. | |
Static Public Member Functions | |
| static Document | object () |
| Create empty object. | |
| static Document | array () |
| Create empty array. | |
| static std::optional< Document > | fromString (std::string_view jsonStr) |
| Create Document from JSON string. | |
| static bool | fromString (std::string_view jsonStr, Document &value) |
| Parse JSON string into existing Document. | |
| static std::optional< Document > | fromBytes (const std::vector< uint8_t > &bytes) |
| Create Document from JSON bytes. | |
| static bool | fromBytes (const std::vector< uint8_t > &bytes, Document &value) |
| Parse JSON bytes into existing Document. | |
Low-level JSON value storage type.
Represents a single JSON value (null, bool, number, string, array, or object). Uses std::variant for efficient type-tagged storage with minimal overhead. Object preserves insertion order using std::vector of key-value pairs. This is the internal storage used by Document - prefer using Document for most operations.
Definition at line 107 of file Document.h.
|
inlinenoexcept |
Boolean constructor.
| value | The boolean value |
|
inlinenoexcept |
Integer constructor.
| value | The integer value |
|
inlinenoexcept |
Unsigned integer constructor.
| value | The unsigned integer value |
|
inlinenoexcept |
64-bit integer constructor
| value | The 64-bit integer value |
|
inlinenoexcept |
Unsigned integer constructor.
| value | The unsigned integer value |
|
inlinenoexcept |
Double constructor.
| value | The double value |
|
inlineexplicit |
String constructor.
| value | The string value |
|
inlineexplicit |
String view constructor.
| value | The string view to convert to string |
|
inlineexplicit |
C-string constructor.
| value | The C-string to convert to string |
|
inline |
String literal constructor (non-explicit for convenience).
| N | Size of the string literal (including null terminator) |
| value | String literal |
|
inlineexplicit |
Array constructor.
| value | The array value |
|
inlineexplicit |
Object constructor.
| value | The object value |
|
inlinenoexcept |
Long integer constructor (SFINAE overload for platforms where long != int && long != int64_t).
| value | The long integer value |
|
inlinenoexcept |
Unsigned long integer constructor (SFINAE overload for platforms where unsigned long != unsigned int && unsigned long != uint64_t).
| value | The unsigned long integer value |
|
inlinenoexcept |
Long long integer constructor (SFINAE overload for platforms where long long != int64_t).
| value | The long long integer value |
|
inlinenoexcept |
Unsigned long long integer constructor (SFINAE overload for platforms where unsigned long long != uint64_t).
| value | The unsigned long long integer value |
|
default |
Copy constructor.
| other | The Document to copy from |
|
defaultnoexcept |
Move constructor.
| other | The Document to move from |
|
inlinestaticnodiscard |
Create empty array.
|
inlinenodiscard |
Access array element with bounds checking.
| index | The index to access |
| std::out_of_range | if index >= array size |
| std::runtime_error | if not an array |
|
inlinenodiscard |
Access array element with bounds checking (const version).
| index | The index to access |
| std::out_of_range | if index >= array size |
| std::runtime_error | if not an array |
|
inlinenodiscard |
Access object member by key (with bounds checking).
| key | The key to access |
| std::runtime_error | if not an object |
| std::out_of_range | if key not found |
|
inlinenodiscard |
Access object member by key (const version).
| key | The key to access |
| std::runtime_error | if not an object |
| std::out_of_range | if key not found |
|
inlinenodiscard |
Get last element of array.
|
inlinenodiscard |
Get last element of array (const).
|
inlinenodiscard |
Begin iterator for arrays.
|
inlinenodiscard |
Begin iterator for arrays (const).
|
inlinenodiscardnoexcept |
Get capacity of array or object.
|
inlinenodiscardnoexcept |
Check if object contains key.
| key | The key to search for (supports dot notation like "user.name" and array access like "data[0]") |
|
inlinenodiscardnoexcept |
Check if container is empty.
|
inlinenodiscard |
End iterator for arrays.
|
inlinenodiscard |
End iterator for arrays (const).
|
inline |
Erase key from object (overload for const char*).
| key | The key to remove |
|
inline |
Erase key from object (overload for std::string).
| key | The key to remove |
|
inline |
Erase element from array by iterator.
| iterator | The iterator type |
| it | The iterator pointing to the element to remove |
|
inline |
Erase key from object.
| key | The key to remove (supports simple keys, dot notation, and JSON Pointer paths) |
|
inlinenodiscardnoexcept |
Get object member by key (const version).
| key | The key to search for |
|
inlinenodiscardnoexcept |
Get object member by key (mutable, returns nullptr if not found).
| key | The key to search for |
|
staticnodiscard |
Create Document from JSON bytes.
| bytes | JSON bytes to parse |
|
staticnodiscard |
Parse JSON bytes into existing Document.
| bytes | JSON bytes to parse | |
| [out] | value | Document to populate |
|
staticnodiscard |
Create Document from JSON string.
| jsonStr | JSON string to parse |
|
staticnodiscard |
Parse JSON string into existing Document.
| jsonStr | JSON string to parse | |
| [out] | value | Document to populate |
|
inlinenodiscard |
Get first element of array.
| std::out_of_range | if array is empty |
|
inlinenodiscard |
Get first element of array (const).
| std::out_of_range | if array is empty |
|
nodiscard |
Get typed value at specified path.
| T | Type to retrieve (string, int, double, bool, Document) |
| path | Path to value (supports: "/key/subkey" JSON Pointer, "key.subkey" dot notation, "arr[0]" array syntax) |
|
nodiscard |
Get typed value at specified path into output parameter.
| T | Type to retrieve (string, int, double, bool, Document) |
| path | Path to value (supports: "/key/subkey" JSON Pointer, "key.subkey" dot notation, "arr[0]" array syntax) | |
| [out] | value | Output parameter to store the result |
|
nodiscard |
Get a mutable reference to value at specified path (zero-copy).
| T | Type to retrieve reference to |
| path | Path to value (supports: "/key/subkey" JSON Pointer, "key.subkey" dot notation, "arr[0]" array syntax) |
|
nodiscard |
Get a const reference to value at specified path.
| T | Type to retrieve reference to |
| path | Path to value (supports: "/key/subkey" JSON Pointer, "key.subkey" dot notation, "arr[0]" array syntax) |
|
inline |
Insert element into array at iterator position.
| iterator | The iterator type |
| pos | The position to insert at |
| value | The value to insert |
|
nodiscard |
Check if value at path is of specified type.
| T | Type to check for |
| path | Path to check (supports: "/key/subkey" JSON Pointer, "key.subkey" dot notation, "arr[0]" array syntax) |
|
nodiscard |
Check if value at path is null.
| path | Path to check (supports: "/key/subkey" JSON Pointer, "key.subkey" dot notation, "arr[0]" array syntax) |
|
inlinenodiscard |
Check if the root value is of a specific type.
| T | Type to check for (must satisfy Checkable concept) |
|
nodiscard |
Check if Document is in valid state.
|
inlinenodiscard |
Get a range view of object keys.
| void nfx::json::Document::merge | ( | const Document & | other, |
| bool | overwriteArrays = true ) |
Merge another Document into this one.
| other | Document to merge |
| overwriteArrays | Whether to overwrite arrays or merge them |
|
inlinestaticnodiscard |
Create empty object.
|
inlinenodiscard |
Begin iterator for objects (returns ObjectIterator with key/value).
|
inlinenodiscard |
End iterator for objects.
|
noexcept |
Three-way comparison.
| other | The Document to compare with |
|
inlinenoexcept |
Assignment from bool.
| value | The boolean value to assign |
Copy assignment.
| other | The Document to copy from |
Move assignment.
| other | The Document to move from |
|
inlinenoexcept |
Assignment from double.
| value | The double value to assign |
|
inlinenoexcept |
Assignment from int64_t.
| value | The integer value to assign |
|
inlinenoexcept |
Assignment from nullptr (null).
|
inline |
Assignment from string.
| value | The string value to assign |
|
inlinenoexcept |
Assignment from uint64_t.
| value | The unsigned integer value to assign |
|
inlinenoexcept |
Equality comparison.
| other | The Document to compare with |
|
inlinenodiscard |
Access or create array element.
| index | The index to access or create |
|
inlinenodiscard |
Access array element (const).
| index | The index to access |
|
inline |
Access or create object field.
| key | The key to access or create |
|
inlinenodiscard |
Access object field (const).
| key | The key to access |
|
inline |
Append to array.
| value | The value to append |
|
inline |
Reserve capacity for array or object.
| capacity | The number of elements to reserve space for |
|
inlinenodiscard |
Get the root value of this document (safe, returns optional).
| T | Type to retrieve |
|
inlinenodiscard |
Get the root value of this document (output parameter version).
| T | Type to retrieve |
| [out] | out | Variable to fill with the value |
|
inlinenodiscard |
Get a mutable reference to the root value (safe, no copy).
| T | Type to retrieve reference to |
|
inlinenodiscard |
Get a reference to the root value (safe, no copy for large objects).
| T | Type to retrieve reference to |
|
inline |
Insert or assign object member.
| key | The key to set |
| value | The value to assign |
| void nfx::json::Document::set | ( | std::string_view | path | ) |
Create empty container at specified path.
| T | Container type (Document, Object, Array) |
| path | Path where to create container (supports: "/key/subkey" JSON Pointer, "key.subkey" dot notation, "arr[0]" array syntax) |
| void nfx::json::Document::set | ( | std::string_view | path, |
| const T & | value ) |
Set typed value at specified path (copy version).
| T | Type to set (string, int, double, bool, Document) |
| path | Path where to set value (supports: "/key/subkey" JSON Pointer, "key.subkey" dot notation, "arr[0]" array syntax) |
| value | Value to set (copied) |
| void nfx::json::Document::set | ( | std::string_view | path, |
| T && | value ) |
Set typed value at specified path (move version).
| T | Type to set (string, int, double, bool, Document) |
| path | Path where to set value (supports: "/key/subkey" JSON Pointer, "key.subkey" dot notation, "arr[0]" array syntax) |
| value | Value to set (moved) |
| void nfx::json::Document::setNull | ( | std::string_view | path | ) |
Set null value at specified path.
| path | Path where to set null (supports: "/key/subkey" JSON Pointer, "key.subkey" dot notation, "arr[0]" array syntax) |
|
inlinenodiscardnoexcept |
Get array size.
|
inlinenodiscard |
Convert to JSON bytes.
|
nodiscard |
Convert to JSON string.
| indent | Indentation level (0 = compact output, >0 = pretty printing with N spaces) |
| bufferSize | Initial buffer capacity hint (0 = use default 4KB) |
|
inlinenodiscardnoexcept |
Get the JSON type.
| void nfx::json::Document::update | ( | std::string_view | path, |
| const Document & | value ) |
Update value at specific path.
| path | Path to update (supports: "/key/subkey" JSON Pointer, "key.subkey" dot notation, "arr[0]" array syntax) |
| value | New value to set |
|
inlinenodiscard |
Get a range view of object values (mutable).
|
inlinenodiscard |
Get a range view of object values (const).
|
inline |
Visit the root value with a visitor (mutable version).
| Visitor | Callable accepting any variant alternative type |
| visitor | Function/lambda to call with the underlying value |
|
inline |
Visit the root value with a visitor (const version).
| Visitor | Callable accepting any variant alternative type |
| visitor | Function/lambda to call with the underlying value |
Example usage with generic lambda:
Example with overloaded lambdas: