nlohmann / json

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

I want the data field to be empty serialized and deserialized to each other #3866

Closed Cdreamfly closed 1 year ago

Cdreamfly commented 1 year ago

Description

class Data{
public:
    NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Data)
};
class test{
public:
    NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(test,cmd,data)
    std::string cmd;
    Data data;
};

Reproduction steps

std::string str = "{\n"
                      "    \"cmd\": \"TtsPlay\", \n"
                      "    \"data\": { }\n"
                      "}";
    json js = json::parse(str);
    test t = js;
    js = t;
    std::cout << js.dump() << std::endl;

Expected vs. actual results

I want the data field to be empty serialized and deserialized to each other

Minimal code example

No response

Error messages

error: expected unqualified-id before ‘;’ token
 2733 | #define NLOHMANN_JSON_TO(v1) nlohmann_json_j[#v1] = nlohmann_json_t.v1;

Compiler and operating system

gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)

Library version

JSON for Modern C++ version 3.11.2

Validation

nlohmann commented 1 year ago

What is your goal with

NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Data)

? As per https://json.nlohmann.me/api/macros/nlohmann_define_type_intrusive/, there must be a member given.

Can you give an example how the JSON you want to generate should look like?

nlohmann commented 1 year ago

(see also https://github.com/nlohmann/json/discussions/3865#discussioncomment-4370686)

nlohmann commented 1 year ago

Do you need further assistance with this issue?