All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cpp-pistache-server.modelbase-header.mustache Maven / Gradle / Ivy

There is a newer version: 7.8.0
Show newest version
{{>licenseInfo}}
/*
 * {{prefix}}ModelBase.h
 *
 * This is the base class for all model classes
 */

#ifndef {{prefix}}ModelBase_H_
#define {{prefix}}ModelBase_H_

{{{defaultInclude}}}
#include "json.hpp"
#include 
#include 
#include 
#include 

{{#modelNamespaceDeclarations}}
namespace {{this}} {
{{/modelNamespaceDeclarations}}

class {{declspec}} {{prefix}}ModelBase
{
public:
    {{prefix}}ModelBase();
    virtual ~{{prefix}}ModelBase();

    virtual void validate() = 0;

    virtual nlohmann::json toJson() const = 0;
    virtual void fromJson(nlohmann::json& json) = 0;

    static std::string toJson(  std::string const& value );
    static std::string toJson(  std::time_t const& value );
    static int32_t toJson( int32_t const value );
    static int64_t toJson( int64_t const value );
    static double toJson( double const value );
    static bool toJson( bool const value );
    static nlohmann::json toJson({{prefix}}ModelBase const& content ); 
};

class {{prefix}}ModelArrayHelper {
public:
	template
	static std::vector fromJson(nlohmann::json& json) {
		T *ptrTest;
		std::vector 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
	static nlohmann::json toJson(std::vector val) {
		nlohmann::json json;
		for(auto item : val){
			json.push_back(item.toJson());
		}
		return json;
	}
};

class {{prefix}}ArrayHelper {
public:
	template
	static std::vector fromJson(nlohmann::json& json) {
		std::vector val;
		nlohmann::from_json(json, val);
		return val;
	}
	template
	static nlohmann::json toJson(std::vector val) {
		nlohmann::json json;
		nlohmann::to_json(json, val);
		return json;
	}
};

class {{prefix}}ModelMapHelper {
public:
	template
	static std::map & fromJson(nlohmann::json& json) {
		T *ptrTest;
		std::map 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(item.key(), entry));
				}
			}
		}
		return val;
	}
	template
	static nlohmann::json toJson(std::map val) {
		nlohmann::json json;
		for (auto const& item : val) {
		  json[item.first] = item.second.toJson();
		}
		return json;
	}
};

class {{prefix}}MapHelper {
public:
	template
	static std::map & fromJson(nlohmann::json& json) {
		std::map val;
		if (!json.empty()) {
			for (auto &item : json.items()) {
				T entry = item.value();
				val.insert(val.end(),
						std::pair(item.key(), entry));
			}
		}
		return val;
	}
	template
	static nlohmann::json toJson(std::map val) {
		nlohmann::json json;
		for (auto const& item : val) {
		  nlohmann::json jitem = item.second;
		  json[item.first] = jitem;
		}
		return json;
	}
};

{{#modelNamespaceDeclarations}}
}
{{/modelNamespaceDeclarations}}

#endif /* {{prefix}}ModelBase_H_ */




© 2015 - 2024 Weber Informatics LLC | Privacy Policy