Skip to content

Commit

Permalink
allow to use unordered maps to have stable key order
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Le Lann committed May 28, 2019
1 parent ee4028b commit f106d6b
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 13 deletions.
35 changes: 22 additions & 13 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12881,6 +12881,27 @@ class serializer
namespace nlohmann
{

template <
template <typename, typename, typename...> class ObjectType,
class StringType,
template<typename> class AllocatorType,
class BasicJsonType>
struct object_traits
{
#if defined(JSON_HAS_CPP_14)
// Use transparent comparator if possible, combined with perfect forwarding
// on find() and count() calls prevents unnecessary string construction.
using object_comparator_t = std::less<>;
#else
using object_comparator_t = std::less<StringType>;
#endif

using object_t = ObjectType<StringType,
BasicJsonType,
object_comparator_t,
AllocatorType<std::pair<const StringType, BasicJsonType>>>;
};

/*!
@brief a class to store JSON values

Expand Down Expand Up @@ -13186,14 +13207,6 @@ class basic_json
/// the template arguments passed to class @ref basic_json.
/// @{

#if defined(JSON_HAS_CPP_14)
// Use transparent comparator if possible, combined with perfect forwarding
// on find() and count() calls prevents unnecessary string construction.
using object_comparator_t = std::less<>;
#else
using object_comparator_t = std::less<StringType>;
#endif

/*!
@brief a type for an object

Expand Down Expand Up @@ -13277,11 +13290,7 @@ class basic_json
7159](http://rfc7159.net/rfc7159), because any order implements the
specified "unordered" nature of JSON objects.
*/
using object_t = ObjectType<StringType,
basic_json,
object_comparator_t,
AllocatorType<std::pair<const StringType,
basic_json>>>;
using object_t = typename object_traits<ObjectType, StringType, AllocatorType, basic_json>::object_t;

/*!
@brief a type for an array
Expand Down
94 changes: 94 additions & 0 deletions test/src/unit-unorderedmap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
__ _____ _____ _____
__| | __| | | | JSON for Modern C++ (test suite)
| | |__ | | | | | | version 3.6.1
|_____|_____|_____|_|___| https://github.com/nlohmann/json
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
SPDX-License-Identifier: MIT
Copyright (c) 2013-2019 Niels Lohmann <http://nlohmann.me>.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "doctest_compatibility.h"

#include <nlohmann/json.hpp>
#include <list>

namespace
{

// a dumb map preserving insertion order
// written solely for the purpose of this test
// in real life, consider something like https://github.com/Tessil/ordered-map

template <typename Key, typename T, typename Alloc = std::allocator<std::pair<const Key, T>>>
class insertion_ordered_map
{
private:
using data_t = std::list<std::pair<const Key, T>, Alloc>;
data_t data;

public:
using key_type = Key;
using value_type = std::pair<const Key, T>;
using size_type = typename data_t::size_type;
using iterator = typename data_t::iterator;
using const_iterator = typename data_t::const_iterator;

size_type size() const { return data.size(); }
size_type max_size() const { return data.max_size(); }
bool empty() const { return data.empty(); }
const_iterator cbegin() const { return data.cbegin(); }
const_iterator cend() const { return data.cend(); }
iterator begin() { return data.begin(); }
iterator end() { return data.end(); }
T& operator[](const key_type & k) {
auto e = end();
auto pos = std::find_if(begin(), e, [&] (std::pair<const Key, T> & p) { return p.first == k; });
return (pos == e ? data.emplace(e, k, T{}) : pos)->second;
}
iterator erase(const_iterator pos) { return data.erase(pos); }
};
}

namespace nlohmann
{
template <class StringType, template<typename> class AllocatorType, class BasicJsonType>
struct object_traits <insertion_ordered_map, StringType, AllocatorType, BasicJsonType>
{
using object_t = insertion_ordered_map<StringType, BasicJsonType, AllocatorType<std::pair<const StringType, BasicJsonType>>>;
};
}

TEST_CASE("use of unordered maps")
{
SECTION("order is stable with insertion_ordered_map")
{
const auto text = "{\"name\":1,\"id\":2}";
CHECK(text == nlohmann::basic_json<insertion_ordered_map>::parse(text).dump());
}

SECTION("keys are sorted with std::map")
{
const auto text = "{\"name\":1,\"id\":2}";
CHECK("{\"id\":2,\"name\":1}" == nlohmann::json::parse(text).dump());
}
}

0 comments on commit f106d6b

Please sign in to comment.