From 27aaf6f84598c98d92d4c1e96b31ab9833e79730 Mon Sep 17 00:00:00 2001 From: gatopeich Date: Sun, 21 Jun 2020 22:28:03 +0100 Subject: [PATCH 01/21] Clean-up ordered_map declarations --- .gitignore | 2 ++ include/nlohmann/ordered_map.hpp | 7 ++++--- single_include/nlohmann/json.hpp | 7 ++++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index bec7f598..9d4fcb37 100644 --- a/.gitignore +++ b/.gitignore @@ -25,7 +25,9 @@ benchmarks/files/numbers/*.json cmake-build-debug test/test-* +test/test_data.hpp /.vs +.vscode doc/mkdocs/venv/ doc/mkdocs/docs/images diff --git a/include/nlohmann/ordered_map.hpp b/include/nlohmann/ordered_map.hpp index 2cf33c42..756b586a 100644 --- a/include/nlohmann/ordered_map.hpp +++ b/include/nlohmann/ordered_map.hpp @@ -17,11 +17,12 @@ struct ordered_map : Container { using key_type = Key; using mapped_type = T; - using value_type = typename Container::value_type; - using size_type = typename Container::size_type; + using typename Container::iterator; + using typename Container::value_type; + using typename Container::size_type; using Container::Container; - std::pair emplace(key_type&& key, T&& t) + std::pair emplace(key_type&& key, T&& t) { for (auto it = this->begin(); it != this->end(); ++it) { diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index c8cabbbf..b524fe69 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -15886,11 +15886,12 @@ struct ordered_map : Container { using key_type = Key; using mapped_type = T; - using value_type = typename Container::value_type; - using size_type = typename Container::size_type; + using typename Container::iterator; + using typename Container::value_type; + using typename Container::size_type; using Container::Container; - std::pair emplace(key_type&& key, T&& t) + std::pair emplace(key_type&& key, T&& t) { for (auto it = this->begin(); it != this->end(); ++it) { From 15337b2cc3c911efdb3d98bfbc50bc8d513bc8bb Mon Sep 17 00:00:00 2001 From: gatopeich Date: Mon, 22 Jun 2020 00:03:48 +0100 Subject: [PATCH 02/21] Ignore allocator hardcoded to match std::map --- include/nlohmann/json.hpp | 1 + include/nlohmann/ordered_map.hpp | 4 ++-- single_include/nlohmann/json.hpp | 5 +++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 15921726..5e53e4f9 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -496,6 +496,7 @@ class basic_json using object_t = ObjectType AllocatorType>>; diff --git a/include/nlohmann/ordered_map.hpp b/include/nlohmann/ordered_map.hpp index 756b586a..2fa00c0d 100644 --- a/include/nlohmann/ordered_map.hpp +++ b/include/nlohmann/ordered_map.hpp @@ -11,8 +11,8 @@ namespace nlohmann /// ordered_map: a minimal map-like container that preserves insertion order /// for use within nlohmann::basic_json template , - class Allocator = std::allocator>, - class Container = std::vector, Allocator>> + class IgnoredAllocator = std::allocator>, + class Container = std::vector>> struct ordered_map : Container { using key_type = Key; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index b524fe69..43da17f5 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -15880,8 +15880,8 @@ namespace nlohmann /// ordered_map: a minimal map-like container that preserves insertion order /// for use within nlohmann::basic_json template , - class Allocator = std::allocator>, - class Container = std::vector, Allocator>> + class IgnoredAllocator = std::allocator>, + class Container = std::vector>> struct ordered_map : Container { using key_type = Key; @@ -16348,6 +16348,7 @@ class basic_json using object_t = ObjectType AllocatorType>>; From ddf0a45abbbb348142919adf3d9d8212ee998f53 Mon Sep 17 00:00:00 2001 From: gatopeich Date: Mon, 22 Jun 2020 18:35:46 +0100 Subject: [PATCH 03/21] Use AllocatorType, instead of hard-coding it for std::map's value_type --- .gitignore | 2 ++ README.md | 2 +- doc/mkdocs/docs/features/types.md | 2 +- include/nlohmann/json.hpp | 4 +--- include/nlohmann/ordered_map.hpp | 4 ++-- single_include/nlohmann/json.hpp | 8 +++----- 6 files changed, 10 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 9d4fcb37..4a39c874 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,8 @@ cmake-build-debug test/test-* test/test_data.hpp +Temporary + /.vs .vscode diff --git a/README.md b/README.md index d7492da3..972d857f 100644 --- a/README.md +++ b/README.md @@ -1527,7 +1527,7 @@ 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 new [`nlohmann::ordered_json`](https://github.com/nlohmann/json/issues/2179) specialization, or 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 diff --git a/doc/mkdocs/docs/features/types.md b/doc/mkdocs/docs/features/types.md index 94e40cba..7754bbca 100644 --- a/doc/mkdocs/docs/features/types.md +++ b/doc/mkdocs/docs/features/types.md @@ -89,7 +89,7 @@ From the template arguments, the following types are derived: ```cpp using object_comparator_t = std::less<>; using object_t = ObjectType>>; + AllocatorType::value_type>>; using array_t = ArrayType>; diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 5e53e4f9..dc44c12c 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -496,9 +496,7 @@ class basic_json using object_t = ObjectType - AllocatorType>>; + AllocatorType::value_type>>; /*! @brief a type for an array diff --git a/include/nlohmann/ordered_map.hpp b/include/nlohmann/ordered_map.hpp index 2fa00c0d..756b586a 100644 --- a/include/nlohmann/ordered_map.hpp +++ b/include/nlohmann/ordered_map.hpp @@ -11,8 +11,8 @@ namespace nlohmann /// ordered_map: a minimal map-like container that preserves insertion order /// for use within nlohmann::basic_json template , - class IgnoredAllocator = std::allocator>, - class Container = std::vector>> + class Allocator = std::allocator>, + class Container = std::vector, Allocator>> struct ordered_map : Container { using key_type = Key; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 43da17f5..2d3d31a0 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -15880,8 +15880,8 @@ namespace nlohmann /// ordered_map: a minimal map-like container that preserves insertion order /// for use within nlohmann::basic_json template , - class IgnoredAllocator = std::allocator>, - class Container = std::vector>> + class Allocator = std::allocator>, + class Container = std::vector, Allocator>> struct ordered_map : Container { using key_type = Key; @@ -16348,9 +16348,7 @@ class basic_json using object_t = ObjectType - AllocatorType>>; + AllocatorType::value_type>>; /*! @brief a type for an array From 064a9c92126822d246b2e08c28b57d0fc2c44c29 Mon Sep 17 00:00:00 2001 From: gatopeich Date: Mon, 22 Jun 2020 18:59:19 +0100 Subject: [PATCH 04/21] Fix regression test --- include/nlohmann/json.hpp | 4 +++- single_include/nlohmann/json.hpp | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index dc44c12c..69afaf05 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -496,7 +496,9 @@ class basic_json using object_t = ObjectType::value_type>>; + // Note: instantiating ObjectType with dummy allocator to extract ::value_type + AllocatorType + >::value_type>>; /*! @brief a type for an array diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 2d3d31a0..e6d87b31 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -16348,7 +16348,9 @@ class basic_json using object_t = ObjectType::value_type>>; + // Note: instantiating ObjectType with dummy allocator to extract ::value_type + AllocatorType + >::value_type>>; /*! @brief a type for an array From 5fe3d3929a523eef4c64f0893db0969a12d3704e Mon Sep 17 00:00:00 2001 From: gatopeich Date: Mon, 22 Jun 2020 19:10:35 +0100 Subject: [PATCH 05/21] Using ordered_json instead of fifo_map in test-regression This is more comprehensive and the "my_workaround_fifo_map" wrapper does not allow to infer value type as required in this branch. This reverts commit 064a9c92126822d246b2e08c28b57d0fc2c44c29. --- include/nlohmann/json.hpp | 4 +--- single_include/nlohmann/json.hpp | 4 +--- test/src/unit-regression.cpp | 6 +----- 3 files changed, 3 insertions(+), 11 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 69afaf05..dc44c12c 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -496,9 +496,7 @@ class basic_json using object_t = ObjectType - >::value_type>>; + AllocatorType::value_type>>; /*! @brief a type for an array diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index e6d87b31..2d3d31a0 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -16348,9 +16348,7 @@ class basic_json using object_t = ObjectType - >::value_type>>; + AllocatorType::value_type>>; /*! @brief a type for an array diff --git a/test/src/unit-regression.cpp b/test/src/unit-regression.cpp index e5cd0337..143508a6 100644 --- a/test/src/unit-regression.cpp +++ b/test/src/unit-regression.cpp @@ -52,15 +52,11 @@ using nlohmann::json; #include #endif -#include "fifo_map.hpp" - ///////////////////////////////////////////////////////////////////// // for #972 ///////////////////////////////////////////////////////////////////// -template -using my_workaround_fifo_map = nlohmann::fifo_map, A>; -using my_json = nlohmann::basic_json; +using my_json = nlohmann::ordered_json; ///////////////////////////////////////////////////////////////////// // for #977 From 49623a75ee670332f8b86cc37fa941542584dcfc Mon Sep 17 00:00:00 2001 From: gatopeich Date: Tue, 23 Jun 2020 11:30:52 +0100 Subject: [PATCH 06/21] Revert "Using ordered_json instead of fifo_map in test-regression" This reverts commit 5fe3d3929a523eef4c64f0893db0969a12d3704e. --- include/nlohmann/json.hpp | 4 +++- single_include/nlohmann/json.hpp | 4 +++- test/src/unit-regression.cpp | 6 +++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index dc44c12c..69afaf05 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -496,7 +496,9 @@ class basic_json using object_t = ObjectType::value_type>>; + // Note: instantiating ObjectType with dummy allocator to extract ::value_type + AllocatorType + >::value_type>>; /*! @brief a type for an array diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 2d3d31a0..e6d87b31 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -16348,7 +16348,9 @@ class basic_json using object_t = ObjectType::value_type>>; + // Note: instantiating ObjectType with dummy allocator to extract ::value_type + AllocatorType + >::value_type>>; /*! @brief a type for an array diff --git a/test/src/unit-regression.cpp b/test/src/unit-regression.cpp index 143508a6..e5cd0337 100644 --- a/test/src/unit-regression.cpp +++ b/test/src/unit-regression.cpp @@ -52,11 +52,15 @@ using nlohmann::json; #include #endif +#include "fifo_map.hpp" + ///////////////////////////////////////////////////////////////////// // for #972 ///////////////////////////////////////////////////////////////////// -using my_json = nlohmann::ordered_json; +template +using my_workaround_fifo_map = nlohmann::fifo_map, A>; +using my_json = nlohmann::basic_json; ///////////////////////////////////////////////////////////////////// // for #977 From acd748e16f0462f73274fe02d37e157f808318ff Mon Sep 17 00:00:00 2001 From: gatopeich Date: Tue, 23 Jun 2020 12:03:21 +0100 Subject: [PATCH 07/21] Use std::map default allocator as a placeholder to extract the actual ObjectType::value_type Still fails on older compilers (GCC <= 5.5) --- include/nlohmann/json.hpp | 9 ++++++--- single_include/nlohmann/json.hpp | 9 ++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 69afaf05..31944e32 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -496,9 +496,12 @@ class basic_json using object_t = ObjectType - >::value_type>>; + // Note the use of std::map default allocator as a placeholder + // to extract the actual ObjectType::value_type + AllocatorType> + >::value_type>>; /*! @brief a type for an array diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index e6d87b31..76ee2764 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -16348,9 +16348,12 @@ class basic_json using object_t = ObjectType - >::value_type>>; + // Note the use of std::map default allocator as a placeholder + // to extract the actual ObjectType::value_type + AllocatorType> + >::value_type>>; /*! @brief a type for an array From fb8c11f25cb6ff4abc305d988a609751726e674e Mon Sep 17 00:00:00 2001 From: gatopeich Date: Tue, 23 Jun 2020 15:01:20 +0100 Subject: [PATCH 08/21] Re-implement ordered_map::erase, so that it can handle pair --- include/nlohmann/json.hpp | 6 +++--- include/nlohmann/ordered_map.hpp | 9 ++++++++- single_include/nlohmann/json.hpp | 15 +++++++++++---- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 31944e32..2aef23b3 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -499,9 +499,9 @@ class basic_json // Note the use of std::map default allocator as a placeholder // to extract the actual ObjectType::value_type AllocatorType> - >::value_type>>; + ObjectType> + >::value_type>>; /*! @brief a type for an array diff --git a/include/nlohmann/ordered_map.hpp b/include/nlohmann/ordered_map.hpp index 756b586a..bb5dfe4e 100644 --- a/include/nlohmann/ordered_map.hpp +++ b/include/nlohmann/ordered_map.hpp @@ -46,7 +46,14 @@ 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 = std::move(*next); // deleted + it->~value_type(); // Destroy but keep allocation + new (&*it) value_type{std::move(*next)}; + } + Container::pop_back(); return 1; } } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 76ee2764..3cf133d5 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -15915,7 +15915,14 @@ 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 = std::move(*next); // deleted + it->~value_type(); // Destroy but keep allocation + new (&*it) value_type{std::move(*next)}; + } + Container::pop_back(); return 1; } } @@ -16351,9 +16358,9 @@ class basic_json // Note the use of std::map default allocator as a placeholder // to extract the actual ObjectType::value_type AllocatorType> - >::value_type>>; + ObjectType> + >::value_type>>; /*! @brief a type for an array From 5e7bdf1cab080543a8a3b428f18b2be854aa2007 Mon Sep 17 00:00:00 2001 From: gatopeich Date: Tue, 23 Jun 2020 15:39:00 +0100 Subject: [PATCH 09/21] Roll-back to hard-coded object_t::value_type --- include/nlohmann/json.hpp | 8 ++------ single_include/nlohmann/json.hpp | 8 ++------ 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 2aef23b3..c85147b3 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -496,12 +496,8 @@ class basic_json using object_t = ObjectType> - >::value_type>>; + // Note: this forces object_t::value_type to match std::map's + AllocatorType>>; /*! @brief a type for an array diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 3cf133d5..18cd4a38 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -16355,12 +16355,8 @@ class basic_json using object_t = ObjectType> - >::value_type>>; + // Note: this forces object_t::value_type to match std::map's + AllocatorType>>; /*! @brief a type for an array From d08fca2bb95a3c6deccd2aa7fa9d86303d9a8d74 Mon Sep 17 00:00:00 2001 From: gatopeich Date: Tue, 23 Jun 2020 15:44:46 +0100 Subject: [PATCH 10/21] Use const Key in ordered map (forgotten in previous commit!) --- include/nlohmann/ordered_map.hpp | 4 ++-- single_include/nlohmann/json.hpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/ordered_map.hpp b/include/nlohmann/ordered_map.hpp index bb5dfe4e..58c54845 100644 --- a/include/nlohmann/ordered_map.hpp +++ b/include/nlohmann/ordered_map.hpp @@ -11,8 +11,8 @@ namespace nlohmann /// ordered_map: a minimal map-like container that preserves insertion order /// for use within nlohmann::basic_json template , - class Allocator = std::allocator>, - class Container = std::vector, Allocator>> + class Allocator = std::allocator>, + class Container = std::vector, Allocator>> struct ordered_map : Container { using key_type = Key; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 18cd4a38..937d5b0e 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -15880,8 +15880,8 @@ namespace nlohmann /// ordered_map: a minimal map-like container that preserves insertion order /// for use within nlohmann::basic_json template , - class Allocator = std::allocator>, - class Container = std::vector, Allocator>> + class Allocator = std::allocator>, + class Container = std::vector, Allocator>> struct ordered_map : Container { using key_type = Key; From 08963d6826997261682320971be1a6c3ca93f8c3 Mon Sep 17 00:00:00 2001 From: gatopeich Date: Tue, 23 Jun 2020 15:48:02 +0100 Subject: [PATCH 11/21] Revert types.md --- doc/mkdocs/docs/features/types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/mkdocs/docs/features/types.md b/doc/mkdocs/docs/features/types.md index 7754bbca..211595e5 100644 --- a/doc/mkdocs/docs/features/types.md +++ b/doc/mkdocs/docs/features/types.md @@ -89,7 +89,7 @@ From the template arguments, the following types are derived: ```cpp using object_comparator_t = std::less<>; using object_t = ObjectType::value_type>>; + AllocatorType>>; using array_t = ArrayType>; From cf18ba2394f7813ebaf8dbc94598db859e5916bf Mon Sep 17 00:00:00 2001 From: gatopeich Date: Tue, 23 Jun 2020 17:50:51 +0100 Subject: [PATCH 12/21] Test initialization with dup keys --- test/src/unit-ordered_json.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/src/unit-ordered_json.cpp b/test/src/unit-ordered_json.cpp index 1a97ece6..e4ffd68a 100644 --- a/test/src/unit-ordered_json.cpp +++ b/test/src/unit-ordered_json.cpp @@ -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}"); } From 49f26a02509d70a6f3cda614f4b0a272e7011978 Mon Sep 17 00:00:00 2001 From: gatopeich Date: Mon, 29 Jun 2020 17:32:55 +0100 Subject: [PATCH 13/21] Have 4 template parameters for ordered_map --- include/nlohmann/ordered_map.hpp | 6 +++--- single_include/nlohmann/json.hpp | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/nlohmann/ordered_map.hpp b/include/nlohmann/ordered_map.hpp index 58c54845..672eb31e 100644 --- a/include/nlohmann/ordered_map.hpp +++ b/include/nlohmann/ordered_map.hpp @@ -11,10 +11,10 @@ namespace nlohmann /// ordered_map: a minimal map-like container that preserves insertion order /// for use within nlohmann::basic_json template , - class Allocator = std::allocator>, - class Container = std::vector, Allocator>> -struct ordered_map : Container + class Allocator = std::allocator>> +struct ordered_map : std::vector { + using Container = std::vector; using key_type = Key; using mapped_type = T; using typename Container::iterator; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 937d5b0e..0f8d48bd 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2773,7 +2773,7 @@ uses the standard template types. */ using json = basic_json<>; -template +template struct ordered_map; /*! @@ -15880,10 +15880,10 @@ namespace nlohmann /// ordered_map: a minimal map-like container that preserves insertion order /// for use within nlohmann::basic_json template , - class Allocator = std::allocator>, - class Container = std::vector, Allocator>> -struct ordered_map : Container + class Allocator = std::allocator>> +struct ordered_map : std::vector { + using Container = std::vector; using key_type = Key; using mapped_type = T; using typename Container::iterator; From 0fc261f0f2be664f61d90499b662d35a7727f426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agust=C3=ADn=20F=2E=20Pozuelo?= Date: Fri, 3 Jul 2020 00:33:31 +0100 Subject: [PATCH 14/21] Make ordered_map compatible with GCC 5.5 --- include/nlohmann/ordered_map.hpp | 16 +++++++++++----- single_include/nlohmann/json.hpp | 18 ++++++++++++------ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/include/nlohmann/ordered_map.hpp b/include/nlohmann/ordered_map.hpp index 672eb31e..a6e006e6 100644 --- a/include/nlohmann/ordered_map.hpp +++ b/include/nlohmann/ordered_map.hpp @@ -11,16 +11,22 @@ namespace nlohmann /// ordered_map: a minimal map-like container that preserves insertion order /// for use within nlohmann::basic_json template , - class Allocator = std::allocator>> -struct ordered_map : std::vector + class Allocator = std::allocator>, + class Container = std::vector, Allocator>> +struct ordered_map : Container { - using Container = std::vector; using key_type = Key; using mapped_type = T; using typename Container::iterator; - using typename Container::value_type; using typename Container::size_type; - using Container::Container; + using typename Container::value_type; + + // Explicit constructors instead of `using Container::Container` + // otherwise older compilers like GCC 5.5 choke on it + ordered_map(const Allocator& alloc = Allocator()) : Container{alloc} {} + template + ordered_map(It first, It last, const Allocator& alloc = Allocator()) + : Container{first, last, alloc} {} std::pair emplace(key_type&& key, T&& t) { diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 0f8d48bd..50e173e2 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2773,7 +2773,7 @@ uses the standard template types. */ using json = basic_json<>; -template +template struct ordered_map; /*! @@ -15880,16 +15880,22 @@ namespace nlohmann /// ordered_map: a minimal map-like container that preserves insertion order /// for use within nlohmann::basic_json template , - class Allocator = std::allocator>> -struct ordered_map : std::vector + class Allocator = std::allocator>, + class Container = std::vector, Allocator>> +struct ordered_map : Container { - using Container = std::vector; using key_type = Key; using mapped_type = T; using typename Container::iterator; - using typename Container::value_type; using typename Container::size_type; - using Container::Container; + using typename Container::value_type; + + // Explicit constructors instead of `using Container::Container` + // otherwise older compilers like GCC 5.5 choke on it + ordered_map(const Allocator& alloc = Allocator()) : Container{alloc} {} + template + ordered_map(It first, It last, const Allocator& alloc = Allocator()) + : Container{first, last, alloc} {} std::pair emplace(key_type&& key, T&& t) { From 93770467a17773dcdd10885285ce6714f0ae3dd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agust=C3=ADn=20F=2E=20Pozuelo?= Date: Fri, 3 Jul 2020 01:28:54 +0100 Subject: [PATCH 15/21] Precisely 4 template arguments for the sake of clang 3.6 (?) --- include/nlohmann/ordered_map.hpp | 6 +++--- single_include/nlohmann/json.hpp | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/nlohmann/ordered_map.hpp b/include/nlohmann/ordered_map.hpp index a6e006e6..4a71951d 100644 --- a/include/nlohmann/ordered_map.hpp +++ b/include/nlohmann/ordered_map.hpp @@ -11,12 +11,12 @@ namespace nlohmann /// ordered_map: a minimal map-like container that preserves insertion order /// for use within nlohmann::basic_json template , - class Allocator = std::allocator>, - class Container = std::vector, Allocator>> -struct ordered_map : Container + class Allocator = std::allocator>> +struct ordered_map : std::vector, Allocator> { using key_type = Key; using mapped_type = T; + using Container = std::vector, Allocator>; using typename Container::iterator; using typename Container::size_type; using typename Container::value_type; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 50e173e2..e5e3bc3e 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2773,7 +2773,7 @@ uses the standard template types. */ using json = basic_json<>; -template +template struct ordered_map; /*! @@ -15880,12 +15880,12 @@ namespace nlohmann /// ordered_map: a minimal map-like container that preserves insertion order /// for use within nlohmann::basic_json template , - class Allocator = std::allocator>, - class Container = std::vector, Allocator>> -struct ordered_map : Container + class Allocator = std::allocator>> +struct ordered_map : std::vector, Allocator> { using key_type = Key; using mapped_type = T; + using Container = std::vector, Allocator>; using typename Container::iterator; using typename Container::size_type; using typename Container::value_type; @@ -16063,7 +16063,7 @@ class basic_json InputAdapterType adapter, detail::parser_callback_tcb = nullptr, bool allow_exceptions = true - ) + ) { return ::nlohmann::detail::parser(std::move(adapter), std::move(cb), allow_exceptions); } @@ -24534,7 +24534,7 @@ template<> inline void swap(nlohmann::json& j1, nlohmann::json& j2) noexcept( is_nothrow_move_constructible::value and is_nothrow_move_assignable::value -) + ) { j1.swap(j2); } From 25f5d75e6e0fbdbff1a6341b112156ecfc726812 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agust=C3=ADn=20F=2E=20Pozuelo?= Date: Fri, 3 Jul 2020 01:44:18 +0100 Subject: [PATCH 16/21] Fix compilation for xcode 9.x --- include/nlohmann/json_fwd.hpp | 2 +- include/nlohmann/ordered_map.hpp | 4 +++- single_include/nlohmann/json.hpp | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/include/nlohmann/json_fwd.hpp b/include/nlohmann/json_fwd.hpp index d9e5428d..332227c1 100644 --- a/include/nlohmann/json_fwd.hpp +++ b/include/nlohmann/json_fwd.hpp @@ -61,7 +61,7 @@ uses the standard template types. */ using json = basic_json<>; -template +template struct ordered_map; /*! diff --git a/include/nlohmann/ordered_map.hpp b/include/nlohmann/ordered_map.hpp index 4a71951d..29c52016 100644 --- a/include/nlohmann/ordered_map.hpp +++ b/include/nlohmann/ordered_map.hpp @@ -22,11 +22,13 @@ struct ordered_map : std::vector, Allocator> using typename Container::value_type; // Explicit constructors instead of `using Container::Container` - // otherwise older compilers like GCC 5.5 choke on it + // otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4) ordered_map(const Allocator& alloc = Allocator()) : Container{alloc} {} template ordered_map(It first, It last, const Allocator& alloc = Allocator()) : Container{first, last, alloc} {} + ordered_map(std::initializer_list init, const Allocator& alloc = Allocator() ) + : Container{init, alloc} {} std::pair emplace(key_type&& key, T&& t) { diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index e5e3bc3e..850ee6ab 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -15891,11 +15891,13 @@ struct ordered_map : std::vector, Allocator> using typename Container::value_type; // Explicit constructors instead of `using Container::Container` - // otherwise older compilers like GCC 5.5 choke on it + // otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4) ordered_map(const Allocator& alloc = Allocator()) : Container{alloc} {} template ordered_map(It first, It last, const Allocator& alloc = Allocator()) : Container{first, last, alloc} {} + ordered_map(std::initializer_list init, const Allocator& alloc = Allocator() ) + : Container{init, alloc} {} std::pair emplace(key_type&& key, T&& t) { From 803c16e5af4ff173b00684e758f8487bf1c8b0ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agust=C3=ADn=20F=2E=20Pozuelo?= Date: Fri, 3 Jul 2020 10:26:05 +0100 Subject: [PATCH 17/21] Clean-up unintended changes to whitespace --- .gitignore | 4 ---- doc/mkdocs/docs/features/types.md | 2 +- single_include/nlohmann/json.hpp | 2 +- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 4a39c874..bec7f598 100644 --- a/.gitignore +++ b/.gitignore @@ -25,11 +25,7 @@ benchmarks/files/numbers/*.json cmake-build-debug test/test-* -test/test_data.hpp -Temporary - /.vs -.vscode doc/mkdocs/venv/ doc/mkdocs/docs/images diff --git a/doc/mkdocs/docs/features/types.md b/doc/mkdocs/docs/features/types.md index 211595e5..94e40cba 100644 --- a/doc/mkdocs/docs/features/types.md +++ b/doc/mkdocs/docs/features/types.md @@ -89,7 +89,7 @@ From the template arguments, the following types are derived: ```cpp using object_comparator_t = std::less<>; using object_t = ObjectType>>; + AllocatorType>>; using array_t = ArrayType>; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 850ee6ab..a944799c 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -24536,7 +24536,7 @@ template<> inline void swap(nlohmann::json& j1, nlohmann::json& j2) noexcept( is_nothrow_move_constructible::value and is_nothrow_move_assignable::value - ) +) { j1.swap(j2); } From 0eb7b0a991b44425cd001f7c550e6f531c9b647e Mon Sep 17 00:00:00 2001 From: gatopeich <7722268+gatopeich@users.noreply.github.com> Date: Thu, 9 Jul 2020 20:47:19 +0100 Subject: [PATCH 18/21] Update README.md per review comments Co-authored-by: Niels Lohmann --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 972d857f..65e948fe 100644 --- a/README.md +++ b/README.md @@ -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 try the new [`nlohmann::ordered_json`](https://github.com/nlohmann/json/issues/2179) specialization, or 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)). +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 From f62b4626be3bef408b94c67330d5f5c4606512c8 Mon Sep 17 00:00:00 2001 From: gatopeich Date: Thu, 9 Jul 2020 21:12:11 +0100 Subject: [PATCH 19/21] Removing comment about AllocatorType per review request --- include/nlohmann/json.hpp | 4 ++-- single_include/nlohmann/json.hpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index c85147b3..15921726 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -496,8 +496,8 @@ class basic_json using object_t = ObjectType>>; + AllocatorType>>; /*! @brief a type for an array diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index a944799c..0b19ea45 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -16363,8 +16363,8 @@ class basic_json using object_t = ObjectType>>; + AllocatorType>>; /*! @brief a type for an array From f9a1fec272d2e169d3ee56e9480c9c78b085c369 Mon Sep 17 00:00:00 2001 From: gatopeich <7722268+gatopeich@users.noreply.github.com> Date: Sat, 11 Jul 2020 00:34:02 +0100 Subject: [PATCH 20/21] Remove redundant comment --- include/nlohmann/ordered_map.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/include/nlohmann/ordered_map.hpp b/include/nlohmann/ordered_map.hpp index 29c52016..105f0167 100644 --- a/include/nlohmann/ordered_map.hpp +++ b/include/nlohmann/ordered_map.hpp @@ -57,7 +57,6 @@ struct ordered_map : std::vector, Allocator> // Since we cannot move const Keys, re-construct them in place for (auto next = it; ++next != this->end(); ++it) { - // *it = std::move(*next); // deleted it->~value_type(); // Destroy but keep allocation new (&*it) value_type{std::move(*next)}; } From 47d154dd49ed82da594f90fc5af0793407128e20 Mon Sep 17 00:00:00 2001 From: gatopeich <7722268+gatopeich@users.noreply.github.com> Date: Sat, 11 Jul 2020 00:34:12 +0100 Subject: [PATCH 21/21] Remove redundant comment --- single_include/nlohmann/json.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 0b19ea45..54f22274 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -15926,7 +15926,6 @@ struct ordered_map : std::vector, Allocator> // Since we cannot move const Keys, re-construct them in place for (auto next = it; ++next != this->end(); ++it) { - // *it = std::move(*next); // deleted it->~value_type(); // Destroy but keep allocation new (&*it) value_type{std::move(*next)}; }