From c7a0d59e164e38017797024104b86f7ee1d795f8 Mon Sep 17 00:00:00 2001 From: barcode Date: Mon, 26 Dec 2022 10:48:21 +0100 Subject: [PATCH] Add helper types to make it easier to create a basic_json type with modified template parameters Signed-off-by: Niels Lohmann --- .../docs/examples/json_base_class_t.cpp | 14 +---- include/nlohmann/json.hpp | 59 +++++++++++++++++++ single_include/nlohmann/json.hpp | 59 +++++++++++++++++++ tests/src/unit-allocator.cpp | 27 +-------- tests/src/unit-alt-string.cpp | 11 +--- tests/src/unit-custom-base-class.cpp | 29 +-------- tests/src/unit-udt.cpp | 5 +- 7 files changed, 127 insertions(+), 77 deletions(-) diff --git a/docs/mkdocs/docs/examples/json_base_class_t.cpp b/docs/mkdocs/docs/examples/json_base_class_t.cpp index 3fb2d46a2..e157ba26c 100644 --- a/docs/mkdocs/docs/examples/json_base_class_t.cpp +++ b/docs/mkdocs/docs/examples/json_base_class_t.cpp @@ -13,19 +13,7 @@ class visitor_adaptor_with_metadata void do_visit(const Ptr& ptr, const Fnc& fnc) const; }; -using json = nlohmann::basic_json < - std::map, - std::vector, - std::string, - bool, - std::int64_t, - std::uint64_t, - double, - std::allocator, - nlohmann::adl_serializer, - std::vector, - visitor_adaptor_with_metadata - >; +using json = nlohmann::json::with_changed_base_class_t; template void visitor_adaptor_with_metadata::visit(const Fnc& fnc) const diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 3f3645524..81a860797 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -226,6 +226,65 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /// SAX interface type, see @ref nlohmann::json_sax using json_sax_t = json_sax; + //////////////////////////////////////////////////////////////////////////////// + // utility templates to create a json type with different template parameters // + //////////////////////////////////////////////////////////////////////////////// + + /// Json type using a different type for storing objects + template class ObjectType2> + using with_changed_object_t = basic_json; + + /// Json type using a different type for storing arrays + template class ArrayType2> + using with_changed_array_t = basic_json; + + /// Json type using a different type for storing strings + template + using with_changed_string_t = basic_json; + + /// Json type using a different type for storing booleans + template + using with_changed_boolean_t = basic_json; + + /// Json type using a different type for storing signed integers + template + using with_changed_integer_t = basic_json; + + /// Json type using a different type for storing unsigned integers + template + using with_changed_unsigned_t = basic_json; + + /// Json type using a different type for storing floating point numbers + template + using with_changed_float_t = basic_json; + + /// Json type using a different type as base allocator + template class AllocatorType2> + using with_changed_allocator_t = basic_json; + + /// Json type using a different type as json serializer + template class JSONSerializer2> + using with_changed_json_serializer_t = basic_json; + + /// Json type using a different type for storing binary data + template + using with_changed_binary_t = basic_json; + + /// Json type using a different type as base class + template + using with_changed_base_class_t = basic_json; + //////////////// // exceptions // //////////////// diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index b5e7417e6..8fe9eafce 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -27348,6 +27348,65 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /// SAX interface type, see @ref nlohmann::json_sax using json_sax_t = json_sax; + //////////////////////////////////////////////////////////////////////////////// + // utility templates to create a json type with different template parameters // + //////////////////////////////////////////////////////////////////////////////// + + /// Json type using a different type for storing objects + template class ObjectType2> + using with_changed_object_t = basic_json; + + /// Json type using a different type for storing arrays + template class ArrayType2> + using with_changed_array_t = basic_json; + + /// Json type using a different type for storing strings + template + using with_changed_string_t = basic_json; + + /// Json type using a different type for storing booleans + template + using with_changed_boolean_t = basic_json; + + /// Json type using a different type for storing signed integers + template + using with_changed_integer_t = basic_json; + + /// Json type using a different type for storing unsigned integers + template + using with_changed_unsigned_t = basic_json; + + /// Json type using a different type for storing floating point numbers + template + using with_changed_float_t = basic_json; + + /// Json type using a different type as base allocator + template class AllocatorType2> + using with_changed_allocator_t = basic_json; + + /// Json type using a different type as json serializer + template class JSONSerializer2> + using with_changed_json_serializer_t = basic_json; + + /// Json type using a different type for storing binary data + template + using with_changed_binary_t = basic_json; + + /// Json type using a different type as base class + template + using with_changed_base_class_t = basic_json; + //////////////// // exceptions // //////////////// diff --git a/tests/src/unit-allocator.cpp b/tests/src/unit-allocator.cpp index bc283ab28..1d1579713 100644 --- a/tests/src/unit-allocator.cpp +++ b/tests/src/unit-allocator.cpp @@ -48,14 +48,7 @@ TEST_CASE("bad_alloc") SECTION("bad_alloc") { // create JSON type using the throwing allocator - using bad_json = nlohmann::basic_json; + using bad_json = nlohmann::json::with_changed_allocator_t; // creating an object should throw CHECK_THROWS_AS(bad_json(bad_json::value_t::object), std::bad_alloc&); @@ -129,14 +122,7 @@ void my_allocator_clean_up(T* p) TEST_CASE("controlled bad_alloc") { // create JSON type using the throwing allocator - using my_json = nlohmann::basic_json; + using my_json = nlohmann::json::with_changed_allocator_t; SECTION("class json_value") { @@ -588,14 +574,7 @@ TEST_CASE("bad my_allocator::construct") { SECTION("my_allocator::construct doesn't forward") { - using bad_alloc_json = nlohmann::basic_json; + using bad_alloc_json = nlohmann::json::with_changed_allocator_t; bad_alloc_json j; j["test"] = bad_alloc_json::array_t(); diff --git a/tests/src/unit-alt-string.cpp b/tests/src/unit-alt-string.cpp index e503a4914..1168304eb 100644 --- a/tests/src/unit-alt-string.cpp +++ b/tests/src/unit-alt-string.cpp @@ -163,16 +163,7 @@ void int_to_string(alt_string& target, std::size_t value) target = std::to_string(value).c_str(); } -using alt_json = nlohmann::basic_json < - std::map, - std::vector, - alt_string, - bool, - std::int64_t, - std::uint64_t, - double, - std::allocator, - nlohmann::adl_serializer >; +using alt_json = nlohmann::json::with_changed_string_t; bool operator<(const char* op1, const alt_string& op2) noexcept { diff --git a/tests/src/unit-custom-base-class.cpp b/tests/src/unit-custom-base-class.cpp index b9e544705..8043919ef 100644 --- a/tests/src/unit-custom-base-class.cpp +++ b/tests/src/unit-custom-base-class.cpp @@ -38,20 +38,7 @@ class json_metadata }; template -using json_with_metadata = - nlohmann::basic_json < - std::map, - std::vector, - std::string, - bool, - std::int64_t, - std::uint64_t, - double, - std::allocator, - nlohmann::adl_serializer, - std::vector, - json_metadata - >; +using json_with_metadata = nlohmann::json::with_changed_base_class_t>; TEST_CASE("JSON Node Metadata") { @@ -268,19 +255,7 @@ class visitor_adaptor void do_visit(const Ptr& ptr, const Fnc& fnc) const; }; -using json_with_visitor_t = nlohmann::basic_json < - std::map, - std::vector, - std::string, - bool, - std::int64_t, - std::uint64_t, - double, - std::allocator, - nlohmann::adl_serializer, - std::vector, - visitor_adaptor - >; +using json_with_visitor_t = nlohmann::json::with_changed_base_class_t; template void visitor_adaptor::visit(const Fnc& fnc) const diff --git a/tests/src/unit-udt.cpp b/tests/src/unit-udt.cpp index 5ad72b208..21926b04d 100644 --- a/tests/src/unit-udt.cpp +++ b/tests/src/unit-udt.cpp @@ -684,8 +684,7 @@ static std::ostream& operator<<(std::ostream& os, small_pod l) TEST_CASE("custom serializer for pods" * doctest::test_suite("udt")) { using custom_json = - nlohmann::basic_json; + nlohmann::json::with_changed_json_serializer_t; auto p = udt::small_pod{42, '/', 42}; custom_json const j = p; @@ -703,7 +702,7 @@ TEST_CASE("custom serializer for pods" * doctest::test_suite("udt")) template struct another_adl_serializer; -using custom_json = nlohmann::basic_json; +using custom_json = nlohmann::json::with_changed_json_serializer_t; template struct another_adl_serializer