json icon indicating copy to clipboard operation
json copied to clipboard

Allow ordered_json in NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE macro

Open ArashPartow opened this issue 11 months ago • 4 comments
trafficstars

Allow for ordered_json type to be used with the define macros provided in macro_scope.hpp.

Usage:

#include <cstdio>

#include <nlohmann/json.hpp>

struct mystruct
{
    int x = 1;
    double y = 2.0;
    std::string z = "three";
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE
(
    mystruct,
    z,
    y,
    x
);

int main()
{
    mystruct ms;

    const auto msjson = nlohmann::to_string(nlohmann::ordered_json{{ "mystruct", ms }});

    const std::string expected = R"({"mystruct":{"z":"three","y":2.0,"x":1}})";

    printf("%s\n",msjson  .c_str());
    printf("%s\n",expected.c_str());

    return 0;
}

ArashPartow avatar Dec 06 '24 06:12 ArashPartow

🔴 Amalgamation check failed! 🔴

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

github-actions[bot] avatar Dec 06 '24 07:12 github-actions[bot]

Coverage Status

coverage: 99.649%. remained the same when pulling 4ac8d89d00d67808c58022131cb6ec001aeea153 on ArashPartow:arashpartow/ordered_json_NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE into a006a7a48bb30a247f0344b788c62c2806edd90b on nlohmann:develop.

coveralls avatar Dec 06 '24 07:12 coveralls

This version in enum handling is what they should really be:

    template<typename BasicJsonType>                                                            \
    inline void from_json(const BasicJsonType& j, ENUM_TYPE& e)                                 \

gregmarr avatar Dec 06 '24 16:12 gregmarr

See also #2843 and #2532

gregmarr avatar Dec 06 '24 16:12 gregmarr

@nlohmann Happy for you to close this PR, as https://github.com/nlohmann/json/pull/4517 might be the more appropriate PR to make the necessary changes.

ArashPartow avatar Dec 10 '24 22:12 ArashPartow

This pull request has been marked as stale because it has had no activity for 30 days. While we won’t close it automatically, we encourage you to update or comment if it is still relevant. Keeping pull requests active and up-to-date helps us review and merge changes more efficiently. Thank you for your contributions!

github-actions[bot] avatar Jan 18 '25 00:01 github-actions[bot]

Closed in favor of #4597.

nlohmann avatar Jan 18 '25 22:01 nlohmann