nfx-serialization 0.3.0
Cross-platform C++ JSON serialization library with extensible trait capabilities
Loading...
Searching...
No Matches
nfx::serialization::json::Document Class Referencefinal

Generic JSON document abstraction for serialization. More...

#include <nfx/serialization/json/Document.h>

Classes

class  Object
 JSON object wrapper for Document. More...
class  Array
 JSON array wrapper for Document. More...
class  PathView
 Path iterator for traversing all paths in a JSON document. More...

Public Member Functions

 Document ()
 Default constructor - creates an empty document.
 Document (const Document &other)
 Copy constructor.
 Document (Document &&other) noexcept
 Move constructor.
 ~Document ()
 Destructor - cleans up document resources.
Documentoperator= (const Document &other)
 Copy assignment operator.
Documentoperator= (Document &&other) noexcept
 Move assignment operator.
bool operator== (const Document &other) const
 Equality comparison operator.
bool operator!= (const Document &other) const
 Inequality comparison operator.
std::string toString (int indent=0) const
 Convert document to JSON string.
std::vector< uint8_t > toBytes () const
 Convert document 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.
bool contains (std::string_view path) const
 Check if a value exists at the specified path.
template<JsonValue T>
std::optional< T > get (std::string_view path) const
 Get typed value at specified path.
template<JsonValue T>
bool get (std::string_view path, T &value) const
 Get typed value at specified path into output parameter.
template<JsonValue T>
void set (std::string_view path, const T &value)
 Set typed value at specified path (copy version).
template<JsonValue T>
void set (std::string_view path, T &&value)
 Set typed value at specified path (move version).
template<JsonContainer 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<JsonCheckable 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.
std::string lastError () const
 Get last error message.

Static Public Member Functions

static std::optional< DocumentfromString (std::string_view jsonStr)
 Create document from JSON string.
static bool fromString (std::string_view jsonStr, Document &doc)
 Parse JSON string into existing document.
static std::optional< DocumentfromBytes (std::span< const uint8_t > bytes)
 Create document from JSON bytes.
static bool fromBytes (std::span< const uint8_t > bytes, Document &doc)
 Parse JSON bytes into existing document.

Friends

class Document_impl
class SchemaValidator_impl

Detailed Description

Generic JSON document abstraction for serialization.

Provides a high-level interface for JSON document manipulation with support for JSON Pointer paths, type-safe value access, and nested object/array operations.

Definition at line 58 of file Document.h.

Constructor & Destructor Documentation

◆ Document() [1/2]

nfx::serialization::json::Document::Document ( const Document & other)

Copy constructor.

Parameters
otherThe document to copy from
Here is the call graph for this function:

◆ Document() [2/2]

nfx::serialization::json::Document::Document ( Document && other)
noexcept

Move constructor.

Parameters
otherThe document to move from
Here is the call graph for this function:

Member Function Documentation

◆ contains()

bool nfx::serialization::json::Document::contains ( std::string_view path) const

Check if a value exists at the specified path.

Parameters
pathJSON Pointer path (e.g., "/user/name") or dot notation (e.g., "user.name")
Returns
true if any JSON value exists at the path, false otherwise

◆ fromBytes() [1/2]

std::optional< Document > nfx::serialization::json::Document::fromBytes ( std::span< const uint8_t > bytes)
static

Create document from JSON bytes.

Parameters
bytesJSON bytes to parse (accepts vector, array, or raw pointer+size)
Returns
Optional document if parsing succeeds, empty optional otherwise

◆ fromBytes() [2/2]

bool nfx::serialization::json::Document::fromBytes ( std::span< const uint8_t > bytes,
Document & doc )
static

Parse JSON bytes into existing document.

Parameters
bytesJSON bytes to parse (accepts vector, array, or raw pointer+size)
[out]docDocument to populate
Returns
true if parsing succeeds, false otherwise
Here is the call graph for this function:

◆ fromString() [1/2]

std::optional< Document > nfx::serialization::json::Document::fromString ( std::string_view jsonStr)
static

Create document from JSON string.

Parameters
jsonStrJSON string to parse
Returns
Optional document if parsing succeeds, empty optional otherwise

◆ fromString() [2/2]

bool nfx::serialization::json::Document::fromString ( std::string_view jsonStr,
Document & doc )
static

Parse JSON string into existing document.

Parameters
jsonStrJSON string to parse
[out]docDocument to populate
Returns
true if parsing succeeds, false otherwise
Here is the call graph for this function:

◆ get() [1/2]

template<JsonValue T>
std::optional< T > nfx::serialization::json::Document::get ( std::string_view path) const

