mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-09 23:36:11 +00:00
[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:
@@ -59,7 +59,7 @@ nlohmann::json ApiResponse::toJson() const
|
||||
return val;
|
||||
}
|
||||
|
||||
void ApiResponse::fromJson(nlohmann::json& val)
|
||||
void ApiResponse::fromJson(const nlohmann::json& val)
|
||||
{
|
||||
if(val.find("code") != val.end())
|
||||
{
|
||||
|
||||
@@ -44,7 +44,7 @@ public:
|
||||
void validate() override;
|
||||
|
||||
nlohmann::json toJson() const override;
|
||||
void fromJson(nlohmann::json& json) override;
|
||||
void fromJson(const nlohmann::json& json) override;
|
||||
|
||||
/////////////////////////////////////////////
|
||||
/// ApiResponse members
|
||||
|
||||
@@ -53,7 +53,7 @@ nlohmann::json Category::toJson() const
|
||||
return val;
|
||||
}
|
||||
|
||||
void Category::fromJson(nlohmann::json& val)
|
||||
void Category::fromJson(const nlohmann::json& val)
|
||||
{
|
||||
if(val.find("id") != val.end())
|
||||
{
|
||||
|
||||
@@ -44,7 +44,7 @@ public:
|
||||
void validate() override;
|
||||
|
||||
nlohmann::json toJson() const override;
|
||||
void fromJson(nlohmann::json& json) override;
|
||||
void fromJson(const nlohmann::json& json) override;
|
||||
|
||||
/////////////////////////////////////////////
|
||||
/// Category members
|
||||
|
||||
@@ -39,7 +39,7 @@ public:
|
||||
virtual void validate() = 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::time_t const& value );
|
||||
@@ -50,100 +50,85 @@ public:
|
||||
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 {
|
||||
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:
|
||||
template<typename T>
|
||||
static std::vector<T> fromJson(nlohmann::json& json) {
|
||||
std::vector<T> val;
|
||||
nlohmann::from_json(json, val);
|
||||
return val;
|
||||
}
|
||||
template<typename T>
|
||||
static nlohmann::json toJson(std::vector<T> val) {
|
||||
nlohmann::json json;
|
||||
nlohmann::to_json(json, val);
|
||||
return json;
|
||||
}
|
||||
};
|
||||
|
||||
class ModelMapHelper {
|
||||
public:
|
||||
template<typename T>
|
||||
static std::map<std::string, T> & fromJson(nlohmann::json& json) {
|
||||
T *ptrTest;
|
||||
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;
|
||||
}
|
||||
template<typename T>
|
||||
static std::vector<T> fromJson(const nlohmann::json& json) {
|
||||
std::vector<T> val;
|
||||
if (!json.empty()) {
|
||||
for (const auto& item : json.items()) {
|
||||
T entry;
|
||||
itemFromJson(entry, item.value());
|
||||
val.push_back(entry);
|
||||
}
|
||||
}
|
||||
return val;
|
||||
}
|
||||
template<typename T>
|
||||
static nlohmann::json toJson(const std::vector<T>& val) {
|
||||
nlohmann::json json;
|
||||
for(const auto& item : val){
|
||||
json.push_back(itemtoJson(item));
|
||||
}
|
||||
return json;
|
||||
}
|
||||
};
|
||||
|
||||
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:
|
||||
template<typename T>
|
||||
static std::map<std::string, T> & fromJson(nlohmann::json& json) {
|
||||
std::map<std::string, T> val;
|
||||
if (!json.empty()) {
|
||||
for (auto &item : json.items()) {
|
||||
T entry = 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) {
|
||||
nlohmann::json jitem = item.second;
|
||||
json[item.first] = jitem;
|
||||
}
|
||||
return json;
|
||||
}
|
||||
template<typename T>
|
||||
static std::map<std::string, T> fromJson(const nlohmann::json& json) {
|
||||
std::map<std::string, T> val;
|
||||
if (!json.empty()) {
|
||||
for (const auto& item : json.items()) {
|
||||
T entry;
|
||||
itemfromJson(entry, item.value());
|
||||
val.insert(val.end(),
|
||||
std::pair<std::string, T>(item.key(), entry));
|
||||
}
|
||||
}
|
||||
return val;
|
||||
}
|
||||
template<typename T>
|
||||
static nlohmann::json toJson(const std::map<std::string, T>& val) {
|
||||
nlohmann::json json;
|
||||
for (const auto& item : val) {
|
||||
nlohmann::json jitem = itemtoJson(item.second);
|
||||
json[item.first] = jitem;
|
||||
}
|
||||
return json;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ nlohmann::json Order::toJson() const
|
||||
return val;
|
||||
}
|
||||
|
||||
void Order::fromJson(nlohmann::json& val)
|
||||
void Order::fromJson(const nlohmann::json& val)
|
||||
{
|
||||
if(val.find("id") != val.end())
|
||||
{
|
||||
|
||||
@@ -44,7 +44,7 @@ public:
|
||||
void validate() override;
|
||||
|
||||
nlohmann::json toJson() const override;
|
||||
void fromJson(nlohmann::json& json) override;
|
||||
void fromJson(const nlohmann::json& json) override;
|
||||
|
||||
/////////////////////////////////////////////
|
||||
/// Order members
|
||||
|
||||
@@ -59,7 +59,8 @@ nlohmann::json Pet::toJson() const
|
||||
jsonArray.push_back(ModelBase::toJson(item));
|
||||
}
|
||||
val["photoUrls"] = jsonArray;
|
||||
}
|
||||
|
||||
}
|
||||
{
|
||||
nlohmann::json jsonArray;
|
||||
for( auto& item : m_Tags )
|
||||
@@ -70,7 +71,7 @@ nlohmann::json Pet::toJson() const
|
||||
if(jsonArray.size() > 0)
|
||||
{
|
||||
val["tags"] = jsonArray;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(m_StatusIsSet)
|
||||
{
|
||||
@@ -81,7 +82,7 @@ nlohmann::json Pet::toJson() const
|
||||
return val;
|
||||
}
|
||||
|
||||
void Pet::fromJson(nlohmann::json& val)
|
||||
void Pet::fromJson(const nlohmann::json& val)
|
||||
{
|
||||
if(val.find("id") != val.end())
|
||||
{
|
||||
@@ -100,33 +101,31 @@ void Pet::fromJson(nlohmann::json& val)
|
||||
setName(val.at("name"));
|
||||
{
|
||||
m_PhotoUrls.clear();
|
||||
nlohmann::json jsonArray;
|
||||
for( auto& item : val["photoUrls"] )
|
||||
{
|
||||
m_PhotoUrls.push_back(item);
|
||||
|
||||
}
|
||||
for( auto& item : val["photoUrls"] )
|
||||
{
|
||||
m_PhotoUrls.push_back(item);
|
||||
|
||||
}
|
||||
}
|
||||
{
|
||||
m_Tags.clear();
|
||||
nlohmann::json jsonArray;
|
||||
if(val.find("tags") != val.end())
|
||||
{
|
||||
for( auto& item : val["tags"] )
|
||||
{
|
||||
|
||||
if(item.is_null())
|
||||
for( auto& item : val["tags"] )
|
||||
{
|
||||
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())
|
||||
|
||||
@@ -47,7 +47,7 @@ public:
|
||||
void validate() override;
|
||||
|
||||
nlohmann::json toJson() const override;
|
||||
void fromJson(nlohmann::json& json) override;
|
||||
void fromJson(const nlohmann::json& json) override;
|
||||
|
||||
/////////////////////////////////////////////
|
||||
/// Pet members
|
||||
|
||||
@@ -53,7 +53,7 @@ nlohmann::json Tag::toJson() const
|
||||
return val;
|
||||
}
|
||||
|
||||
void Tag::fromJson(nlohmann::json& val)
|
||||
void Tag::fromJson(const nlohmann::json& val)
|
||||
{
|
||||
if(val.find("id") != val.end())
|
||||
{
|
||||
|
||||
@@ -44,7 +44,7 @@ public:
|
||||
void validate() override;
|
||||
|
||||
nlohmann::json toJson() const override;
|
||||
void fromJson(nlohmann::json& json) override;
|
||||
void fromJson(const nlohmann::json& json) override;
|
||||
|
||||
/////////////////////////////////////////////
|
||||
/// Tag members
|
||||
|
||||
@@ -89,7 +89,7 @@ nlohmann::json User::toJson() const
|
||||
return val;
|
||||
}
|
||||
|
||||
void User::fromJson(nlohmann::json& val)
|
||||
void User::fromJson(const nlohmann::json& val)
|
||||
{
|
||||
if(val.find("id") != val.end())
|
||||
{
|
||||
|
||||
@@ -44,7 +44,7 @@ public:
|
||||
void validate() override;
|
||||
|
||||
nlohmann::json toJson() const override;
|
||||
void fromJson(nlohmann::json& json) override;
|
||||
void fromJson(const nlohmann::json& json) override;
|
||||
|
||||
/////////////////////////////////////////////
|
||||
/// User members
|
||||
|
||||
Reference in New Issue
Block a user