nlohmann / json

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

New macro for named JSON convertor generation #4092

Open radistmorse opened 11 months ago

radistmorse commented 11 months ago

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.

coveralls commented 11 months ago

Coverage Status

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

github-actions[bot] commented 9 months ago

🔴 Amalgamation check failed! 🔴

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

Hackerl commented 4 months ago

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

nlohmann commented 4 months ago

When the CI is not failing any more.