forked from loafle/openapi-generator-original
[cpp-pistache]Add support for map (#1359)
* Add support for map * Add support for nested maps * Simplify Array and Map Helper * Use const reference wherever possible
This commit is contained in:
parent
36991a4e14
commit
f8f3a08282
@ -73,7 +73,7 @@ void {{classname}}::{{operationIdSnakeCase}}_handler(const Pistache::Rest::Reque
|
|||||||
nlohmann::json request_body = nlohmann::json::parse(request.body());
|
nlohmann::json request_body = nlohmann::json::parse(request.body());
|
||||||
{{^isPrimitiveType}}{{^isContainer}}
|
{{^isPrimitiveType}}{{^isContainer}}
|
||||||
{{paramName}}.fromJson(request_body);
|
{{paramName}}.fromJson(request_body);
|
||||||
{{/isContainer}}{{/isPrimitiveType}}{{#isContainer}} {{paramName}} = {{#isListContainer}} {{prefix}}ModelArrayHelper{{/isListContainer}}{{#isMapContainer}} {{prefix}}ModelMapHelper{{/isMapContainer}}::fromJson<{{items.baseType}}>(request_body);{{/isContainer}}
|
{{/isContainer}}{{/isPrimitiveType}}{{#isContainer}} {{paramName}} = {{#isListContainer}} {{prefix}}ArrayHelper{{/isListContainer}}{{#isMapContainer}} {{prefix}}MapHelper{{/isMapContainer}}::fromJson<{{items.baseType}}>(request_body);{{/isContainer}}
|
||||||
{{#isPrimitiveType}}
|
{{#isPrimitiveType}}
|
||||||
// The conversion is done automatically by the json library
|
// The conversion is done automatically by the json library
|
||||||
{{paramName}} = request_body;
|
{{paramName}} = request_body;
|
||||||
|
@ -34,7 +34,7 @@ public:
|
|||||||
void validate() override;
|
void validate() override;
|
||||||
|
|
||||||
nlohmann::json toJson() const override;
|
nlohmann::json toJson() const override;
|
||||||
void fromJson(nlohmann::json& json) override;
|
void fromJson(const nlohmann::json& json) override;
|
||||||
|
|
||||||
/////////////////////////////////////////////
|
/////////////////////////////////////////////
|
||||||
/// {{classname}} members
|
/// {{classname}} members
|
||||||
|
@ -45,20 +45,34 @@ nlohmann::json {{classname}}::toJson() const
|
|||||||
if(jsonArray.size() > 0)
|
if(jsonArray.size() > 0)
|
||||||
{
|
{
|
||||||
val["{{baseName}}"] = jsonArray;
|
val["{{baseName}}"] = jsonArray;
|
||||||
}
|
} {{/required}}
|
||||||
{{/required}}
|
|
||||||
}
|
}
|
||||||
{{/isListContainer}}{{^isListContainer}}{{^isPrimitiveType}}{{^required}}if(m_{{name}}IsSet)
|
{{/isListContainer}}{{#isMapContainer}}{
|
||||||
|
nlohmann::json jsonObj;
|
||||||
|
for( auto const& item : m_{{name}} )
|
||||||
|
{ {{^items.isContainer}}
|
||||||
|
jsonObj[item.first] = {{prefix}}ModelBase::toJson(item.second);{{/items.isContainer}} {{#items.isListContainer}}
|
||||||
|
jsonObj[item.first] = {{prefix}}ArrayHelper::toJson<{{{items.items.datatype}}}>(item.second);
|
||||||
|
{{/items.isListContainer}} {{#items.isMapContainer}}
|
||||||
|
jsonObj[item.first] = {{prefix}}MapHelper::toJson<{{{items.items.datatype}}}>(item.second); {{/items.isMapContainer}}
|
||||||
|
}
|
||||||
|
{{#required}}val["{{baseName}}"] = jsonObj; {{/required}}{{^required}}
|
||||||
|
if(jsonObj.size() > 0)
|
||||||
|
{
|
||||||
|
val["{{baseName}}"] = jsonObj;
|
||||||
|
} {{/required}}
|
||||||
|
}
|
||||||
|
{{/isMapContainer}}{{^isContainer}}{{^isPrimitiveType}}{{^required}}if(m_{{name}}IsSet)
|
||||||
{
|
{
|
||||||
val["{{baseName}}"] = {{prefix}}ModelBase::toJson(m_{{name}});
|
val["{{baseName}}"] = {{prefix}}ModelBase::toJson(m_{{name}});
|
||||||
}
|
}
|
||||||
{{/required}}{{#required}}val["{{baseName}}"] = {{prefix}}ModelBase::toJson(m_{{name}});
|
{{/required}}{{#required}}val["{{baseName}}"] = {{prefix}}ModelBase::toJson(m_{{name}});
|
||||||
{{/required}}{{/isPrimitiveType}}{{/isListContainer}}{{/vars}}
|
{{/required}}{{/isPrimitiveType}}{{/isContainer}}{{/vars}}
|
||||||
|
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
void {{classname}}::fromJson(nlohmann::json& val)
|
void {{classname}}::fromJson(const nlohmann::json& val)
|
||||||
{
|
{
|
||||||
{{#vars}}{{#isPrimitiveType}}{{^isListContainer}}{{^required}}if(val.find("{{baseName}}") != val.end())
|
{{#vars}}{{#isPrimitiveType}}{{^isListContainer}}{{^required}}if(val.find("{{baseName}}") != val.end())
|
||||||
{
|
{
|
||||||
@ -67,33 +81,53 @@ void {{classname}}::fromJson(nlohmann::json& val)
|
|||||||
{{/required}}{{#required}}{{setter}}(val.at("{{baseName}}"));
|
{{/required}}{{#required}}{{setter}}(val.at("{{baseName}}"));
|
||||||
{{/required}}{{/isListContainer}}{{/isPrimitiveType}}{{#isListContainer}}{
|
{{/required}}{{/isListContainer}}{{/isPrimitiveType}}{{#isListContainer}}{
|
||||||
m_{{name}}.clear();
|
m_{{name}}.clear();
|
||||||
nlohmann::json jsonArray;
|
|
||||||
{{^required}}if(val.find("{{baseName}}") != val.end())
|
{{^required}}if(val.find("{{baseName}}") != val.end())
|
||||||
{
|
{
|
||||||
{{/required}}
|
{{/required}}
|
||||||
for( auto& item : val["{{baseName}}"] )
|
for( auto& item : val["{{baseName}}"] )
|
||||||
{
|
|
||||||
{{#isPrimitiveType}}m_{{name}}.push_back(item);
|
|
||||||
{{/isPrimitiveType}}{{^isPrimitiveType}}{{#items.isString}}m_{{name}}.push_back(item);
|
|
||||||
{{/items.isString}}{{^items.isString}}{{#items.isDateTime}}m_{{name}}.push_back(item);
|
|
||||||
{{/items.isDateTime}}{{^items.isDateTime}}
|
|
||||||
if(item.is_null())
|
|
||||||
{
|
{
|
||||||
m_{{name}}.push_back( {{{items.datatype}}}() );
|
{{#isPrimitiveType}}m_{{name}}.push_back(item);
|
||||||
|
{{/isPrimitiveType}}{{^isPrimitiveType}}{{#items.isString}}m_{{name}}.push_back(item);
|
||||||
|
{{/items.isString}}{{^items.isString}}{{#items.isDateTime}}m_{{name}}.push_back(item);
|
||||||
|
{{/items.isDateTime}}{{^items.isDateTime}}
|
||||||
|
if(item.is_null())
|
||||||
|
{
|
||||||
|
m_{{name}}.push_back( {{{items.datatype}}}() );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
{{{items.datatype}}} newItem;
|
||||||
|
newItem.fromJson(item);
|
||||||
|
m_{{name}}.push_back( newItem );
|
||||||
|
}
|
||||||
|
{{/items.isDateTime}}{{/items.isString}}{{/isPrimitiveType}}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
{{{items.datatype}}} newItem;
|
|
||||||
newItem.fromJson(item);
|
|
||||||
m_{{name}}.push_back( newItem );
|
|
||||||
}
|
|
||||||
{{/items.isDateTime}}{{/items.isString}}{{/isPrimitiveType}}
|
|
||||||
}
|
|
||||||
{{^required}}
|
{{^required}}
|
||||||
}
|
}
|
||||||
{{/required}}
|
{{/required}}
|
||||||
}
|
}
|
||||||
{{/isListContainer}}{{^isListContainer}}{{^isPrimitiveType}}{{^required}}if(val.find("{{baseName}}") != val.end())
|
{{/isListContainer}}{{#isMapContainer}}{
|
||||||
|
m_{{name}}.clear();
|
||||||
|
{{^required}}if(val.find("{{baseName}}") != val.end())
|
||||||
|
{
|
||||||
|
{{/required}}
|
||||||
|
if(val["{{baseName}}"].is_object()) { {{^items.isContainer}}
|
||||||
|
m_{{name}} = {{prefix}}MapHelper::fromJson<{{{items.datatype}}}>(val["{{baseName}}"]);
|
||||||
|
{{/items.isContainer}} {{#items.isContainer}}
|
||||||
|
for( auto& item : val["{{baseName}}"].items() )
|
||||||
|
{ {{#items.isMapContainer}}
|
||||||
|
{{{items.datatype}}} newItem = {{prefix}}MapHelper::fromJson<{{{items.items.datatype}}}>(item.value());
|
||||||
|
{{/items.isMapContainer}}{{#items.isListContainer}}
|
||||||
|
{{{items.datatype}}} newItem = {{prefix}}ArrayHelper::fromJson<{{{items.items.datatype}}}>(item.value());
|
||||||
|
{{/items.isListContainer}}
|
||||||
|
m_{{name}}.insert(m_{{name}}.end(), std::pair< std::string, {{{items.datatype}}} >(item.key(), newItem));
|
||||||
|
} {{/items.isContainer}}
|
||||||
|
}
|
||||||
|
{{^required}}
|
||||||
|
}
|
||||||
|
{{/required}}
|
||||||
|
}
|
||||||
|
{{/isMapContainer}}{{^isContainer}}{{^isPrimitiveType}}{{^required}}if(val.find("{{baseName}}") != val.end())
|
||||||
{
|
{
|
||||||
{{#isString}}{{setter}}(val.at("{{baseName}}"));{{/isString}}{{#isByteArray}}{{setter}}(val.at("{{baseName}}"));{{/isByteArray}}{{#isBinary}}{{setter}}(val.at("{{baseName}}"));
|
{{#isString}}{{setter}}(val.at("{{baseName}}"));{{/isString}}{{#isByteArray}}{{setter}}(val.at("{{baseName}}"));{{/isByteArray}}{{#isBinary}}{{setter}}(val.at("{{baseName}}"));
|
||||||
{{/isBinary}}{{^isString}}{{#isDateTime}}{{setter}}(val.at("{{baseName}}"));
|
{{/isBinary}}{{^isString}}{{#isDateTime}}{{setter}}(val.at("{{baseName}}"));
|
||||||
@ -107,7 +141,7 @@ void {{classname}}::fromJson(nlohmann::json& val)
|
|||||||
}
|
}
|
||||||
{{/required}}{{#required}}{{#isString}}{{setter}}(val.at("{{baseName}}"));
|
{{/required}}{{#required}}{{#isString}}{{setter}}(val.at("{{baseName}}"));
|
||||||
{{/isString}}{{^isString}}{{#isDateTime}}{{setter}}(val.at("{{baseName}}"));
|
{{/isString}}{{^isString}}{{#isDateTime}}{{setter}}(val.at("{{baseName}}"));
|
||||||
{{/isDateTime}}{{/isString}}{{/required}}{{/isPrimitiveType}}{{/isListContainer}}{{/vars}}
|
{{/isDateTime}}{{/isString}}{{/required}}{{/isPrimitiveType}}{{/isContainer}}{{/vars}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ public:
|
|||||||
virtual void validate() = 0;
|
virtual void validate() = 0;
|
||||||
|
|
||||||
virtual nlohmann::json toJson() const = 0;
|
virtual nlohmann::json toJson() const = 0;
|
||||||
virtual void fromJson(nlohmann::json& json) = 0;
|
virtual void fromJson(const nlohmann::json& json) = 0;
|
||||||
|
|
||||||
static std::string toJson( std::string const& value );
|
static std::string toJson( std::string const& value );
|
||||||
static std::string toJson( std::time_t const& value );
|
static std::string toJson( std::time_t const& value );
|
||||||
@ -39,100 +39,85 @@ public:
|
|||||||
static nlohmann::json toJson({{prefix}}ModelBase const& content );
|
static nlohmann::json toJson({{prefix}}ModelBase const& content );
|
||||||
};
|
};
|
||||||
|
|
||||||
class {{prefix}}ModelArrayHelper {
|
|
||||||
public:
|
|
||||||
template<typename T>
|
|
||||||
static std::vector<T> fromJson(nlohmann::json& json) {
|
|
||||||
T *ptrTest;
|
|
||||||
std::vector<T> val;
|
|
||||||
if (dynamic_cast<{{prefix}}ModelBase*>(ptrTest) != nullptr) {
|
|
||||||
if (!json.empty()) {
|
|
||||||
for (auto &item : json.items()) {
|
|
||||||
T entry;
|
|
||||||
entry.fromJson(item.value());
|
|
||||||
val.push_back(entry);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
template<typename T>
|
|
||||||
static nlohmann::json toJson(std::vector<T> val) {
|
|
||||||
nlohmann::json json;
|
|
||||||
for(auto item : val){
|
|
||||||
json.push_back(item.toJson());
|
|
||||||
}
|
|
||||||
return json;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class {{prefix}}ArrayHelper {
|
class {{prefix}}ArrayHelper {
|
||||||
|
private:
|
||||||
|
template<typename T, typename std::enable_if<!std::is_base_of<ModelBase, T>::value>::value>
|
||||||
|
static void itemFromJson(T& item, const nlohmann::json& json){
|
||||||
|
item = json;
|
||||||
|
}
|
||||||
|
static void itemFromJson(ModelBase& item, const nlohmann::json& json){
|
||||||
|
item.fromJson(json);
|
||||||
|
}
|
||||||
|
template<typename T, typename std::enable_if<!std::is_base_of<ModelBase, T>::value>::value>
|
||||||
|
static nlohmann::json itemtoJson(const T& item){
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
static nlohmann::json itemtoJson(const ModelBase& item){
|
||||||
|
return item.toJson();
|
||||||
|
}
|
||||||
public:
|
public:
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static std::vector<T> fromJson(nlohmann::json& json) {
|
static std::vector<T> fromJson(const nlohmann::json& json) {
|
||||||
std::vector<T> val;
|
std::vector<T> val;
|
||||||
nlohmann::from_json(json, val);
|
if (!json.empty()) {
|
||||||
return val;
|
for (const auto& item : json.items()) {
|
||||||
}
|
T entry;
|
||||||
template<typename T>
|
itemFromJson(entry, item.value());
|
||||||
static nlohmann::json toJson(std::vector<T> val) {
|
val.push_back(entry);
|
||||||
nlohmann::json json;
|
}
|
||||||
nlohmann::to_json(json, val);
|
}
|
||||||
return json;
|
return val;
|
||||||
}
|
}
|
||||||
};
|
template<typename T>
|
||||||
|
static nlohmann::json toJson(const std::vector<T>& val) {
|
||||||
class {{prefix}}ModelMapHelper {
|
nlohmann::json json;
|
||||||
public:
|
for(const auto& item : val){
|
||||||
template<typename T>
|
json.push_back(itemtoJson(item));
|
||||||
static std::map<std::string, T> & fromJson(nlohmann::json& json) {
|
}
|
||||||
T *ptrTest;
|
return json;
|
||||||
std::map<std::string, T> val;
|
}
|
||||||
if (dynamic_cast<{{prefix}}ModelBase*>(ptrTest) != nullptr) {
|
|
||||||
if (!json.empty()) {
|
|
||||||
for (auto &item : json.items()) {
|
|
||||||
T entry;
|
|
||||||
entry.fromJson(item.value());
|
|
||||||
val.insert(val.end(),
|
|
||||||
std::pair<std::string, T>(item.key(), entry));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
template<typename T>
|
|
||||||
static nlohmann::json toJson(std::map<std::string, T> val) {
|
|
||||||
nlohmann::json json;
|
|
||||||
for (auto const& item : val) {
|
|
||||||
json[item.first] = item.second.toJson();
|
|
||||||
}
|
|
||||||
return json;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class {{prefix}}MapHelper {
|
class {{prefix}}MapHelper {
|
||||||
|
private:
|
||||||
|
template<typename T, typename std::enable_if<!std::is_base_of<ModelBase, T>::value>::value>
|
||||||
|
static void itemFromJson(T &item, const nlohmann::json& json){
|
||||||
|
item = json;
|
||||||
|
}
|
||||||
|
static void itemFromJson(ModelBase &item, const nlohmann::json& json){
|
||||||
|
item.fromJson(json);
|
||||||
|
}
|
||||||
|
template<typename T, typename std::enable_if<!std::is_base_of<ModelBase, T>::value>::value>
|
||||||
|
static nlohmann::json itemtoJson(const T& item){
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
static nlohmann::json itemtoJson(const ModelBase& item){
|
||||||
|
return item.toJson();
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static std::map<std::string, T> & fromJson(nlohmann::json& json) {
|
static std::map<std::string, T> fromJson(const nlohmann::json& json) {
|
||||||
std::map<std::string, T> val;
|
std::map<std::string, T> val;
|
||||||
if (!json.empty()) {
|
if (!json.empty()) {
|
||||||
for (auto &item : json.items()) {
|
for (const auto& item : json.items()) {
|
||||||
T entry = item.value();
|
T entry;
|
||||||
val.insert(val.end(),
|
itemfromJson(entry, item.value());
|
||||||
std::pair<std::string, T>(item.key(), entry));
|
val.insert(val.end(),
|
||||||
}
|
std::pair<std::string, T>(item.key(), entry));
|
||||||
}
|
}
|
||||||
return val;
|
}
|
||||||
}
|
return val;
|
||||||
template<typename T>
|
}
|
||||||
static nlohmann::json toJson(std::map<std::string, T> val) {
|
template<typename T>
|
||||||
nlohmann::json json;
|
static nlohmann::json toJson(const std::map<std::string, T>& val) {
|
||||||
for (auto const& item : val) {
|
nlohmann::json json;
|
||||||
nlohmann::json jitem = item.second;
|
for (const auto& item : val) {
|
||||||
json[item.first] = jitem;
|
nlohmann::json jitem = itemtoJson(item.second);
|
||||||
}
|
json[item.first] = jitem;
|
||||||
return json;
|
}
|
||||||
}
|
return json;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
{{#modelNamespaceDeclarations}}
|
{{#modelNamespaceDeclarations}}
|
||||||
|
@ -22,9 +22,9 @@
|
|||||||
#include <pistache/http.h>
|
#include <pistache/http.h>
|
||||||
#include <pistache/router.h>
|
#include <pistache/router.h>
|
||||||
#include <pistache/http_headers.h>
|
#include <pistache/http_headers.h>
|
||||||
|
|
||||||
#include <pistache/optional.h>
|
#include <pistache/optional.h>
|
||||||
|
|
||||||
|
|
||||||
#include "ApiResponse.h"
|
#include "ApiResponse.h"
|
||||||
#include "Pet.h"
|
#include "Pet.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
@ -22,9 +22,9 @@
|
|||||||
#include <pistache/http.h>
|
#include <pistache/http.h>
|
||||||
#include <pistache/router.h>
|
#include <pistache/router.h>
|
||||||
#include <pistache/http_headers.h>
|
#include <pistache/http_headers.h>
|
||||||
|
|
||||||
#include <pistache/optional.h>
|
#include <pistache/optional.h>
|
||||||
|
|
||||||
|
|
||||||
#include "Order.h"
|
#include "Order.h"
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
@ -71,7 +71,7 @@ void UserApi::create_users_with_array_input_handler(const Pistache::Rest::Reques
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
nlohmann::json request_body = nlohmann::json::parse(request.body());
|
nlohmann::json request_body = nlohmann::json::parse(request.body());
|
||||||
user = ModelArrayHelper::fromJson<User>(request_body);
|
user = ArrayHelper::fromJson<User>(request_body);
|
||||||
this->create_users_with_array_input(user, response);
|
this->create_users_with_array_input(user, response);
|
||||||
} catch (std::runtime_error & e) {
|
} catch (std::runtime_error & e) {
|
||||||
//send a 400 error
|
//send a 400 error
|
||||||
@ -87,7 +87,7 @@ void UserApi::create_users_with_list_input_handler(const Pistache::Rest::Request
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
nlohmann::json request_body = nlohmann::json::parse(request.body());
|
nlohmann::json request_body = nlohmann::json::parse(request.body());
|
||||||
user = ModelArrayHelper::fromJson<User>(request_body);
|
user = ArrayHelper::fromJson<User>(request_body);
|
||||||
this->create_users_with_list_input(user, response);
|
this->create_users_with_list_input(user, response);
|
||||||
} catch (std::runtime_error & e) {
|
} catch (std::runtime_error & e) {
|
||||||
//send a 400 error
|
//send a 400 error
|
||||||
|
@ -22,9 +22,9 @@
|
|||||||
#include <pistache/http.h>
|
#include <pistache/http.h>
|
||||||
#include <pistache/router.h>
|
#include <pistache/router.h>
|
||||||
#include <pistache/http_headers.h>
|
#include <pistache/http_headers.h>
|
||||||
|
|
||||||
#include <pistache/optional.h>
|
#include <pistache/optional.h>
|
||||||
|
|
||||||
|
|
||||||
#include "User.h"
|
#include "User.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
@ -59,7 +59,7 @@ nlohmann::json ApiResponse::toJson() const
|
|||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ApiResponse::fromJson(nlohmann::json& val)
|
void ApiResponse::fromJson(const nlohmann::json& val)
|
||||||
{
|
{
|
||||||
if(val.find("code") != val.end())
|
if(val.find("code") != val.end())
|
||||||
{
|
{
|
||||||
|
@ -44,7 +44,7 @@ public:
|
|||||||
void validate() override;
|
void validate() override;
|
||||||
|
|
||||||
nlohmann::json toJson() const override;
|
nlohmann::json toJson() const override;
|
||||||
void fromJson(nlohmann::json& json) override;
|
void fromJson(const nlohmann::json& json) override;
|
||||||
|
|
||||||
/////////////////////////////////////////////
|
/////////////////////////////////////////////
|
||||||
/// ApiResponse members
|
/// ApiResponse members
|
||||||
|
@ -53,7 +53,7 @@ nlohmann::json Category::toJson() const
|
|||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Category::fromJson(nlohmann::json& val)
|
void Category::fromJson(const nlohmann::json& val)
|
||||||
{
|
{
|
||||||
if(val.find("id") != val.end())
|
if(val.find("id") != val.end())
|
||||||
{
|
{
|
||||||
|
@ -44,7 +44,7 @@ public:
|
|||||||
void validate() override;
|
void validate() override;
|
||||||
|
|
||||||
nlohmann::json toJson() const override;
|
nlohmann::json toJson() const override;
|
||||||
void fromJson(nlohmann::json& json) override;
|
void fromJson(const nlohmann::json& json) override;
|
||||||
|
|
||||||
/////////////////////////////////////////////
|
/////////////////////////////////////////////
|
||||||
/// Category members
|
/// Category members
|
||||||
|
@ -39,7 +39,7 @@ public:
|
|||||||
virtual void validate() = 0;
|
virtual void validate() = 0;
|
||||||
|
|
||||||
virtual nlohmann::json toJson() const = 0;
|
virtual nlohmann::json toJson() const = 0;
|
||||||
virtual void fromJson(nlohmann::json& json) = 0;
|
virtual void fromJson(const nlohmann::json& json) = 0;
|
||||||
|
|
||||||
static std::string toJson( std::string const& value );
|
static std::string toJson( std::string const& value );
|
||||||
static std::string toJson( std::time_t const& value );
|
static std::string toJson( std::time_t const& value );
|
||||||
@ -50,100 +50,85 @@ public:
|
|||||||
static nlohmann::json toJson(ModelBase const& content );
|
static nlohmann::json toJson(ModelBase const& content );
|
||||||
};
|
};
|
||||||
|
|
||||||
class ModelArrayHelper {
|
|
||||||
public:
|
|
||||||
template<typename T>
|
|
||||||
static std::vector<T> fromJson(nlohmann::json& json) {
|
|
||||||
T *ptrTest;
|
|
||||||
std::vector<T> val;
|
|
||||||
if (dynamic_cast<ModelBase*>(ptrTest) != nullptr) {
|
|
||||||
if (!json.empty()) {
|
|
||||||
for (auto &item : json.items()) {
|
|
||||||
T entry;
|
|
||||||
entry.fromJson(item.value());
|
|
||||||
val.push_back(entry);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
template<typename T>
|
|
||||||
static nlohmann::json toJson(std::vector<T> val) {
|
|
||||||
nlohmann::json json;
|
|
||||||
for(auto item : val){
|
|
||||||
json.push_back(item.toJson());
|
|
||||||
}
|
|
||||||
return json;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class ArrayHelper {
|
class ArrayHelper {
|
||||||
|
private:
|
||||||
|
template<typename T, typename std::enable_if<!std::is_base_of<ModelBase, T>::value>::value>
|
||||||
|
static void itemFromJson(T& item, const nlohmann::json& json){
|
||||||
|
item = json;
|
||||||
|
}
|
||||||
|
static void itemFromJson(ModelBase& item, const nlohmann::json& json){
|
||||||
|
item.fromJson(json);
|
||||||
|
}
|
||||||
|
template<typename T, typename std::enable_if<!std::is_base_of<ModelBase, T>::value>::value>
|
||||||
|
static nlohmann::json itemtoJson(const T& item){
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
static nlohmann::json itemtoJson(const ModelBase& item){
|
||||||
|
return item.toJson();
|
||||||
|
}
|
||||||
public:
|
public:
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static std::vector<T> fromJson(nlohmann::json& json) {
|
static std::vector<T> fromJson(const nlohmann::json& json) {
|
||||||
std::vector<T> val;
|
std::vector<T> val;
|
||||||
nlohmann::from_json(json, val);
|
if (!json.empty()) {
|
||||||
return val;
|
for (const auto& item : json.items()) {
|
||||||
}
|
T entry;
|
||||||
template<typename T>
|
itemFromJson(entry, item.value());
|
||||||
static nlohmann::json toJson(std::vector<T> val) {
|
val.push_back(entry);
|
||||||
nlohmann::json json;
|
}
|
||||||
nlohmann::to_json(json, val);
|
}
|
||||||
return json;
|
return val;
|
||||||
}
|
}
|
||||||
};
|
template<typename T>
|
||||||
|
static nlohmann::json toJson(const std::vector<T>& val) {
|
||||||
class ModelMapHelper {
|
nlohmann::json json;
|
||||||
public:
|
for(const auto& item : val){
|
||||||
template<typename T>
|
json.push_back(itemtoJson(item));
|
||||||
static std::map<std::string, T> & fromJson(nlohmann::json& json) {
|
}
|
||||||
T *ptrTest;
|
return json;
|
||||||
std::map<std::string, T> val;
|
}
|
||||||
if (dynamic_cast<ModelBase*>(ptrTest) != nullptr) {
|
|
||||||
if (!json.empty()) {
|
|
||||||
for (auto &item : json.items()) {
|
|
||||||
T entry;
|
|
||||||
entry.fromJson(item.value());
|
|
||||||
val.insert(val.end(),
|
|
||||||
std::pair<std::string, T>(item.key(), entry));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
template<typename T>
|
|
||||||
static nlohmann::json toJson(std::map<std::string, T> val) {
|
|
||||||
nlohmann::json json;
|
|
||||||
for (auto const& item : val) {
|
|
||||||
json[item.first] = item.second.toJson();
|
|
||||||
}
|
|
||||||
return json;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class MapHelper {
|
class MapHelper {
|
||||||
|
private:
|
||||||
|
template<typename T, typename std::enable_if<!std::is_base_of<ModelBase, T>::value>::value>
|
||||||
|
static void itemFromJson(T &item, const nlohmann::json& json){
|
||||||
|
item = json;
|
||||||
|
}
|
||||||
|
static void itemFromJson(ModelBase &item, const nlohmann::json& json){
|
||||||
|
item.fromJson(json);
|
||||||
|
}
|
||||||
|
template<typename T, typename std::enable_if<!std::is_base_of<ModelBase, T>::value>::value>
|
||||||
|
static nlohmann::json itemtoJson(const T& item){
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
static nlohmann::json itemtoJson(const ModelBase& item){
|
||||||
|
return item.toJson();
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static std::map<std::string, T> & fromJson(nlohmann::json& json) {
|
static std::map<std::string, T> fromJson(const nlohmann::json& json) {
|
||||||
std::map<std::string, T> val;
|
std::map<std::string, T> val;
|
||||||
if (!json.empty()) {
|
if (!json.empty()) {
|
||||||
for (auto &item : json.items()) {
|
for (const auto& item : json.items()) {
|
||||||
T entry = item.value();
|
T entry;
|
||||||
val.insert(val.end(),
|
itemfromJson(entry, item.value());
|
||||||
std::pair<std::string, T>(item.key(), entry));
|
val.insert(val.end(),
|
||||||
}
|
std::pair<std::string, T>(item.key(), entry));
|
||||||
}
|
}
|
||||||
return val;
|
}
|
||||||
}
|
return val;
|
||||||
template<typename T>
|
}
|
||||||
static nlohmann::json toJson(std::map<std::string, T> val) {
|
template<typename T>
|
||||||
nlohmann::json json;
|
static nlohmann::json toJson(const std::map<std::string, T>& val) {
|
||||||
for (auto const& item : val) {
|
nlohmann::json json;
|
||||||
nlohmann::json jitem = item.second;
|
for (const auto& item : val) {
|
||||||
json[item.first] = jitem;
|
nlohmann::json jitem = itemtoJson(item.second);
|
||||||
}
|
json[item.first] = jitem;
|
||||||
return json;
|
}
|
||||||
}
|
return json;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -77,7 +77,7 @@ nlohmann::json Order::toJson() const
|
|||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Order::fromJson(nlohmann::json& val)
|
void Order::fromJson(const nlohmann::json& val)
|
||||||
{
|
{
|
||||||
if(val.find("id") != val.end())
|
if(val.find("id") != val.end())
|
||||||
{
|
{
|
||||||
|
@ -44,7 +44,7 @@ public:
|
|||||||
void validate() override;
|
void validate() override;
|
||||||
|
|
||||||
nlohmann::json toJson() const override;
|
nlohmann::json toJson() const override;
|
||||||
void fromJson(nlohmann::json& json) override;
|
void fromJson(const nlohmann::json& json) override;
|
||||||
|
|
||||||
/////////////////////////////////////////////
|
/////////////////////////////////////////////
|
||||||
/// Order members
|
/// Order members
|
||||||
|
@ -59,7 +59,8 @@ nlohmann::json Pet::toJson() const
|
|||||||
jsonArray.push_back(ModelBase::toJson(item));
|
jsonArray.push_back(ModelBase::toJson(item));
|
||||||
}
|
}
|
||||||
val["photoUrls"] = jsonArray;
|
val["photoUrls"] = jsonArray;
|
||||||
}
|
|
||||||
|
}
|
||||||
{
|
{
|
||||||
nlohmann::json jsonArray;
|
nlohmann::json jsonArray;
|
||||||
for( auto& item : m_Tags )
|
for( auto& item : m_Tags )
|
||||||
@ -70,7 +71,7 @@ nlohmann::json Pet::toJson() const
|
|||||||
if(jsonArray.size() > 0)
|
if(jsonArray.size() > 0)
|
||||||
{
|
{
|
||||||
val["tags"] = jsonArray;
|
val["tags"] = jsonArray;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(m_StatusIsSet)
|
if(m_StatusIsSet)
|
||||||
{
|
{
|
||||||
@ -81,7 +82,7 @@ nlohmann::json Pet::toJson() const
|
|||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Pet::fromJson(nlohmann::json& val)
|
void Pet::fromJson(const nlohmann::json& val)
|
||||||
{
|
{
|
||||||
if(val.find("id") != val.end())
|
if(val.find("id") != val.end())
|
||||||
{
|
{
|
||||||
@ -100,33 +101,31 @@ void Pet::fromJson(nlohmann::json& val)
|
|||||||
setName(val.at("name"));
|
setName(val.at("name"));
|
||||||
{
|
{
|
||||||
m_PhotoUrls.clear();
|
m_PhotoUrls.clear();
|
||||||
nlohmann::json jsonArray;
|
for( auto& item : val["photoUrls"] )
|
||||||
for( auto& item : val["photoUrls"] )
|
{
|
||||||
{
|
m_PhotoUrls.push_back(item);
|
||||||
m_PhotoUrls.push_back(item);
|
|
||||||
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
m_Tags.clear();
|
m_Tags.clear();
|
||||||
nlohmann::json jsonArray;
|
|
||||||
if(val.find("tags") != val.end())
|
if(val.find("tags") != val.end())
|
||||||
{
|
{
|
||||||
for( auto& item : val["tags"] )
|
for( auto& item : val["tags"] )
|
||||||
{
|
|
||||||
|
|
||||||
if(item.is_null())
|
|
||||||
{
|
{
|
||||||
m_Tags.push_back( Tag() );
|
|
||||||
|
if(item.is_null())
|
||||||
|
{
|
||||||
|
m_Tags.push_back( Tag() );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Tag newItem;
|
||||||
|
newItem.fromJson(item);
|
||||||
|
m_Tags.push_back( newItem );
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
Tag newItem;
|
|
||||||
newItem.fromJson(item);
|
|
||||||
m_Tags.push_back( newItem );
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(val.find("status") != val.end())
|
if(val.find("status") != val.end())
|
||||||
|
@ -47,7 +47,7 @@ public:
|
|||||||
void validate() override;
|
void validate() override;
|
||||||
|
|
||||||
nlohmann::json toJson() const override;
|
nlohmann::json toJson() const override;
|
||||||
void fromJson(nlohmann::json& json) override;
|
void fromJson(const nlohmann::json& json) override;
|
||||||
|
|
||||||
/////////////////////////////////////////////
|
/////////////////////////////////////////////
|
||||||
/// Pet members
|
/// Pet members
|
||||||
|
@ -53,7 +53,7 @@ nlohmann::json Tag::toJson() const
|
|||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tag::fromJson(nlohmann::json& val)
|
void Tag::fromJson(const nlohmann::json& val)
|
||||||
{
|
{
|
||||||
if(val.find("id") != val.end())
|
if(val.find("id") != val.end())
|
||||||
{
|
{
|
||||||
|
@ -44,7 +44,7 @@ public:
|
|||||||
void validate() override;
|
void validate() override;
|
||||||
|
|
||||||
nlohmann::json toJson() const override;
|
nlohmann::json toJson() const override;
|
||||||
void fromJson(nlohmann::json& json) override;
|
void fromJson(const nlohmann::json& json) override;
|
||||||
|
|
||||||
/////////////////////////////////////////////
|
/////////////////////////////////////////////
|
||||||
/// Tag members
|
/// Tag members
|
||||||
|
@ -89,7 +89,7 @@ nlohmann::json User::toJson() const
|
|||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
void User::fromJson(nlohmann::json& val)
|
void User::fromJson(const nlohmann::json& val)
|
||||||
{
|
{
|
||||||
if(val.find("id") != val.end())
|
if(val.find("id") != val.end())
|
||||||
{
|
{
|
||||||
|
@ -44,7 +44,7 @@ public:
|
|||||||
void validate() override;
|
void validate() override;
|
||||||
|
|
||||||
nlohmann::json toJson() const override;
|
nlohmann::json toJson() const override;
|
||||||
void fromJson(nlohmann::json& json) override;
|
void fromJson(const nlohmann::json& json) override;
|
||||||
|
|
||||||
/////////////////////////////////////////////
|
/////////////////////////////////////////////
|
||||||
/// User members
|
/// User members
|
||||||
|
Loading…
x
Reference in New Issue
Block a user