Skip to content

Commit

Permalink
Merge pull request #1765 from crazyjul/fix/items-with-alt-string
Browse files Browse the repository at this point in the history
Allow items() to be used with custom string
  • Loading branch information
nlohmann committed Oct 5, 2019
2 parents dae0fe7 + 4615f5a commit d187488
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
14 changes: 10 additions & 4 deletions include/nlohmann/detail/iterators/iteration_proxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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) {}
Expand Down Expand Up @@ -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);

Expand All @@ -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;
Expand Down
14 changes: 10 additions & 4 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3186,6 +3186,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:
Expand All @@ -3194,6 +3199,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
Expand All @@ -3203,9 +3209,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) {}
Expand Down Expand Up @@ -3238,7 +3244,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);

Expand All @@ -3249,7 +3255,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;
Expand Down
34 changes: 34 additions & 0 deletions test/src/unit-alt-string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ class alt_string
friend bool ::operator<(const char*, const alt_string&);
};

void int_to_string( alt_string& target, int value )
{
target = std::to_string(value).c_str();
}

using alt_json = nlohmann::basic_json <
std::map,
std::vector,
Expand Down Expand Up @@ -232,6 +237,35 @@ TEST_CASE("alternative string type")
CHECK(dump == R"({"foo":"bar"})");
}

SECTION("items")
{
auto doc = alt_json::parse("{\"foo\": \"bar\"}");

for ( auto item : doc.items() )
{
CHECK( item.key() == "foo" );
CHECK( item.value() == "bar" );
}

auto doc_array = alt_json::parse("[\"foo\", \"bar\"]");

for ( auto item : doc_array.items() )
{
if (item.key() == "0" )
{
CHECK( item.value() == "foo" );
}
else if (item.key() == "1" )
{
CHECK( item.value() == "bar" );
}
else
{
CHECK( false );
}
}
}

SECTION("equality")
{
alt_json doc;
Expand Down

0 comments on commit d187488

Please sign in to comment.