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 2 months ago • 4 comments

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