nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
42.01k stars 6.63k forks source link

Add NLOHMANN_DEFINE_DERIVED_TYPE_* macros #4033

Open rotolof opened 1 year ago

rotolof commented 1 year ago

I created the new macros:

to recreate the proposed fix to #2199. The macros accept as second parameter the base class.

Usage example:

class A {
  double Aa;
  double Ab;
  NLOHMANN_DEFINE_TYPE_INTRUSIVE(A, Aa, Ab)
};

class B : public A {
  int Ba;
  int Bb;
  NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE(B, A, Ba, Bb)
};

What they do is basically calling the to_json/from_json functions of the base class before serialising/deserialising the class members:

void to_json(nlohmann::json& j, const B& b) {
    nlohmann::to_json(j, static_cast<const A&>(b));
    // ...
}

void from_json(const nlohmann::json& j, B& b) {
    nlohmann::from_json(j, static_cast<A&>(b));
    // ...
}

Pull request checklist

Read the Contribution Guidelines for detailed information.

coveralls commented 1 year ago

Coverage Status

coverage: 100.0%. remained the same when pulling b6e2fd10bba4f1a4432e3090d9161b0e7afc3e1b on rotolof:derived_macros into edffad036d5a93ab5a10f72a7d835eeb0d2948f9 on nlohmann:develop.

hacker-cb commented 11 months ago

Any updates about this PR?

rotolof commented 11 months ago

I just solved the conflict that had arisen in the meantime.

nlohmann commented 11 months ago

I am currently finishing #2998 to finally have a stable CI. Once this is merged, please update your branch.

nlohmann commented 11 months ago

(Also, please update from the develop branch and fix the conflicts.)

eyalk1 commented 11 months ago

since this code now actually modifies the json object from base - what do you guys think about using merge_patch or += instead of = for each member? for example:

#define NLOHMANN_JSON_PAIR(member) {#member, nlohmann_json_t.member},

#define NLOHMANN_INIT_PAIRS(...)                                               \
  NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_PAIR, __VA_ARGS__))

#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_INHERITANCE(derived, base, ...)     \
  void to_json(nlohmann::json &nlohmann_json_j,                                \
               derived const &nlohmann_json_t) {                               \
    to_json(nlohmann_json_j, static_cast<base const &>(nlohmann_json_t));      \
    nlohmann_json_j.merge_patch(nlohmann::json{                                \
        NLOHMANN_JSON_EXPAND(NLOHMANN_INIT_PAIRS(__VA_ARGS__))});              \
  }                                                                            \
  void from_json(nlohmann::json const &nlohmann_json_j,                        \
                 derived &nlohmann_json_t) {                                   \
    from_json(nlohmann_json_j, static_cast<base &>(nlohmann_json_t));          \
    NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \
  }
rotolof commented 10 months ago

I updated to the latest develop and added documentation, let me know it is ok now

github-actions[bot] commented 10 months ago

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @rotolof Please read and follow the Contribution Guidelines.

github-actions[bot] commented 10 months ago

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @rotolof Please read and follow the Contribution Guidelines.

gregmarr commented 10 months ago

@rotolof Looks like you might be using a different version of astyle (perhaps even a newer one) which has different behavior than the current version being used. https://github.com/nlohmann/json/pull/4180#issuecomment-1758627278

rotolof commented 10 months ago

@gregmarr Thanks, I was using macOS brew's astyle that is 3.4.9. I tried with the suggested 3.1 but it does not support squeeze-lines, wtf?

nlohmann commented 10 months ago

Comment that line from the config and try again. Sorry for the inconvenience - I did not find the time to fix this yet.

Aeshal commented 3 months ago

Are there any chances to release that soon? Seems like the most of work has been done and it just hanging almost a year now.

rotolof commented 3 months ago

Are there any chances to release that soon? Seems like the most of work has been done and it just hanging almost a year now.

Sorry, I completely forgot about this until now! Let me check it again.

Aeshal commented 1 month ago

Is this change ok now? Can it possibly be integrated soon?

rotolof commented 1 month ago

Is this change ok now? Can it possibly be integrated soon?

I think that everything is fine, I had updated with the latest develop and all checks had passed, is anything missing @nlohmann?