相关文章推荐
讲道义的米饭  ·  PostgreSql 的PL/pgSQL ...·  2 年前    · 
刚分手的马铃薯  ·  java - ...·  2 年前    · 
调皮的小笼包  ·  .NET ...·  2 年前    · 
霸气的胡萝卜  ·  python3 ...·  2 年前    · 

How to Serialize derived class to JSON object? #2199

@heartfly

Description

@heartfly

What do you want to achieve?

I'm trying to serialize my objects. They look like this (overly simplified):

class A {
 double Aa;
 double Ab;
class B : public A{
  int Ba;
  int Bb;

What have you tried?

I'm trying to create a JSON object like this:

B = {
 "Aa": 1.0,
 "Ab": 2.0,
 "Ba": 3,
 "Bb": 4

Can you provide a small code example?

inline void to_json( nlohmann::json& JSON, const A& a ) {
  JSON = { "Aa", a.Aa, "Ab", a.Ab };
inline void to_json( nlohmann::json& JSON, const B& b ) {
 // how to call the base class(A)'s to_json and from_json function?
  JSON = { "Ba", b.Ba, "Bb", b.Bb };

Please help me know how to call the base class's to_json and from_json function.

Describe which system (OS, compiler) you are using.
LLVM 3.8.1

Describe which version of the library you are using (release version, develop branch).
Fairly recent from master branch.