Skip to content

Commit

Permalink
⚡ keys are now returned as const reference #1098
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed May 26, 2018
1 parent 4639bb2 commit 90eb0a9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion include/nlohmann/detail/iterators/iter_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ class iter_impl
@brief return the key of an object iterator
@pre The iterator is initialized; i.e. `m_object != nullptr`.
*/
typename object_t::key_type key() const
const typename object_t::key_type& key() const
{
assert(m_object != nullptr);

Expand Down
18 changes: 14 additions & 4 deletions include/nlohmann/detail/iterators/iteration_proxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ template<typename IteratorType> class iteration_proxy
IteratorType anchor;
/// an index for arrays (used to create key names)
std::size_t array_index = 0;
/// a string representation of the array index
std::string array_index_str = "0";
/// an empty string (to return a reference for primitive values)
const std::string empty_str = "";

public:
explicit iteration_proxy_internal(IteratorType it) noexcept : anchor(it) {}
Expand All @@ -35,7 +39,13 @@ template<typename IteratorType> class iteration_proxy
iteration_proxy_internal& operator++()
{
++anchor;
++array_index;

assert(anchor.m_object != nullptr);
if (anchor.m_object->is_array())
{
// update array index and string representation
array_index_str = std::to_string(++array_index);
}

return *this;
}
Expand All @@ -47,23 +57,23 @@ template<typename IteratorType> class iteration_proxy
}

/// return key of the iterator
std::string key() const
const std::string& key() const
{
assert(anchor.m_object != nullptr);

switch (anchor.m_object->type())
{
// use integer array index as key
case value_t::array:
return std::to_string(array_index);
return array_index_str;

// use key from the object
case value_t::object:
return anchor.key();

// use an empty key for all primitive types
default:
return "";
return empty_str;
}
}

Expand Down
20 changes: 15 additions & 5 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4636,7 +4636,7 @@ class iter_impl
@brief return the key of an object iterator
@pre The iterator is initialized; i.e. `m_object != nullptr`.
*/
typename object_t::key_type key() const
const typename object_t::key_type& key() const
{
assert(m_object != nullptr);

Expand Down Expand Up @@ -4691,6 +4691,10 @@ template<typename IteratorType> class iteration_proxy
IteratorType anchor;
/// an index for arrays (used to create key names)
std::size_t array_index = 0;
/// a string representation of the array index
std::string array_index_str = "0";
/// an empty string (to return a reference for primitive values)
const std::string empty_str = "";

public:
explicit iteration_proxy_internal(IteratorType it) noexcept : anchor(it) {}
Expand All @@ -4705,7 +4709,13 @@ template<typename IteratorType> class iteration_proxy
iteration_proxy_internal& operator++()
{
++anchor;
++array_index;

assert(anchor.m_object != nullptr);
if (anchor.m_object->is_array())
{
// update array index and string representation
array_index_str = std::to_string(++array_index);
}

return *this;
}
Expand All @@ -4717,23 +4727,23 @@ template<typename IteratorType> class iteration_proxy
}

/// return key of the iterator
std::string key() const
const std::string& key() const
{
assert(anchor.m_object != nullptr);

switch (anchor.m_object->type())
{
// use integer array index as key
case value_t::array:
return std::to_string(array_index);
return array_index_str;

// use key from the object
case value_t::object:
return anchor.key();

// use an empty key for all primitive types
default:
return "";
return empty_str;
}
}

Expand Down

0 comments on commit 90eb0a9

Please sign in to comment.