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

Add helper types to make it easier to create a basic_json type with modified template parameters #3898

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
14 changes: 1 addition & 13 deletions docs/examples/json_base_class_t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::uint8_t>,
visitor_adaptor_with_metadata
>;
using json = nlohmann::json::with_changed_base_class_t<visitor_adaptor_with_metadata>;

template <class Fnc>
void visitor_adaptor_with_metadata::visit(const Fnc& fnc) const
Expand Down
59 changes: 59 additions & 0 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,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<basic_json>;

////////////////////////////////////////////////////////////////////////////////
// utility templates to create a json type with different template parameters //
////////////////////////////////////////////////////////////////////////////////

/// Json type using a different type for storing objects
template<template<typename, typename, typename...> class ObjectType2>
using with_changed_object_t = basic_json<ObjectType2, ArrayType, StringType, BooleanType,
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;

/// Json type using a different type for storing arrays
template<template<typename, typename...> class ArrayType2>
using with_changed_array_t = basic_json<ObjectType, ArrayType2, StringType, BooleanType,
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;

/// Json type using a different type for storing strings
template<class StringType2>
using with_changed_string_t = basic_json<ObjectType, ArrayType, StringType2, BooleanType,
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;

/// Json type using a different type for storing booleans
template<class BooleanType2>
using with_changed_boolean_t = basic_json<ObjectType, ArrayType, StringType, BooleanType2,
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;

/// Json type using a different type for storing signed integers
template<class NumberIntegerType2>
using with_changed_integer_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
NumberIntegerType2, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;

/// Json type using a different type for storing unsigned integers
template<class NumberUnsignedType2>
using with_changed_unsigned_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
NumberIntegerType, NumberUnsignedType2, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;

/// Json type using a different type for storing floating point numbers
template<class NumberFloatType2>
using with_changed_float_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
NumberIntegerType, NumberUnsignedType, NumberFloatType2, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;

/// Json type using a different type as base allocator
template<template<typename> class AllocatorType2>
using with_changed_allocator_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType2, JSONSerializer, BinaryType, CustomBaseClass>;

/// Json type using a different type as json serializer
template<template<typename, typename = void> class JSONSerializer2>
using with_changed_json_serializer_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer2, BinaryType, CustomBaseClass>;

/// Json type using a different type for storing binary data
template<class BinaryType2>
using with_changed_binary_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType2, CustomBaseClass>;

/// Json type using a different type as base class
template<class CustomBaseClass2>
using with_changed_base_class_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass2>;

////////////////
// exceptions //
////////////////
Expand Down
59 changes: 59 additions & 0 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19391,6 +19391,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<basic_json>;

////////////////////////////////////////////////////////////////////////////////
// utility templates to create a json type with different template parameters //
////////////////////////////////////////////////////////////////////////////////

/// Json type using a different type for storing objects
template<template<typename, typename, typename...> class ObjectType2>
using with_changed_object_t = basic_json<ObjectType2, ArrayType, StringType, BooleanType,
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;

/// Json type using a different type for storing arrays
template<template<typename, typename...> class ArrayType2>
using with_changed_array_t = basic_json<ObjectType, ArrayType2, StringType, BooleanType,
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;

/// Json type using a different type for storing strings
template<class StringType2>
using with_changed_string_t = basic_json<ObjectType, ArrayType, StringType2, BooleanType,
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;

/// Json type using a different type for storing booleans
template<class BooleanType2>
using with_changed_boolean_t = basic_json<ObjectType, ArrayType, StringType, BooleanType2,
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;

/// Json type using a different type for storing signed integers
template<class NumberIntegerType2>
using with_changed_integer_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
NumberIntegerType2, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;

/// Json type using a different type for storing unsigned integers
template<class NumberUnsignedType2>
using with_changed_unsigned_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
NumberIntegerType, NumberUnsignedType2, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;

