schema.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. #include "schema.h"
  2. #include "rapidjson\rapidjson.h"
  3. #include "rapidjson\document.h"
  4. #include "rapidjson\writer.h"
  5. #include "rapidjson\stringbuffer.h"
  6. namespace schema{
  7. namespace serializor {
  8. namespace {
  9. template<class AllocatorType> static inline void serialize(rapidjson::Value& json, AllocatorType & allocator, const char * name, const char v[]);
  10. template<class AllocatorType> static inline void serialize(rapidjson::Value& json, AllocatorType & allocator, const char * name, const std::string& v);
  11. template<class AllocatorType, typename T> static inline void serialize(rapidjson::Value& json, AllocatorType & allocator, const char * name, const T& v);
  12. template<class AllocatorType, typename T> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_RECT_FIELD<T>& schema_element);
  13. template<class AllocatorType, typename T> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const char * name, const std::vector<T>& schema_elements);
  14. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_QUESTION& schema_element);
  15. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_ELEMENT_FIELD& schema_element);
  16. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_MATRIX& schema_element);
  17. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_MATRIX_KEGUANTI& schema_element);
  18. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_LOCATE_POINT& schema_element);
  19. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_LOCATE_AREA& schema_element);
  20. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_LOACTE_CROSS& schema_element);
  21. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_MATRIX_TIANTUKAOHAO& schema_element);
  22. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_MATRIX_QUEKAOBIAOJI& schema_element);
  23. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_MATRIX_ABJUANBIAOJI& schema_element);
  24. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_MATRIX_XUANZUOTI& schema_element);
  25. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_CODE& schema_element);
  26. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_CLIP& schema_element);
  27. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_PAGE& schema_element);
  28. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const char * name, const SCHEMA_VERSION& version);
  29. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const char * name, const SCHEMA& schema_element);
  30. static inline void deserialize(float&dst, const rapidjson::Value &json);
  31. static inline void deserialize(double&dst, const rapidjson::Value &json);
  32. static inline void deserialize(bool&dst, const rapidjson::Value &json);
  33. static inline void deserialize(int&dst, const rapidjson::Value &json);
  34. static inline void deserialize(char dst[], const rapidjson::Value &json);
  35. static inline void deserialize(std::string & dst, const rapidjson::Value &json);
  36. template<typename T>static inline void deserialize(T&dst, const rapidjson::Value &json);
  37. template<typename T>static inline void deserialize(T dst[], int& count, int group_size[], int& group_count, const rapidjson::Value &json);
  38. template<typename T>static inline void deserialize(T dst[], int& count, const rapidjson::Value &json);
  39. template<typename T>static bool deserialize(const rapidjson::Value &json, SCHEMA_RECT_FIELD<T> &schema_element);
  40. template<typename T>static bool deserialize(std::vector<T> &schema_elements, const rapidjson::Value &json);
  41. static bool deserialize(const rapidjson::Value &json, SCHEMA_ELEMENT_FIELD &schema_element);
  42. static bool deserialize(const rapidjson::Value &json, SCHEMA_MATRIX &schema_element);
  43. static bool deserialize(const rapidjson::Value &json, SCHEMA_CLIP &schema_element);
  44. static bool deserialize(const rapidjson::Value &json, SCHEMA_CODE &schema_element);
  45. static bool deserialize(const rapidjson::Value &json, SCHEMA_MATRIX_KEGUANTI &schema_element);
  46. static bool deserialize(const rapidjson::Value &json, SCHEMA_LOCATE_AREA &schema_element);
  47. static bool deserialize(const rapidjson::Value &json, SCHEMA_LOACTE_CROSS &schema_element);
  48. static bool deserialize(const rapidjson::Value &json, SCHEMA_LOCATE_POINT &schema_element);
  49. static bool deserialize(const rapidjson::Value &json, SCHEMA_MATRIX_QUEKAOBIAOJI &schema_element);
  50. static bool deserialize(const rapidjson::Value &json, SCHEMA_MATRIX_TIANTUKAOHAO &schema_element);
  51. static bool deserialize(const rapidjson::Value &json, SCHEMA_MATRIX_ABJUANBIAOJI &schema_element);
  52. static bool deserialize(const rapidjson::Value &json, SCHEMA_MATRIX_XUANZUOTI &schema_element);
  53. static bool deserialize(const rapidjson::Value &json, SCHEMA_PAGE &schema_element);
  54. static bool deserialize(const rapidjson::Value &json, SCHEMA_QUESTION &schema_element);
  55. static bool deserialize(const rapidjson::Value &json, SCHEMA &_schema);
  56. static bool deserialize(const rapidjson::Value &json, SCHEMA_VERSION &version);
  57. /*************************************/
  58. template<class AllocatorType> static inline void serialize(rapidjson::Value& json, AllocatorType & allocator, const char * name, const char v[]){
  59. json.AddMember(rapidjson::Value::StringRefType(name), rapidjson::Value::StringRefType(static_cast<const char*>(v)), allocator);
  60. }
  61. template<class AllocatorType> static inline void serialize(rapidjson::Value& json, AllocatorType & allocator, const char * name, const std::string& v){
  62. json.AddMember(rapidjson::Value::StringRefType(name), rapidjson::Value::StringRefType(v.c_str(), v.length()), allocator);
  63. }
  64. template<class AllocatorType, typename T> static inline void serialize(rapidjson::Value& json, AllocatorType & allocator, const char * name, const T& v){
  65. json.AddMember(rapidjson::Value::StringRefType(name), v, allocator);
  66. }
  67. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_QUESTION& schema_element){
  68. serialize(json, allocator, "nID", schema_element.nID);
  69. serialize(json, allocator, "question_code", schema_element.question_code);
  70. serialize(json, allocator, "question_code_new", schema_element.question_code_new);
  71. serialize(json, allocator, "newqt", schema_element.newqt);
  72. serialize(json, allocator, "xiaowen", schema_element.xiaowen);
  73. serialize(json, allocator, "maxQuestionNO", schema_element.maxQuestionNO);
  74. serialize(json, allocator, "tigan", schema_element.tigan);
  75. serialize(json, allocator, "answers", schema_element.answers);
  76. serialize(json, allocator, "answerA", schema_element.answerA);
  77. serialize(json, allocator, "answerB", schema_element.answerB);
  78. serialize(json, allocator, "jiexi", schema_element.jiexi);
  79. serialize(json, allocator, "zhishidian", schema_element.zhishidian);
  80. serialize(json, allocator, "courseCode", schema_element.courseCode);
  81. serialize(json, allocator, "score", schema_element.score);
  82. serialize(json, allocator, "halfScore", schema_element.halfScore);
  83. serialize(json, allocator, "doubleEvl", schema_element.doubleEvl);
  84. serialize(json, allocator, "holdValue", schema_element.holdValue);
  85. serialize(json, allocator, "questionType", schema_element.questionType);
  86. serialize(json, allocator, "nMarkUnit", schema_element.nMarkUnit);
  87. serialize(json, allocator, "openOption", schema_element.openOption);
  88. serialize(json, allocator, "nLevelOne", schema_element.qtLevelOne);
  89. serialize(json, allocator, "nLevelTwo", schema_element.qtLevelTwo);
  90. serialize(json, allocator, "nPostLevelOne", schema_element.qtPostLevelOne);
  91. serialize(json, allocator, "nPostLevelTwo", schema_element.qtPostLevelTwo);
  92. serialize(json, allocator, "showMarkTypeIndex", schema_element.showMarkTypeIndex);
  93. serialize(json, allocator, "postMarkTypeID", schema_element.postMarkTypeID);
  94. serialize(json, allocator, "showSubjectTypeIndex", schema_element.showSubjectTypeIndex);
  95. serialize(json, allocator, "postSubjectTypeID", schema_element.postSubjectTypeID);
  96. }
  97. template<class AllocatorType, typename T> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_RECT_FIELD<T>& schema_element){
  98. serialize(json, allocator, "centerx", schema_element.centerx);
  99. serialize(json, allocator, "centery", schema_element.centery);
  100. serialize(json, allocator, "height", schema_element.height);
  101. serialize(json, allocator, "width", schema_element.width);
  102. }
  103. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_ELEMENT_FIELD& schema_element){
  104. serialize(json, allocator, "nID", schema_element.nID);
  105. serialize(json, allocator, static_cast<const SCHEMA_RECT_FIELD<float>&>(schema_element));
  106. }
  107. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_MATRIX& schema_element){
  108. serialize(json, allocator, "nRows", schema_element.nRows);
  109. serialize(json, allocator, "nCols", schema_element.nCols);
  110. serialize(json, allocator, "optionWidth", schema_element.optionWidth);
  111. serialize(json, allocator, "optionHeight", schema_element.optionHeight);
  112. serialize(json, allocator, "direction", schema_element.direction);
  113. serialize(json, allocator, "outputchars", schema_element.outputchars);
  114. serialize(json, allocator, "outputcharsnew", schema_element.outputcharsnew);
  115. serialize(json, allocator, static_cast<const SCHEMA_ELEMENT_FIELD&>(schema_element));
  116. }
  117. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_MATRIX_KEGUANTI& schema_element){
  118. rapidjson::Value question_groups(rapidjson::kArrayType);
  119. for (int idx_i = 0, idx_q = 0; idx_i < schema_element.group_count; idx_i++)
  120. {
  121. rapidjson::Value question_ids(rapidjson::kArrayType);
  122. for (int idx_j = 0; idx_j < schema_element.group_size[idx_i]; idx_j++, idx_q++){
  123. question_ids.PushBack(schema_element.question_ids[idx_q], allocator);
  124. }
  125. question_groups.PushBack(question_ids, allocator);
  126. }
  127. json.AddMember("question_ids", question_groups, allocator);
  128. serialize(json, allocator, "question_type", schema_element.question_type);
  129. serialize(json, allocator, static_cast<const SCHEMA_MATRIX&>(schema_element));
  130. }
  131. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_LOCATE_POINT& schema_element){
  132. serialize(json, allocator, static_cast<const SCHEMA_ELEMENT_FIELD&>(schema_element));
  133. }
  134. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_LOCATE_AREA& schema_element){
  135. serialize(json, allocator, static_cast<const SCHEMA_ELEMENT_FIELD&>(schema_element));
  136. }
  137. /*SCHEMA_LOACTE_CROSS*/
  138. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_LOACTE_CROSS& schema_element){
  139. serialize(json, allocator, "sign", schema_element.sign);
  140. serialize(json, allocator, "angle", schema_element.angle);
  141. serialize(json, allocator, static_cast<const SCHEMA_ELEMENT_FIELD&>(schema_element));
  142. }
  143. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_MATRIX_TIANTUKAOHAO& schema_element){
  144. serialize(json, allocator, static_cast<const SCHEMA_MATRIX&>(schema_element));
  145. }
  146. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_MATRIX_QUEKAOBIAOJI& schema_element){
  147. serialize(json, allocator, static_cast<const SCHEMA_MATRIX&>(schema_element));
  148. }
  149. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_MATRIX_ABJUANBIAOJI& schema_element){
  150. serialize(json, allocator, static_cast<const SCHEMA_MATRIX&>(schema_element));
  151. }
  152. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_MATRIX_XUANZUOTI& schema_element){
  153. serialize(json, allocator, "xuantishu", schema_element.xuantishu);
  154. serialize(json, allocator, "err_handler_type", schema_element.err_handler_type);
  155. serialize(json, allocator, static_cast<const SCHEMA_MATRIX&>(schema_element));
  156. }
  157. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_CODE& schema_element){
  158. serialize(json, allocator, "bDirection", schema_element.bDirection);
  159. serialize(json, allocator, static_cast<const SCHEMA_ELEMENT_FIELD&>(schema_element));
  160. }
  161. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_CLIP& schema_element){
  162. rapidjson::Value question_ids(rapidjson::kArrayType);
  163. for (int idx_i = 0; idx_i < schema_element.question_count; idx_i++)
  164. {
  165. question_ids.PushBack(schema_element.question_ids[idx_i], allocator);
  166. }
  167. serialize(json, allocator, "area_name", schema_element.area_name);
  168. serialize(json, allocator, "markUnit", schema_element.markUnit);
  169. serialize(json, allocator, "issplit", schema_element.issplit);
  170. serialize(json, allocator, "markUnitPart", schema_element.markUnitPart);
  171. serialize(json, allocator, "question_type", schema_element.question_type);
  172. serialize(json, allocator, "area_name_by_questionNo", schema_element.area_name_by_questionNo);
  173. serialize(json, allocator, "area_name_by_questionNoNew", schema_element.area_name_by_questionNoNew);
  174. serialize(json, allocator, "xuanzutiArea", schema_element.xuanzutiArea);
  175. json.AddMember("question_ids", question_ids, allocator);
  176. serialize(json, allocator, static_cast<const SCHEMA_ELEMENT_FIELD&>(schema_element));
  177. }
  178. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const SCHEMA_PAGE& schema_element){
  179. serialize(json, allocator, "abjuanbjiaoji", schema_element.abjuanbjiaoji);
  180. serialize(json, allocator, "clips", schema_element.clips);
  181. serialize(json, allocator, "clips2", schema_element.clips2);
  182. serialize(json, allocator, "codes", schema_element.codes);
  183. serialize(json, allocator, "keguantiMatrix", schema_element.keguantiMatrix);
  184. serialize(json, allocator, "locateAreas", schema_element.locateAreas);
  185. serialize(json, allocator, "locateCrosses", schema_element.locateCrosses);
  186. serialize(json, allocator, "locatePoints", schema_element.locatePoints);
  187. serialize(json, allocator, "matrixs", schema_element.matrixs);
  188. serialize(json, allocator, "quekaobiaoji", schema_element.quekaobiaoji);
  189. serialize(json, allocator, "tiantukaohao", schema_element.tiantukaohao);
  190. serialize(json, allocator, "xuanzuoti", schema_element.xuanzuoti);
  191. serialize(json, allocator, "angle", schema_element.angle);
  192. serialize(json, allocator, "height", schema_element.height);
  193. serialize(json, allocator, "index", schema_element.index);
  194. serialize(json, allocator, "nID", schema_element.nID);
  195. serialize(json, allocator, "width", schema_element.width);
  196. }
  197. template<class AllocatorType, typename T> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const char * name, const std::vector<T>& schema_elements){
  198. rapidjson::Value array_(rapidjson::kArrayType);
  199. for (int idx_i = 0; idx_i < schema_elements.size(); idx_i++)
  200. {
  201. rapidjson::Value elment(rapidjson::kObjectType);
  202. const T & element_ = schema_elements[idx_i];
  203. serialize(elment, allocator, element_);
  204. array_.PushBack(elment, allocator);
  205. }
  206. json.AddMember(rapidjson::Value::StringRefType(name), array_, allocator);
  207. }
  208. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const char * name, const SCHEMA_VERSION& version){
  209. rapidjson::Value object_(rapidjson::kObjectType);
  210. serialize(object_, allocator, "majorNum", version.majorNum);
  211. serialize(object_, allocator, "minorNum", version.minorNum);
  212. serialize(object_, allocator, "company", version.company);
  213. json.AddMember(rapidjson::Value::StringRefType(name), object_, allocator);
  214. }
  215. template<class AllocatorType> static void serialize(rapidjson::Value& json, AllocatorType & allocator, const char * name, const SCHEMA& schema_element){
  216. rapidjson::Value object_(rapidjson::kObjectType);
  217. serialize(object_, allocator, "course_code", schema_element.course_code);
  218. // serialize(object_, allocator, "strTimu", schema_element.strTiMuTotal);
  219. serialize(object_, allocator, "lastKeGuanTiDirection", schema_element.lastKeGuanTiDirection);
  220. serialize(object_, allocator, "nNextObjID", schema_element.nNextObjID);
  221. serialize(object_, allocator, "state", schema_element.state);
  222. serialize(object_, allocator, "totalScore", schema_element.totalScore);
  223. serialize(object_, allocator, "pages", schema_element.pages);
  224. serialize(object_, allocator, "questions", schema_element.questions);
  225. json.AddMember(rapidjson::Value::StringRefType(name), object_, allocator);
  226. }
  227. template<typename T>static inline void deserialize(T&dst, const rapidjson::Value &json){
  228. dst = (T)json.GetInt();
  229. }
  230. static inline void deserialize(float&dst, const rapidjson::Value &json){
  231. dst = json.GetDouble();
  232. }
  233. static inline void deserialize(double&dst, const rapidjson::Value &json){
  234. dst = json.GetDouble();
  235. }
  236. static inline void deserialize(bool&dst, const rapidjson::Value &json){
  237. dst = json.GetBool();
  238. }
  239. static inline void deserialize(int&dst, const rapidjson::Value &json){
  240. dst = json.GetInt();
  241. }
  242. static inline void deserialize(char dst[], const rapidjson::Value &json){
  243. strcpy(dst, json.GetString());
  244. }
  245. template<typename T>static inline void deserialize(T dst[], int& count, int group_size[], int& group_count, const rapidjson::Value &json){
  246. group_count = json.Size();
  247. count = 0;
  248. for (int idx_i = 0, idx_x = 0; idx_i < json.Size(); idx_i++)
  249. {
  250. const rapidjson::Value &json2 = json[idx_i];
  251. group_size[idx_i] = json2.Size();
  252. for (int idx_j = 0; idx_j < group_size[idx_i]; idx_j++, idx_x++)
  253. {
  254. deserialize(dst[idx_x], json2[idx_j]);
  255. count++;
  256. }
  257. }
  258. }
  259. template<typename T>static inline void deserialize(T dst[], int& count, const rapidjson::Value &json){
  260. count = json.Size();
  261. for (int idx_i = 0; idx_i < count; idx_i++)
  262. {
  263. deserialize(dst[idx_i], json[idx_i]);
  264. }
  265. }
  266. static inline void deserialize(std::string & dst, const rapidjson::Value &json){
  267. dst = json.GetString();
  268. }
  269. template<typename T>static bool deserialize(const rapidjson::Value &json, SCHEMA_RECT_FIELD<T> &schema_element){
  270. deserialize(schema_element.centerx, json["centerx"]);
  271. deserialize(schema_element.centery, json["centery"]);
  272. deserialize(schema_element.width, json["width"]);
  273. deserialize(schema_element.height, json["height"]);
  274. return true;
  275. }
  276. static bool deserialize(const rapidjson::Value &json, SCHEMA_ELEMENT_FIELD &schema_element){
  277. deserialize(schema_element.nID, json["nID"]);
  278. deserialize(json, static_cast<SCHEMA_RECT_FIELD<float>&>(schema_element));
  279. return true;
  280. }
  281. static bool deserialize(const rapidjson::Value &json, SCHEMA_MATRIX &schema_element){
  282. deserialize(schema_element.nRows, json["nRows"]);
  283. deserialize(schema_element.nCols, json["nCols"]);
  284. deserialize(schema_element.optionWidth, json["optionWidth"]);
  285. deserialize(schema_element.optionHeight, json["optionHeight"]);
  286. deserialize(schema_element.direction, json["direction"]);
  287. deserialize(schema_element.outputchars, json["outputchars"]);
  288. deserialize(schema_element.outputcharsnew, json["outputcharsnew"]);
  289. deserialize(json, static_cast<SCHEMA_ELEMENT_FIELD&>(schema_element));
  290. return true;
  291. }
  292. static bool deserialize(const rapidjson::Value &json, SCHEMA_CLIP &schema_element){
  293. deserialize(schema_element.area_name, json["area_name"]);
  294. deserialize(schema_element.markUnit, json["markUnit"]);
  295. deserialize(schema_element.issplit, json["issplit"]);
  296. deserialize(schema_element.markUnitPart, json["markUnitPart"]);
  297. deserialize(schema_element.question_type, json["question_type"]);
  298. deserialize(schema_element.area_name_by_questionNo, json["area_name_by_questionNo"]);
  299. deserialize(schema_element.area_name_by_questionNoNew, json["area_name_by_questionNoNew"]);
  300. deserialize(schema_element.question_ids, schema_element.question_count, json["question_ids"]);
  301. if (json.HasMember("xuanzutiArea"))
  302. deserialize(schema_element.xuanzutiArea, json["xuanzutiArea"]);
  303. deserialize(json, static_cast<SCHEMA_ELEMENT_FIELD&>(schema_element));
  304. return true;
  305. }
  306. static bool deserialize(const rapidjson::Value &json, SCHEMA_CODE &schema_element){
  307. deserialize(schema_element.bDirection, json["bDirection"]);
  308. deserialize(json, static_cast<SCHEMA_ELEMENT_FIELD&>(schema_element));
  309. return true;
  310. }
  311. static bool deserialize(const rapidjson::Value &json, SCHEMA_LOCATE_AREA &schema_element){
  312. deserialize(json, static_cast<SCHEMA_ELEMENT_FIELD&>(schema_element));
  313. return true;
  314. }
  315. static bool deserialize(const rapidjson::Value &json, SCHEMA_LOACTE_CROSS &schema_element){
  316. if (json.FindMember("sign") != json.MemberEnd())deserialize(schema_element.sign, json["sign"]);
  317. if (json.FindMember("angle") != json.MemberEnd())deserialize(schema_element.angle, json["angle"]);
  318. deserialize(json, static_cast<SCHEMA_ELEMENT_FIELD&>(schema_element));
  319. return true;
  320. }
  321. static bool deserialize(const rapidjson::Value &json, SCHEMA_LOCATE_POINT &schema_element){
  322. deserialize(json, static_cast<SCHEMA_ELEMENT_FIELD&>(schema_element));
  323. return true;
  324. }
  325. static bool deserialize(const rapidjson::Value &json, SCHEMA_MATRIX_QUEKAOBIAOJI &schema_element){
  326. deserialize(json, static_cast<SCHEMA_MATRIX&>(schema_element));
  327. return true;
  328. }
  329. static bool deserialize(const rapidjson::Value &json, SCHEMA_MATRIX_TIANTUKAOHAO &schema_element){
  330. deserialize(json, static_cast<SCHEMA_MATRIX&>(schema_element));
  331. return true;
  332. }
  333. static bool deserialize(const rapidjson::Value &json, SCHEMA_MATRIX_ABJUANBIAOJI &schema_element){
  334. deserialize(json, static_cast<SCHEMA_MATRIX&>(schema_element));
  335. return true;
  336. }
  337. static bool deserialize(const rapidjson::Value &json, SCHEMA_MATRIX_KEGUANTI &schema_element){
  338. deserialize(schema_element.question_type, json["question_type"]);
  339. deserialize(schema_element.question_ids, schema_element.question_count, schema_element.group_size, schema_element.group_count, json["question_ids"]);
  340. deserialize(json, static_cast<SCHEMA_MATRIX&>(schema_element));
  341. return true;
  342. }
  343. static bool deserialize(const rapidjson::Value &json, SCHEMA_MATRIX_XUANZUOTI &schema_element){
  344. deserialize(schema_element.xuantishu, json["xuantishu"]);
  345. deserialize(schema_element.err_handler_type, json["err_handler_type"]);
  346. deserialize(json, static_cast<SCHEMA_MATRIX&>(schema_element));
  347. return true;
  348. }
  349. static bool deserialize(const rapidjson::Value &json, SCHEMA_PAGE &schema_element){
  350. deserialize(schema_element.abjuanbjiaoji, json["abjuanbjiaoji"]);
  351. deserialize(schema_element.angle, json["angle"]);
  352. deserialize(schema_element.clips, json["clips"]);
  353. deserialize(schema_element.clips2, json["clips2"]);
  354. deserialize(schema_element.codes, json["codes"]);
  355. deserialize(schema_element.height, json["height"]);
  356. deserialize(schema_element.index, json["index"]);
  357. deserialize(schema_element.keguantiMatrix, json["keguantiMatrix"]);
  358. deserialize(schema_element.locateAreas, json["locateAreas"]);
  359. deserialize(schema_element.locateCrosses, json["locateCrosses"]);
  360. deserialize(schema_element.locatePoints, json["locatePoints"]);
  361. deserialize(schema_element.matrixs, json["matrixs"]);
  362. deserialize(schema_element.nID, json["nID"]);
  363. deserialize(schema_element.quekaobiaoji, json["quekaobiaoji"]);
  364. deserialize(schema_element.tiantukaohao, json["tiantukaohao"]);
  365. deserialize(schema_element.width, json["width"]);
  366. deserialize(schema_element.xuanzuoti, json["xuanzuoti"]);
  367. return true;
  368. }
  369. static bool deserialize(const rapidjson::Value &json, SCHEMA_QUESTION &schema_element){
  370. deserialize(schema_element.answerA, json["answerA"]);
  371. deserialize(schema_element.answerB, json["answerB"]);
  372. deserialize(schema_element.answers, json["answers"]);
  373. deserialize(schema_element.courseCode, json["courseCode"]);
  374. deserialize(schema_element.doubleEvl, json["doubleEvl"]);
  375. deserialize(schema_element.halfScore, json["halfScore"]);
  376. deserialize(schema_element.holdValue, json["holdValue"]);
  377. deserialize(schema_element.jiexi, json["jiexi"]);
  378. deserialize(schema_element.nID, json["nID"]);
  379. deserialize(schema_element.nMarkUnit, json["nMarkUnit"]);
  380. deserialize(schema_element.openOption, json["openOption"]);
  381. deserialize(schema_element.questionType, json["questionType"]);
  382. deserialize(schema_element.question_code, json["question_code"]);
  383. deserialize(schema_element.question_code_new, json["question_code_new"]);
  384. deserialize(schema_element.newqt, json["newqt"]);
  385. deserialize(schema_element.xiaowen, json["xiaowen"]);
  386. if (json.HasMember("maxQuestionNO"))
  387. deserialize(schema_element.maxQuestionNO, json["maxQuestionNO"]);
  388. deserialize(schema_element.score, json["score"]);
  389. deserialize(schema_element.tigan, json["tigan"]);
  390. deserialize(schema_element.zhishidian, json["zhishidian"]);
  391. deserialize(schema_element.qtLevelOne, json["nLevelOne"]);
  392. deserialize(schema_element.qtLevelTwo, json["nLevelTwo"]);
  393. deserialize(schema_element.qtPostLevelOne, json["nPostLevelOne"]);
  394. deserialize(schema_element.qtPostLevelTwo, json["nPostLevelTwo"]);
  395. deserialize(schema_element.showMarkTypeIndex, json["showMarkTypeIndex"]);
  396. deserialize(schema_element.postMarkTypeID, json["postMarkTypeID"]);
  397. deserialize(schema_element.showSubjectTypeIndex, json["showSubjectTypeIndex"]);
  398. deserialize(schema_element.postSubjectTypeID, json["postSubjectTypeID"]);
  399. return true;
  400. }
  401. template<typename T>static bool deserialize(std::vector<T> &schema_elements, const rapidjson::Value &json){
  402. schema_elements.resize(json.Size());
  403. for (int idx_i = 0; idx_i < json.Size(); idx_i++)
  404. {
  405. deserialize(json[idx_i], schema_elements[idx_i]);
  406. }
  407. return true;
  408. }
  409. static bool deserialize(const rapidjson::Value &json, SCHEMA &_schema){
  410. deserialize(_schema.course_code, json["course_code"]);
  411. //deserialize(_schema.strTiMuTotal, json["strTimu"]);
  412. deserialize(_schema.nNextObjID, json["nNextObjID"]);
  413. deserialize(_schema.state, json["state"]);
  414. deserialize(_schema.totalScore, json["totalScore"]);
  415. deserialize(_schema.questions, json["questions"]);
  416. deserialize(_schema.pages, json["pages"]);
  417. return true;
  418. }
  419. static bool deserialize(const rapidjson::Value &json, SCHEMA_VERSION &version){
  420. deserialize(version.majorNum, json["majorNum"]);
  421. deserialize(version.minorNum, json["minorNum"]);
  422. deserialize(version.company, json["company"]);
  423. return true;
  424. }
  425. }
  426. void SchemJsonSerializor::serialize(std::string &json, const schema::SCHEMA & _schema, const SCHEMA_VERSION &version /*= CURRENT_SCHEMA_VERSTION*/)
  427. {
  428. rapidjson::Document document;
  429. rapidjson::Document::AllocatorType & allocator = document.GetAllocator();
  430. rapidjson::Value root(rapidjson::kObjectType);
  431. ::schema::serializor::serialize(root, allocator,"schemaVersion", version);
  432. ::schema::serializor::serialize(root, allocator, "schema", _schema);
  433. rapidjson::StringBuffer buffer;
  434. rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
  435. root.Accept(writer);
  436. json = buffer.GetString();
  437. }
  438. bool SchemJsonSerializor::deserialize(const std::string &json, schema::SCHEMA & _schema, SCHEMA_VERSION &version)
  439. {
  440. rapidjson::Document document;
  441. document.Parse(json.c_str());
  442. if (document.IsObject()){
  443. bool success = ::schema::serializor::deserialize(document["schemaVersion"], version);
  444. if (!success) return false;
  445. success = ::schema::serializor::deserialize(document["schema"], _schema);
  446. if (!success) return false;
  447. return true;
  448. }
  449. return false;
  450. }
  451. }
  452. }