nfx-json 1.5.2
Modern C++20 JSON library with schema validation and generation
Loading...
Searching...
No Matches
Document.h
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2026 nfx
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
29
30#pragma once
31
32#include "Concepts.h"
33
34#include <algorithm>
35#include <compare>
36#include <cstdint>
37#include <memory>
38#include <optional>
39#include <span>
40#include <stdexcept>
41#include <string>
42#include <string_view>
43#include <variant>
44#include <vector>
45
46namespace nfx::json
47{
48 // Forward declarations
49 class Document;
50
54 enum class Type : uint8_t
55 {
56 Null,
57 Boolean,
58 Integer,
59 UnsignedInteger,
60 Double,
61 String,
62 Array,
63 Object
64 };
65
69 using Object = std::vector<std::pair<std::string, Document>>;
70
74 using Array = std::vector<Document>;
75
76 //----------------------------------------------
77 // Type trait specializations for Object and Array
78 //----------------------------------------------
79
83 template <>
84 struct is_json_container<Object> : std::true_type
85 {
86 };
87
91 template <>
92 struct is_json_container<Array> : std::true_type
93 {
94 };
95
96 //=====================================================================
97 // Document class
98 //=====================================================================
99
107 class Document final
108 {
109 public:
110 //----------------------------------------------
111 // Construction
112 //----------------------------------------------
113
115 inline Document();
116
121 inline Document( bool value ) noexcept;
122
127 inline Document( int value ) noexcept;
128
133 inline Document( unsigned int value ) noexcept;
134
139 inline Document( int64_t value ) noexcept;
140
145 inline Document( uint64_t value ) noexcept;
146
151 inline Document( double value ) noexcept;
152
157 inline explicit Document( std::string value );
158
163 inline explicit Document( std::string_view value );
164
169 inline explicit Document( const char* value );
170
176 template <size_t N>
177 inline Document( const char ( &value )[N] );
178
182 inline Document( std::nullptr_t ) noexcept;
183
188 inline explicit Document( Array value );
189
194 inline explicit Document( Object value );
195
200 template <typename T>
201 inline Document(
202 T value,
203 std::enable_if_t<
204 std::is_same_v<T, long> && !std::is_same_v<long, int> && !std::is_same_v<long, int64_t>,
205 int> = 0 ) noexcept;
206
212 template <typename T>
213 inline Document(
214 T value,
215 std::enable_if_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>,
218 int> = 0 ) noexcept;
219
224 template <typename T>
225 inline Document(
226 T value,
227 std::enable_if_t<std::is_same_v<T, long long> && !std::is_same_v<long long, int64_t>, int> = 0 ) noexcept;
228
234 template <typename T>
235 inline Document(
236 T value,
237 std::enable_if_t<
238 std::is_same_v<T, unsigned long long> && !std::is_same_v<unsigned long long, uint64_t>,
239 int> = 0 ) noexcept;
240
245 Document( const Document& other ) = default;
246
251#if defined( __GNUC__ ) && !defined( __clang__ )
252# pragma GCC diagnostic push
253# pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
254#endif
255 Document( Document&& other ) noexcept = default;
256#if defined( __GNUC__ ) && !defined( __clang__ )
257# pragma GCC diagnostic pop
258#endif
259
261 ~Document() = default;
262
263 //----------------------------------------------
264 // Assignment
265 //----------------------------------------------
266
272 Document& operator=( const Document& other ) = default;
273
279 Document& operator=( Document&& other ) noexcept = default;
280
286 inline Document& operator=( std::string value );
287
293 inline Document& operator=( bool value ) noexcept;
294
300 inline Document& operator=( int64_t value ) noexcept;
301
307 inline Document& operator=( uint64_t value ) noexcept;
308
314 inline Document& operator=( double value ) noexcept;
315
320 inline Document& operator=( std::nullptr_t ) noexcept;
321
322 //----------------------------------------------
323 // Comparison
324 //----------------------------------------------
325
331 inline bool operator==( const Document& other ) const noexcept;
332
339 std::strong_ordering operator<=>( const Document& other ) const noexcept;
340
341 //----------------------------------------------
342 // Static factory methods
343 //----------------------------------------------
344
349 [[nodiscard]] inline static Document object();
350
355 [[nodiscard]] inline static Document array();
356
357 //----------------------------------------------
358 // Type queries
359 //----------------------------------------------
360
365 [[nodiscard]] inline Type type() const noexcept;
366
367 //----------------------------------------------
368 // Root value access
369 //----------------------------------------------
370
380 template <Value T>
381 [[nodiscard]] inline std::optional<T> root() const;
382
391 template <Value T>
392 [[nodiscard]] inline bool root( T& out ) const;
393
403 template <Value T>
404 [[nodiscard]] inline std::optional<std::reference_wrapper<const T>> rootRef() const;
405
414 template <Value T>
415 [[nodiscard]] inline std::optional<std::reference_wrapper<T>> rootRef();
416
423 template <Checkable T>
424 [[nodiscard]] inline bool isRoot() const;
425
458 template <typename Visitor>
459 inline decltype( auto ) visit( Visitor&& visitor ) const;
460
468 template <typename Visitor>
469 inline decltype( auto ) visit( Visitor&& visitor );
470
471 //----------------------------------------------
472 // Object operations
473 //----------------------------------------------
474
480 [[nodiscard]] inline Document* find( std::string_view key ) noexcept;
481
487 [[nodiscard]] inline const Document* find( std::string_view key ) const noexcept;
488
494 inline void set( std::string key, Document value );
495
501 [[nodiscard]] inline bool contains( std::string_view key ) const noexcept;
502
510 [[nodiscard]] inline Document& at( std::string_view key );
511
519 [[nodiscard]] inline const Document& at( std::string_view key ) const;
520
528 [[nodiscard]] inline Document& at( size_t index );
529
537 [[nodiscard]] inline const Document& at( size_t index ) const;
538
551 inline Document& operator[]( std::string_view key );
552
558 [[nodiscard]] inline const Document& operator[]( std::string_view key ) const;
559
573 [[nodiscard]] inline Document& operator[]( size_t index );
574
580 [[nodiscard]] inline const Document& operator[]( size_t index ) const;
581
583 inline void clear();
584
590 inline size_t erase( std::string_view key );
591
597 inline size_t erase( const char* key );
598
604 inline size_t erase( const std::string& key );
605
612 template <typename iterator, typename = std::enable_if_t<!std::is_convertible_v<iterator, std::string_view>>>
613 inline iterator erase( iterator it );
614
622 template <typename iterator>
623 inline iterator insert( iterator pos, Document value );
624
625 //----------------------------------------------
626 // Array operations
627 //----------------------------------------------
628
633 [[nodiscard]] inline size_t size() const noexcept;
634
639 [[nodiscard]] inline bool empty() const noexcept;
640
645 inline void push_back( Document value );
646
653 inline void reserve( size_t capacity );
654
659 [[nodiscard]] inline size_t capacity() const noexcept;
660
666 [[nodiscard]] inline Document& front();
667
673 [[nodiscard]] inline const Document& front() const;
674
679 [[nodiscard]] inline Document& back();
680
685 [[nodiscard]] inline const Document& back() const;
686
691 [[nodiscard]] inline auto begin();
692
697 [[nodiscard]] inline auto end();
698
703 [[nodiscard]] inline auto begin() const;
704
709 [[nodiscard]] inline auto end() const;
710
711 //----------------------------------------------
712 // Document::ObjectIterator
713 //----------------------------------------------
714
717 {
718 public:
720 using iterator_category = std::forward_iterator_tag;
721
723 using value_type = std::pair<const std::string, Document>;
724
726 using difference_type = std::ptrdiff_t;
727
729 using pointer = const value_type*;
730
732 using reference = const value_type&;
733
735 using MapIterator = Object::const_iterator;
736
739
745
747 ObjectIterator( const ObjectIterator& ) = default;
748
750 ObjectIterator( ObjectIterator&& ) noexcept = default;
751
754 ObjectIterator& operator=( const ObjectIterator& ) = default;
755
758 ObjectIterator& operator=( ObjectIterator&& ) noexcept = default;
759
764 inline const std::string& key() const;
765
770 inline const Document& value() const;
771
776 inline Document& value();
777
782 inline const Document& operator*() const;
783
788 inline Document& operator*();
789
794 inline ObjectIterator& operator++();
795
801 inline bool operator!=( const ObjectIterator& other ) const;
802
808 inline bool operator==( const ObjectIterator& other ) const;
809
814 inline MapIterator base() const;
815 };
816
817 //----------------------------------------------
818 // Object iteration
819 //----------------------------------------------
820
825 [[nodiscard]] inline ObjectIterator objectBegin() const;
826
831 [[nodiscard]] inline ObjectIterator objectEnd() const;
832
833 //----------------------------------------------
834 // Document::KeysView class
835 //----------------------------------------------
836
839 {
840 const Object* m_obj;
841
842 public:
845 {
846 Object::const_iterator m_it;
847
848 public:
850 using iterator_category = std::forward_iterator_tag;
851
853 using value_type = std::string;
854
856 using difference_type = std::ptrdiff_t;
857
859 using pointer = const std::string*;
860
862 using reference = const std::string&;
863
868 inline iterator( Object::const_iterator it );
869
871 iterator( const iterator& ) = default;
872
874 iterator( iterator&& ) noexcept = default;
875
878 iterator& operator=( const iterator& ) = default;
879
882 iterator& operator=( iterator&& ) noexcept = default;
883
888 inline reference operator*() const;
889
894 inline pointer operator->() const;
895
900 inline iterator& operator++();
901
906 inline iterator operator++( int );
907
913 inline bool operator==( const iterator& other ) const;
914
920 inline bool operator!=( const iterator& other ) const;
921 };
922
927 inline KeysView( const Object* obj );
928
933 inline iterator begin() const;
934
939 inline iterator end() const;
940 };
941
944 {
945 const Object* m_obj;
946
947 public:
950 {
951 Object::const_iterator m_it;
952
953 public:
955 using iterator_category = std::forward_iterator_tag;
956
959
961 using difference_type = std::ptrdiff_t;
962
964 using pointer = const Document*;
965
967 using reference = const Document&;
968
973 inline const_iterator( Object::const_iterator it );
974
976 const_iterator( const const_iterator& ) = default;
977
979 const_iterator( const_iterator&& ) noexcept = default;
980
983 const_iterator& operator=( const const_iterator& ) = default;
984
987 const_iterator& operator=( const_iterator&& ) noexcept = default;
988
993 inline reference operator*() const;
994
999 inline pointer operator->() const;
1000
1005 inline const_iterator& operator++();
1006
1011 inline const_iterator operator++( int );
1012
1018 inline bool operator==( const const_iterator& other ) const;
1019
1025 inline bool operator!=( const const_iterator& other ) const;
1026 };
1027
1030 {
1031 Object::iterator m_it;
1032
1033 public:
1035 using iterator_category = std::forward_iterator_tag;
1036
1039
1041 using difference_type = std::ptrdiff_t;
1042
1045
1048
1053 inline iterator( Object::iterator it );
1054
1056 iterator( const iterator& ) = default;
1057
1059 iterator( iterator&& ) noexcept = default;
1060
1063 iterator& operator=( const iterator& ) = default;
1064
1067 iterator& operator=( iterator&& ) noexcept = default;
1068
1073 inline reference operator*() const;
1074
1079 inline pointer operator->() const;
1080
1085 inline iterator& operator++();
1086
1091 inline iterator operator++( int );
1092
1098 inline bool operator==( const iterator& other ) const;
1099
1105 inline bool operator!=( const iterator& other ) const;
1106 };
1107
1112 inline ValuesView( const Object* obj );
1113
1118 inline const_iterator begin() const;
1119
1124 inline const_iterator end() const;
1125 };
1126
1129 {
1130 Object* m_obj;
1131
1132 public:
1135 {
1136 Object::iterator m_it;
1137
1138 public:
1140 using iterator_category = std::forward_iterator_tag;
1144 using difference_type = std::ptrdiff_t;
1149
1154 inline iterator( Object::iterator it );
1155
1157 iterator( const iterator& ) = default;
1158
1160 iterator( iterator&& ) noexcept = default;
1161
1164 iterator& operator=( const iterator& ) = default;
1165
1168 iterator& operator=( iterator&& ) noexcept = default;
1169
1174 inline reference operator*() const;
1175
1180 inline pointer operator->() const;
1181
1186 inline iterator& operator++();
1187
1192 inline iterator operator++( int );
1193
1199 inline bool operator==( const iterator& other ) const;
1200
1206 inline bool operator!=( const iterator& other ) const;
1207 };
1208
1214
1219 inline iterator begin();
1220
1225 inline iterator end();
1226 };
1227
1233 [[nodiscard]] inline KeysView keys() const;
1234
1240 [[nodiscard]] inline ValuesView values() const;
1241
1247 [[nodiscard]] inline MutableValuesView values();
1248
1249 //----------------------------------------------
1250 // Serialization
1251 //----------------------------------------------
1252
1259 [[nodiscard]] std::string toString( int indent = 0, size_t bufferSize = 0 ) const;
1260
1265 [[nodiscard]] inline std::vector<uint8_t> toBytes() const;
1266
1267 //----------------------------------------------
1268 // High-level API with path support
1269 //----------------------------------------------
1270
1276 [[nodiscard]] static std::optional<Document> fromString( std::string_view jsonStr );
1277
1284 [[nodiscard]] static bool fromString( std::string_view jsonStr, Document& value );
1285
1291 [[nodiscard]] static std::optional<Document> fromBytes( const std::vector<uint8_t>& bytes );
1292
1299 [[nodiscard]] static bool fromBytes( const std::vector<uint8_t>& bytes, Document& value );
1300
1306 void merge( const Document& other, bool overwriteArrays = true );
1307
1314 void update( std::string_view path, const Document& value );
1315
1316 //----------------------------------------------
1317 // Template-based path operations
1318 //----------------------------------------------
1319
1327 template <Value T>
1328 [[nodiscard]] std::optional<T> get( std::string_view path ) const;
1329
1338 template <Value T>
1339 [[nodiscard]] bool get( std::string_view path, T& value ) const;
1340
1353 template <Value T>
1354 [[nodiscard]] std::optional<std::reference_wrapper<const T>> getRef( std::string_view path ) const;
1355
1366 template <Value T>
1367 [[nodiscard]] std::optional<std::reference_wrapper<T>> getRef( std::string_view path );
1368
1376 template <Value T>
1377 void set( std::string_view path, const T& value );
1378
1386 template <Value T>
1387 void set( std::string_view path, T&& value );
1388
1389 //-----------------------------
1390 // Type-only creation
1391 //-----------------------------
1392
1399 template <Container T>
1400 void set( std::string_view path );
1401
1407 void setNull( std::string_view path );
1408
1416 template <Checkable T>
1417 [[nodiscard]] bool is( std::string_view path ) const;
1418
1425 [[nodiscard]] bool isNull( std::string_view path ) const;
1426
1431 [[nodiscard]] bool isValid() const;
1432
1433 //----------------------------------------------
1434 // Document::PathView class
1435 //----------------------------------------------
1436
1449 class PathView final
1450 {
1451 public:
1452 //-----------------------------
1453 // Forward declaration
1454 //-----------------------------
1455
1456 class iterator;
1457
1458 //-----------------------------
1459 // Format
1460 //-----------------------------
1461
1465 enum class Format : bool
1466 {
1467 JsonPointer = 0,
1468 DotNotation
1469 };
1470
1471 //-----------------------------
1472 // Path entry structure
1473 //-----------------------------
1474
1478 struct Entry
1479 {
1480 std::string path;
1481 std::unique_ptr<Document> valuePtr;
1482 size_t depth;
1483 bool isLeaf;
1484
1486 inline Entry();
1487
1492 Entry( Entry&& other ) noexcept = default;
1493
1499 Entry& operator=( Entry&& other ) noexcept = default;
1500
1505 Entry( const Entry& other );
1506
1512 Entry& operator=( const Entry& other );
1513
1518 inline const Document& value() const;
1519 };
1520
1521 //-----------------------------
1522 // Construction
1523 //-----------------------------
1524
1538 explicit PathView(
1539 const Document& doc, Format format = Format::JsonPointer, bool includeContainers = true );
1540
1541 //-----------------------------
1542 // Range interface
1543 //-----------------------------
1544
1549 inline iterator begin() const;
1550
1555 inline iterator end() const;
1556
1557 //-----------------------------
1558 // Direct access
1559 //-----------------------------
1560
1565 inline size_t size() const;
1566
1571 inline bool empty() const;
1572
1578 inline const Entry& operator[]( size_t index ) const;
1579
1580 //-----------------------------
1581 // Filtering
1582 //-----------------------------
1583
1588 std::vector<Entry> leaves() const;
1589
1590 //-----------------------------
1591 // Document::PathView::iterator class
1592 //-----------------------------
1593
1598 {
1599 public:
1601 using iterator_category = std::forward_iterator_tag;
1602
1605
1607 using difference_type = std::ptrdiff_t;
1608
1610 using pointer = const Entry*;
1611
1613 using reference = const Entry&;
1614
1616 inline iterator();
1617
1623 inline iterator( const std::vector<Entry>* entries, size_t index );
1624
1629 inline reference operator*() const;
1630
1635 inline pointer operator->() const;
1636
1642
1647 inline iterator operator++( int );
1648
1654 inline bool operator==( const iterator& other ) const;
1655
1661 inline bool operator!=( const iterator& other ) const;
1662
1663 private:
1664 const std::vector<Entry>* m_entries;
1665 size_t m_index;
1666 };
1667
1668 private:
1669 //-----------------------------
1670 // Private helper methods
1671 //-----------------------------
1672
1677 void buildEntries( const Document& doc );
1678
1684 std::string formatPath( const std::vector<std::string>& segments ) const;
1685
1686 //----------------------------------------------
1687 // Private data members
1688 //----------------------------------------------
1689
1690 Format m_format;
1691 bool m_includeContainers;
1692 std::vector<Entry> m_entries;
1693 };
1694
1695 private:
1696 //----------------------------------------------
1697 // Private internal access
1698 //----------------------------------------------
1699
1706 template <typename T>
1707 const T& rootInternal() const;
1708
1715 template <typename T>
1716 T& rootInternal();
1717
1718 //----------------------------------------------
1719 // Private path helpers
1720 //----------------------------------------------
1721
1728 bool containsPath( std::string_view path ) const;
1729
1736 size_t erasePath( std::string_view path );
1737
1738 //----------------------------------------------
1739 // Private navigation methods
1740 //----------------------------------------------
1741
1749 Document* navigateToPath( std::string_view path, bool createPath = false );
1750
1757 const Document* navigateToPath( std::string_view path ) const;
1758
1765 Document* navigateToJsonPointer( std::string_view pointer, bool createPath = false );
1766
1772 const Document* navigateToJsonPointer( std::string_view pointer ) const;
1773
1777 static std::string unescapeJsonPointerToken( std::string_view token ) noexcept;
1778
1782 static bool isValidArrayIndex( std::string_view token ) noexcept;
1783
1784 //----------------------------------------------
1785 // Private members
1786 //----------------------------------------------
1787
1788#if defined( __GNUC__ ) && !defined( __clang__ )
1789# pragma GCC diagnostic push
1790# pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
1791#endif
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
1795#endif
1796 };
1797} // namespace nfx::json
1798
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).
Definition Document.h:69
std::vector< Document > Array
Array for JSON arrays.
Definition Document.h:74
Type
JSON value types.
Definition Document.h:55
Type trait to identify JSON container types (Object, Array).
Definition Concepts.h:57
Low-level JSON value storage type.
Definition Document.h:108
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().
Definition Document.h:717
std::ptrdiff_t difference_type
iterator difference type
Definition Document.h:726
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)
Definition Document.h:720
std::pair< const std::string, Document > value_type
iterator value type
Definition Document.h:723
MapIterator it
The underlying iterator.
Definition Document.h:738
const value_type & reference
iterator reference type
Definition Document.h:732
const value_type * pointer
iterator pointer type
Definition Document.h:729
ObjectIterator(MapIterator iter)
Construct from underlying iterator.
ObjectIterator(ObjectIterator &&) noexcept=default
Move constructor.
Object::const_iterator MapIterator
Underlying iterator type for Object.
Definition Document.h:735
Range adapter for object keys.
Definition Document.h:839
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
Definition Document.h:853
const std::string & reference
iterator reference type
Definition Document.h:862
std::forward_iterator_tag iterator_category
iterator category (forward iterator)
Definition Document.h:850
std::ptrdiff_t difference_type
iterator difference type
Definition Document.h:856
const std::string * pointer
iterator pointer type
Definition Document.h:859
Range adapter for object values.
Definition Document.h:944
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
Definition Document.h:967
std::forward_iterator_tag iterator_category
iterator category (forward iterator)
Definition Document.h:955
const_iterator(Object::const_iterator it)
Construct const_iterator from underlying Object iterator.
Document value_type
iterator value type
Definition Document.h:958
const_iterator(const const_iterator &)=default
Copy constructor.
const_iterator(const_iterator &&) noexcept=default
Move constructor.
const Document * pointer
iterator pointer type
Definition Document.h:964
std::ptrdiff_t difference_type
iterator difference type
Definition Document.h:961
std::forward_iterator_tag iterator_category
iterator category (forward iterator)
Definition Document.h:1035
std::ptrdiff_t difference_type
iterator difference type
Definition Document.h:1041
Document value_type
iterator value type
Definition Document.h:1038
Document * pointer
iterator pointer type
Definition Document.h:1044
iterator(Object::iterator it)
Construct iterator from underlying Object iterator.
iterator(const iterator &)=default
Copy constructor.
Document & reference
iterator reference type
Definition Document.h:1047
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.
iterator(const iterator &)=default
Copy constructor.
Document * pointer
iterator pointer type
Definition Document.h:1146
std::ptrdiff_t difference_type
iterator difference type
Definition Document.h:1144
Document & reference
iterator reference type
Definition Document.h:1148
iterator(Object::iterator it)
Construct iterator from underlying Object iterator.
std::forward_iterator_tag iterator_category
iterator category (forward iterator)
Definition Document.h:1140
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.
Definition Document.h:1466
@ JsonPointer
RFC 6901 JSON Pointer format (e.g., "/user/addresses/0/city").
Definition Document.h:1467
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.
Definition Document.h:1479
bool isLeaf
True if value is a primitive (not object/array).
Definition Document.h:1483
const Document & value() const
Get the value as a Document reference.
Entry & operator=(const Entry &other)
Copy assignment.
std::string path
Full path to this value.
Definition Document.h:1480
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).
Definition Document.h:1481
size_t depth
Nesting depth (0 = root level).
Definition Document.h:1482
Entry(Entry &&other) noexcept=default
Move constructor.
Forward iterator for path entries.
Definition Document.h:1598
const Entry & reference
Reference to value type.
Definition Document.h:1613
Entry value_type
Type of value yielded by iterator.
Definition Document.h:1604
iterator & operator++()
Pre-increment operator.
std::ptrdiff_t difference_type
Type for iterator difference.
Definition Document.h:1607
iterator operator++(int)
Post-increment operator.
pointer operator->() const
Arrow operator.
bool operator!=(const iterator &other) const
Inequality comparison.
std::forward_iterator_tag iterator_category
iterator category tag for STL compatibility
Definition Document.h:1601
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.
Definition Document.h:1610
bool operator==(const iterator &other) const
Equality comparison.
Concept for all JSON-compatible value types.
Definition Concepts.h:94
Concept for JSON container types only.
Definition Concepts.h:101
Concept for types that can be checked with is<T>().
Definition Concepts.h:108