Merge pull request #2206 from gatopeich/issue2179

Simple ordered_json that works on all supported compilers
This commit is contained in:
Niels Lohmann 2020-07-11 13:15:40 +02:00 committed by GitHub
commit 5f146cb853
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 20 deletions

View File

@ -1527,7 +1527,9 @@ This library will not support comments in the future. If you wish to use comment
### Order of object keys ### 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 ### Memory Release

View File

@ -61,7 +61,7 @@ uses the standard template types.
*/ */
using json = basic_json<>; 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; struct ordered_map;
/*! /*!

View File

@ -11,17 +11,26 @@ namespace nlohmann
/// ordered_map: a minimal map-like container that preserves insertion order /// ordered_map: a minimal map-like container that preserves insertion order
/// for use within nlohmann::basic_json<ordered_map> /// for use within nlohmann::basic_json<ordered_map>
template <class Key, class T, class IgnoredLess = std::less<Key>, template <class Key, class T, class IgnoredLess = std::less<Key>,
class Allocator = std::allocator<std::pair<Key, T>>, class Allocator = std::allocator<std::pair<const Key, T>>>
class Container = std::vector<std::pair<Key, T>, Allocator>> struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
struct ordered_map : Container
{ {
using key_type = Key; using key_type = Key;
using mapped_type = T; using mapped_type = T;
using value_type = typename Container::value_type; using Container = std::vector<std::pair<const Key, T>, Allocator>;
using size_type = typename Container::size_type; using typename Container::iterator;
using Container::Container; 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) for (auto it = this->begin(); it != this->end(); ++it)
{ {
@ -45,7 +54,13 @@ struct ordered_map : Container
{ {
if (it->first == key) 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; return 1;
} }
} }

View File

@ -2773,7 +2773,7 @@ uses the standard template types.
*/ */
using json = basic_json<>; 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; struct ordered_map;
/*! /*!
@ -15880,17 +15880,26 @@ namespace nlohmann
/// ordered_map: a minimal map-like container that preserves insertion order /// ordered_map: a minimal map-like container that preserves insertion order
/// for use within nlohmann::basic_json<ordered_map> /// for use within nlohmann::basic_json<ordered_map>
template <class Key, class T, class IgnoredLess = std::less<Key>, template <class Key, class T, class IgnoredLess = std::less<Key>,
class Allocator = std::allocator<std::pair<Key, T>>, class Allocator = std::allocator<std::pair<const Key, T>>>
class Container = std::vector<std::pair<Key, T>, Allocator>> struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
struct ordered_map : Container
{ {
using key_type = Key; using key_type = Key;
using mapped_type = T; using mapped_type = T;
using value_type = typename Container::value_type; using Container = std::vector<std::pair<const Key, T>, Allocator>;
using size_type = typename Container::size_type; using typename Container::iterator;
using Container::Container; 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) for (auto it = this->begin(); it != this->end(); ++it)
{ {
@ -15914,7 +15923,13 @@ struct ordered_map : Container
{ {
if (it->first == key) 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; return 1;
} }
} }

View File

@ -58,4 +58,15 @@ TEST_CASE("ordered_json")
CHECK(j.dump() == "{\"element2\":2,\"element3\":3}"); CHECK(j.dump() == "{\"element2\":2,\"element3\":3}");
CHECK(oj.dump() == "{\"element3\":3,\"element2\":2}"); 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}");
} }