What's the best way to serialize to json a class method? #3943
Answered
by
nlohmann
johnsturgeon
asked this question in
Q&A
-
Let's say I have for example this class: class Team {
public:
std::string team_name;
int rushing_yards;
int passing_yards;
// rushing + passing yards
int total_yards();
}; How can I represent that as: { "team_name": "Panthers", "rushing_yards": 100, "passing_yards": 50, "total_yards": 150 } |
Beta Was this translation helpful? Give feedback.
Answered by
nlohmann
Feb 12, 2023
Replies: 1 comment 1 reply
-
See https://json.nlohmann.me/features/arbitrary_types/ - you need to define a |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
johnsturgeon
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://json.nlohmann.me/features/arbitrary_types/ - you need to define a
to_json
function in the same namespace of your class. The macros do not work as they would not be able to serializetotal_yards
.