Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Serialize derived class to JSON object? #2199

Closed
heartfly opened this issue Jun 18, 2020 · 4 comments
Closed

How to Serialize derived class to JSON object? #2199

heartfly opened this issue Jun 18, 2020 · 4 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@heartfly
Copy link

heartfly commented Jun 18, 2020

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.

@heartfly heartfly changed the title How to serialize subclasses to JSON object? How to derived class to JSON object? Jun 18, 2020
@nlohmann
Copy link
Owner

This should work:

#include <iostream>
#include "json.hpp"

using json = nlohmann::json;

struct A {
    double Aa;
    double Ab;
};

struct B : public A{
    int Ba;
    int Bb;
};

void to_json( nlohmann::json& JSON, const A& a ) {
    JSON = { {"Aa", a.Aa}, {"Ab", a.Ab} };
}
void to_json( nlohmann::json& JSON, const B& b ) {
    nlohmann::to_json(JSON, static_cast<A>(b));
    JSON.update({ {"Ba", b.Ba}, {"Bb", b.Bb} });
}

int main() {
    A a;
    a.Aa = 1.11;
    a.Ab = 2.22;
    
    B b;
    b.Aa = 1.1;
    b.Ab = 2.2;
    b.Ba = 3;
    b.Bb = 4;
    
    json ja = a;
    std::cout << ja << std::endl;
    
    json jb = b;
    std::cout << jb << std::endl;
}

Output:

{"Aa":1.11,"Ab":2.22}
{"Aa":1.1,"Ab":2.2,"Ba":3,"Bb":4}

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jun 18, 2020
@heartfly
Copy link
Author

thx.
but how can I call the super class's from_json?
I try this code:

    void from_json( const nlohmann::json& JSON, A& a ) {
        JSON.at("Aa").get_to(a.Aa);
        JSON.at("Ab").get_to(a.Ab);
    }
    void from_json( const nlohmann::json& JSON, B& b ) {
        nlohmann::from_json(JSON, static_cast<A>(b));
        JSON.at("Ba").get_to(b.Ba);
        JSON.at("Ba").get_to(b.Bb);
    }

but got an error

  • No matching function for call to object of type 'const nlohmann::detail::from_json_fn'.

@heartfly heartfly changed the title How to derived class to JSON object? How to Serialize derived class to JSON object? Jun 18, 2020
@nlohmann
Copy link
Owner

The error message gives a hint:

json.hpp:3604:10: Candidate function [with BasicJsonType = nlohmann::basic_json<std::map, std::vector, std::__1::basic_string<char>, bool, long long, unsigned long long, double, std::allocator, adl_serializer, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >, T = A] not viable: expects an l-value for 2nd argument

Changing the second argument to a reference works:

void from_json( const nlohmann::json& JSON, B& b ) {
    nlohmann::from_json(JSON, static_cast<A&>(b));
    JSON.at("Ba").get_to(b.Ba);
    JSON.at("Bb").get_to(b.Bb);
}

@heartfly
Copy link
Author

It works! thank you @nlohmann

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants