diff --git a/drogon_ctl/templates/model_cc.csp b/drogon_ctl/templates/model_cc.csp index 262cadee..cf5b22c6 100644 --- a/drogon_ctl/templates/model_cc.csp +++ b/drogon_ctl/templates/model_cc.csp @@ -300,3 +300,28 @@ void {{className}}::updateArgs(drogon::orm::internal::SqlBinder &binder) const } %> } + +Json::Value {{className}}::toJson() const +{ + Json::Value ret; +<%c++for(auto col:cols){%> + if(get<%c++$$<()) + { +<%c++if(col._colDatabaseType=="date"){%> + ret["<%c++$$<"]=get<%c++$$<()->toCustomedFormattedStringLocal("%Y-%m-%d", false); +<%c++}else if(col._colDatabaseType.find("timestamp")!=std::string::npos){%> + ret["<%c++$$<"]=get<%c++$$<()->toCustomedFormattedStringLocal("%Y-%m-%d %H:%M:%S", true); +<%c++}else if(col._colDatabaseType=="bytea"){%> + ret["<%c++$$<"]=drogon::base64Encode((const unsigned char *)get<%c++$$<()->data(),get<%c++$$<()->size()); +<%c++}else{%> + ret["<%c++$$<"]=getValueOf<%c++$$<(); +<%c++}%> + } + else + { + ret["<%c++$$<"]=Json::Value(); + } +<%c++ +}%> + return ret; +} diff --git a/drogon_ctl/templates/model_h.csp b/drogon_ctl/templates/model_h.csp index bc37a1a1..ac46b9b5 100644 --- a/drogon_ctl/templates/model_h.csp +++ b/drogon_ctl/templates/model_h.csp @@ -13,6 +13,7 @@ using namespace drogon_ctl; #include #include #include +#include #include #include #include @@ -112,6 +113,8 @@ auto cols=@@.get>("columns"); const std::vector updateColumns() const; void updateArgs(drogon::orm::internal::SqlBinder &binder) const; + Json::Value toJson() const; + private: <%c++ for(auto col:cols) diff --git a/lib/inc/drogon/utils/Utilities.h b/lib/inc/drogon/utils/Utilities.h index 8a037725..6289f0c6 100755 --- a/lib/inc/drogon/utils/Utilities.h +++ b/lib/inc/drogon/utils/Utilities.h @@ -27,7 +27,7 @@ std::string binaryStringToHex(unsigned char *ptr, size_t length); std::string hexToBinaryString(unsigned char *ptr, size_t length); std::vector splitString(const std::string &str, const std::string &separator); std::string getuuid(); -std::string base64Encode(unsigned char const *bytes_to_encode, unsigned int in_len); +std::string base64Encode(const unsigned char *bytes_to_encode, unsigned int in_len); std::string base64Decode(std::string const &encoded_string); std::string urlDecode(const std::string &szToDecode); int gzipCompress(const char *data, const size_t ndata, diff --git a/lib/src/Utilities.cc b/lib/src/Utilities.cc index 63e04177..03bd371a 100755 --- a/lib/src/Utilities.cc +++ b/lib/src/Utilities.cc @@ -170,7 +170,7 @@ std::string getuuid() return binaryStringToHex(uu, 16); } -std::string base64Encode(unsigned char const *bytes_to_encode, unsigned int in_len) +std::string base64Encode(const unsigned char *bytes_to_encode, unsigned int in_len) { std::string ret; int i = 0;