123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- #pragma once
- #include <cstddef> // size_t
- #include <iterator> // input_iterator_tag
- #include <string> // string, to_string
- #include <tuple> // tuple_size, get, tuple_element
- #include <utility> // move
- #if JSON_HAS_RANGES
- #include <ranges> // enable_borrowed_range
- #endif
- #include <nlohmann/detail/abi_macros.hpp>
- #include <nlohmann/detail/meta/type_traits.hpp>
- #include <nlohmann/detail/value_t.hpp>
- NLOHMANN_JSON_NAMESPACE_BEGIN
- namespace detail
- {
- template<typename string_type>
- void int_to_string( string_type& target, std::size_t value )
- {
-
- using std::to_string;
- target = to_string(value);
- }
- template<typename IteratorType> class iteration_proxy_value
- {
- public:
- using difference_type = std::ptrdiff_t;
- using value_type = iteration_proxy_value;
- using pointer = value_type *;
- using reference = value_type &;
- using iterator_category = std::input_iterator_tag;
- using string_type = typename std::remove_cv< typename std::remove_reference<decltype( std::declval<IteratorType>().key() ) >::type >::type;
- private:
-
- IteratorType anchor{};
-
- std::size_t array_index = 0;
-
- mutable std::size_t array_index_last = 0;
-
- mutable string_type array_index_str = "0";
-
- string_type empty_str{};
- public:
- explicit iteration_proxy_value() = default;
- explicit iteration_proxy_value(IteratorType it, std::size_t array_index_ = 0)
- noexcept(std::is_nothrow_move_constructible<IteratorType>::value
- && std::is_nothrow_default_constructible<string_type>::value)
- : anchor(std::move(it))
- , array_index(array_index_)
- {}
- iteration_proxy_value(iteration_proxy_value const&) = default;
- iteration_proxy_value& operator=(iteration_proxy_value const&) = default;
-
- iteration_proxy_value(iteration_proxy_value&&)
- noexcept(std::is_nothrow_move_constructible<IteratorType>::value
- && std::is_nothrow_move_constructible<string_type>::value) = default;
- iteration_proxy_value& operator=(iteration_proxy_value&&)
- noexcept(std::is_nothrow_move_assignable<IteratorType>::value
- && std::is_nothrow_move_assignable<string_type>::value) = default;
- ~iteration_proxy_value() = default;
-
- const iteration_proxy_value& operator*() const
- {
- return *this;
- }
-
- iteration_proxy_value& operator++()
- {
- ++anchor;
- ++array_index;
- return *this;
- }
- iteration_proxy_value operator++(int)&
- {
- auto tmp = iteration_proxy_value(anchor, array_index);
- ++anchor;
- ++array_index;
- return tmp;
- }
-
- bool operator==(const iteration_proxy_value& o) const
- {
- return anchor == o.anchor;
- }
-
- bool operator!=(const iteration_proxy_value& o) const
- {
- return anchor != o.anchor;
- }
-
- const string_type& key() const
- {
- JSON_ASSERT(anchor.m_object != nullptr);
- switch (anchor.m_object->type())
- {
-
- case value_t::array:
- {
- if (array_index != array_index_last)
- {
- int_to_string( array_index_str, array_index );
- array_index_last = array_index;
- }
- return array_index_str;
- }
-
- case value_t::object:
- return anchor.key();
-
- case value_t::null:
- case value_t::string:
- case value_t::boolean:
- case value_t::number_integer:
- case value_t::number_unsigned:
- case value_t::number_float:
- case value_t::binary:
- case value_t::discarded:
- default:
- return empty_str;
- }
- }
-
- typename IteratorType::reference value() const
- {
- return anchor.value();
- }
- };
- template<typename IteratorType> class iteration_proxy
- {
- private:
-
- typename IteratorType::pointer container = nullptr;
- public:
- explicit iteration_proxy() = default;
-
- explicit iteration_proxy(typename IteratorType::reference cont) noexcept
- : container(&cont) {}
- iteration_proxy(iteration_proxy const&) = default;
- iteration_proxy& operator=(iteration_proxy const&) = default;
- iteration_proxy(iteration_proxy&&) noexcept = default;
- iteration_proxy& operator=(iteration_proxy&&) noexcept = default;
- ~iteration_proxy() = default;
-
- iteration_proxy_value<IteratorType> begin() const noexcept
- {
- return iteration_proxy_value<IteratorType>(container->begin());
- }
-
- iteration_proxy_value<IteratorType> end() const noexcept
- {
- return iteration_proxy_value<IteratorType>(container->end());
- }
- };
- template<std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
- auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
- {
- return i.key();
- }
- template<std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
- auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
- {
- return i.value();
- }
- }
- NLOHMANN_JSON_NAMESPACE_END
- namespace std
- {
- #if defined(__clang__)
-
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wmismatched-tags"
- #endif
- template<typename IteratorType>
- class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>>
- : public std::integral_constant<std::size_t, 2> {};
- template<std::size_t N, typename IteratorType>
- class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >>
- {
- public:
- using type = decltype(
- get<N>(std::declval <
- ::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
- };
- #if defined(__clang__)
- #pragma clang diagnostic pop
- #endif
- }
- #if JSON_HAS_RANGES
- template <typename IteratorType>
- inline constexpr bool ::std::ranges::enable_borrowed_range<::nlohmann::detail::iteration_proxy<IteratorType>> = true;
- #endif
|