-
-
[旧帖] [求助][求助]请大家指点下,这段c++代码优化的思路 0.00雪花
-
发表于: 2013-7-29 12:38 1307
-
这段代码是生成json的代码,我想请高手给个优化的思路
#ifndef _BMLIB_BMJSON_H #define _BMLIB_BMJSON_H #include "BMStrUtils.h" namespace BMLib { class BMJson { public: static string& AddJsonField(string& str,const char* pszKey,const char* pszValue) { if(str == "") str += "\""; else str += ",\""; str += pszKey ; str += "\""; str += ":"; str += "\""; str += pszValue; str += "\""; return str; } static string& AddJsonObjectField(string& str,const char* pszKey,const char* pszValue) { if(str == "") str += "\""; else str += ",\""; str += pszKey ; str += "\""; str += ":"; str += pszValue; return str; } static string& AddJsonField(string& str,const char* pszKey,int nValue) { if(str == "") str += "\""; else str += ",\""; str += pszKey ; str += "\""; str += ":"; string strValue; BMStrUtils::Format(strValue,"%d",nValue); str += strValue; return str; } static string & AddJsonField(string &str,const char * pszKey,char cValue,int iType = 0) { if(str == "") str += "\""; else str += ",\""; str += pszKey ; str += "\""; str += ":"; string strValue; if(iType == 0) { BMStrUtils::Format(strValue,"%d",cValue); } else{ BMStrUtils::Format(strValue,"%c",cValue); } str += strValue; return str; } static string & AddJsonField(string &str,const char * pszKey,unsigned char cValue) { if(str == "") str += "\""; else str += ",\""; str += pszKey ; str += "\""; str += ":"; string strValue; BMStrUtils::Format(strValue,"%d",cValue); str += strValue; return str; } static string & AddJsonField(string &str,const char * pszKey,short sValue) { if(str == "") str += "\""; else str += ",\""; str += pszKey ; str += "\""; str += ":"; string strValue; BMStrUtils::Format(strValue,"%d",sValue); str += strValue; return str; } static string & AddJsonField(string &str,const char * pszKey,unsigned int iValue) { if(str == "") str += "\""; else str += ",\""; str += pszKey ; str += "\""; str += ":"; string strValue; BMStrUtils::Format(strValue,"%u",iValue); str += strValue; return str; } static string& AddJsonField(string& str,const char* pszKey,float fValue) { if(str == "") str += "\""; else str += ",\""; str += pszKey ; str += "\""; str += ":"; string strValue; BMStrUtils::Format(strValue,"%f",fValue); str += strValue; return str; } //2012-6-22 static string& AddJsonField(string& str,const char* pszKey,long long llValue) { if(str == "") str += "\""; else str += ",\""; str += pszKey ; str += "\""; str += ":"; string strValue; BMStrUtils::Format(strValue,"%ld",llValue); str += strValue; return str; } static string& AddJsonField(string& str,const char* pszKey,long lValue) { if(str == "") str += "\""; else str += ",\""; str += pszKey ; str += "\""; str += ":"; string strValue; BMStrUtils::Format(strValue,"%ld",lValue); str += strValue; return str; } static string& AddJsonField(string& str,const char* pszKey,bool bValue) { if(str == "") str += "\""; else str += ",\""; str += pszKey ; str += "\""; str += ":"; if(bValue) str += "true"; else str += "false"; return str; } static string& AddJsonFirstField(string& str,const char* pszKey,const char* pszValue) { if(str == "") str += "\""; else str += "\""; str += pszKey ; str += "\""; str += ":"; str += "\""; str += pszValue; str += "\""; return str; } static string& AddJsonFirstObjectField(string& str,const char* pszKey,const char* pszValue) { str += "\""; str += pszKey ; str += "\""; str += ":"; str += pszValue; return str; } static string& AddJsonFirstField(string& str,const char* pszKey,int nValue) { str += "\""; str += pszKey ; str += "\""; str += ":"; string strValue; BMStrUtils::Format(strValue,"%d",nValue); str += strValue; return str; } static string& AddJsonFirstField(string& str,const char* pszKey,float fValue) { str += "\""; str += pszKey ; str += "\""; str += ":"; string strValue; BMStrUtils::Format(strValue,"%f",fValue); str += strValue; return str; } static string& AddJsonFirstField(string& str,const char* pszKey,bool bValue) { str += "\""; str += pszKey ; str += "\""; str += ":"; if(bValue) str += "true"; else str += "false"; return str; } static string& AddJsonObject(string& str,const char* pszJson) { if(str == "") str += "{"; else str += ",{"; str += pszJson; str += "}"; return str; } static string& AddJsonArray(string& str,const char* pszJson) { str += "["; str += pszJson; str += "]"; return str; } }; } #endif
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课
赞赏
他的文章
看原图
赞赏
雪币:
留言: