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 nested classes to semi-flat JSON object? #1859

Closed
jlconlin opened this issue Dec 5, 2019 · 2 comments
Closed

How to serialize nested classes to semi-flat JSON object? #1859

jlconlin opened this issue Dec 5, 2019 · 2 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@jlconlin
Copy link

jlconlin commented Dec 5, 2019

  • Describe what you want to achieve.
    I'm trying to serialize my objects. They look like this (overly simplified):
struct A{
 double Aa;
 double Ab;
};

struct B{
  int Ba;
  int Bb;
};

struct super{
  A myA;
  B myB;
};

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

super = {
 "Aa": 1.0,
 "Ab": 2.0,
 "Ba": 3,
 "Bb": 4
}
  • Describe what you tried.
    I have functions like this:
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 ) {
  JSON = { "Ba", b.Ba, "Bb", b.Bb };
}
inline void to_json( nlohmann::json& JSON, const super& sup ) {
  JSON = {
   sup.myA, 
   sup.myB
  };
}

I'd like to have a single object with keys: "Aa", "Ab", "Ba", "Bb". Instead I get an array of two objects:

super = [
  { "Aa": 1.0, "Ab": 2.0 },
  { "Ba": 3, "Bb": 4 }
]

Please help me know what I'm doing wrong.

  • 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.

@nlohmann
Copy link
Owner

nlohmann commented Dec 8, 2019

The braces are interpreted as an array (there is no real nice syntax for C++...). You need additional braces - a list of pairs is interpreted as object:

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 ) {
  JSON = { {"Ba", b.Ba}, {"Bb", b.Bb} };
}
inline void to_json( nlohmann::json& JSON, const super& sup ) {
  JSON = sup.myA;
  JSON.update(sup.myB);
}

Function update is used to merge two objects.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Dec 8, 2019
@jlconlin
Copy link
Author

jlconlin commented Dec 9, 2019

update is exactly what I needed. Thanks!

@jlconlin jlconlin closed this as completed Dec 9, 2019
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