Skip to content

NLOHMANN_DEFINE_TYPE_* fails with zero members #4041

@dawinaj

Description

@dawinaj

Description

I found it annoying when working with multiple tiny polymorphic classes, trying to provide identical interface for each of them, that I can't use the macro for a class that has no members to (de)serialize.

Reproduction steps

Use (probably) any of the NLOHMANN_DEFINE_TYPE_* macros with only first argument and no members.
F.e. NLOHMANN_DEFINE_TYPE_INTRUSIVE(MyClass)

Expected vs. actual results

Expected:

friend void to_json(nlohmann::json&, const MyClass&) {}
friend void from_json(const nlohmann::json&, MyClass&) {}

Actual:

friend void to_json(nlohmann::json& nlohmann_json_j, const MyClass& nlohmann_json_t) {
	nlohmann_json_j[] = nlohmann_json_t.;
}
friend void from_json(const nlohmann::json& nlohmann_json_j, MyClass& nlohmann_json_t) {
	nlohmann_json_j.at().get_to(nlohmann_json_t.);
}

Minimal code example

class MyClass
{
	NLOHMANN_DEFINE_TYPE_INTRUSIVE(MyClass);
};

Error messages

Error	C2661	'nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::at': no overloaded function takes 0 arguments
Error	C2661	'nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::at': no overloaded function takes 0 arguments
Error	C2661	'nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::at': no overloaded function takes 0 arguments
Error	C2661	'nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::at': no overloaded function takes 0 arguments
Error	C2059	syntax error: ')'
Error	C2059	syntax error: ')'
Error	C2059	syntax error: ')'
Error	C2059	syntax error: ')'
Error	C2059	syntax error: ']'
Error	C2059	syntax error: ']'
Error	C2059	syntax error: ']'
Error	C2059	syntax error: ']'

Compiler and operating system

Microsoft Visual C++ 2022

Library version

3.11.2

Validation

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions