69 using Object = std::vector<std::pair<std::string, Document>>;
74 using Array = std::vector<Document>;
163 inline explicit Document( std::string_view value );
200 template <
typename T>
204 std::is_same_v<T, long> && !std::is_same_v<long, int> && !std::is_same_v<long, int64_t>,
212 template <typename T>
216 std::is_same_v<T,
unsigned long> && !std::is_same_v<
unsigned long,
unsigned int> &&
217 !std::is_same_v<
unsigned long, uint64_t>,
224 template <typename T>
227 std::enable_if_t<std::is_same_v<T,
long long> && !std::is_same_v<
long long, int64_t>,
int> = 0 ) noexcept;
234 template <typename T>
238 std::is_same_v<T,
unsigned long long> && !std::is_same_v<
unsigned long long, uint64_t>,
251#if defined( __GNUC__ ) && !defined( __clang__ )
252# pragma GCC diagnostic push
253# pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
256#if defined( __GNUC__ ) && !defined( __clang__ )
257# pragma GCC diagnostic pop
381 [[nodiscard]] inline std::optional<T>
root() const;
392 [[nodiscard]] inline
bool root( T& out ) const;
404 [[nodiscard]] inline std::optional<std::reference_wrapper<const T>>
rootRef() const;
415 [[nodiscard]] inline std::optional<std::reference_wrapper<T>>
rootRef();
424 [[nodiscard]] inline
bool isRoot() const;
458 template <typename Visitor>
459 inline decltype( auto )
visit( Visitor&& visitor ) const;
468 template <typename Visitor>
469 inline decltype( auto )
visit( Visitor&& visitor );
487 [[nodiscard]] inline const
Document*
find( std::string_view key ) const noexcept;
501 [[nodiscard]] inline
bool contains( std::string_view key ) const noexcept;
519 [[nodiscard]] inline const
Document&
at( std::string_view key ) const;
537 [[nodiscard]] inline const
Document&
at(
size_t index ) const;
551 inline
Document& operator[]( std::string_view key );
558 [[nodiscard]] inline const
Document& operator[]( std::string_view key ) const;
573 [[nodiscard]] inline
Document& operator[](
size_t index );
580 [[nodiscard]] inline const
Document& operator[](
size_t index ) const;
590 inline
size_t erase( std::string_view key );
597 inline
size_t erase( const
char* key );
604 inline
size_t erase( const std::
string& key );
612 template <typename iterator, typename = std::enable_if_t<!std::is_convertible_v<iterator, std::string_view>>>
613 inline iterator
erase( iterator it );
622 template <typename iterator>
633 [[nodiscard]] inline
size_t size() const noexcept;
639 [[nodiscard]] inline
bool empty() const noexcept;
659 [[nodiscard]] inline
size_t capacity() const noexcept;
697 [[nodiscard]] inline auto
end();
703 [[nodiscard]] inline auto
begin() const;
709 [[nodiscard]] inline auto
end() const;
764 inline const std::
string&
key() const;
846 Object::const_iterator m_it;
913 inline
bool operator==( const
iterator& other ) const;
920 inline
bool operator!=( const
iterator& other ) const;
951 Object::const_iterator m_it;
1031 Object::iterator m_it;
1136 Object::iterator m_it;
1259 [[nodiscard]] std::
string toString(
int indent = 0,
size_t bufferSize = 0 ) const;
1265 [[nodiscard]] inline std::vector<uint8_t>
toBytes() const;
1328 [[nodiscard]] std::optional<T>
get( std::string_view path ) const;
1339 [[nodiscard]]
bool get( std::string_view path, T& value ) const;
1354 [[nodiscard]] std::optional<std::reference_wrapper<const T>>
getRef( std::string_view path ) const;
1367 [[nodiscard]] std::optional<std::reference_wrapper<T>>
getRef( std::string_view path );
1377 void set( std::string_view path, const T& value );
1387 void set( std::string_view path, T&& value );
1400 void set( std::string_view path );
1417 [[nodiscard]]
bool is( std::string_view path ) const;
1425 [[nodiscard]]
bool isNull( std::string_view path ) const;
1623 inline iterator(
const std::vector<Entry>* entries,
size_t index );
1664 const std::vector<Entry>* m_entries;
1677 void buildEntries(
const Document& doc );
1684 std::string formatPath(
const std::vector<std::string>& segments )
const;
1691 bool m_includeContainers;
1692 std::vector<Entry> m_entries;
1706 template <
typename T>
1707 const T& rootInternal()
const;
1715 template <
typename T>
1728 bool containsPath( std::string_view path )
const;
1736 size_t erasePath( std::string_view path );
1749 Document* navigateToPath( std::string_view path,
bool createPath =
false );
1757 const Document* navigateToPath( std::string_view path )
const;
1765 Document* navigateToJsonPointer( std::string_view pointer,
bool createPath =
false );
1772 const Document* navigateToJsonPointer( std::string_view pointer )
const;
1777 static std::string unescapeJsonPointerToken( std::string_view token )
noexcept;
1782 static bool isValidArrayIndex( std::string_view token )
noexcept;
1788#if defined( __GNUC__ ) && !defined( __clang__ )
1789# pragma GCC diagnostic push
1790# pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
1792 std::variant<std::nullptr_t, bool, int64_t, uint64_t, double, std::string, Array, Object> m_data;
1793#if defined( __GNUC__ ) && !defined( __clang__ )
1794# pragma GCC diagnostic pop
1799#include "detail/Document.inl"
C++20 concepts for JSON type constraints.
std::vector< std::pair< std::string, Document > > Object
Ordered map for JSON objects (preserves insertion order).
std::vector< Document > Array
Array for JSON arrays.
Type trait to identify JSON container types (Object, Array).
Low-level JSON value storage type.
Document(int64_t value) noexcept
64-bit integer constructor
Document & operator=(Document &&other) noexcept=default
Move assignment.
static std::optional< Document > fromBytes(const std::vector< uint8_t > &bytes)
Create Document from JSON bytes.
Document & front()
Get first element of array.
Document & operator=(std::string value)
Assignment from string.
Document(int value) noexcept
Integer constructor.
bool isRoot() const
Check if the root value is of a specific type.
bool operator==(const Document &other) const noexcept
Equality comparison.
size_t capacity() const noexcept
Get capacity of array or object.
void clear()
Clear object or array.
Document & operator=(bool value) noexcept
Assignment from bool.
bool is(std::string_view path) const
Check if value at path is of specified type.
Document(const char *value)
C-string constructor.
Type type() const noexcept
Get the JSON type.
void merge(const Document &other, bool overwriteArrays=true)
Merge another Document into this one.
static std::optional< Document > fromString(std::string_view jsonStr)
Create Document from JSON string.
Document & operator=(int64_t value) noexcept
Assignment from int64_t.
Document(unsigned int value) noexcept
Unsigned integer constructor.
std::optional< T > get(std::string_view path) const
Get typed value at specified path.
void reserve(size_t capacity)
Reserve capacity for array or object.
decltype(auto) visit(Visitor &&visitor) const
Visit the root value with a visitor (const version).
ObjectIterator objectEnd() const
End iterator for objects.
Document(uint64_t value) noexcept
Unsigned integer constructor.
std::string toString(int indent=0, size_t bufferSize=0) const
Convert to JSON string.
void set(std::string key, Document value)
Insert or assign object member.
std::vector< uint8_t > toBytes() const
Convert to JSON bytes.
bool contains(std::string_view key) const noexcept
Check if object contains key.
Document & operator=(uint64_t value) noexcept
Assignment from uint64_t.
auto begin()
Begin iterator for arrays.
Document(std::string_view value)
String view constructor.
Document(Document &&other) noexcept=default
Move constructor.
void update(std::string_view path, const Document &value)
Update value at specific path.
std::optional< T > root() const
Get the root value of this document (safe, returns optional).
~Document()=default
Destructor.
ObjectIterator objectBegin() const
Begin iterator for objects (returns ObjectIterator with key/value).
Document(double value) noexcept
Double constructor.
static Document object()
Create empty object.
bool isNull(std::string_view path) const
Check if value at path is null.
ValuesView values() const
Get a range view of object values (const).
Document(std::nullptr_t) noexcept
nullptr constructor (creates null JSON value)
void push_back(Document value)
Append to array.
KeysView keys() const
Get a range view of object keys.
size_t erase(std::string_view key)
Erase key from object.
void setNull(std::string_view path)
Set null value at specified path.
Document * find(std::string_view key) noexcept
Get object member by key (mutable, returns nullptr if not found).
bool isValid() const
Check if Document is in valid state.
Document & operator=(const Document &other)=default
Copy assignment.
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 !...
Document(Array value)
Array constructor.
Document & operator=(std::nullptr_t) noexcept
Assignment from nullptr (null).
bool empty() const noexcept
Check if container is empty.
iterator insert(iterator pos, Document value)
Insert element into array at iterator position.
std::strong_ordering operator<=>(const Document &other) const noexcept
Three-way comparison.
Document & operator=(double value) noexcept
Assignment from double.
Document(std::string value)
String constructor.
static Document array()
Create empty array.
Document()
Default constructor.
std::optional< std::reference_wrapper< const T > > rootRef() const
Get a reference to the root value (safe, no copy for large objects).
Document(const char(&value)[N])
String literal constructor (non-explicit for convenience).
std::optional< std::reference_wrapper< const T > > getRef(std::string_view path) const
Get a const reference to value at specified path.
Document & at(std::string_view key)
Access object member by key (with bounds checking).
size_t size() const noexcept
Get array size.
Document(bool value) noexcept
Boolean constructor.
auto end()
End iterator for arrays.
Document(Object value)
Object constructor.
Document & back()
Get last element of array.
Object iterator wrapper that provides key() and value().
std::ptrdiff_t difference_type
iterator difference type
ObjectIterator(const ObjectIterator &)=default
Copy constructor.
const std::string & key() const
Get the key of the current element.
const Document & value() const
Get the value of the current element (const).
MapIterator base() const
Get the underlying iterator.
std::forward_iterator_tag iterator_category
iterator category (forward iterator)
std::pair< const std::string, Document > value_type
iterator value type
MapIterator it
The underlying iterator.
const value_type & reference
iterator reference type
const value_type * pointer
iterator pointer type
ObjectIterator(MapIterator iter)
Construct from underlying iterator.
ObjectIterator(ObjectIterator &&) noexcept=default
Move constructor.
Object::const_iterator MapIterator
Underlying iterator type for Object.
Range adapter for object keys.
iterator begin() const
Get iterator to first key.
iterator end() const
Get iterator past last key.
KeysView(const Object *obj)
Construct KeysView from object pointer.
iterator(Object::const_iterator it)
Construct iterator from underlying Object iterator.
iterator(iterator &&) noexcept=default
Move constructor.
iterator(const iterator &)=default
Copy constructor.
std::string value_type
iterator value type
const std::string & reference
iterator reference type
std::forward_iterator_tag iterator_category
iterator category (forward iterator)
std::ptrdiff_t difference_type
iterator difference type
const std::string * pointer
iterator pointer type
Range adapter for object values.
ValuesView(const Object *obj)
Construct ValuesView from object pointer.
const_iterator end() const
Get iterator past last value.
const_iterator begin() const
Get iterator to first value.
const Document & reference
iterator reference type
std::forward_iterator_tag iterator_category
iterator category (forward iterator)
const_iterator(Object::const_iterator it)
Construct const_iterator from underlying Object iterator.
Document value_type
iterator value type
const_iterator(const const_iterator &)=default
Copy constructor.
const_iterator(const_iterator &&) noexcept=default
Move constructor.
const Document * pointer
iterator pointer type
std::ptrdiff_t difference_type
iterator difference type
std::forward_iterator_tag iterator_category
iterator category (forward iterator)
std::ptrdiff_t difference_type
iterator difference type
Document value_type
iterator value type
Document * pointer
iterator pointer type
iterator(Object::iterator it)
Construct iterator from underlying Object iterator.
iterator(const iterator &)=default
Copy constructor.
Document & reference
iterator reference type
iterator(iterator &&) noexcept=default
Move constructor.
MutableValuesView(Object *obj)
Construct MutableValuesView from object pointer.
iterator end()
Get iterator past last value.
iterator begin()
Get iterator to first value.
Value iterator (mutable).
iterator(const iterator &)=default
Copy constructor.
Document * pointer
iterator pointer type
std::ptrdiff_t difference_type
iterator difference type
Document & reference
iterator reference type
Document value_type
iterator value type
iterator(Object::iterator it)
Construct iterator from underlying Object iterator.
std::forward_iterator_tag iterator_category
iterator category (forward iterator)
iterator(iterator &&) noexcept=default
Move constructor.
PathView(const Document &doc, Format format=Format::JsonPointer, bool includeContainers=true)
Construct PathView for a document.
bool empty() const
Check if empty.
Format
Format for path string representation.
@ JsonPointer
RFC 6901 JSON Pointer format (e.g., "/user/addresses/0/city").
iterator begin() const
Get iterator to first entry.
iterator end() const
Get iterator past last entry.
const Entry & operator[](size_t index) const
Access entry by index.
std::vector< Entry > leaves() const
Get only leaf entries (primitives).
size_t size() const
Get number of path entries.
Represents a single path entry in the document.
bool isLeaf
True if value is a primitive (not object/array).
const Document & value() const
Get the value as a Document reference.
Entry & operator=(const Entry &other)
Copy assignment.
Entry()
Default constructor.
std::string path
Full path to this value.
Entry & operator=(Entry &&other) noexcept=default
Move assignment.
Entry(const Entry &other)
Copy constructor.
std::unique_ptr< Document > valuePtr
The value at this path (owned pointer).
size_t depth
Nesting depth (0 = root level).
Entry(Entry &&other) noexcept=default
Move constructor.
Forward iterator for path entries.
const Entry & reference
Reference to value type.
Entry value_type
Type of value yielded by iterator.
iterator & operator++()
Pre-increment operator.
std::ptrdiff_t difference_type
Type for iterator difference.
iterator operator++(int)
Post-increment operator.
pointer operator->() const
Arrow operator.
bool operator!=(const iterator &other) const
Inequality comparison.
iterator()
Default constructor.
std::forward_iterator_tag iterator_category
iterator category tag for STL compatibility
reference operator*() const
Dereference operator.
iterator(const std::vector< Entry > *entries, size_t index)
Construct iterator at position.
const Entry * pointer
Pointer to value type.
bool operator==(const iterator &other) const
Equality comparison.
Concept for all JSON-compatible value types.
Concept for JSON container types only.
Concept for types that can be checked with is<T>().