Merge pull request #1765 from crazyjul/fix/items-with-alt-string

Allow items() to be used with custom string
This commit is contained in:
Niels Lohmann 2019-10-05 22:11:14 +02:00 committed by GitHub
commit d187488e0d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 8 deletions

View file

@ -12,6 +12,11 @@ namespace nlohmann
{
namespace detail
{
template<typename string_type>
void int_to_string( string_type& target, int value )
{
target = std::to_string(value);
}
template <typename IteratorType> class iteration_proxy_value
{
public:
@ -20,6 +25,7 @@ template <typename IteratorType> class 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:
/// the iterator
@ -29,9 +35,9 @@ template <typename IteratorType> class iteration_proxy_value
/// last stringified array index
mutable std::size_t array_index_last = 0;
/// a string representation of the array index
mutable std::string array_index_str = "0";
mutable string_type array_index_str = "0";
/// an empty string (to return a reference for primitive values)
const std::string empty_str = "";
const string_type empty_str = "";
public:
explicit iteration_proxy_value(IteratorType it) noexcept : anchor(it) {}
@ -64,7 +70,7 @@ template <typename IteratorType> class iteration_proxy_value
}
/// return key of the iterator
const std::string& key() const
const string_type& key() const
{
assert(anchor.m_object != nullptr);
@ -75,7 +81,7 @@ template <typename IteratorType> class iteration_proxy_value
{
if (array_index != array_index_last)
{
array_index_str = std::to_string(array_index);
int_to_string( array_index_str, array_index );
array_index_last = array_index;
}
return array_index_str;