/// Json type using a different type for storing floating point numbers
template<class NumberFloatType2>
using with_changed_float_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
NumberIntegerType, NumberUnsignedType, NumberFloatType2, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>;

/// Json type using a different type as base allocator
template<template<typename> class AllocatorType2>
using with_changed_allocator_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType2, JSONSerializer, BinaryType, CustomBaseClass>;

/// Json type using a different type as json serializer
template<template<typename, typename = void> class JSONSerializer2>
using with_changed_json_serializer_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer2, BinaryType, CustomBaseClass>;

/// Json type using a different type for storing binary data
template<class BinaryType2>
using with_changed_binary_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType2, CustomBaseClass>;

/// Json type using a different type as base class
template<class CustomBaseClass2>
using with_changed_base_class_t = basic_json<ObjectType, ArrayType, StringType, BooleanType,
NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass2>;

////////////////
// exceptions //
////////////////
Expand Down
27 changes: 3 additions & 24 deletions tests/src/unit-allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,7 @@ TEST_CASE("bad_alloc")
SECTION("bad_alloc")
{
// create JSON type using the throwing allocator
using bad_json = nlohmann::basic_json<std::map,
std::vector,
std::string,
bool,
std::int64_t,
std::uint64_t,
double,
bad_allocator>;
using bad_json = nlohmann::json::with_changed_allocator_t<bad_allocator>;

// creating an object should throw
CHECK_THROWS_AS(bad_json(bad_json::value_t::object), std::bad_alloc&);
Expand Down Expand Up @@ -123,14 +116,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<std::map,
std::vector,
std::string,
bool,
std::int64_t,
std::uint64_t,
double,
my_allocator>;
using my_json = nlohmann::json::with_changed_allocator_t<my_allocator>;

SECTION("class json_value")
{
Expand Down Expand Up @@ -247,14 +233,7 @@ TEST_CASE("bad my_allocator::construct")
{
SECTION("my_allocator::construct doesn't forward")
{
using bad_alloc_json = nlohmann::basic_json<std::map,
std::vector,
std::string,
bool,
std::int64_t,
std::uint64_t,
double,
allocator_no_forward>;
using bad_alloc_json = nlohmann::json::with_changed_allocator_t<allocator_no_forward>;

bad_alloc_json j;
j["test"] = bad_alloc_json::array_t();
Expand Down
11 changes: 1 addition & 10 deletions tests/src/unit-alt-string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,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<alt_string>;


bool operator<(const char* op1, const alt_string& op2) noexcept
Expand Down
29 changes: 2 additions & 27 deletions tests/src/unit-custom-base-class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,7 @@ class json_metadata
};

template<class T>
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<std::uint8_t>,
json_metadata<T>
>;
using json_with_metadata = nlohmann::json::with_changed_base_class_t<json_metadata<T>>;

TEST_CASE("JSON Node Metadata")
{
Expand Down Expand Up @@ -215,19 +202,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<std::uint8_t>,
visitor_adaptor
>;
using json_with_visitor_t = nlohmann::json::with_changed_base_class_t<visitor_adaptor>;


template <class Fnc>
Expand Down
5 changes: 2 additions & 3 deletions tests/src/unit-udt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,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<std::map, std::vector, std::string, bool,
std::int64_t, std::uint64_t, double, std::allocator, pod_serializer>;
nlohmann::json::with_changed_json_serializer_t<pod_serializer>;

auto p = udt::small_pod{42, '/', 42};
custom_json const j = p;
Expand All @@ -659,7 +658,7 @@ TEST_CASE("custom serializer for pods" * doctest::test_suite("udt"))
template <typename T, typename>
struct another_adl_serializer;

using custom_json = nlohmann::basic_json<std::map, std::vector, std::string, bool, std::int64_t, std::uint64_t, double, std::allocator, another_adl_serializer>;
using custom_json = nlohmann::json::with_changed_json_serializer_t<another_adl_serializer>;

template <typename T, typename>
struct another_adl_serializer
Expand Down
Loading