45namespace nfx::serialization::json
72 friend class Document_impl;
73 friend class SchemaValidator_impl;
150 static std::optional<Document>
fromString( std::string_view jsonStr );
165 static std::optional<Document>
fromBytes( std::span<const uint8_t> bytes );
233 template <JsonValue T>
234 std::optional<T>
get( std::string_view path )
const;
243 template <JsonValue T>
244 bool get( std::string_view path, T& value )
const;
256 template <JsonValue T>
257 void set( std::string_view path,
const T& value );
265 template <JsonValue T>
266 void set( std::string_view path, T&& value );
277 template <JsonContainer T>
278 void set( std::string_view path );
300 template <JsonCheckable T>
301 bool is( std::string_view path )
const;
308 bool isNull( std::string_view path )
const;
341 friend class Document;
342 friend class Document_impl;
349 Object( Document* doc, std::string_view path );
482 template <JsonValue T>
483 std::optional<T>
get( std::string_view path )
const;
492 template <JsonValue T>
493 bool get( std::string_view path, T& value )
const;
505 template <JsonValue T>
506 void set( std::string_view path,
const T& value );
514 template <JsonValue T>
515 void set( std::string_view path, T&& value );
637 std::string keyAt(
size_t index )
const;
644 Document valueAt(
size_t index )
const;
669 friend class Document;
670 friend class Document_impl;
677 Array( Document* doc, std::string_view path );
810 template <JsonValue T>
811 std::optional<T>
get(
size_t index )
const;
820 template <JsonValue T>
821 bool get(
size_t index, T& value )
const;
833 template <JsonValue T>
834 std::optional<T>
get( std::string_view path )
const;
843 template <JsonValue T>
844 bool get( std::string_view path, T& value )
const;
856 template <JsonValue T>
857 void set(
size_t index,
const T& value );
865 template <JsonValue T>
866 void set(
size_t index, T&& value );
878 template <JsonValue T>
879 void set( std::string_view path,
const T& value );
887 template <JsonValue T>
888 void set( std::string_view path, T&& value );
899 template <JsonValue T>
907 template <JsonValue T>
938 template <JsonValue T>
939 void insert(
size_t index,
const T& value );
947 template <JsonValue T>
955 void insert(
size_t index, Document& value );
1198 bool includeContainers =
true );
1224 size_t size()
const {
return m_entries.size(); }
1230 bool empty()
const {
return m_entries.empty(); }
1272 : m_entries{ nullptr },
1282 Iterator(
const std::vector<Entry>* entries,
size_t index )
1283 : m_entries{ entries },
1328 if ( !m_entries && !other.m_entries )
1334 return other.m_index >= other.m_entries->size();
1336 if ( !other.m_entries )
1338 return m_index >= m_entries->size();
1340 return m_entries == other.m_entries && m_index == other.m_index;
1351 const std::vector<Entry>* m_entries;
1364 void buildEntries(
const Document& doc );
1371 std::string formatPath(
const std::vector<std::string>& segments )
const;
1378 bool m_includeContainers;
1379 std::vector<Entry> m_entries;
C++20 concepts for JSON serialization type constraints.
Type trait to identify JSON container types (Object, Array).
Generic JSON document abstraction for serialization.
Document(Document &&other) noexcept
Move constructor.
std::string lastError() const
Get last error message.
static bool fromString(std::string_view jsonStr, Document &doc)
Parse JSON string into existing document.
void set(std::string_view path, T &&value)
Set typed value at specified path (move version).
bool isNull(std::string_view path) const
Check if value at path is null.
void update(std::string_view path, const Document &value)
Update value at specific path.
Document()
Default constructor - creates an empty document.
bool is(std::string_view path) const
Check if value at path is of specified type.
void set(std::string_view path)
Create empty container at specified path.
bool operator==(const Document &other) const
Equality comparison operator.
bool isValid() const
Check if document is in valid state.
~Document()
Destructor - cleans up document resources.
void set(std::string_view path, const T &value)
Set typed value at specified path (copy version).
bool contains(std::string_view path) const
Check if a value exists at the specified path.
std::optional< T > get(std::string_view path) const
Get typed value at specified path.
static std::optional< Document > fromBytes(std::span< const uint8_t > bytes)
Create document from JSON bytes.
Document & operator=(const Document &other)
Copy assignment operator.
std::string toString(int indent=0) const
Convert document to JSON string.
void setNull(std::string_view path)
Set null value at specified path.
static std::optional< Document > fromString(std::string_view jsonStr)
Create document from JSON string.
Document(const Document &other)
Copy constructor.
void merge(const Document &other, bool overwriteArrays=true)
Merge another document into this one.
Document & operator=(Document &&other) noexcept
Move assignment operator.
static bool fromBytes(std::span< const uint8_t > bytes, Document &doc)
Parse JSON bytes into existing document.
std::vector< uint8_t > toBytes() const
Convert document to JSON bytes.
bool get(std::string_view path, T &value) const
Get typed value at specified path into output parameter.
bool operator!=(const Document &other) const
Inequality comparison operator.
JSON object wrapper for Document.
bool get(std::string_view path, T &value) const
Get field value into output parameter.
size_t size() const
Get number of fields in object.
Document::Object & operator=(Document::Object &&other) noexcept
Move assignment operator.
bool isValid() const
Check if object is valid.
void clear()
Clear all fields from object.
Document::Object & operator=(const Document::Object &other)
Copy assignment operator.
bool contains(std::string_view fieldName) const
Check if a field exists in this object.
Iterator end() const
Get iterator past last field.
void set(std::string_view path, const T &value)
Set field value (copy version).
std::string lastError() const
Get last error message.
void set(std::string_view path, T &&value)
Set field value (move version).
Iterator begin() const
Get iterator to first field.
std::optional< T > get(std::string_view path) const
Get field value by key.
std::vector< uint8_t > toBytes() const
Convert object to JSON bytes.
Object(Document::Object &&other) noexcept
Move constructor.
Object()
Default constructor - creates invalid object.
~Object()=default
Destructor.
bool operator==(const Document::Object &other) const
Equality comparison operator.
bool removeField(std::string_view key)
Remove field from object.
bool operator!=(const Document::Object &other) const
Inequality comparison operator.
Object(const Document::Object &other)
Copy constructor.
std::string toString(int indent=0) const
Convert object to JSON string.
Forward iterator for Object fields.
value_type operator*() const
Dereference operator.
Iterator & operator++()
Pre-increment operator.
std::ptrdiff_t difference_type
Type for iterator difference.
bool operator!=(const Iterator &other) const
Inequality comparison.
Iterator(const Object *obj, size_t index)
Construct iterator at specific position.
value_type & reference
Reference to value type.
value_type * pointer
Pointer to value type.
std::pair< std::string, Document > value_type
Type of value yielded by iterator.
Iterator()
Default constructor - creates end iterator.
Iterator operator++(int)
Post-increment operator.
bool operator==(const Iterator &other) const
Equality comparison.
std::forward_iterator_tag iterator_category
Iterator category tag for STL compatibility.
JSON array wrapper for Document.
void append(Document &value)
Append Document to end of array (reference version).
bool operator==(const Document::Array &other) const
Equality comparison.
std::optional< T > get(size_t index) const
Get element at index.
void insert(size_t index, const T &value)
Insert element at index (copy version).
void set(size_t index, const T &value)
Set element at index (copy version).
std::string toString(int indent=0) const
Convert array to JSON string.
bool get(size_t index, T &value) const
Get element at index into output parameter.
void set(size_t index, T &&value)
Set element at index (move version).
void insert(size_t index, Document::Array &value)
Insert Array at index (reference version).
Document::Array & operator=(const Document::Array &other)
Copy assignment operator.
void set(std::string_view path, const T &value)
Set nested element at path (copy version).
bool contains(std::string_view indexStr) const
Check if an element exists at the specified index.
Array(Document::Array &&other) noexcept
Move constructor.
bool operator!=(const Document::Array &other) const
Inequality comparison.
void append(Document::Object &value)
Append Object to end of array (reference version).
Document::Array & operator=(Document::Array &&other) noexcept
Move assignment operator.
void append(T &&value)
Append element to end of array (move version).
size_t size() const
Get number of elements in array.
bool isValid() const
Check if array is valid.
void insert(size_t index, Document &value)
Insert Document at index (reference version).
Iterator end() const
Get iterator past last element.
~Array()=default
Destructor.
void insert(size_t index, Document::Object &value)
Insert Object at index (reference version).
void set(std::string_view path, T &&value)
Set nested element at path (move version).
bool remove(size_t index)
Remove element at index.
void clear()
Clear all elements from array.
std::vector< uint8_t > toBytes() const
Convert array to JSON bytes.
void append(Document::Array &value)
Append Array to end of array (reference version).
Array(const Document::Array &other)
Copy constructor.
void insert(size_t index, T &&value)
Insert element at index (move version).
void append(const T &value)
Append element to end of array (copy version).
Array()
Default constructor - creates empty array.
std::string lastError() const
Get last error message.
std::optional< T > get(std::string_view path) const
Get nested element at path.
Iterator begin() const
Get iterator to first element.
bool get(std::string_view path, T &value) const
Get nested element at path into output parameter.
Forward iterator for Array elements.
Document value_type
Type of value yielded by iterator.
value_type * pointer
Pointer to value type.
Iterator(const Array *arr, size_t index)
Construct iterator at specific position.
Iterator & operator++()
Pre-increment operator.
std::ptrdiff_t difference_type
Type for iterator difference.
bool operator!=(const Iterator &other) const
Inequality comparison.
bool operator==(const Iterator &other) const
Equality comparison.
value_type operator*() const
Dereference operator.
std::forward_iterator_tag iterator_category
Iterator category tag for STL compatibility.
value_type & reference
Reference to value type.
Iterator operator++(int)
Post-increment operator.
Iterator()
Default constructor - creates end iterator.
bool empty() const
Check if empty.
Iterator end() const
Get iterator past last entry.
Format
Format for path string representation.
@ DotNotation
Dot notation format (e.g., "user.addresses[0].city").
@ JsonPointer
RFC 6901 JSON Pointer format (e.g., "/user/addresses/0/city").
size_t size() const
Get number of path entries.
const Entry & operator[](size_t index) const
Access entry by index.
std::vector< Entry > leaves() const
Get only leaf entries (primitives).
Iterator begin() const
Get iterator to first entry.
PathView(const Document &doc, Format format=Format::JsonPointer, bool includeContainers=true)
Construct PathView for a document.
Represents a single path entry in the document.
Entry(const Entry &other)
Copy constructor (deep copy).
bool isLeaf
True if value is a primitive (not object/array).
std::unique_ptr< Document > valuePtr
The value at this path (owned pointer).
Entry()
Default constructor.
Entry(Entry &&other) noexcept=default
Move constructor.
std::string path
Full path to this value.
Entry & operator=(const Entry &other)
Copy assignment (deep copy).
Entry & operator=(Entry &&other) noexcept=default
Move assignment.
size_t depth
Nesting depth (0 = root level).
const Document & value() const
Get the value as a Document reference.
Forward iterator for path entries.
bool operator==(const Iterator &other) const
Equality comparison.
bool operator!=(const Iterator &other) const
Inequality comparison.
std::forward_iterator_tag iterator_category
Iterator category tag for STL compatibility.
Iterator operator++(int)
Post-increment operator.
Iterator(const std::vector< Entry > *entries, size_t index)
Construct iterator at position.
const Entry * pointer
Pointer to value type.
Entry value_type
Type of value yielded by iterator.
Iterator & operator++()
Pre-increment operator.
reference operator*() const
Dereference operator.
pointer operator->() const
Arrow operator.
Iterator()
Default constructor - creates end iterator.
std::ptrdiff_t difference_type
Type for iterator difference.
const Entry & reference
Reference to value type.