-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Add NLOHMANN_DEFINE_DERIVED_TYPE_* macros #4033
Conversation
Any updates about this PR? |
# Conflicts: # include/nlohmann/detail/macro_scope.hpp # single_include/nlohmann/json.hpp
I just solved the conflict that had arisen in the meantime. |
I am currently finishing #2998 to finally have a stable CI. Once this is merged, please update your branch. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add documentation. See https://github.com/nlohmann/json/blob/develop/docs/mkdocs/docs/api/macros/index.md for a starting point.
(Also, please update from the develop branch and fix the conflicts.) |
since this code now actually modifies the json object from base - what do you guys think about using #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__)) \
} |
# Conflicts: # single_include/nlohmann/json_fwd.hpp
I updated to the latest develop and added documentation, let me know it is ok now |
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated. @rotolof |
1 similar comment
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated. @rotolof |
@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. #4180 (comment) |
@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 |
Comment that line from the config and try again. Sorry for the inconvenience - I did not find the time to fix this yet. |
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. |
# Conflicts: # tests/src/unit-udt_macro.cpp
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? |
Would love to see this released too. |
@nlohmann can you please relate to this changes? Can we release that? It's ready from half a year now... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
Thanks! |
I created the new macros:
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT
to recreate the proposed fix to #2199.
The macros accept as second parameter the base class.
Usage example:
What they do is basically calling the
to_json
/from_json
functions of the base class before serialising/deserialising the class members:Pull request checklist
Read the Contribution Guidelines for detailed information.
include/nlohmann
directory, runmake amalgamate
to create the single-header filessingle_include/nlohmann/json.hpp
andsingle_include/nlohmann/json_fwd.hpp
. The whole process is described here.