1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
# json4cpp — Iteration
## Iterator Types
The `basic_json` class provides a full set of iterators modeled after STL
container iterators. All are defined in
`include/nlohmann/detail/iterators/`:
| Type | Class | Header |
|---|---|---|
| `iterator` | `iter_impl<basic_json>` | `iter_impl.hpp` |
| `const_iterator` | `iter_impl<const basic_json>` | `iter_impl.hpp` |
| `reverse_iterator` | `json_reverse_iterator<iterator>` | `json_reverse_iterator.hpp` |
| `const_reverse_iterator` | `json_reverse_iterator<const_iterator>` | `json_reverse_iterator.hpp` |
## `iter_impl` Internals
The `iter_impl<BasicJsonType>` template is the core iterator
implementation. It wraps an `internal_iterator` struct:
```cpp
struct internal_iterator
{
typename BasicJsonType::object_t::iterator object_iterator;
typename BasicJsonType::array_t::iterator array_iterator;
primitive_iterator_t primitive_iterator;
};
```
Only one of these three fields is active at a time, determined by the
`m_object` pointer's `type()`:
- **Object**: uses `object_iterator` (delegates to the underlying map/ordered_map iterator)
- **Array**: uses `array_iterator` (delegates to `std::vector::iterator`)
- **Primitive** (null, boolean, number, string, binary): uses `primitive_iterator_t`
### `primitive_iterator_t`
Primitive types are treated as single-element containers. The
`primitive_iterator_t` is a wrapper around `std::ptrdiff_t`:
- Value `0` → points to the element (equivalent to `begin()`)
- Value `1` → past-the-end (equivalent to `end()`)
- Value `-1` → before-the-begin (sentinel, `end_value`)
```cpp
json j = 42;
for (auto it = j.begin(); it != j.end(); ++it) {
// executes exactly once, *it == 42
}
```
## Range Functions
### Forward Iteration
```cpp
iterator begin() noexcept;
const_iterator begin() const noexcept;
const_iterator cbegin() const noexcept;
iterator end() noexcept;
const_iterator end() const noexcept;
const_iterator cend() const noexcept;
```
```cpp
json j = {1, 2, 3};
for (auto it = j.begin(); it != j.end(); ++it) {
std::cout << *it << " ";
}
// Output: 1 2 3
```
### Reverse Iteration
```cpp
reverse_iterator rbegin() noexcept;
const_reverse_iterator rbegin() const noexcept;
const_reverse_iterator crbegin() const noexcept;
reverse_iterator rend() noexcept;
const_reverse_iterator rend() const noexcept;
const_reverse_iterator crend() const noexcept;
```
```cpp
json j = {1, 2, 3};
for (auto it = j.rbegin(); it != j.rend(); ++it) {
std::cout << *it << " ";
}
// Output: 3 2 1
```
## Range-Based For Loops
### Simple Iteration
```cpp
json j = {"alpha", "beta", "gamma"};
for (const auto& element : j) {
std::cout << element << "\n";
}
```
### Mutable Iteration
```cpp
json j = {1, 2, 3};
for (auto& element : j) {
element = element.get<int>() * 2;
}
// j is now [2, 4, 6]
```
### Object Iteration
When iterating over objects, each element is a **value** (not a key-value
pair). Use `it.key()` and `it.value()` on an explicit iterator, or use
`items()`:
```cpp
json j = {{"name", "alice"}, {"age", 30}};
// Method 1: explicit iterator
for (auto it = j.begin(); it != j.end(); ++it) {
std::cout << it.key() << ": " << it.value() << "\n";
}
// Method 2: range-for gives values only
for (const auto& val : j) {
// val is the value, key is not accessible
}
```
## `items()` — Key-Value Iteration
```cpp
iteration_proxy<iterator> items() noexcept;
iteration_proxy<const_iterator> items() const noexcept;
```
Returns an `iteration_proxy` that wraps the iterator to provide `key()`
and `value()` accessors in range-based for loops:
```cpp
json j = {{"name", "alice"}, {"age", 30}, {"active", true}};
for (auto& [key, value] : j.items()) {
std::cout << key << " = " << value << "\n";
}
```
### Array Keys
For arrays, `items()` synthesizes string keys from the index:
```cpp
json j = {"a", "b", "c"};
for (auto& [key, value] : j.items()) {
std::cout << key << ": " << value << "\n";
}
// Output:
// 0: "a"
// 1: "b"
// 2: "c"
```
### Primitive Keys
For primitives, the key is always `""` (empty string):
```cpp
json j = 42;
for (auto& [key, value] : j.items()) {
// key == "", value == 42
}
```
## `iteration_proxy` Implementation
Defined in `include/nlohmann/detail/iterators/iteration_proxy.hpp`:
```cpp
template<typename IteratorType>
class iteration_proxy
{
class iteration_proxy_value
{
IteratorType anchor; // underlying iterator
std::size_t array_index = 0; // cached index for arrays
mutable std::size_t array_index_last = 0;
mutable string_type array_index_str = "0";
const string_type& key() const;
typename IteratorType::reference value() const;
};
public:
iteration_proxy_value begin() const noexcept;
iteration_proxy_value end() const noexcept;
};
```
The `key()` method dispatches based on the value type:
- **Array**: converts `array_index` to string (`std::to_string`)
- **Object**: returns `anchor.key()`
- **Other**: returns empty string
## Structured Bindings
C++17 structured bindings work with `items()`:
```cpp
json j = {{"x", 1}, {"y", 2}};
for (const auto& [key, value] : j.items()) {
// key is const std::string&, value is const json&
}
```
This is enabled by specializations in `<nlohmann/detail/iterators/iteration_proxy.hpp>`:
```cpp
namespace std {
template<std::size_t N, typename IteratorType>
struct tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType>>;
template<typename IteratorType>
struct tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>>;
}
```
## Iterator Arithmetic (Arrays Only)
Array iterators support random access:
```cpp
json j = {10, 20, 30, 40, 50};
auto it = j.begin();
it += 2; // points to 30
auto it2 = it + 1; // points to 40
auto diff = it2 - it; // 1
j[it - j.begin()]; // equivalent of *it
```
Object iterators support only increment and decrement (bidirectional).
## `json_reverse_iterator`
Extends `std::reverse_iterator` with `key()` and `value()` methods:
```cpp
template<typename Base>
class json_reverse_iterator : public std::reverse_iterator<Base>
{
public:
// Inherited from std::reverse_iterator:
// operator*, operator->, operator++, operator--, operator+, operator- ...
// Added:
const typename Base::key_type& key() const;
typename Base::reference value() const;
};
```
```cpp
json j = {{"a", 1}, {"b", 2}, {"c", 3}};
for (auto it = j.rbegin(); it != j.rend(); ++it) {
std::cout << it.key() << ": " << it.value() << "\n";
}
// Output (reversed iteration order)
```
## Iterator Invalidation
Iterator invalidation follows the rules of the underlying containers:
| Operation | Object (`std::map`) | Array (`std::vector`) |
|---|---|---|
| `push_back()` | Not invalidated | May invalidate all |
| `insert()` | Not invalidated | Invalidates at/after pos |
| `erase()` | Only erased | At/after erased pos |
| `clear()` | All invalidated | All invalidated |
| `operator[]` (new key) | Not invalidated | May invalidate all |
For `ordered_json` (backed by `std::vector`), all iterators may be
invalidated on any insertion/erasure since the ordered_map inherits from
`std::vector`.
## Iterating Null Values
Null values behave as empty containers:
```cpp
json j; // null
for (const auto& el : j) {
// never executes
}
assert(j.begin() == j.end());
```
## Complete Example
```cpp
#include <nlohmann/json.hpp>
#include <iostream>
using json = nlohmann::json;
int main() {
json config = {
{"server", {
{"host", "localhost"},
{"port", 8080},
{"features", {"auth", "logging", "metrics"}}
}},
{"debug", false}
};
// Iterate top-level keys
for (auto& [key, value] : config.items()) {
std::cout << key << " [" << value.type_name() << "]\n";
}
// Iterate nested array
for (const auto& feature : config["server"]["features"]) {
std::cout << " feature: " << feature << "\n";
}
// Reverse iterate
auto& features = config["server"]["features"];
for (auto it = features.rbegin(); it != features.rend(); ++it) {
std::cout << " reverse: " << *it << "\n";
}
}
```
|