From 3eeffcda8749aea4b7129c8ceb7b967ba9b6a319 Mon Sep 17 00:00:00 2001 From: Dharshika-11 Date: Fri, 12 Jun 2026 14:40:27 +0530 Subject: [PATCH 1/2] fix(cpp-httplib-server): handle missing optional JSON properties --- .../cpp-httplib-server/model-header.mustache | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/cpp-httplib-server/model-header.mustache b/modules/openapi-generator/src/main/resources/cpp-httplib-server/model-header.mustache index c109c8ec073c..ff9068977203 100644 --- a/modules/openapi-generator/src/main/resources/cpp-httplib-server/model-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-httplib-server/model-header.mustache @@ -225,9 +225,27 @@ public: {{/isArray}}{{/isEnum}}{{/vars}} - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE({{vendorExtensions.modelClassName}}, - {{#vars}} {{nameInCamelCase}}{{^-last}},{{/-last}}{{/vars}}) +friend void to_json(nlohmann::json& j, const {{vendorExtensions.modelClassName}}& o) +{ + {{#vars}} + j["{{baseName}}"] = o.{{nameInCamelCase}}; + {{/vars}} +} + +friend void from_json(const nlohmann::json& j, {{vendorExtensions.modelClassName}}& o) +{ + {{#requiredVars}} + j.at("{{baseName}}").get_to(o.{{nameInCamelCase}}); + {{/requiredVars}} + + {{#optionalVars}} + if (j.contains("{{baseName}}") && !j.at("{{baseName}}").is_null()) + { + j.at("{{baseName}}").get_to(o.{{nameInCamelCase}}); + } + {{/optionalVars}} +} + private: {{#vars}} From 787238039ab6fc1900a2141f204fe19b5f5a314a Mon Sep 17 00:00:00 2001 From: William Cheng Date: Wed, 7 Oct 2026 19:12:31 +0800 Subject: [PATCH 2/2] update samples --- .../feature-test/models/Address.h | 26 ++++++++- .../feature-test/models/Animal.h | 16 ++++- .../feature-test/models/ArrayTypes.h | 37 +++++++++++- .../feature-test/models/BankAccount.h | 18 +++++- .../feature-test/models/Company.h | 29 +++++++++- .../feature-test/models/CreatedResponse.h | 21 ++++++- .../feature-test/models/CreditCard.h | 18 +++++- .../feature-test/models/Department.h | 21 ++++++- .../feature-test/models/Dog.h | 21 ++++++- .../feature-test/models/Employee.h | 28 ++++++++- .../feature-test/models/EnumTypes.h | 27 ++++++++- .../feature-test/models/ErrorResponse.h | 24 +++++++- .../feature-test/models/NotFoundResponse.h | 19 +++++- .../models/NullableOptionalTypes.h | 34 ++++++++++- .../feature-test/models/PrimitiveTypes.h | 58 ++++++++++++++++++- .../feature-test/models/SimpleObject.h | 21 ++++++- .../feature-test/models/SuccessResponse.h | 16 ++++- .../models/TestAllParameterTypes200Response.h | 32 +++++++++- .../models/TestApiKeySecurity200Response.h | 17 +++++- .../models/TestBasicSecurity200Response.h | 17 +++++- .../models/TestBearerSecurity200Response.h | 17 +++++- .../models/TestCookieParameters200Response.h | 17 +++++- .../models/TestHeaderParameters200Response.h | 17 +++++- .../models/TestHeaderParameters401Response.h | 17 +++++- .../models/TestOAuth2Security200Response.h | 17 +++++- .../models/TestQueryParameters200Response.h | 17 +++++- .../TestQueryParametersDeepObjectParameter.h | 22 ++++++- .../petstore/models/ApiResponse.h | 27 ++++++++- .../petstore/models/Category.h | 22 ++++++- .../petstore/models/ComplexParamsResponse.h | 42 +++++++++++++- .../petstore/models/DeepObj.h | 27 ++++++++- .../petstore/models/DeepObjBaz.h | 22 ++++++- .../petstore/models/NullableExample.h | 17 +++++- .../petstore/models/Order.h | 42 +++++++++++++- .../cpp-httplib-server/petstore/models/Pet.h | 36 +++++++++++- .../cpp-httplib-server/petstore/models/Tag.h | 22 ++++++- .../cpp-httplib-server/petstore/models/User.h | 52 ++++++++++++++++- 37 files changed, 822 insertions(+), 111 deletions(-) diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/Address.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/Address.h index eaf778a713e4..46530d4123e8 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/Address.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/Address.h @@ -32,9 +32,29 @@ class Address void setCountry(const std::string& country); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(Address, - street, city, zipCode, country) +friend void to_json(nlohmann::json& j, const Address& o) +{ + j["street"] = o.street; + j["city"] = o.city; + j["zipCode"] = o.zipCode; + j["country"] = o.country; +} + +friend void from_json(const nlohmann::json& j, Address& o) +{ + j.at("street").get_to(o.street); + j.at("city").get_to(o.city); + + if (j.contains("zipCode") && !j.at("zipCode").is_null()) + { + j.at("zipCode").get_to(o.zipCode); + } + if (j.contains("country") && !j.at("country").is_null()) + { + j.at("country").get_to(o.country); + } +} + private: std::string street; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/Animal.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/Animal.h index e3f179bc7196..419997622a34 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/Animal.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/Animal.h @@ -28,9 +28,19 @@ class Animal void setType(const std::string& type); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(Animal, - name, type) +friend void to_json(nlohmann::json& j, const Animal& o) +{ + j["name"] = o.name; + j["type"] = o.type; +} + +friend void from_json(const nlohmann::json& j, Animal& o) +{ + j.at("name").get_to(o.name); + j.at("type").get_to(o.type); + +} + private: std::string name; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/ArrayTypes.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/ArrayTypes.h index 2f93f30e1416..df95e21a53c2 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/ArrayTypes.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/ArrayTypes.h @@ -48,9 +48,40 @@ class ArrayTypes void setNestedArray(const std::vector>& nestedArray); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(ArrayTypes, - stringArray, intArray, objectArray, enumArray, nestedArray) +friend void to_json(nlohmann::json& j, const ArrayTypes& o) +{ + j["stringArray"] = o.stringArray; + j["intArray"] = o.intArray; + j["objectArray"] = o.objectArray; + j["enumArray"] = o.enumArray; + j["nestedArray"] = o.nestedArray; +} + +friend void from_json(const nlohmann::json& j, ArrayTypes& o) +{ + + if (j.contains("stringArray") && !j.at("stringArray").is_null()) + { + j.at("stringArray").get_to(o.stringArray); + } + if (j.contains("intArray") && !j.at("intArray").is_null()) + { + j.at("intArray").get_to(o.intArray); + } + if (j.contains("objectArray") && !j.at("objectArray").is_null()) + { + j.at("objectArray").get_to(o.objectArray); + } + if (j.contains("enumArray") && !j.at("enumArray").is_null()) + { + j.at("enumArray").get_to(o.enumArray); + } + if (j.contains("nestedArray") && !j.at("nestedArray").is_null()) + { + j.at("nestedArray").get_to(o.nestedArray); + } +} + private: std::vector stringArray; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/BankAccount.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/BankAccount.h index e5d88115e447..72dc4e875102 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/BankAccount.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/BankAccount.h @@ -30,9 +30,21 @@ class BankAccount void setBankName(const std::string& bankName); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(BankAccount, - paymentType, accountNumber, bankName) +friend void to_json(nlohmann::json& j, const BankAccount& o) +{ + j["paymentType"] = o.paymentType; + j["accountNumber"] = o.accountNumber; + j["bankName"] = o.bankName; +} + +friend void from_json(const nlohmann::json& j, BankAccount& o) +{ + j.at("paymentType").get_to(o.paymentType); + j.at("accountNumber").get_to(o.accountNumber); + j.at("bankName").get_to(o.bankName); + +} + private: std::string paymentType; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/Company.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/Company.h index acb61e0db444..609330127e18 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/Company.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/Company.h @@ -35,9 +35,32 @@ class Company void setMetadata(const std::map& metadata); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(Company, - name, headquarters, departments, metadata) +friend void to_json(nlohmann::json& j, const Company& o) +{ + j["name"] = o.name; + j["headquarters"] = o.headquarters; + j["departments"] = o.departments; + j["metadata"] = o.metadata; +} + +friend void from_json(const nlohmann::json& j, Company& o) +{ + j.at("name").get_to(o.name); + + if (j.contains("headquarters") && !j.at("headquarters").is_null()) + { + j.at("headquarters").get_to(o.headquarters); + } + if (j.contains("departments") && !j.at("departments").is_null()) + { + j.at("departments").get_to(o.departments); + } + if (j.contains("metadata") && !j.at("metadata").is_null()) + { + j.at("metadata").get_to(o.metadata); + } +} + private: std::string name; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/CreatedResponse.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/CreatedResponse.h index b3f94e4cec83..ae8124706474 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/CreatedResponse.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/CreatedResponse.h @@ -31,9 +31,24 @@ class CreatedResponse void setLocation(const std::string& location); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(CreatedResponse, - status, id, location) +friend void to_json(nlohmann::json& j, const CreatedResponse& o) +{ + j["status"] = o.status; + j["id"] = o.id; + j["location"] = o.location; +} + +friend void from_json(const nlohmann::json& j, CreatedResponse& o) +{ + j.at("status").get_to(o.status); + j.at("id").get_to(o.id); + + if (j.contains("location") && !j.at("location").is_null()) + { + j.at("location").get_to(o.location); + } +} + private: std::string status; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/CreditCard.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/CreditCard.h index f53af49ef132..425aed5ee24a 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/CreditCard.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/CreditCard.h @@ -53,9 +53,21 @@ class CreditCard - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(CreditCard, - paymentType, cardNumber, cardType) +friend void to_json(nlohmann::json& j, const CreditCard& o) +{ + j["paymentType"] = o.paymentType; + j["cardNumber"] = o.cardNumber; + j["cardType"] = o.cardType; +} + +friend void from_json(const nlohmann::json& j, CreditCard& o) +{ + j.at("paymentType").get_to(o.paymentType); + j.at("cardNumber").get_to(o.cardNumber); + j.at("cardType").get_to(o.cardType); + +} + private: std::string paymentType; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/Department.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/Department.h index fa133c2bed92..2ea91972176e 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/Department.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/Department.h @@ -32,9 +32,24 @@ class Department void setEmployees(const std::vector& employees); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(Department, - name, manager, employees) +friend void to_json(nlohmann::json& j, const Department& o) +{ + j["name"] = o.name; + j["manager"] = o.manager; + j["employees"] = o.employees; +} + +friend void from_json(const nlohmann::json& j, Department& o) +{ + j.at("name").get_to(o.name); + j.at("manager").get_to(o.manager); + + if (j.contains("employees") && !j.at("employees").is_null()) + { + j.at("employees").get_to(o.employees); + } +} + private: std::string name; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/Dog.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/Dog.h index fc075c18b8bd..ff42ec891936 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/Dog.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/Dog.h @@ -31,9 +31,24 @@ class Dog : public Animal void setBarkVolume(const int& barkVolume); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(Dog, - breed, barkVolume) +friend void to_json(nlohmann::json& j, const Dog& o) +{ + j["breed"] = o.breed; + j["barkVolume"] = o.barkVolume; +} + +friend void from_json(const nlohmann::json& j, Dog& o) +{ + j.at("name").get_to(o.name); + j.at("type").get_to(o.type); + j.at("breed").get_to(o.breed); + + if (j.contains("barkVolume") && !j.at("barkVolume").is_null()) + { + j.at("barkVolume").get_to(o.barkVolume); + } +} + private: std::string breed; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/Employee.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/Employee.h index 1a7bb642a773..8e4e9f528c79 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/Employee.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/Employee.h @@ -37,9 +37,31 @@ class Employee void setSkills(const std::vector& skills); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(Employee, - id, name, email, address, skills) +friend void to_json(nlohmann::json& j, const Employee& o) +{ + j["id"] = o.id; + j["name"] = o.name; + j["email"] = o.email; + j["address"] = o.address; + j["skills"] = o.skills; +} + +friend void from_json(const nlohmann::json& j, Employee& o) +{ + j.at("id").get_to(o.id); + j.at("name").get_to(o.name); + j.at("email").get_to(o.email); + + if (j.contains("address") && !j.at("address").is_null()) + { + j.at("address").get_to(o.address); + } + if (j.contains("skills") && !j.at("skills").is_null()) + { + j.at("skills").get_to(o.skills); + } +} + private: int id; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/EnumTypes.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/EnumTypes.h index 6ea087bf17e3..76bff32acbee 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/EnumTypes.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/EnumTypes.h @@ -156,9 +156,30 @@ class EnumTypes - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(EnumTypes, - stringEnum, numericEnum, statusCode) +friend void to_json(nlohmann::json& j, const EnumTypes& o) +{ + j["stringEnum"] = o.stringEnum; + j["numericEnum"] = o.numericEnum; + j["statusCode"] = o.statusCode; +} + +friend void from_json(const nlohmann::json& j, EnumTypes& o) +{ + + if (j.contains("stringEnum") && !j.at("stringEnum").is_null()) + { + j.at("stringEnum").get_to(o.stringEnum); + } + if (j.contains("numericEnum") && !j.at("numericEnum").is_null()) + { + j.at("numericEnum").get_to(o.numericEnum); + } + if (j.contains("statusCode") && !j.at("statusCode").is_null()) + { + j.at("statusCode").get_to(o.statusCode); + } +} + private: std::optional stringEnum; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/ErrorResponse.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/ErrorResponse.h index 72f143d780e9..c433e339a352 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/ErrorResponse.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/ErrorResponse.h @@ -32,9 +32,27 @@ class ErrorResponse void setDetails(const std::vector& details); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(ErrorResponse, - message, code, details) +friend void to_json(nlohmann::json& j, const ErrorResponse& o) +{ + j["message"] = o.message; + j["code"] = o.code; + j["details"] = o.details; +} + +friend void from_json(const nlohmann::json& j, ErrorResponse& o) +{ + j.at("message").get_to(o.message); + + if (j.contains("code") && !j.at("code").is_null()) + { + j.at("code").get_to(o.code); + } + if (j.contains("details") && !j.at("details").is_null()) + { + j.at("details").get_to(o.details); + } +} + private: std::string message; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/NotFoundResponse.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/NotFoundResponse.h index 9b209b5d3f2e..e27be594ca8d 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/NotFoundResponse.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/NotFoundResponse.h @@ -28,9 +28,22 @@ class NotFoundResponse void setResource(const std::string& resource); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(NotFoundResponse, - error, resource) +friend void to_json(nlohmann::json& j, const NotFoundResponse& o) +{ + j["error"] = o.error; + j["resource"] = o.resource; +} + +friend void from_json(const nlohmann::json& j, NotFoundResponse& o) +{ + j.at("error").get_to(o.error); + + if (j.contains("resource") && !j.at("resource").is_null()) + { + j.at("resource").get_to(o.resource); + } +} + private: std::string error; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/NullableOptionalTypes.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/NullableOptionalTypes.h index 7e5df57ad443..196cdf2d0a9c 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/NullableOptionalTypes.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/NullableOptionalTypes.h @@ -36,9 +36,37 @@ class NullableOptionalTypes void setFieldWithDefault(const int& fieldWithDefault); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(NullableOptionalTypes, - requiredField, optionalField, nullableField, optionalNullableField, fieldWithDefault) +friend void to_json(nlohmann::json& j, const NullableOptionalTypes& o) +{ + j["requiredField"] = o.requiredField; + j["optionalField"] = o.optionalField; + j["nullableField"] = o.nullableField; + j["optionalNullableField"] = o.optionalNullableField; + j["fieldWithDefault"] = o.fieldWithDefault; +} + +friend void from_json(const nlohmann::json& j, NullableOptionalTypes& o) +{ + j.at("requiredField").get_to(o.requiredField); + + if (j.contains("optionalField") && !j.at("optionalField").is_null()) + { + j.at("optionalField").get_to(o.optionalField); + } + if (j.contains("nullableField") && !j.at("nullableField").is_null()) + { + j.at("nullableField").get_to(o.nullableField); + } + if (j.contains("optionalNullableField") && !j.at("optionalNullableField").is_null()) + { + j.at("optionalNullableField").get_to(o.optionalNullableField); + } + if (j.contains("fieldWithDefault") && !j.at("fieldWithDefault").is_null()) + { + j.at("fieldWithDefault").get_to(o.fieldWithDefault); + } +} + private: std::string requiredField; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/PrimitiveTypes.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/PrimitiveTypes.h index 89894663074b..8bd6566cfae7 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/PrimitiveTypes.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/PrimitiveTypes.h @@ -47,9 +47,61 @@ class PrimitiveTypes void setPasswordField(const std::string& passwordField); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(PrimitiveTypes, - stringField, intField, longField, floatField, doubleField, boolField, byteField, binaryField, dateField, dateTimeField, passwordField) +friend void to_json(nlohmann::json& j, const PrimitiveTypes& o) +{ + j["stringField"] = o.stringField; + j["intField"] = o.intField; + j["longField"] = o.longField; + j["floatField"] = o.floatField; + j["doubleField"] = o.doubleField; + j["boolField"] = o.boolField; + j["byteField"] = o.byteField; + j["binaryField"] = o.binaryField; + j["dateField"] = o.dateField; + j["dateTimeField"] = o.dateTimeField; + j["passwordField"] = o.passwordField; +} + +friend void from_json(const nlohmann::json& j, PrimitiveTypes& o) +{ + j.at("stringField").get_to(o.stringField); + j.at("intField").get_to(o.intField); + j.at("boolField").get_to(o.boolField); + + if (j.contains("longField") && !j.at("longField").is_null()) + { + j.at("longField").get_to(o.longField); + } + if (j.contains("floatField") && !j.at("floatField").is_null()) + { + j.at("floatField").get_to(o.floatField); + } + if (j.contains("doubleField") && !j.at("doubleField").is_null()) + { + j.at("doubleField").get_to(o.doubleField); + } + if (j.contains("byteField") && !j.at("byteField").is_null()) + { + j.at("byteField").get_to(o.byteField); + } + if (j.contains("binaryField") && !j.at("binaryField").is_null()) + { + j.at("binaryField").get_to(o.binaryField); + } + if (j.contains("dateField") && !j.at("dateField").is_null()) + { + j.at("dateField").get_to(o.dateField); + } + if (j.contains("dateTimeField") && !j.at("dateTimeField").is_null()) + { + j.at("dateTimeField").get_to(o.dateTimeField); + } + if (j.contains("passwordField") && !j.at("passwordField").is_null()) + { + j.at("passwordField").get_to(o.passwordField); + } +} + private: std::string stringField; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/SimpleObject.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/SimpleObject.h index eae83fbbe860..54454a7cc04d 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/SimpleObject.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/SimpleObject.h @@ -31,9 +31,24 @@ class SimpleObject void setDescription(const std::string& description); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(SimpleObject, - id, name, description) +friend void to_json(nlohmann::json& j, const SimpleObject& o) +{ + j["id"] = o.id; + j["name"] = o.name; + j["description"] = o.description; +} + +friend void from_json(const nlohmann::json& j, SimpleObject& o) +{ + j.at("id").get_to(o.id); + j.at("name").get_to(o.name); + + if (j.contains("description") && !j.at("description").is_null()) + { + j.at("description").get_to(o.description); + } +} + private: int id; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/SuccessResponse.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/SuccessResponse.h index a6de84bce543..638fc5e2a88d 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/SuccessResponse.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/SuccessResponse.h @@ -28,9 +28,19 @@ class SuccessResponse void setData(const nlohmann::json& data); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(SuccessResponse, - status, data) +friend void to_json(nlohmann::json& j, const SuccessResponse& o) +{ + j["status"] = o.status; + j["data"] = o.data; +} + +friend void from_json(const nlohmann::json& j, SuccessResponse& o) +{ + j.at("status").get_to(o.status); + j.at("data").get_to(o.data); + +} + private: std::string status; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/TestAllParameterTypes200Response.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/TestAllParameterTypes200Response.h index ce9ade6be871..c9ce8d4fe620 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/TestAllParameterTypes200Response.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/TestAllParameterTypes200Response.h @@ -33,9 +33,35 @@ class TestAllParameterTypes200Response void setCorrelationId(const std::string& correlationId); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(TestAllParameterTypes200Response, - message, resourceId, filter, correlationId) +friend void to_json(nlohmann::json& j, const TestAllParameterTypes200Response& o) +{ + j["message"] = o.message; + j["resourceId"] = o.resourceId; + j["filter"] = o.filter; + j["correlationId"] = o.correlationId; +} + +friend void from_json(const nlohmann::json& j, TestAllParameterTypes200Response& o) +{ + + if (j.contains("message") && !j.at("message").is_null()) + { + j.at("message").get_to(o.message); + } + if (j.contains("resourceId") && !j.at("resourceId").is_null()) + { + j.at("resourceId").get_to(o.resourceId); + } + if (j.contains("filter") && !j.at("filter").is_null()) + { + j.at("filter").get_to(o.filter); + } + if (j.contains("correlationId") && !j.at("correlationId").is_null()) + { + j.at("correlationId").get_to(o.correlationId); + } +} + private: std::string message; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/TestApiKeySecurity200Response.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/TestApiKeySecurity200Response.h index f2ebdc38cc2d..9fdb84095d84 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/TestApiKeySecurity200Response.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/TestApiKeySecurity200Response.h @@ -26,9 +26,20 @@ class TestApiKeySecurity200Response void setMessage(const std::string& message); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(TestApiKeySecurity200Response, - message) +friend void to_json(nlohmann::json& j, const TestApiKeySecurity200Response& o) +{ + j["message"] = o.message; +} + +friend void from_json(const nlohmann::json& j, TestApiKeySecurity200Response& o) +{ + + if (j.contains("message") && !j.at("message").is_null()) + { + j.at("message").get_to(o.message); + } +} + private: std::string message; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/TestBasicSecurity200Response.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/TestBasicSecurity200Response.h index c2a923e72215..8900b6f56aba 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/TestBasicSecurity200Response.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/TestBasicSecurity200Response.h @@ -26,9 +26,20 @@ class TestBasicSecurity200Response void setMessage(const std::string& message); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(TestBasicSecurity200Response, - message) +friend void to_json(nlohmann::json& j, const TestBasicSecurity200Response& o) +{ + j["message"] = o.message; +} + +friend void from_json(const nlohmann::json& j, TestBasicSecurity200Response& o) +{ + + if (j.contains("message") && !j.at("message").is_null()) + { + j.at("message").get_to(o.message); + } +} + private: std::string message; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/TestBearerSecurity200Response.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/TestBearerSecurity200Response.h index 0cb7cbe8421d..40cf56377419 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/TestBearerSecurity200Response.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/TestBearerSecurity200Response.h @@ -26,9 +26,20 @@ class TestBearerSecurity200Response void setMessage(const std::string& message); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(TestBearerSecurity200Response, - message) +friend void to_json(nlohmann::json& j, const TestBearerSecurity200Response& o) +{ + j["message"] = o.message; +} + +friend void from_json(const nlohmann::json& j, TestBearerSecurity200Response& o) +{ + + if (j.contains("message") && !j.at("message").is_null()) + { + j.at("message").get_to(o.message); + } +} + private: std::string message; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/TestCookieParameters200Response.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/TestCookieParameters200Response.h index a5548cc28b0d..b7abe7241e5f 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/TestCookieParameters200Response.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/TestCookieParameters200Response.h @@ -26,9 +26,20 @@ class TestCookieParameters200Response void setMessage(const std::string& message); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(TestCookieParameters200Response, - message) +friend void to_json(nlohmann::json& j, const TestCookieParameters200Response& o) +{ + j["message"] = o.message; +} + +friend void from_json(const nlohmann::json& j, TestCookieParameters200Response& o) +{ + + if (j.contains("message") && !j.at("message").is_null()) + { + j.at("message").get_to(o.message); + } +} + private: std::string message; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/TestHeaderParameters200Response.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/TestHeaderParameters200Response.h index cb091980a63d..11a3d4485a58 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/TestHeaderParameters200Response.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/TestHeaderParameters200Response.h @@ -26,9 +26,20 @@ class TestHeaderParameters200Response void setMessage(const std::string& message); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(TestHeaderParameters200Response, - message) +friend void to_json(nlohmann::json& j, const TestHeaderParameters200Response& o) +{ + j["message"] = o.message; +} + +friend void from_json(const nlohmann::json& j, TestHeaderParameters200Response& o) +{ + + if (j.contains("message") && !j.at("message").is_null()) + { + j.at("message").get_to(o.message); + } +} + private: std::string message; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/TestHeaderParameters401Response.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/TestHeaderParameters401Response.h index 7b6e0d5ed738..e67dfcad6ae0 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/TestHeaderParameters401Response.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/TestHeaderParameters401Response.h @@ -26,9 +26,20 @@ class TestHeaderParameters401Response void setMessage(const std::string& message); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(TestHeaderParameters401Response, - message) +friend void to_json(nlohmann::json& j, const TestHeaderParameters401Response& o) +{ + j["message"] = o.message; +} + +friend void from_json(const nlohmann::json& j, TestHeaderParameters401Response& o) +{ + + if (j.contains("message") && !j.at("message").is_null()) + { + j.at("message").get_to(o.message); + } +} + private: std::string message; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/TestOAuth2Security200Response.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/TestOAuth2Security200Response.h index 8e232d4866a2..02053643fc73 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/TestOAuth2Security200Response.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/TestOAuth2Security200Response.h @@ -26,9 +26,20 @@ class TestOAuth2Security200Response void setMessage(const std::string& message); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(TestOAuth2Security200Response, - message) +friend void to_json(nlohmann::json& j, const TestOAuth2Security200Response& o) +{ + j["message"] = o.message; +} + +friend void from_json(const nlohmann::json& j, TestOAuth2Security200Response& o) +{ + + if (j.contains("message") && !j.at("message").is_null()) + { + j.at("message").get_to(o.message); + } +} + private: std::string message; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/TestQueryParameters200Response.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/TestQueryParameters200Response.h index 7b7b05c4d919..9de69121bd15 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/TestQueryParameters200Response.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/TestQueryParameters200Response.h @@ -26,9 +26,20 @@ class TestQueryParameters200Response void setMessage(const std::string& message); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(TestQueryParameters200Response, - message) +friend void to_json(nlohmann::json& j, const TestQueryParameters200Response& o) +{ + j["message"] = o.message; +} + +friend void from_json(const nlohmann::json& j, TestQueryParameters200Response& o) +{ + + if (j.contains("message") && !j.at("message").is_null()) + { + j.at("message").get_to(o.message); + } +} + private: std::string message; diff --git a/samples/server/petstore/cpp-httplib-server/feature-test/models/TestQueryParametersDeepObjectParameter.h b/samples/server/petstore/cpp-httplib-server/feature-test/models/TestQueryParametersDeepObjectParameter.h index 76d6f4edeaea..441ecba2dafb 100644 --- a/samples/server/petstore/cpp-httplib-server/feature-test/models/TestQueryParametersDeepObjectParameter.h +++ b/samples/server/petstore/cpp-httplib-server/feature-test/models/TestQueryParametersDeepObjectParameter.h @@ -29,9 +29,25 @@ class TestQueryParametersDeepObjectParameter void setAge(const int& age); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(TestQueryParametersDeepObjectParameter, - name, age) +friend void to_json(nlohmann::json& j, const TestQueryParametersDeepObjectParameter& o) +{ + j["name"] = o.name; + j["age"] = o.age; +} + +friend void from_json(const nlohmann::json& j, TestQueryParametersDeepObjectParameter& o) +{ + + if (j.contains("name") && !j.at("name").is_null()) + { + j.at("name").get_to(o.name); + } + if (j.contains("age") && !j.at("age").is_null()) + { + j.at("age").get_to(o.age); + } +} + private: std::string name; diff --git a/samples/server/petstore/cpp-httplib-server/petstore/models/ApiResponse.h b/samples/server/petstore/cpp-httplib-server/petstore/models/ApiResponse.h index 3cb5f3ed9df6..9d5519d7c83f 100644 --- a/samples/server/petstore/cpp-httplib-server/petstore/models/ApiResponse.h +++ b/samples/server/petstore/cpp-httplib-server/petstore/models/ApiResponse.h @@ -31,9 +31,30 @@ class ApiResponse void setMessage(const std::string& message); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(ApiResponse, - code, type, message) +friend void to_json(nlohmann::json& j, const ApiResponse& o) +{ + j["code"] = o.code; + j["type"] = o.type; + j["message"] = o.message; +} + +friend void from_json(const nlohmann::json& j, ApiResponse& o) +{ + + if (j.contains("code") && !j.at("code").is_null()) + { + j.at("code").get_to(o.code); + } + if (j.contains("type") && !j.at("type").is_null()) + { + j.at("type").get_to(o.type); + } + if (j.contains("message") && !j.at("message").is_null()) + { + j.at("message").get_to(o.message); + } +} + private: int code; diff --git a/samples/server/petstore/cpp-httplib-server/petstore/models/Category.h b/samples/server/petstore/cpp-httplib-server/petstore/models/Category.h index 432674c5607b..9f523f56f002 100644 --- a/samples/server/petstore/cpp-httplib-server/petstore/models/Category.h +++ b/samples/server/petstore/cpp-httplib-server/petstore/models/Category.h @@ -28,9 +28,25 @@ class Category void setName(const std::string& name); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(Category, - id, name) +friend void to_json(nlohmann::json& j, const Category& o) +{ + j["id"] = o.id; + j["name"] = o.name; +} + +friend void from_json(const nlohmann::json& j, Category& o) +{ + + if (j.contains("id") && !j.at("id").is_null()) + { + j.at("id").get_to(o.id); + } + if (j.contains("name") && !j.at("name").is_null()) + { + j.at("name").get_to(o.name); + } +} + private: long id; diff --git a/samples/server/petstore/cpp-httplib-server/petstore/models/ComplexParamsResponse.h b/samples/server/petstore/cpp-httplib-server/petstore/models/ComplexParamsResponse.h index eaab72300df9..be85e9258265 100644 --- a/samples/server/petstore/cpp-httplib-server/petstore/models/ComplexParamsResponse.h +++ b/samples/server/petstore/cpp-httplib-server/petstore/models/ComplexParamsResponse.h @@ -147,9 +147,45 @@ class ComplexParamsResponse - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(ComplexParamsResponse, - deepObj, enumParam, pipeArr, spaceArr, xEnumHeader, cookieEnum) +friend void to_json(nlohmann::json& j, const ComplexParamsResponse& o) +{ + j["deepObj"] = o.deepObj; + j["enumParam"] = o.enumParam; + j["pipeArr"] = o.pipeArr; + j["spaceArr"] = o.spaceArr; + j["x-enum-header"] = o.xEnumHeader; + j["cookieEnum"] = o.cookieEnum; +} + +friend void from_json(const nlohmann::json& j, ComplexParamsResponse& o) +{ + + if (j.contains("deepObj") && !j.at("deepObj").is_null()) + { + j.at("deepObj").get_to(o.deepObj); + } + if (j.contains("enumParam") && !j.at("enumParam").is_null()) + { + j.at("enumParam").get_to(o.enumParam); + } + if (j.contains("pipeArr") && !j.at("pipeArr").is_null()) + { + j.at("pipeArr").get_to(o.pipeArr); + } + if (j.contains("spaceArr") && !j.at("spaceArr").is_null()) + { + j.at("spaceArr").get_to(o.spaceArr); + } + if (j.contains("x-enum-header") && !j.at("x-enum-header").is_null()) + { + j.at("x-enum-header").get_to(o.xEnumHeader); + } + if (j.contains("cookieEnum") && !j.at("cookieEnum").is_null()) + { + j.at("cookieEnum").get_to(o.cookieEnum); + } +} + private: DeepObj deepObj; diff --git a/samples/server/petstore/cpp-httplib-server/petstore/models/DeepObj.h b/samples/server/petstore/cpp-httplib-server/petstore/models/DeepObj.h index 25350d579e94..43a084f25633 100644 --- a/samples/server/petstore/cpp-httplib-server/petstore/models/DeepObj.h +++ b/samples/server/petstore/cpp-httplib-server/petstore/models/DeepObj.h @@ -32,9 +32,30 @@ class DeepObj void setBaz(const DeepObjBaz& baz); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(DeepObj, - foo, bar, baz) +friend void to_json(nlohmann::json& j, const DeepObj& o) +{ + j["foo"] = o.foo; + j["bar"] = o.bar; + j["baz"] = o.baz; +} + +friend void from_json(const nlohmann::json& j, DeepObj& o) +{ + + if (j.contains("foo") && !j.at("foo").is_null()) + { + j.at("foo").get_to(o.foo); + } + if (j.contains("bar") && !j.at("bar").is_null()) + { + j.at("bar").get_to(o.bar); + } + if (j.contains("baz") && !j.at("baz").is_null()) + { + j.at("baz").get_to(o.baz); + } +} + private: std::string foo; diff --git a/samples/server/petstore/cpp-httplib-server/petstore/models/DeepObjBaz.h b/samples/server/petstore/cpp-httplib-server/petstore/models/DeepObjBaz.h index 5220c128694b..c33b79c5021c 100644 --- a/samples/server/petstore/cpp-httplib-server/petstore/models/DeepObjBaz.h +++ b/samples/server/petstore/cpp-httplib-server/petstore/models/DeepObjBaz.h @@ -29,9 +29,25 @@ class DeepObjBaz void setY(const int& Y); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(DeepObjBaz, - X, Y) +friend void to_json(nlohmann::json& j, const DeepObjBaz& o) +{ + j["x"] = o.X; + j["y"] = o.Y; +} + +friend void from_json(const nlohmann::json& j, DeepObjBaz& o) +{ + + if (j.contains("x") && !j.at("x").is_null()) + { + j.at("x").get_to(o.X); + } + if (j.contains("y") && !j.at("y").is_null()) + { + j.at("y").get_to(o.Y); + } +} + private: std::string X; diff --git a/samples/server/petstore/cpp-httplib-server/petstore/models/NullableExample.h b/samples/server/petstore/cpp-httplib-server/petstore/models/NullableExample.h index cd532bccb1b8..3e30203000cd 100644 --- a/samples/server/petstore/cpp-httplib-server/petstore/models/NullableExample.h +++ b/samples/server/petstore/cpp-httplib-server/petstore/models/NullableExample.h @@ -27,9 +27,20 @@ class NullableExample void setMaybeString(const std::string& maybeString); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(NullableExample, - maybeString) +friend void to_json(nlohmann::json& j, const NullableExample& o) +{ + j["maybeString"] = o.maybeString; +} + +friend void from_json(const nlohmann::json& j, NullableExample& o) +{ + + if (j.contains("maybeString") && !j.at("maybeString").is_null()) + { + j.at("maybeString").get_to(o.maybeString); + } +} + private: std::string maybeString; diff --git a/samples/server/petstore/cpp-httplib-server/petstore/models/Order.h b/samples/server/petstore/cpp-httplib-server/petstore/models/Order.h index 7e7d3aade171..a64be8f723b5 100644 --- a/samples/server/petstore/cpp-httplib-server/petstore/models/Order.h +++ b/samples/server/petstore/cpp-httplib-server/petstore/models/Order.h @@ -74,9 +74,45 @@ class Order - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(Order, - id, petId, quantity, shipDate, status, complete) +friend void to_json(nlohmann::json& j, const Order& o) +{ + j["id"] = o.id; + j["petId"] = o.petId; + j["quantity"] = o.quantity; + j["shipDate"] = o.shipDate; + j["status"] = o.status; + j["complete"] = o.complete; +} + +friend void from_json(const nlohmann::json& j, Order& o) +{ + + if (j.contains("id") && !j.at("id").is_null()) + { + j.at("id").get_to(o.id); + } + if (j.contains("petId") && !j.at("petId").is_null()) + { + j.at("petId").get_to(o.petId); + } + if (j.contains("quantity") && !j.at("quantity").is_null()) + { + j.at("quantity").get_to(o.quantity); + } + if (j.contains("shipDate") && !j.at("shipDate").is_null()) + { + j.at("shipDate").get_to(o.shipDate); + } + if (j.contains("status") && !j.at("status").is_null()) + { + j.at("status").get_to(o.status); + } + if (j.contains("complete") && !j.at("complete").is_null()) + { + j.at("complete").get_to(o.complete); + } +} + private: long id; diff --git a/samples/server/petstore/cpp-httplib-server/petstore/models/Pet.h b/samples/server/petstore/cpp-httplib-server/petstore/models/Pet.h index a120edbd44fc..17d376bed808 100644 --- a/samples/server/petstore/cpp-httplib-server/petstore/models/Pet.h +++ b/samples/server/petstore/cpp-httplib-server/petstore/models/Pet.h @@ -76,9 +76,39 @@ class Pet - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(Pet, - id, name, photoUrls, status, category, tags) +friend void to_json(nlohmann::json& j, const Pet& o) +{ + j["id"] = o.id; + j["name"] = o.name; + j["photoUrls"] = o.photoUrls; + j["status"] = o.status; + j["category"] = o.category; + j["tags"] = o.tags; +} + +friend void from_json(const nlohmann::json& j, Pet& o) +{ + j.at("name").get_to(o.name); + j.at("photoUrls").get_to(o.photoUrls); + + if (j.contains("id") && !j.at("id").is_null()) + { + j.at("id").get_to(o.id); + } + if (j.contains("status") && !j.at("status").is_null()) + { + j.at("status").get_to(o.status); + } + if (j.contains("category") && !j.at("category").is_null()) + { + j.at("category").get_to(o.category); + } + if (j.contains("tags") && !j.at("tags").is_null()) + { + j.at("tags").get_to(o.tags); + } +} + private: long id; diff --git a/samples/server/petstore/cpp-httplib-server/petstore/models/Tag.h b/samples/server/petstore/cpp-httplib-server/petstore/models/Tag.h index d8255563cdeb..a062e35c7fee 100644 --- a/samples/server/petstore/cpp-httplib-server/petstore/models/Tag.h +++ b/samples/server/petstore/cpp-httplib-server/petstore/models/Tag.h @@ -28,9 +28,25 @@ class Tag void setName(const std::string& name); - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(Tag, - id, name) +friend void to_json(nlohmann::json& j, const Tag& o) +{ + j["id"] = o.id; + j["name"] = o.name; +} + +friend void from_json(const nlohmann::json& j, Tag& o) +{ + + if (j.contains("id") && !j.at("id").is_null()) + { + j.at("id").get_to(o.id); + } + if (j.contains("name") && !j.at("name").is_null()) + { + j.at("name").get_to(o.name); + } +} + private: long id; diff --git a/samples/server/petstore/cpp-httplib-server/petstore/models/User.h b/samples/server/petstore/cpp-httplib-server/petstore/models/User.h index ca4e9d5ff0de..6197f6d1246b 100644 --- a/samples/server/petstore/cpp-httplib-server/petstore/models/User.h +++ b/samples/server/petstore/cpp-httplib-server/petstore/models/User.h @@ -78,9 +78,55 @@ class User - // JSON serialization using NLOHMANN INTRUSIVE macro (must be inside class to access private members) - NLOHMANN_DEFINE_TYPE_INTRUSIVE(User, - id, username, firstName, lastName, email, password, phone, userStatus) +friend void to_json(nlohmann::json& j, const User& o) +{ + j["id"] = o.id; + j["username"] = o.username; + j["firstName"] = o.firstName; + j["lastName"] = o.lastName; + j["email"] = o.email; + j["password"] = o.password; + j["phone"] = o.phone; + j["userStatus"] = o.userStatus; +} + +friend void from_json(const nlohmann::json& j, User& o) +{ + + if (j.contains("id") && !j.at("id").is_null()) + { + j.at("id").get_to(o.id); + } + if (j.contains("username") && !j.at("username").is_null()) + { + j.at("username").get_to(o.username); + } + if (j.contains("firstName") && !j.at("firstName").is_null()) + { + j.at("firstName").get_to(o.firstName); + } + if (j.contains("lastName") && !j.at("lastName").is_null()) + { + j.at("lastName").get_to(o.lastName); + } + if (j.contains("email") && !j.at("email").is_null()) + { + j.at("email").get_to(o.email); + } + if (j.contains("password") && !j.at("password").is_null()) + { + j.at("password").get_to(o.password); + } + if (j.contains("phone") && !j.at("phone").is_null()) + { + j.at("phone").get_to(o.phone); + } + if (j.contains("userStatus") && !j.at("userStatus").is_null()) + { + j.at("userStatus").get_to(o.userStatus); + } +} + private: long id;