TarsJson.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. #ifndef __TARS_JSON_H__
  2. #define __TARS_JSON_H__
  3. #include <iostream>
  4. #include <cassert>
  5. #include <vector>
  6. #include <map>
  7. #include <string>
  8. #include <stdexcept>
  9. #include <string.h>
  10. #include "tup/TarsType.h"
  11. #include "util/tc_json.h"
  12. #include "util/tc_common.h"
  13. namespace tars
  14. {
  15. class JsonInput
  16. {
  17. public:
  18. template<typename T>
  19. static void readJson(T& c, const JsonValuePtr & p, bool isRequire = true, typename std::enable_if<std::is_same<T, bool>::value, void ***>::type dummy = 0)
  20. {
  21. if(p && p->getType() == eJsonTypeBoolean)
  22. {
  23. c = (T)dynamic_cast<JsonValueBoolean*>(p.get())->value;
  24. }
  25. else if (isRequire)
  26. {
  27. char s[128];
  28. snprintf(s, sizeof(s), "read type mismatch, get type: %d.", p->getType());
  29. throw TC_Json_Exception(s);
  30. }
  31. }
  32. template<typename T>
  33. static void readJson(T& c, const JsonValuePtr & p, bool isRequire = true, typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, bool>::value, void ***>::type dummy = 0)
  34. {
  35. if(p && p->getType() == eJsonTypeNum) {
  36. c = (T)dynamic_cast<JsonValueNum*>(p.get())->lvalue;
  37. }
  38. else if (isRequire)
  39. {
  40. char s[128];
  41. snprintf(s, sizeof(s), "read type mismatch, get type: %d.", p->getType());
  42. throw TC_Json_Exception(s);
  43. }
  44. }
  45. template<typename T>
  46. static void readJson(T& n, const JsonValuePtr & p, bool isRequire = true, typename std::enable_if<std::is_floating_point<T>::value, void ***>::type dummy = 0)
  47. {
  48. if(p && p->getType() == eJsonTypeNum)
  49. {
  50. n = (T)dynamic_cast<JsonValueNum*>(p.get())->value;
  51. }
  52. else if (isRequire)
  53. {
  54. char s[128];
  55. snprintf(s, sizeof(s), "read type mismatch, get type: %d.", p->getType());
  56. throw TC_Json_Exception(s);
  57. }
  58. else if (p && p->getType() == eJsonTypeNull)
  59. {
  60. n = std::numeric_limits<T>::quiet_NaN();
  61. }
  62. }
  63. template<typename T>
  64. static void readJson(T& c, const JsonValuePtr & p, bool isRequire = true, typename std::enable_if<std::is_enum<T>::value, void ***>::type dummy = 0)
  65. {
  66. if(p && p->getType() == eJsonTypeNum)
  67. {
  68. c = (T)dynamic_cast<JsonValueNum*>(p.get())->lvalue;
  69. }
  70. else if (isRequire)
  71. {
  72. char s[128];
  73. snprintf(s, sizeof(s), "read type mismatch, get type: %d.", p->getType());
  74. throw TC_Json_Exception(s);
  75. }
  76. }
  77. template<typename T>
  78. static void readJson(T& s, const JsonValuePtr & p, bool isRequire = true, typename std::enable_if<std::is_same<T, string>::value, void ***>::type dummy = 0)
  79. {
  80. if(p && p->getType() == eJsonTypeString)
  81. {
  82. s = dynamic_cast<JsonValueString*>(p.get())->value;
  83. }
  84. else if (isRequire)
  85. {
  86. char s[128];
  87. snprintf(s, sizeof(s), "read 'string' type mismatch, get type: %d.", p->getType());
  88. throw TC_Json_Exception(s);
  89. }
  90. }
  91. static void readJson(char *buf, const UInt32 bufLen, UInt32 & readLen, const JsonValuePtr & p, bool isRequire = true)
  92. {
  93. if(p && p->getType() == eJsonTypeString)
  94. {
  95. JsonValueString *pString = dynamic_cast<JsonValueString*>(p.get());
  96. if((UInt32)pString->value.size()>bufLen)
  97. {
  98. char s[128];
  99. snprintf(s, sizeof(s), "invalid char * size, size: %u", (UInt32)pString->value.size());
  100. throw TC_Json_Exception(s);
  101. }
  102. memcpy(buf,pString->value.c_str(),pString->value.size());
  103. readLen = (UInt32)pString->value.size();
  104. }
  105. else if (isRequire)
  106. {
  107. char s[128];
  108. snprintf(s, sizeof(s), "read 'char *' type mismatch, get type: %d.", p->getType());
  109. throw TC_Json_Exception(s);
  110. }
  111. }
  112. /// 读取结构数组
  113. template<typename T>
  114. static void readJson(T* v, const UInt32 len, UInt32 & readLen, const JsonValuePtr & p, bool isRequire = true)
  115. {
  116. if(p && p->getType() == eJsonTypeArray)
  117. {
  118. JsonValueArray *pArray = dynamic_cast<JsonValueArray*>(p.get());
  119. if(pArray->value.size()>len)
  120. {
  121. char s[128];
  122. snprintf(s, sizeof(s), "read 'T *' invalid size, size: %u", (uint32_t)pArray->value.size());
  123. throw TC_Json_Exception(s);
  124. }
  125. size_t i = 0;
  126. for(auto it = pArray->value.begin(); it != pArray->value.end(); ++it, ++i )
  127. {
  128. readJson(v[i], *it);
  129. }
  130. readLen=pArray->value.size();
  131. }
  132. else if (isRequire)
  133. {
  134. char s[128];
  135. snprintf(s, sizeof(s), "read 'T *' type mismatch, get type: %d.", p->getType());
  136. throw TC_Json_Exception(s);
  137. }
  138. }
  139. // template<typename T>
  140. // static void readJson(T& v, const JsonValuePtr & p, bool isRequire = true, typename jce::disable_if<jce::is_convertible<T*, TarsStructBase*>, void ***>::type dummy = 0)
  141. // {
  142. // Int32 n = 0;
  143. // readJson(n, p, isRequire);
  144. // v = (T) n;
  145. // }
  146. /// 读取结构
  147. template<typename T>
  148. static void readJson(T& v, const JsonValuePtr &p, bool isRequire = true, typename std::enable_if<std::is_convertible<T*, TarsStructBase*>::value, void ***>::type dummy = 0)
  149. {
  150. if(p && p->getType() == eJsonTypeObj)
  151. {
  152. JsonValueObj *pObj = dynamic_cast<JsonValueObj*>(p.get());
  153. v.readFromJson(pObj);
  154. }
  155. else if (isRequire)
  156. {
  157. char s[128];
  158. snprintf(s, sizeof(s), "read 'Char' type mismatch, get type: %d.", p->getType());
  159. throw TC_Json_Exception(s);
  160. }
  161. }
  162. template<typename V, typename Cmp, typename Alloc>
  163. static void readJson(std::map<string, V, Cmp, Alloc>& m, const JsonValuePtr & p, bool isRequire = true)
  164. {
  165. if(p && p->getType() == eJsonTypeObj)
  166. {
  167. JsonValueObj *pObj = dynamic_cast<JsonValueObj*>(p.get());
  168. auto iter=pObj->value.begin();
  169. for(;iter!=pObj->value.end();++iter)
  170. {
  171. std::pair<string, V> pr;
  172. pr.first=iter->first;
  173. readJson(pr.second,iter->second);
  174. m.insert(pr);
  175. }
  176. }
  177. else if (isRequire)
  178. {
  179. char s[128];
  180. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", p->getType());
  181. throw TC_Json_Exception(s);
  182. }
  183. }
  184. template<typename V, typename H, typename Cmp, typename Alloc>
  185. static void readJson(std::unordered_map<string, V, H, Cmp, Alloc>& m, const JsonValuePtr & p, bool isRequire = true)
  186. {
  187. if(p && p->getType() == eJsonTypeObj)
  188. {
  189. JsonValueObj *pObj = dynamic_cast<JsonValueObj*>(p.get());
  190. auto iter=pObj->value.begin();
  191. for(;iter!=pObj->value.end();++iter)
  192. {
  193. std::pair<string, V> pr;
  194. pr.first=iter->first;
  195. readJson(pr.second,iter->second);
  196. m.insert(pr);
  197. }
  198. }
  199. else if (isRequire)
  200. {
  201. char s[128];
  202. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", p->getType());
  203. throw TC_Json_Exception(s);
  204. }
  205. }
  206. //char特殊处理
  207. template<typename V, typename Cmp, typename Alloc>
  208. static void readJson(std::map<Char, V, Cmp, Alloc>& m, const JsonValuePtr & p, bool isRequire = true)
  209. {
  210. if(p && p->getType() == eJsonTypeObj)
  211. {
  212. JsonValueObj *pObj = dynamic_cast<JsonValueObj*>(p.get());
  213. auto iter=pObj->value.begin();
  214. for(;iter!=pObj->value.end();++iter)
  215. {
  216. std::pair<Char, V> pr;
  217. pr.first=TC_Common::strto<Int32>(iter->first);
  218. readJson(pr.second,iter->second);
  219. m.insert(pr);
  220. }
  221. }
  222. else if (isRequire)
  223. {
  224. char s[128];
  225. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", p->getType());
  226. throw TC_Json_Exception(s);
  227. }
  228. }
  229. template<typename V, typename Cmp, typename Alloc>
  230. static void readJson(std::map<unsigned char, V, Cmp, Alloc>& m, const JsonValuePtr & p, bool isRequire = true)
  231. {
  232. if(p && p->getType() == eJsonTypeObj)
  233. {
  234. JsonValueObj *pObj = dynamic_cast<JsonValueObj*>(p.get());
  235. auto iter=pObj->value.begin();
  236. for(;iter!=pObj->value.end();++iter)
  237. {
  238. std::pair<Char, V> pr;
  239. pr.first=TC_Common::strto<Int32>(iter->first);
  240. readJson(pr.second,iter->second);
  241. m.insert(pr);
  242. }
  243. }
  244. else if (isRequire)
  245. {
  246. char s[128];
  247. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", p->getType());
  248. throw TC_Json_Exception(s);
  249. }
  250. }
  251. template<typename K, typename V, typename H, typename Cmp>
  252. static void readJson(std::map<K, V, H, Cmp>& m, const JsonValuePtr & p, bool isRequire = true, typename std::enable_if<std::is_integral<K>::value, void ***>::type dummy = 0)
  253. {
  254. if(p && p->getType() == eJsonTypeObj)
  255. {
  256. JsonValueObj *pObj = dynamic_cast<JsonValueObj*>(p.get());
  257. auto iter=pObj->value.begin();
  258. for(;iter!=pObj->value.end();++iter)
  259. {
  260. std::pair<K, V> pr;
  261. pr.first=TC_Common::strto<K>(iter->first);
  262. readJson(pr.second,iter->second);
  263. m.insert(pr);
  264. }
  265. }
  266. else if (isRequire)
  267. {
  268. char s[128];
  269. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", p->getType());
  270. throw TC_Json_Exception(s);
  271. }
  272. }
  273. template<typename K, typename V, typename H, typename Cmp>
  274. static void readJson(std::map<K, V, H, Cmp>& m, const JsonValuePtr & p, bool isRequire = true, typename std::enable_if<std::is_floating_point<K>::value, void ***>::type dummy = 0)
  275. {
  276. if(p && p->getType() == eJsonTypeObj)
  277. {
  278. JsonValueObj *pObj = dynamic_cast<JsonValueObj*>(p.get());
  279. auto iter=pObj->value.begin();
  280. for(;iter!=pObj->value.end();++iter)
  281. {
  282. std::pair<K, V> pr;
  283. pr.first=TC_Common::strto<K>(iter->first);
  284. readJson(pr.second,iter->second);
  285. m.insert(pr);
  286. }
  287. }
  288. else if (isRequire)
  289. {
  290. char s[128];
  291. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", p->getType());
  292. throw TC_Json_Exception(s);
  293. }
  294. }
  295. template<typename K, typename V, typename Cmp, typename Alloc>
  296. static void readJson(std::map<K, V, Cmp, Alloc>& m, const JsonValuePtr & p, bool isRequire = true, typename std::enable_if<std::is_enum<K>::value, void ***>::type dummy = 0)
  297. {
  298. if(p && p->getType() == eJsonTypeObj)
  299. {
  300. JsonValueObj *pObj = dynamic_cast<JsonValueObj*>(p.get());
  301. auto iter=pObj->value.begin();
  302. for(;iter!=pObj->value.end();++iter)
  303. {
  304. std::pair<K, V> pr;
  305. pr.first=(K)TC_Common::strto<int>(iter->first);
  306. readJson(pr.second,iter->second);
  307. m.insert(pr);
  308. }
  309. }
  310. else if (isRequire)
  311. {
  312. char s[128];
  313. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", p->getType());
  314. throw TC_Json_Exception(s);
  315. }
  316. }
  317. template<typename K, typename V, typename Cmp, typename Alloc>
  318. static void readJson(std::map<K, V, Cmp, Alloc>& m, const JsonValuePtr & p, bool isRequire = true, typename std::enable_if<std::is_convertible<K*, TarsStructBase*>::value, void ***>::type dummy = 0)
  319. {
  320. if(p && p->getType() == eJsonTypeObj)
  321. {
  322. JsonValueObj *pObj = dynamic_cast<JsonValueObj*>(p.get());
  323. auto iter=pObj->value.begin();
  324. for(;iter!=pObj->value.end();++iter)
  325. {
  326. std::pair<K, V> pr;
  327. pr.first.readFromJsonString(iter->first);
  328. readJson(pr.second,iter->second);
  329. m.insert(pr);
  330. }
  331. }
  332. else if (isRequire)
  333. {
  334. char s[128];
  335. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", p->getType());
  336. throw TC_Json_Exception(s);
  337. }
  338. // char s[128];
  339. // snprintf(s, sizeof(s), "map key is not Basic type. map key is only string|bool|num");
  340. // throw TC_Json_Exception(s);
  341. }
  342. template<typename K, typename V, typename H, typename Cmp, typename Alloc>
  343. static void readJson(std::unordered_map<K, V, H, Cmp, Alloc>& m, const JsonValuePtr & p, bool isRequire = true, typename std::enable_if<std::is_integral<K>::value, void ***>::type dummy = 0)
  344. {
  345. if(p && p->getType() == eJsonTypeObj)
  346. {
  347. JsonValueObj *pObj = dynamic_cast<JsonValueObj*>(p.get());
  348. auto iter=pObj->value.begin();
  349. for(;iter!=pObj->value.end();++iter)
  350. {
  351. std::pair<K, V> pr;
  352. pr.first=TC_Common::strto<K>(iter->first);
  353. readJson(pr.second,iter->second);
  354. m.insert(pr);
  355. }
  356. }
  357. else if (isRequire)
  358. {
  359. char s[128];
  360. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", p->getType());
  361. throw TC_Json_Exception(s);
  362. }
  363. }
  364. template<typename K, typename V, typename H, typename Cmp, typename Alloc>
  365. static void readJson(std::unordered_map<K, V, H, Cmp, Alloc>& m, const JsonValuePtr & p, bool isRequire = true, typename std::enable_if<std::is_floating_point<K>::value, void ***>::type dummy = 0)
  366. {
  367. if(p && p->getType() == eJsonTypeObj)
  368. {
  369. JsonValueObj *pObj = dynamic_cast<JsonValueObj*>(p.get());
  370. auto iter=pObj->value.begin();
  371. for(;iter!=pObj->value.end();++iter)
  372. {
  373. std::pair<K, V> pr;
  374. pr.first=TC_Common::strto<K>(iter->first);
  375. readJson(pr.second,iter->second);
  376. m.insert(pr);
  377. }
  378. }
  379. else if (isRequire)
  380. {
  381. char s[128];
  382. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", p->getType());
  383. throw TC_Json_Exception(s);
  384. }
  385. }
  386. template<typename K, typename V, typename Cmp, typename Alloc>
  387. static void readJson(std::unordered_map<K, V, Cmp, Alloc>& m, const JsonValuePtr & p, bool isRequire = true, typename std::enable_if<std::is_enum<K>::value, void ***>::type dummy = 0)
  388. {
  389. if(p && p->getType() == eJsonTypeObj)
  390. {
  391. JsonValueObj *pObj = dynamic_cast<JsonValueObj*>(p.get());
  392. auto iter=pObj->value.begin();
  393. for(;iter!=pObj->value.end();++iter)
  394. {
  395. std::pair<K, V> pr;
  396. pr.first=TC_Common::strto<int>(iter->first);
  397. readJson(pr.second,iter->second);
  398. m.insert(pr);
  399. }
  400. }
  401. else if (isRequire)
  402. {
  403. char s[128];
  404. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", p->getType());
  405. throw TC_Json_Exception(s);
  406. }
  407. }
  408. template<typename K, typename V, typename Cmp, typename Alloc>
  409. static void readJson(std::unordered_map<K, V, Cmp, Alloc>& m, const JsonValuePtr & p, bool isRequire = true, typename std::enable_if<std::is_convertible<K*, TarsStructBase*>::value, void ***>::type dummy = 0)
  410. {
  411. if(p && p->getType() == eJsonTypeObj)
  412. {
  413. JsonValueObj *pObj = dynamic_cast<JsonValueObj*>(p.get());
  414. auto iter=pObj->value.begin();
  415. for(;iter!=pObj->value.end();++iter)
  416. {
  417. std::pair<K, V> pr;
  418. pr.first.readFromJsonString(iter->first);
  419. readJson(pr.second,iter->second);
  420. m.insert(pr);
  421. }
  422. }
  423. else if (isRequire)
  424. {
  425. char s[128];
  426. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", p->getType());
  427. throw TC_Json_Exception(s);
  428. }
  429. // char s[128];
  430. // snprintf(s, sizeof(s), "map key is not Basic type. map key is only string|bool|num");
  431. // throw TC_Json_Exception(s);
  432. }
  433. template<typename T, typename Alloc>
  434. static void readJson(std::vector<T, Alloc>& v, const JsonValuePtr & p, bool isRequire = true, typename std::enable_if<!std::is_same<T, bool>::value, void ***>::type dummy = 0)
  435. {
  436. if(p && p->getType() == eJsonTypeArray)
  437. {
  438. JsonValueArray *pArray = dynamic_cast<JsonValueArray*>(p.get());
  439. v.resize(pArray->value.size());
  440. size_t i = 0;
  441. for(auto it = pArray->value.begin(); it != pArray->value.end(); ++it, ++i )
  442. {
  443. readJson(v[i], *it);
  444. }
  445. }
  446. else if (isRequire)
  447. {
  448. char s[128];
  449. snprintf(s, sizeof(s), "read 'vector' type mismatch, get type: %d.", p->getType());
  450. throw TC_Json_Exception(s);
  451. }
  452. }
  453. template<typename T, typename Alloc>
  454. static void readJson(std::vector<T, Alloc>& v, const JsonValuePtr & p, bool isRequire = true, typename std::enable_if<std::is_same<T, bool>::value, void ***>::type dummy = 0)
  455. {
  456. if(p && p->getType() == eJsonTypeArray)
  457. {
  458. JsonValueArray *pArray = dynamic_cast<JsonValueArray*>(p.get());
  459. v.resize(pArray->value.size());
  460. size_t i = 0;
  461. for(auto it = pArray->value.begin(); it != pArray->value.end(); ++it, ++i )
  462. {
  463. //vector<bool> 特殊处理
  464. bool b;
  465. readJson(b, *it);
  466. v[i] = b;
  467. }
  468. }
  469. else if (isRequire)
  470. {
  471. char s[128];
  472. snprintf(s, sizeof(s), "read 'vector' type mismatch, get type: %d.", p->getType());
  473. throw TC_Json_Exception(s);
  474. }
  475. }
  476. template<typename T, typename Cmp, typename Alloc>
  477. static void readJson(std::set<T, Cmp, Alloc>& v, const JsonValuePtr & p, bool isRequire = true)
  478. {
  479. if(p && p->getType() == eJsonTypeArray)
  480. {
  481. JsonValueArray *pArray = dynamic_cast<JsonValueArray*>(p.get());
  482. size_t i = 0;
  483. for(auto it = pArray->value.begin(); it != pArray->value.end(); ++it, ++i )
  484. {
  485. T t;
  486. readJson(t, *it);
  487. v.insert(t);
  488. }
  489. }
  490. else if (isRequire)
  491. {
  492. char s[128];
  493. snprintf(s, sizeof(s), "read 'vector' type mismatch, get type: %d.", p->getType());
  494. throw TC_Json_Exception(s);
  495. }
  496. }
  497. template<typename K, typename H, typename Cmp, typename Alloc>
  498. static void readJson(std::unordered_set<K, H, Cmp, Alloc>& v, const JsonValuePtr & p, bool isRequire = true)
  499. {
  500. if(p && p->getType() == eJsonTypeArray)
  501. {
  502. JsonValueArray *pArray = dynamic_cast<JsonValueArray*>(p.get());
  503. size_t i = 0;
  504. for(auto it = pArray->value.begin(); it != pArray->value.end(); ++it, ++i )
  505. {
  506. K t;
  507. readJson(t, *it);
  508. v.insert(t);
  509. }
  510. }
  511. else if (isRequire)
  512. {
  513. char s[128];
  514. snprintf(s, sizeof(s), "read 'vector' type mismatch, get type: %d.", p->getType());
  515. throw TC_Json_Exception(s);
  516. }
  517. }
  518. };
  519. class JsonOutput
  520. {
  521. public:
  522. template<class T>
  523. static JsonValueBooleanPtr writeJson(T b, typename std::enable_if<std::is_same<T, bool>::value, void ***>::type dummy = 0)
  524. {
  525. return new JsonValueBoolean(b);
  526. }
  527. template<class T>
  528. static JsonValueNumPtr writeJson(T b, typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, bool>::value, void ***>::type dummy = 0)
  529. {
  530. return (new JsonValueNum((int64_t)b,true));
  531. }
  532. template<class T>
  533. static JsonValueNumPtr writeJson(T b, typename std::enable_if<std::is_floating_point<T>::value, void ***>::type dummy = 0)
  534. {
  535. return (new JsonValueNum(b, false));
  536. }
  537. template<class T>
  538. static JsonValueStringPtr writeJson(const T &b, typename std::enable_if<std::is_same<T, string>::value, void ***>::type dummy = 0)
  539. {
  540. return (new JsonValueString(b));
  541. }
  542. template<typename T>
  543. static JsonValueNumPtr writeJson(const T& v, typename std::enable_if<std::is_enum<T>::value, void ***>::type dummy = 0)
  544. {
  545. return writeJson((Int32) v);
  546. }
  547. static JsonValueStringPtr writeJson(const char *buf, const UInt32 len)
  548. {
  549. return (new JsonValueString(string(buf,len)));
  550. }
  551. template<typename V, typename Cmp, typename Alloc>
  552. static JsonValueObjPtr writeJson(const std::map<string, V, Cmp, Alloc>& m)
  553. {
  554. JsonValueObjPtr pObj=new JsonValueObj();
  555. for (auto i = m.begin(); i != m.end(); ++i)
  556. {
  557. pObj->value.insert(make_pair(i->first, writeJson(i->second)));
  558. }
  559. return pObj;
  560. }
  561. template<typename V, typename H, typename Cmp, typename Alloc>
  562. static JsonValueObjPtr writeJson(const std::unordered_map<string, V, H, Cmp, Alloc>& m)
  563. {
  564. JsonValueObjPtr pObj=new JsonValueObj();
  565. for (auto i = m.begin(); i != m.end(); ++i)
  566. {
  567. pObj->value.insert(make_pair(i->first, writeJson(i->second)));
  568. }
  569. return pObj;
  570. }
  571. template<typename K, typename V, typename Cmp, typename Alloc>
  572. static JsonValueObjPtr writeJson(const std::map<K, V, Cmp, Alloc>& m, typename std::enable_if<std::is_integral<K>::value, void ***>::type dummy = 0)
  573. {
  574. JsonValueObjPtr pObj=new JsonValueObj();
  575. for (auto i = m.begin(); i != m.end(); ++i)
  576. {
  577. pObj->value.insert(make_pair(TC_Common::tostr(i->first), writeJson(i->second)));
  578. }
  579. return pObj;
  580. }
  581. template<typename K, typename V, typename H, typename Cmp, typename Alloc>
  582. static JsonValueObjPtr writeJson(const std::unordered_map<K, V, H, Cmp, Alloc>& m, typename std::enable_if<std::is_integral<K>::value, void ***>::type dummy = 0)
  583. {
  584. JsonValueObjPtr pObj=new JsonValueObj();
  585. for (auto i = m.begin(); i != m.end(); ++i)
  586. {
  587. pObj->value.insert(make_pair(TC_Common::tostr(i->first), writeJson(i->second)));
  588. }
  589. return pObj;
  590. }
  591. template<typename K, typename V, typename Cmp, typename Alloc>
  592. static JsonValueObjPtr writeJson(const std::map<K, V, Cmp, Alloc>& m, typename std::enable_if<std::is_floating_point<K>::value, void ***>::type dummy = 0)
  593. {
  594. JsonValueObjPtr pObj=new JsonValueObj();
  595. for (auto i = m.begin(); i != m.end(); ++i)
  596. {
  597. pObj->value.insert(make_pair(TC_Common::tostr(i->first), writeJson(i->second)));
  598. }
  599. return pObj;
  600. }
  601. template<typename K, typename V, typename H, typename Cmp, typename Alloc>
  602. static JsonValueObjPtr writeJson(const std::unordered_map<K, V, H, Cmp, Alloc>& m, typename std::enable_if<std::is_floating_point<K>::value, void ***>::type dummy = 0)
  603. {
  604. JsonValueObjPtr pObj=new JsonValueObj();
  605. for (auto i = m.begin(); i != m.end(); ++i)
  606. {
  607. pObj->value.insert(make_pair(TC_Common::tostr(i->first), writeJson(i->second)));
  608. }
  609. return pObj;
  610. }
  611. template<typename K, typename V, typename Cmp, typename Alloc>
  612. static JsonValueObjPtr writeJson(const std::map<K, V, Cmp, Alloc>& m, typename std::enable_if<std::is_enum<K>::value, void ***>::type dummy = 0)
  613. {
  614. JsonValueObjPtr pObj=new JsonValueObj();
  615. for (auto i = m.begin(); i != m.end(); ++i)
  616. {
  617. pObj->value.insert(make_pair(TC_Common::tostr(i->first), writeJson(i->second)));
  618. }
  619. return pObj;
  620. }
  621. template<typename K, typename V, typename H, typename Cmp, typename Alloc>
  622. static JsonValueObjPtr writeJson(const std::unordered_map<K, V, H, Cmp, Alloc>& m, typename std::enable_if<std::is_enum<K>::value, void ***>::type dummy = 0)
  623. {
  624. JsonValueObjPtr pObj=new JsonValueObj();
  625. for (auto i = m.begin(); i != m.end(); ++i)
  626. {
  627. pObj->value.insert(make_pair(TC_Common::tostr(i->first), writeJson(i->second)));
  628. }
  629. return pObj;
  630. }
  631. template<typename K, typename V, typename Cmp, typename Alloc>
  632. static JsonValueObjPtr writeJson(const std::map<K, V, Cmp, Alloc>& m, typename std::enable_if<std::is_convertible<K*, TarsStructBase*>::value, void ***>::type dummy = 0)
  633. {
  634. JsonValueObjPtr pObj=new JsonValueObj();
  635. for (auto i = m.begin(); i != m.end(); ++i)
  636. {
  637. pObj->value.insert(make_pair(i->first.writeToJsonString(), writeJson(i->second)));
  638. }
  639. return pObj;
  640. }
  641. template<typename K, typename V, typename H, typename Cmp, typename Alloc>
  642. static JsonValueObjPtr writeJson(const std::unordered_map<K, V, H, Cmp, Alloc>& m, typename std::enable_if<std::is_convertible<K*, TarsStructBase*>::value, void ***>::type dummy = 0)
  643. {
  644. JsonValueObjPtr pObj=new JsonValueObj();
  645. for (auto i = m.begin(); i != m.end(); ++i)
  646. {
  647. pObj->value.insert(make_pair(i->first.writeToJsonString(), writeJson(i->second)));
  648. }
  649. return pObj;
  650. }
  651. template<typename T, typename Alloc>
  652. static JsonValueArrayPtr writeJson(const std::vector<T, Alloc>& v, typename std::enable_if<!std::is_same<T, bool>::value, void ***>::type dummy = 0)
  653. {
  654. JsonValueArrayPtr pArray=new JsonValueArray();
  655. pArray->value.resize(v.size());
  656. for (size_t i = 0; i < v.size(); i++)
  657. {
  658. pArray->value[i] = writeJson(v[i]);
  659. }
  660. return pArray;
  661. }
  662. template<typename T, typename Alloc>
  663. static JsonValueArrayPtr writeJson(const std::vector<T, Alloc>& v, typename std::enable_if<std::is_same<T, bool>::value, void ***>::type dummy = 0)
  664. {
  665. JsonValueArrayPtr pArray=new JsonValueArray();
  666. pArray->value.resize(v.size());
  667. for (size_t i = 0; i < v.size(); i++) {
  668. //vector<bool> 特殊处理
  669. pArray->value[i] = writeJson((bool)v[i]);
  670. }
  671. return pArray;
  672. }
  673. template<typename T, typename Cmp, typename Alloc>
  674. static JsonValueArrayPtr writeJson(const std::set<T, Cmp, Alloc>& v)
  675. {
  676. JsonValueArrayPtr pArray=new JsonValueArray();
  677. pArray->value.resize(v.size());
  678. auto it = v.begin();
  679. int i = 0;
  680. while(it != v.end())
  681. {
  682. pArray->value[i] = (writeJson(*it));
  683. ++it;
  684. ++i;
  685. }
  686. return pArray;
  687. }
  688. template<typename T, typename H, typename Cmp, typename Alloc>
  689. static JsonValueArrayPtr writeJson(const std::unordered_set<T, H, Cmp, Alloc>& v)
  690. {
  691. JsonValueArrayPtr pArray=new JsonValueArray();
  692. pArray->value.resize(v.size());
  693. auto it = v.begin();
  694. int i = 0;
  695. while(it != v.end())
  696. {
  697. pArray->value[i] = (writeJson(*it));
  698. ++it;
  699. ++i;
  700. }
  701. return pArray;
  702. }
  703. template<typename T>
  704. static JsonValueArrayPtr writeJson(const T *v, const UInt32 len)
  705. {
  706. JsonValueArrayPtr pArray=new JsonValueArray();
  707. pArray->value.resize(len);
  708. for (size_t i = 0; i < len; ++i)
  709. {
  710. pArray->value[i] = (writeJson(v[i]));
  711. }
  712. return pArray;
  713. }
  714. template<typename T>
  715. static JsonValueObjPtr writeJson(const T& v, typename std::enable_if<std::is_convertible<T*, TarsStructBase*>::value, void ***>::type dummy = 0)
  716. {
  717. return JsonValueObjPtr::dynamicCast(v.writeToJson());
  718. }
  719. };
  720. ////////////////////////////////////////////////////////////////////////////////////////////////////
  721. }
  722. #endif