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

Add serialization-only user defined type macros #3816

Merged
merged 7 commits into from
Nov 26, 2023

Conversation

ZeronSix
Copy link
Contributor

@ZeronSix ZeronSix commented Nov 2, 2022

This pull requests adds NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE and NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE macros.

Suppose we have a non-default-constructible type person and another type that contains a field of type person. For example:

#include <nlohmann/json.hpp>

#include <string>
#include <vector>

struct person {
    std::string name;
    int age;

    person(std::string name, int age)
        : name{std::move(name)}
        , age{age}
    {}
};

struct persons {
    std::vector<person> list;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(person, name, age);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(persons, list);

An attempt to use the NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE macro to generate serialization/deserialization code will lead to a compilation error:

test.cpp: In function ‘void from_json(const json&, persons&)’:
include/nlohmann/detail/macro_scope.hpp:385:81: error: no matching function for call to ‘nlohmann::json_abi_v3_11_2::basic_json<>::get_to(std::vector<person>&) const’
  385 | #define NLOHMANN_JSON_FROM(v1) nlohmann_json_j.at(#v1).get_to(nlohmann_json_t.v1);

README.md provides an example how to fix this by providing a specialization of adl_serializer. However, this requires writing quite a bit of code and might even be unnecessary in cases when we only need serialization without deserialization.

In this situations, we can use added macros to generate only to_json functions.

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(person, name, age);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(persons, list);

Pull request checklist

Read the Contribution Guidelines for detailed information.

  • Changes are described in the pull request, or an existing issue is referenced.
  • The test suite compiles and runs without error.
  • Code coverage is 100%. Test cases can be added by editing the test suite.
  • The source code is amalgamated; that is, after making changes to the sources in the include/nlohmann directory, run make amalgamate to create the single-header files single_include/nlohmann/json.hpp and single_include/nlohmann/json_fwd.hpp. The whole process is described here.

Please don't

  • The C++11 support varies between different compilers and versions. Please note the list of supported compilers. Some compilers like GCC 4.7 (and earlier), Clang 3.3 (and earlier), or Microsoft Visual Studio 13.0 and earlier are known not to work due to missing or incomplete C++11 support. Please refrain from proposing changes that work around these compiler's limitations with #ifdefs or other means.
  • Specifically, I am aware of compilation problems with Microsoft Visual Studio (there even is an issue label for this kind of bug). I understand that even in 2016, complete C++11 support isn't there yet. But please also understand that I do not want to drop features or uglify the code just to make Microsoft's sub-standard compiler happy. The past has shown that there are ways to express the functionality such that the code compiles with the most recent MSVC - unfortunately, this is not the main objective of the project.
  • Please refrain from proposing changes that would break JSON conformance. If you propose a conformant extension of JSON to be supported by the library, please motivate this extension.
  • Please do not open pull requests that address multiple issues.

@coveralls
Copy link

coveralls commented Nov 2, 2022

Coverage Status

coverage: 100.0%. remained the same when pulling ba7f94d on ZeronSix:only-serialize-macros into edffad0 on nlohmann:develop.

Copy link
Owner

@nlohmann nlohmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nlohmann
Copy link
Owner

(Also, please update from the develop branch as it has a working CI now.)

@nlohmann nlohmann added the please rebase Please rebase your branch to origin/develop label Sep 24, 2023
@ZeronSix
Copy link
Contributor Author

ZeronSix commented Oct 2, 2023

I also noticed that the source code highlighting is a bit off in these pages, see #4169.

@ZeronSix ZeronSix force-pushed the only-serialize-macros branch 2 times, most recently from f0843f4 to a2d5270 Compare October 2, 2023 11:48
Copy link
Owner

@nlohmann nlohmann left a 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.

@nlohmann nlohmann added review needed It would be great if someone could review the proposed changes. and removed please rebase Please rebase your branch to origin/develop labels Oct 5, 2023
@nlohmann nlohmann added this to the Release 3.11.3 milestone Oct 5, 2023
@nlohmann nlohmann merged commit 360ce45 into nlohmann:develop Nov 26, 2023
111 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation L review needed It would be great if someone could review the proposed changes. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants