Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple ordered_json that works on all supported compilers #2206

Merged
merged 21 commits into from
Jul 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1527,7 +1527,9 @@ This library will not support comments in the future. If you wish to use comment

### Order of object keys

By default, the library does not preserve the **insertion order of object elements**. This is standards-compliant, as the [JSON standard](https://tools.ietf.org/html/rfc8259.html) defines objects as "an unordered collection of zero or more name/value pairs". If you do want to preserve the insertion order, you can specialize the object type with containers like [`tsl::ordered_map`](https://github.com/Tessil/ordered-map) ([integration](https://github.com/nlohmann/json/issues/546#issuecomment-304447518)) or [`nlohmann::fifo_map`](https://github.com/nlohmann/fifo_map) ([integration](https://github.com/nlohmann/json/issues/485#issuecomment-333652309)).
By default, the library does not preserve the **insertion order of object elements**. This is standards-compliant, as the [JSON standard](https://tools.ietf.org/html/rfc8259.html) defines objects as "an unordered collection of zero or more name/value pairs".

If you do want to preserve the insertion order, you can try the type [`nlohmann::ordered_json`](https://github.com/nlohmann/json/issues/2179). Alternatively, you can use a more sophisticated ordered map like [`tsl::ordered_map`](https://github.com/Tessil/ordered-map) ([integration](https://github.com/nlohmann/json/issues/546#issuecomment-304447518)) or [`nlohmann::fifo_map`](https://github.com/nlohmann/fifo_map) ([integration](https://github.com/nlohmann/json/issues/485#issuecomment-333652309)).

### Memory Release

Expand Down
2 changes: 1 addition & 1 deletion include/nlohmann/json_fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ uses the standard template types.
*/
using json = basic_json<>;

template<class Key, class T, class IgnoredLess, class Allocator, class Container>
template<class Key, class T, class IgnoredLess, class Allocator>
struct ordered_map;

/*!
Expand Down
31 changes: 23 additions & 8 deletions include/nlohmann/ordered_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,26 @@ namespace nlohmann
/// ordered_map: a minimal map-like container that preserves insertion order
/// for use within nlohmann::basic_json<ordered_map>
template <class Key, class T, class IgnoredLess = std::less<Key>,
class Allocator = std::allocator<std::pair<Key, T>>,
class Container = std::vector<std::pair<Key, T>, Allocator>>
struct ordered_map : Container
class Allocator = std::allocator<std::pair<const Key, T>>>
struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
{
using key_type = Key;
using mapped_type = T;
using value_type = typename Container::value_type;
using size_type = typename Container::size_type;
using Container::Container;
using Container = std::vector<std::pair<const Key, T>, Allocator>;
using typename Container::iterator;
using typename Container::size_type;
using typename Container::value_type;

std::pair<typename Container::iterator, bool> emplace(key_type&& key, T&& t)
// Explicit constructors instead of `using Container::Container`
// otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4)
ordered_map(const Allocator& alloc = Allocator()) : Container{alloc} {}
template <class It>
ordered_map(It first, It last, const Allocator& alloc = Allocator())
: Container{first, last, alloc} {}
ordered_map(std::initializer_list<T> init, const Allocator& alloc = Allocator() )
: Container{init, alloc} {}

std::pair<iterator, bool> emplace(key_type&& key, T&& t)
{
for (auto it = this->begin(); it != this->end(); ++it)
{
Expand All @@ -45,7 +54,13 @@ struct ordered_map : Container
{
if (it->first == key)
{
Container::erase(it);
// Since we cannot move const Keys, re-construct them in place
for (auto next = it; ++next != this->end(); ++it)
{
it->~value_type(); // Destroy but keep allocation
new (&*it) value_type{std::move(*next)};
}
Container::pop_back();
return 1;
}
}
Expand Down
37 changes: 26 additions & 11 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2773,7 +2773,7 @@ uses the standard template types.
*/
using json = basic_json<>;

template<class Key, class T, class IgnoredLess, class Allocator, class Container>
template<class Key, class T, class IgnoredLess, class Allocator>
struct ordered_map;

/*!
Expand Down Expand Up @@ -15880,17 +15880,26 @@ namespace nlohmann
/// ordered_map: a minimal map-like container that preserves insertion order
/// for use within nlohmann::basic_json<ordered_map>
template <class Key, class T, class IgnoredLess = std::less<Key>,
class Allocator = std::allocator<std::pair<Key, T>>,
class Container = std::vector<std::pair<Key, T>, Allocator>>
struct ordered_map : Container
class Allocator = std::allocator<std::pair<const Key, T>>>
struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
{
using key_type = Key;
using mapped_type = T;
using value_type = typename Container::value_type;
using size_type = typename Container::size_type;
using Container::Container;

std::pair<typename Container::iterator, bool> emplace(key_type&& key, T&& t)
using Container = std::vector<std::pair<const Key, T>, Allocator>;
using typename Container::iterator;
using typename Container::size_type;
using typename Container::value_type;

// Explicit constructors instead of `using Container::Container`
// otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4)
ordered_map(const Allocator& alloc = Allocator()) : Container{alloc} {}
template <class It>
ordered_map(It first, It last, const Allocator& alloc = Allocator())
: Container{first, last, alloc} {}
ordered_map(std::initializer_list<T> init, const Allocator& alloc = Allocator() )
: Container{init, alloc} {}

std::pair<iterator, bool> emplace(key_type&& key, T&& t)
{
for (auto it = this->begin(); it != this->end(); ++it)
{
Expand All @@ -15914,7 +15923,13 @@ struct ordered_map : Container
{
if (it->first == key)
{
Container::erase(it);
// Since we cannot move const Keys, re-construct them in place
for (auto next = it; ++next != this->end(); ++it)
{
it->~value_type(); // Destroy but keep allocation
new (&*it) value_type{std::move(*next)};
}
Container::pop_back();
return 1;
}
}
Expand Down Expand Up @@ -16049,7 +16064,7 @@ class basic_json
InputAdapterType adapter,
detail::parser_callback_t<basic_json>cb = nullptr,
bool allow_exceptions = true
)
)
{
return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter), std::move(cb), allow_exceptions);
}
Expand Down
11 changes: 11 additions & 0 deletions test/src/unit-ordered_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,15 @@ TEST_CASE("ordered_json")

CHECK(j.dump() == "{\"element2\":2,\"element3\":3}");
CHECK(oj.dump() == "{\"element3\":3,\"element2\":2}");

// There are no dup keys cause constructor calls emplace...
json multi {{"z", 1}, {"m", 2}, {"m", 3}, {"y", 4}, {"m", 5}};
CHECK(multi.size() == 3);
CHECK(multi.dump() == "{\"m\":2,\"y\":4,\"z\":1}");

ordered_json multi_ordered {{"z", 1}, {"m", 2}, {"m", 3}, {"y", 4}, {"m", 5}};
CHECK(multi_ordered.size() == 3);
CHECK(multi_ordered.dump() == "{\"z\":1,\"m\":2,\"y\":4}");
CHECK(multi_ordered.erase("m") == 1);
CHECK(multi_ordered.dump() == "{\"z\":1,\"y\":4}");
}