14#include <nlohmann/detail/exceptions.hpp>
15#include <nlohmann/detail/iterators/internal_iterator.hpp>
16#include <nlohmann/detail/iterators/primitive_iterator.hpp>
17#include <nlohmann/detail/macro_scope.hpp>
18#include <nlohmann/detail/meta/cpp_future.hpp>
19#include <nlohmann/detail/meta/type_traits.hpp>
20#include <nlohmann/detail/value_t.hpp>
22NLOHMANN_JSON_NAMESPACE_BEGIN
46template<
typename BasicJsonType>
50 using other_iter_impl = iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value,
typename std::remove_const<BasicJsonType>::type,
const BasicJsonType>::type>;
52 friend other_iter_impl;
57 using object_t =
typename BasicJsonType::object_t;
58 using array_t =
typename BasicJsonType::array_t;
61 "iter_impl only accepts (const) basic_json");
63 static_assert(std::is_base_of<std::bidirectional_iterator_tag, std::bidirectional_iterator_tag>::value
64 && std::is_base_of<std::bidirectional_iterator_tag, typename std::iterator_traits<typename array_t::iterator>::iterator_category>
::value,
65 "basic_json iterator assumes array and object type iterators satisfy the LegacyBidirectionalIterator named requirement.");
80 using pointer =
typename std::conditional<std::is_const<BasicJsonType>::value,
81 typename BasicJsonType::const_pointer,
82 typename BasicJsonType::pointer>::type;
85 typename std::conditional<std::is_const<BasicJsonType>::value,
86 typename BasicJsonType::const_reference,
87 typename BasicJsonType::reference>::type;
89 iter_impl() =
default;
90 ~iter_impl() =
default;
91 iter_impl(iter_impl&&) noexcept = default;
92 iter_impl& operator=(iter_impl&&) noexcept = default;
100 explicit iter_impl(
pointer object) noexcept : m_object(
object)
102 JSON_ASSERT(m_object !=
nullptr);
104 switch (m_object->m_data.m_type)
108 m_it.object_iterator =
typename object_t::iterator();
114 m_it.array_iterator =
typename array_t::iterator();
150 iter_impl(
const iter_impl<const BasicJsonType>& other) noexcept
151 : m_object(other.m_object),
m_it(other.m_it)
160 iter_impl&
operator=(
const iter_impl<const BasicJsonType>& other)
noexcept
164 m_object = other.m_object;
175 iter_impl(
const iter_impl<
typename std::remove_const<BasicJsonType>::type>& other) noexcept
176 : m_object(other.m_object),
m_it(other.m_it)
185 iter_impl&
operator=(
const iter_impl<
typename std::remove_const<BasicJsonType>::type>& other)
noexcept
187 m_object = other.m_object;
192 JSON_PRIVATE_UNLESS_TESTED:
197 void set_begin() noexcept
199 JSON_ASSERT(m_object !=
nullptr);
201 switch (m_object->m_data.m_type)
205 m_it.object_iterator = m_object->m_data.m_value.object->begin();
211 m_it.array_iterator = m_object->m_data.m_value.array->begin();
218 m_it.primitive_iterator.set_end();
231 m_it.primitive_iterator.set_begin();
243 JSON_ASSERT(m_object !=
nullptr);
245 switch (m_object->m_data.m_type)
249 m_it.object_iterator = m_object->m_data.m_value.object->end();
255 m_it.array_iterator = m_object->m_data.m_value.array->end();
269 m_it.primitive_iterator.set_end();
282 JSON_ASSERT(m_object !=
nullptr);
284 switch (m_object->m_data.m_type)
288 JSON_ASSERT(
m_it.object_iterator != m_object->m_data.m_value.object->end());
289 return m_it.object_iterator->second;
294 JSON_ASSERT(
m_it.array_iterator != m_object->m_data.m_value.array->end());
295 return *
m_it.array_iterator;
299 JSON_THROW(invalid_iterator::create(214,
"cannot get value", m_object));
310 if (JSON_HEDLEY_LIKELY(
m_it.primitive_iterator.is_begin()))
315 JSON_THROW(invalid_iterator::create(214,
"cannot get value", m_object));
326 JSON_ASSERT(m_object !=
nullptr);
328 switch (m_object->m_data.m_type)
332 JSON_ASSERT(
m_it.object_iterator != m_object->m_data.m_value.object->end());
333 return &(
m_it.object_iterator->second);
338 JSON_ASSERT(
m_it.array_iterator != m_object->m_data.m_value.array->end());
339 return &*
m_it.array_iterator;
352 if (JSON_HEDLEY_LIKELY(
m_it.primitive_iterator.is_begin()))
357 JSON_THROW(invalid_iterator::create(214,
"cannot get value", m_object));
379 JSON_ASSERT(m_object !=
nullptr);
381 switch (m_object->m_data.m_type)
385 std::advance(
m_it.object_iterator, 1);
391 std::advance(
m_it.array_iterator, 1);
405 ++
m_it.primitive_iterator;
430 JSON_ASSERT(m_object !=
nullptr);
432 switch (m_object->m_data.m_type)
436 std::advance(
m_it.object_iterator, -1);
442 std::advance(
m_it.array_iterator, -1);
456 --
m_it.primitive_iterator;
468 template <
typename IterImpl, detail::enable_if_t < (std::is_same<IterImpl, iter_impl>::value || std::is_same<IterImpl, other_iter_impl>::value), std::
nullptr_t > =
nullptr >
472 if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
474 JSON_THROW(invalid_iterator::create(212,
"cannot compare iterators of different containers", m_object));
478 if (m_object ==
nullptr)
483 switch (m_object->m_data.m_type)
486 return (
m_it.object_iterator == other.m_it.object_iterator);
489 return (
m_it.array_iterator == other.m_it.array_iterator);
500 return (
m_it.primitive_iterator == other.m_it.primitive_iterator);
508 template <
typename IterImpl, detail::enable_if_t < (std::is_same<IterImpl, iter_impl>::value || std::is_same<IterImpl, other_iter_impl>::value), std::
nullptr_t > =
nullptr >
521 if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
523 JSON_THROW(invalid_iterator::create(212,
"cannot compare iterators of different containers", m_object));
527 if (m_object ==
nullptr)
533 switch (m_object->m_data.m_type)
536 JSON_THROW(invalid_iterator::create(213,
"cannot compare order of object iterators", m_object));
539 return (
m_it.array_iterator < other.
m_it.array_iterator);
550 return (
m_it.primitive_iterator < other.
m_it.primitive_iterator);
560 return !other.operator < (*this);
587 JSON_ASSERT(m_object !=
nullptr);
589 switch (m_object->m_data.m_type)
592 JSON_THROW(invalid_iterator::create(209,
"cannot use offsets with object iterators", m_object));
596 std::advance(
m_it.array_iterator, i);
610 m_it.primitive_iterator += i;
666 JSON_ASSERT(m_object !=
nullptr);
668 switch (m_object->m_data.m_type)
671 JSON_THROW(invalid_iterator::create(209,
"cannot use offsets with object iterators", m_object));
674 return m_it.array_iterator - other.
m_it.array_iterator;
685 return m_it.primitive_iterator - other.
m_it.primitive_iterator;
695 JSON_ASSERT(m_object !=
nullptr);
697 switch (m_object->m_data.m_type)
700 JSON_THROW(invalid_iterator::create(208,
"cannot use operator[] for object iterators", m_object));
703 return *std::next(
m_it.array_iterator, n);
706 JSON_THROW(invalid_iterator::create(214,
"cannot get value", m_object));
717 if (JSON_HEDLEY_LIKELY(
m_it.primitive_iterator.get_value() == -n))
722 JSON_THROW(invalid_iterator::create(214,
"cannot get value", m_object));
731 const typename object_t::key_type&
key()
const
733 JSON_ASSERT(m_object !=
nullptr);
735 if (JSON_HEDLEY_LIKELY(m_object->is_object()))
737 return m_it.object_iterator->first;
740 JSON_THROW(invalid_iterator::create(207,
"cannot use key() for non-object iterators", m_object));
752 JSON_PRIVATE_UNLESS_TESTED:
760NLOHMANN_JSON_NAMESPACE_END
bool operator>(const iter_impl &other) const
comparison: greater than
定义 iter_impl.hpp:567
bool operator>=(const iter_impl &other) const
comparison: greater than or equal
定义 iter_impl.hpp:576
std::bidirectional_iterator_tag iterator_category
定义 iter_impl.hpp:73
bool operator==(const IterImpl &other) const
comparison: equal
定义 iter_impl.hpp:469
typename BasicJsonType::value_type value_type
定义 iter_impl.hpp:76
iter_impl operator++(int) &
post-increment (it++)
定义 iter_impl.hpp:366
iter_impl & operator=(const iter_impl< typename std::remove_const< BasicJsonType >::type > &other) noexcept
converting assignment
定义 iter_impl.hpp:185
iter_impl(const iter_impl< const BasicJsonType > &other) noexcept
const copy constructor
定义 iter_impl.hpp:150
iter_impl operator--(int) &
post-decrement (it–)
定义 iter_impl.hpp:417
internal_iterator< typename std::remove_const< BasicJsonType >::type > m_it
定义 iter_impl.hpp:756
reference operator[](difference_type n) const
access to successor
定义 iter_impl.hpp:693
const object_t::key_type & key() const
return the key of an object iterator
定义 iter_impl.hpp:731
iter_impl & operator--()
pre-decrement (–it)
定义 iter_impl.hpp:428
iter_impl & operator+=(difference_type i)
add to iterator
定义 iter_impl.hpp:585
iter_impl operator-(difference_type i) const
subtract from iterator
定义 iter_impl.hpp:653
friend iter_impl operator+(difference_type i, const iter_impl &it)
addition of distance and iterator
定义 iter_impl.hpp:642
void set_end() noexcept
set the iterator past the last value
定义 iter_impl.hpp:241
reference value() const
return the value of an iterator
定义 iter_impl.hpp:747
iter_impl & operator=(const iter_impl< const BasicJsonType > &other) noexcept
converting assignment
定义 iter_impl.hpp:160
bool operator!=(const IterImpl &other) const
comparison: not equal
定义 iter_impl.hpp:509
typename std::conditional< std::is_const< BasicJsonType >::value, typename BasicJsonType::const_reference, typename BasicJsonType::reference >::type reference
定义 iter_impl.hpp:84
reference operator*() const
return a reference to the value pointed to by the iterator
定义 iter_impl.hpp:280
typename BasicJsonType::difference_type difference_type
定义 iter_impl.hpp:78
bool operator<(const iter_impl &other) const
comparison: smaller
定义 iter_impl.hpp:518
iter_impl operator+(difference_type i) const
add to iterator
定义 iter_impl.hpp:631
iter_impl & operator++()
pre-increment (++it)
定义 iter_impl.hpp:377
typename std::conditional< std::is_const< BasicJsonType >::value, typename BasicJsonType::const_pointer, typename BasicJsonType::pointer >::type pointer
定义 iter_impl.hpp:80
iter_impl & operator-=(difference_type i)
subtract from iterator
定义 iter_impl.hpp:622
pointer operator->() const
dereference the iterator
定义 iter_impl.hpp:324
iter_impl(const iter_impl< typename std::remove_const< BasicJsonType >::type > &other) noexcept
converting constructor
定义 iter_impl.hpp:175
difference_type operator-(const iter_impl &other) const
return difference
定义 iter_impl.hpp:664
bool operator<=(const iter_impl &other) const
comparison: less than or equal
定义 iter_impl.hpp:558
定义 iteration_proxy.hpp:30
proxy class for the items() function
定义 iteration_proxy.hpp:151
定义 primitive_iterator.hpp:30
detail namespace with internal helper functions
定义 from_json.hpp:43
@ null
null value
定义 value_t.hpp:55
@ number_integer
number value (signed integer)
定义 value_t.hpp:60
@ boolean
boolean value
定义 value_t.hpp:59
@ discarded
discarded by the parser callback function
定义 value_t.hpp:64
@ binary
binary array (ordered collection of bytes)
定义 value_t.hpp:63
@ object
object (unordered set of name/value pairs)
定义 value_t.hpp:56
@ string
string value
定义 value_t.hpp:58
@ number_float
number value (floating-point)
定义 value_t.hpp:62
@ number_unsigned
number value (unsigned integer)
定义 value_t.hpp:61
@ array
array (ordered collection of values)
定义 value_t.hpp:57
an iterator value
定义 internal_iterator.hpp:25