Skip to content
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
1 change: 1 addition & 0 deletions cmake/clang_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ set(CLANG_CXXFLAGS
-Wno-covered-switch-default
-Wno-unsafe-buffer-usage
-Wno-missing-noreturn
-Wno-gnu-zero-variadic-macro-arguments
)
22 changes: 22 additions & 0 deletions include/nlohmann/detail/macro_scope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,28 @@
template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { nlohmann::to_json(nlohmann_json_j, static_cast<const BaseType &>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) }

/*!
@brief macro
@def NLOHMANN_DEFINE_EMPTY_TYPE_INTRUSIVE
@since version 3.12.0
*/
#define NLOHMANN_DEFINE_EMPTY_TYPE_INTRUSIVE(Type) \
template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
friend void to_json(BasicJsonType& nlohmann_json_j, const Type&) { nlohmann_json_j = BasicJsonType::object(); } \
template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
friend void from_json(const BasicJsonType&, Type&) noexcept { }

/*!
@brief macro
@def NLOHMANN_DEFINE_EMPTY_TYPE_NON_INTRUSIVE
@since version 3.12.0
*/
#define NLOHMANN_DEFINE_EMPTY_TYPE_NON_INTRUSIVE(Type) \
template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
void to_json(BasicJsonType& nlohmann_json_j, const Type&) { nlohmann_json_j = BasicJsonType::object(); } \
template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
void from_json(const BasicJsonType&, Type&) noexcept { }

// inspired from https://stackoverflow.com/a/26745591
// allows calling any std function as if (e.g., with begin):
// using std::begin; begin(x);
Expand Down
22 changes: 22 additions & 0 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2908,6 +2908,28 @@ JSON_HEDLEY_DIAGNOSTIC_POP
template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { nlohmann::to_json(nlohmann_json_j, static_cast<const BaseType &>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) }

/*!
@brief macro
@def NLOHMANN_DEFINE_EMPTY_TYPE_INTRUSIVE
@since version 3.12.0
*/
#define NLOHMANN_DEFINE_EMPTY_TYPE_INTRUSIVE(Type) \
template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
friend void to_json(BasicJsonType& nlohmann_json_j, const Type&) { nlohmann_json_j = BasicJsonType::object(); } \
template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
friend void from_json(const BasicJsonType&, Type&) noexcept { }

/*!
@brief macro
@def NLOHMANN_DEFINE_EMPTY_TYPE_NON_INTRUSIVE
@since version 3.12.0
*/
#define NLOHMANN_DEFINE_EMPTY_TYPE_NON_INTRUSIVE(Type) \
template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
void to_json(BasicJsonType& nlohmann_json_j, const Type&) { nlohmann_json_j = BasicJsonType::object(); } \
template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
void from_json(const BasicJsonType&, Type&) noexcept { }

// inspired from https://stackoverflow.com/a/26745591
// allows calling any std function as if (e.g., with begin):
// using std::begin; begin(x);
Expand Down
33 changes: 33 additions & 0 deletions tests/src/unit-udt_macro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,3 +707,36 @@ TEST_CASE_TEMPLATE("Serialization of non-default-constructible classes via NLOHM
R"([{"age":1,"hair_color":"brown","name":"Erik"},{"age":2,"hair_color":"black","name":"Kyle"}])"));
}
}

namespace emptys
{
class empty_intrusive
{
NLOHMANN_DEFINE_EMPTY_TYPE_INTRUSIVE(empty_intrusive)
};

class empty_non_intrusive
{
};

NLOHMANN_DEFINE_EMPTY_TYPE_NON_INTRUSIVE(empty_non_intrusive)

} // namespace emptys

TEST_CASE_TEMPLATE("Serialization/deserialization of classes with no member variables", T,
emptys::empty_intrusive,
emptys::empty_non_intrusive)
{
SECTION("empty")
{
{
T empty;
std::string const s = json(empty).dump();
CHECK(s == "{}");

nlohmann::json const json_empty = nlohmann::json::parse(s);
T empty2;
json_empty.get_to(empty2);
}
}
}
Loading