jonaslagoni / asyncapi-quicktype-template

Template for generating typed models from AsyncAPI message payloads.
6 stars 3 forks source link

C++, from/to_Json work but get_xxx() individual element don't #22

Closed naderafshari closed 2 years ago

naderafshari commented 2 years ago

When we apply from_json(), and then pass the structure to to_Json() and apply dump(), we can verify that we get the JsonString we stared with back. However, the problem we are having is that when we want to retrieve an individual member by using get_xxx() we get null. However, if we individually set_xxx() the member then the get_xxx() returns the correct value. Does anyone know what the problem is?

std::cout << "Hdr JsonSting before FromJson: " << ev.jsonString.get() << std::endl;
nlohmann::json j1 = nlohmann::json::parse(ev.jsonString.get());
nlohmann::from_json(j1,hdr);

// per to_json code
nlohmann::json j3;
j3 = nlohmann::json::object();
j3["correlationId"] = hdr.get_correlation_id();
std::cout <<  "saved correlation_id: " << j3.dump() << std::endl;

// segfault because it's dereferencing a nullptr
//std::cout <<  "get correlationId: " << *hdr.get_correlation_id().get() << std::endl;

nlohmann::json j2;
nlohmann::to_json(j2,hdr);
std::cout << "Hdr JsonString after toJson: " << j2.dump() << std::endl;

nlohmann::json j4 = nlohmann::json::parse(ev.jsonString.get());
std::cout <<  "raw correlation_id: " << j4["correlation_id"] << std::endl;
std::cout <<  "raw datacontenttype: " << j4["datacontenttype"] << std::endl;
hdr.set_correlation_id  (j4["correlation_id"]);
hdr.set_datacontenttype (j4["datacontenttype"]);
hdr.set_dataschema      (j4["dataschema"]);
hdr.set_event_time      (j4["event_time"]);
hdr.set_id              (j4["id"]);
hdr.set_source          (j4["source"]);
hdr.set_specversion     (j4["specversion"]);
hdr.set_subject         (j4["subject"]);
hdr.set_type            (j4["type"]);
std::cout <<  "get correlationId: " << hdr.get_correlation_id().get() << std::endl;
std::cout <<  "get correlationId: " << *hdr.get_correlation_id().get() << std::endl;

Output:

Hdr JsonSting before FromJson: {"correlation_id":"correlation_id_1","datacontenttype":"datacontenttype_1","dataschema":"dataschema_1","event_time":"event_time_1","id":"id_1","source":"source_1","specversion":"specversion_1","subject":"subject_1","type":"type_1"}
saved correlation_id: {"correlationId":null}
Hdr JsonString after toJson: {"correlationId":null,"datacontenttype":"datacontenttype_1","dataschema":"dataschema_1","eventTime":null,"id":"id_1","source":"source_1","specversion":"specversion_1","subject":"subject_1","type":"type_1"}
raw correlation_id: "correlation_id_1"
raw datacontenttype: "datacontenttype_1"
get correlationId: 0xa386f0
get correlationId: correlation_id_1
jonaslagoni commented 2 years ago

Hey @naderafshari, as this is only a problem with the underlying library, please redirect this issue to https://github.com/quicktype/quicktype/issues 🙂