json_decoder.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (C) 2021 Duowan Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing,
  11. * software distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __X_PACK_JSON_DECODER_H
  17. #define __X_PACK_JSON_DECODER_H
  18. #include <fstream>
  19. #include "rapidjson_custom.h"
  20. #include "thirdparty/rapidjson/document.h"
  21. #include "thirdparty/rapidjson/error/en.h"
  22. #include "xdecoder.h"
  23. namespace xpack {
  24. class JsonDecoder:public XDecoder<JsonDecoder>, private noncopyable {
  25. friend class XDecoder<JsonDecoder>;
  26. friend class JsonData;
  27. class MemberIterator {
  28. friend class JsonDecoder;
  29. public:
  30. MemberIterator(const rapidjson::Value::ConstMemberIterator iter, JsonDecoder* parent):_iter(iter),_parent(parent){}
  31. bool operator != (const MemberIterator &that) const {
  32. return _iter != that._iter;
  33. }
  34. MemberIterator& operator ++ () {
  35. ++_iter;
  36. return *this;
  37. }
  38. const char *Key() const {
  39. return _iter->name.GetString();
  40. }
  41. JsonDecoder& Val() const {
  42. return _parent->member(*this, *(_parent->alloc()));
  43. }
  44. private:
  45. rapidjson::Value::ConstMemberIterator _iter;
  46. JsonDecoder* _parent;
  47. };
  48. public:
  49. using xdoc_type::decode;
  50. typedef MemberIterator Iterator;
  51. JsonDecoder(const std::string& str, bool isfile=false):xdoc_type(NULL, ""),_doc(new rapidjson::Document),_val(_doc) {
  52. std::string err;
  53. std::string data;
  54. do {
  55. if (isfile) {
  56. std::ifstream fs(str.c_str(), std::ifstream::binary);
  57. if (!fs) {
  58. err = "Open file["+str+"] fail.";
  59. break;
  60. }
  61. std::string _tmp((std::istreambuf_iterator<char>(fs)), std::istreambuf_iterator<char>());
  62. data.swap(_tmp);
  63. _doc->Parse(data);
  64. } else {
  65. _doc->Parse(str);
  66. }
  67. if (_doc->HasParseError()) {
  68. size_t offset = _doc->GetErrorOffset();
  69. std::string parse_err(rapidjson::GetParseError_En(_doc->GetParseError()));
  70. if (isfile) {
  71. std::string err_data = data.substr(offset, 32);
  72. err = "Parse json file \""+str+"\" fail. err="+parse_err+". offset="+err_data;
  73. break;
  74. } else {
  75. std::string err_data = str.substr(offset, 32);
  76. err = "Parse json string fail. err="+parse_err+". offset="+err_data;
  77. break;
  78. }
  79. }
  80. return;
  81. } while (false);
  82. delete _doc;
  83. _doc = NULL;
  84. throw std::runtime_error(err);
  85. }
  86. JsonDecoder(const rapidjson::Value*v):xdoc_type(NULL, ""),_doc(NULL),_val(v) {
  87. }
  88. ~JsonDecoder() {
  89. for (size_t i=0; i<_collector.size(); ++i) {
  90. delete _collector[i];
  91. }
  92. if (NULL != _doc) {
  93. delete _doc;
  94. _doc = NULL;
  95. }
  96. }
  97. inline const char * Type() const {
  98. return "json";
  99. }
  100. public:
  101. // decode
  102. #define XPACK_JSON_DECODE(f, ...) \
  103. const rapidjson::Value *v = get_val(key); \
  104. bool ret = false; \
  105. if (NULL != v) { \
  106. try { \
  107. val = __VA_ARGS__ v->f(); \
  108. } catch (const std::exception&e) { \
  109. (void)e; \
  110. decode_exception("type unmatch", key); \
  111. } \
  112. ret = true; \
  113. } else if (NULL!=key && Extend::Mandatory(ext)) { \
  114. decode_exception("mandatory key not found", key); \
  115. } \
  116. return ret
  117. bool decode(const char*key, std::string &val, const Extend *ext) {
  118. const rapidjson::Value *v = get_val(key);
  119. if (NULL != v) {
  120. try {
  121. val = std::string(v->GetString(), v->GetStringLength());
  122. } catch (const std::exception&e) {
  123. (void)e;
  124. decode_exception("type unmatch", key);
  125. }
  126. return true;
  127. } else if (NULL!=key && Extend::Mandatory(ext)) {
  128. decode_exception("mandatory key not found", key);
  129. }
  130. return false;
  131. }
  132. bool decode(const char*key, bool &val, const Extend *ext) {
  133. const rapidjson::Value *v = get_val(key);
  134. if (NULL == v) {
  135. if (NULL!=key && Extend::Mandatory(ext)) {
  136. decode_exception("mandatory key not found", key);
  137. }
  138. return false;
  139. } else if (v->IsBool()) {
  140. val = v->GetBool();
  141. return true;
  142. } else if (v->IsInt64()) {
  143. val = (0 != (v->GetInt64()));
  144. return true;
  145. } else {
  146. decode_exception("wish bool, but not bool or int", key);
  147. return false;
  148. }
  149. }
  150. bool decode(const char*key, char &val, const Extend *ext) {
  151. XPACK_JSON_DECODE(GetInt, (char));
  152. }
  153. bool decode(const char*key, signed char &val, const Extend *ext) {
  154. XPACK_JSON_DECODE(GetInt, (char));
  155. }
  156. bool decode(const char*key, unsigned char &val, const Extend *ext) {
  157. XPACK_JSON_DECODE(GetInt, (unsigned char));
  158. }
  159. bool decode(const char*key, short &val, const Extend *ext) {
  160. XPACK_JSON_DECODE(GetInt, (short));
  161. }
  162. bool decode(const char*key, unsigned short &val, const Extend *ext) {
  163. XPACK_JSON_DECODE(GetInt, (unsigned short));
  164. }
  165. bool decode(const char*key, int &val, const Extend *ext) {
  166. XPACK_JSON_DECODE(GetInt);
  167. }
  168. bool decode(const char*key, unsigned int &val, const Extend *ext) {
  169. XPACK_JSON_DECODE(GetUint);
  170. }
  171. bool decode(const char*key, long &val, const Extend *ext) {
  172. XPACK_JSON_DECODE(GetInt64, (long));
  173. }
  174. bool decode(const char*key, unsigned long &val, const Extend *ext) {
  175. XPACK_JSON_DECODE(GetUint64, (unsigned long));
  176. }
  177. bool decode(const char*key, long long &val, const Extend *ext) {
  178. XPACK_JSON_DECODE(GetInt64, (long long));
  179. }
  180. bool decode(const char*key, unsigned long long &val, const Extend *ext) {
  181. XPACK_JSON_DECODE(GetUint64, (unsigned long long));
  182. }
  183. bool decode(const char*key, float &val, const Extend *ext) {
  184. XPACK_JSON_DECODE(GetFloat);
  185. }
  186. bool decode(const char*key, double &val, const Extend *ext) {
  187. XPACK_JSON_DECODE(GetDouble);
  188. }
  189. bool decode(const char*key, long double &val, const Extend *ext) {
  190. XPACK_JSON_DECODE(GetDouble, (long double));
  191. }
  192. // map<int, T> xml not support use number as label.
  193. // So this function is defined here instead of xdecoder.h
  194. template <class K, class T>
  195. typename x_enable_if<numeric<K>::is_integer, bool>::type decode(const char*key, std::map<K,T>& val, const Extend *ext) {
  196. return decode_map<std::map<K,T>, K, T>(key, val, ext, Util::atoi);
  197. }
  198. #ifdef XPACK_SUPPORT_QT
  199. template <class K, class T>
  200. typename x_enable_if<numeric<K>::is_integer, bool>::type decode(const char*key, QMap<K,T>& val, const Extend *ext) {
  201. return decode_map<QMap<K,T>, K, T>(key, val, ext, Util::atoi);
  202. }
  203. #endif
  204. // array
  205. size_t Size() {
  206. if (_val->IsArray()) {
  207. return (size_t)_val->Size();
  208. } else {
  209. return 0;
  210. }
  211. }
  212. JsonDecoder& operator[](size_t index) {
  213. JsonDecoder *d = alloc();
  214. member(index, *d);
  215. return *d;
  216. }
  217. JsonDecoder& operator[](const char*key) {
  218. JsonDecoder *d = alloc();
  219. member(key, *d);
  220. return *d;
  221. }
  222. // iter
  223. Iterator Begin() {
  224. return Iterator(_val->MemberBegin(), this);
  225. }
  226. Iterator End() {
  227. return Iterator(_val->MemberEnd(), this);
  228. }
  229. operator bool() const {
  230. return NULL != _val;
  231. }
  232. private:
  233. JsonDecoder():xdoc_type(NULL, ""),_doc(NULL),_val(NULL) {
  234. }
  235. JsonDecoder& member(size_t index, JsonDecoder&d) const {
  236. if (NULL != _val && _val->IsArray()) {
  237. if (index < (size_t)_val->Size()) {
  238. d.init_base(this, index);
  239. d._val = &(*_val)[(rapidjson::SizeType)index];
  240. } else {
  241. decode_exception("Out of index", NULL);
  242. }
  243. } else {
  244. decode_exception("not array", NULL);
  245. }
  246. return d;
  247. }
  248. JsonDecoder& member(const char*key, JsonDecoder&d) const {
  249. if (NULL != _val && _val->IsObject()) {
  250. rapidjson::Value::ConstMemberIterator iter;
  251. if (_val->MemberEnd()!=(iter=_val->FindMember(key)) && !(iter->value.IsNull())) {
  252. d.init_base(this, key);
  253. d._val = &(iter->value);
  254. }
  255. } else {
  256. decode_exception("not object", key);
  257. }
  258. return d;
  259. }
  260. JsonDecoder& member(const Iterator &iter, JsonDecoder&d) const {
  261. d.init_base(iter._parent, iter._iter->name.GetString());
  262. d._val = &(iter._iter->value);
  263. return d;
  264. }
  265. const rapidjson::Value* get_val(const char *key) {
  266. if (NULL == key) {
  267. return _val;
  268. } else if (NULL != _val) {
  269. rapidjson::Value::ConstMemberIterator iter = _val->FindMember(key);
  270. if (iter != _val->MemberEnd() && !(iter->value.IsNull())) {
  271. return &iter->value;
  272. } else {
  273. return NULL;
  274. }
  275. } else {
  276. return NULL;
  277. }
  278. }
  279. rapidjson::Document* _doc;
  280. const rapidjson::Value* _val;
  281. };
  282. }
  283. #endif