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
|
### jsoncons::staj_object_iterator
```cpp
#include <jsoncons/staj_iterator.hpp>
template<
typename Key,
typename T,
typename CharT=char
> class staj_object_iterator
```
A `staj_object_iterator` is an [InputIterator](https://en.cppreference.com/w/cpp/named_req/InputIterator) that
accesses the individual stream events from a [staj_cursor](staj_cursor.md) and, provided that when it is constructed
the current stream event has type `begin_object`, it returns the elements
of the JSON object as items of type `std::pair<string_type,T>`. If when it is constructed the current stream event
does not have type `begin_object`, it becomes equal to the default-constructed iterator.
#### Member types
Member type |Definition
------------------------------------|------------------------------
`char_type`|`CharT`
`key_type`|`Key`
`value_type`|`std::pair<Key,T>`
`difference_type`|`std::ptrdiff_t`
`pointer`|`value_type*`
`reference`|`value_type&`
`iterator_category`|[std::input_iterator_tag](https://en.cppreference.com/w/cpp/iterator/iterator_tags)
#### Constructors
staj_object_iterator() noexcept; (1)
staj_object_iterator(basic_staj_cursor<char_type>& cursor); (2)
staj_object_iterator(basic_staj_cursor<char_type>& cursor,
std::error_code& ec); (3)
staj_object_iterator(const staj_object_iterator& iter); (4)
(1) Constructs the end iterator
(2) Constructs a `staj_object_iterator` that refers to the first member of the object
following the current stream event `begin_object`. If there is no such member,
returns the end iterator. If a parsing error is encountered, throws a
[ser_error](ser_error.md).
(3) Constructs a `staj_object_iterator` that refers to the first member of the object
following the current stream event `begin_object`. If there is no such member,
returns the end iterator. If a parsing error is encountered, returns the end iterator
and sets `ec`.
(4) Copy constructor
#### Member functions
const key_value_type& operator*() const
const key_value_type* operator->() const
staj_object_iterator& operator++();
staj_object_iterator operator++(int);
staj_object_iterator& increment(std::error_code& ec);
Advances the iterator to the next object member.
#### Non-member functions
template <typename Key, typename T, typename CharT>
staj_object_iterator<Key, T, CharT> begin(staj_object_iterator<Key, T, CharT> iter); (1)
template <typename T, typename CharT>
staj_object_iterator<Key, T, CharT> end(staj_object_iterator<Key, T, CharT>) noexcept; (2)
template <typename Key,typename T,typename CharT>
bool operator==(const staj_object_iterator<Key, T, CharT>& a,
const staj_object_iterator<Key, T, CharT>& b) (3)
template <typename Key,typename T,typename CharT>
bool operator!=(const staj_object_iterator<Key, T, CharT>& a, (4)
const staj_object_iterator<Key, T, CharT>& b)
(1)-(2) For range-based for loop support.
(3)-(4) As required by LegacyInputIterator
### Examples
#### Iterate over a JSON object, returning key-json value pairs
```cpp
const std::string example = R"(
{
"application": "hiking",
"reputons": [
{
"rater": "HikingAsylum.array_example.com",
"assertion": "advanced",
"rated": "Marilyn C",
"rating": 0.90
}
]
}
)";
int main()
{
json_string_cursor cursor(example);
auto iter = staj_object_iterator<std::string,json>(cursor);
for (const auto& kv : iter)
{
std::cout << kv.first << ":\n" << pretty_print(kv.second) << "\n";
}
std::cout << "\n\n";
}
```
Output:
```
application:
"hiking"
reputons:
[
{
"assertion": "advanced",
"rated": "Marilyn C",
"rater": "HikingAsylum.array_example.com",
"rating": 0.90
}
]
```
|