nlohmann/json

NLOHMANN_DEFINE_TYPE_* fails with zero members

Open

#4041 aperta il 23 mag 2023

Vedi su GitHub
 (7 commenti) (0 reazioni) (0 assegnatari)C++ (7395 fork)batch import
confirmedgood first issuekind: bug

Metriche repository

Star
 (49.695 star)
Metriche merge PR
 (Merge medio 68g 5h) (26 PR mergiate in 30 g)

Descrizione

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

Guida contributor