Get typed value at specified path.

Template Parameters
TType to retrieve (string, int, double, bool, Document, Object, Array)
Parameters
pathJSON Pointer path to value
Returns
Optional containing value if exists and correct type, empty otherwise

◆ get() [2/2]

template<JsonValue T>
bool nfx::serialization::json::Document::get ( std::string_view path,
T & value ) const

Get typed value at specified path into output parameter.

Template Parameters
TType to retrieve (string, int, double, bool, Document, Object, Array)
Parameters
pathJSON Pointer path to value
[out]valueOutput parameter to store the result
Returns
true if value exists and was successfully retrieved, false otherwise

◆ is()

template<JsonCheckable T>
bool nfx::serialization::json::Document::is ( std::string_view path) const

Check if value at path is of specified type.

Template Parameters
TType to check for
Parameters
pathJSON Pointer path to check
Returns
true if value exists and is of type T, false otherwise

◆ isNull()

bool nfx::serialization::json::Document::isNull ( std::string_view path) const

Check if value at path is null.

Parameters
pathJSON Pointer path to check
Returns
true if value is null, false otherwise

◆ isValid()

bool nfx::serialization::json::Document::isValid ( ) const

Check if document is in valid state.

Returns
true if document is valid, false otherwise

◆ lastError()

std::string nfx::serialization::json::Document::lastError ( ) const

Get last error message.

Returns
Error message string

◆ merge()

void nfx::serialization::json::Document::merge ( const Document & other,
bool overwriteArrays = true )

Merge another document into this one.

Parameters
otherDocument to merge
overwriteArraysWhether to overwrite arrays or merge them
Here is the call graph for this function:

◆ operator!=()

bool nfx::serialization::json::Document::operator!= ( const Document & other) const

Inequality comparison operator.

Parameters
otherThe document to compare with
Returns
true if documents are not equal, false otherwise
Here is the call graph for this function:

◆ operator=() [1/2]

Document & nfx::serialization::json::Document::operator= ( const Document & other)

Copy assignment operator.

Parameters
otherThe document to copy from
Returns
Reference to this document
Here is the call graph for this function:

◆ operator=() [2/2]

Document & nfx::serialization::json::Document::operator= ( Document && other)
noexcept

Move assignment operator.

Parameters
otherThe document to move from
Returns
Reference to this document
Here is the call graph for this function:

◆ operator==()

bool nfx::serialization::json::Document::operator== ( const Document & other) const

Equality comparison operator.

Parameters
otherThe document to compare with
Returns
true if documents are equal, false otherwise
Here is the call graph for this function:

◆ set() [1/3]

template<JsonContainer T>
void nfx::serialization::json::Document::set ( std::string_view path)

Create empty container at specified path.

Template Parameters
TContainer type (Document, Object, Array)
Parameters
pathJSON Pointer path where to create container

◆ set() [2/3]

template<JsonValue T>
void nfx::serialization::json::Document::set ( std::string_view path,
const T & value )

Set typed value at specified path (copy version).

Template Parameters
TType to set (string, int, double, bool, Document, Object, Array)
Parameters
pathJSON Pointer path where to set value
valueValue to set (copied)

◆ set() [3/3]

template<JsonValue T>
void nfx::serialization::json::Document::set ( std::string_view path,
T && value )

Set typed value at specified path (move version).

Template Parameters
TType to set (string, int, double, bool, Document, Object, Array)
Parameters
pathJSON Pointer path where to set value
valueValue to set (moved)

◆ setNull()

void nfx::serialization::json::Document::setNull ( std::string_view path)

Set null value at specified path.

Parameters
pathJSON Pointer path where to set null

◆ toBytes()

std::vector< uint8_t > nfx::serialization::json::Document::toBytes ( ) const

Convert document to JSON bytes.

Returns
JSON byte representation

◆ toString()

std::string nfx::serialization::json::Document::toString ( int indent = 0) const

Convert document to JSON string.

Parameters
indentIndentation level for pretty printing
  • 0: compact output (no whitespace)
  • >0: pretty-print with specified spaces per level
Returns
JSON string representation

◆ update()

void nfx::serialization::json::Document::update ( std::string_view path,
const Document & value )

Update value at specific path.

Parameters
pathJSON Pointer path to update
valueNew value to set
Here is the call graph for this function:

◆ Document_impl

friend class Document_impl
friend

Definition at line 72 of file Document.h.

◆ SchemaValidator_impl

friend class SchemaValidator_impl
friend

Definition at line 73 of file Document.h.


The documentation for this class was generated from the following file: