nlohmann / nlohmann/json

NLOHMANN_DEFINE_TYPE_* fails with zero members

Open
#4,041 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

confirmed kind: bug solution: proposed fix
Dominant language
C++
Stars
50.6k
Forks
7.5k
Avg merge
4d 17h
Merged PRs (30d)
58

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Search the repository for the definitions of NLOHMANN_DEFINE_TYPE_INTRUSIVE and the related NLOHMANN_DEFINE_TYPE_* macros, then reproduce the empty-class example with C++ and the reported compiler. Done means the zero-member form compiles and produces usable empty serialization functions, while existing member forms continue to pass the unit tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.