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

New macro for named JSON convertor generation #4092

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from

Conversation

radistmorse
Copy link

This patch adds 4 new NLOHMANN_DEFINE_TYPE_<***>_WITH_NAMES macros. They behave the same way as the ones that are there, but require explicit JSON names. Useful for the situation when the fields in the class are following the naming convention that you do not want to expose to JSON, e.g.

    class address {
      private:
        std::string m_street;
        int m_housenumber;
        int m_postcode;

      public:
        NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_NAMES(address, "street", m_street, "housenumber", m_housenumber, "postcode", m_postcode)
    };

or if the name in JSON cannot be used as the field name because it is reserved, e.g.

    struct weird {
        std::string static_comp;
        std::string class_type;
        std::string public_field;
    };
    NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_NAMES(weird, "static", static_comp, "class", class_type, "public", public_field)

Also, this patch includes the unit tests for the new macros, and the update for the README to describe the new and existing macros better.


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.

@coveralls
Copy link

Coverage Status

coverage: 100.0%. remained the same when pulling 0c4d816 on radistmorse:patch-1 into 5d27543 on nlohmann:develop.

@nlohmann nlohmann added the please rebase Please rebase your branch to origin/develop label Sep 24, 2023
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.

Please update to the latest develop branch and resolve conflicts.

@github-actions
Copy link

github-actions bot commented Oct 4, 2023

🔴 Amalgamation check failed! 🔴

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

@Hackerl
Copy link

Hackerl commented Mar 8, 2024

This feature is very good, when can it be merged?

@nlohmann
Copy link
Owner

nlohmann commented Mar 8, 2024

When the CI is not failing any more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation L please rebase Please rebase your branch to origin/develop tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants