/src/test/java/com/alibaba/json/bvtVO/DataTransaction2.java

https://github.com/alibaba/fastjson · Java · 319 lines · 212 code · 83 blank · 24 comment · 0 complexity · d5ebbb987c04f1418aaa7fbe74cdc003 MD5 · raw file

  1. package com.alibaba.json.bvtVO;
  2. import java.io.Serializable;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import org.junit.Assert;
  8. import com.alibaba.fastjson.JSON;
  9. /**
  10. * 交易消息体
  11. * */
  12. public class DataTransaction2 implements Serializable {
  13. private static final long serialVersionUID = 1L;
  14. private Head head = new Head();
  15. private Body body = new Body();
  16. public DataTransaction2() {
  17. }
  18. /**
  19. * Head
  20. **/
  21. class Head {
  22. private String appid;
  23. private String transcode;
  24. private String seqno;
  25. private User user = new User();
  26. private Ret ret = new Ret();
  27. public Head() {
  28. }
  29. class User {
  30. private String id;
  31. public String getId() {
  32. return id;
  33. }
  34. public void setId(String id) {
  35. this.id = id;
  36. }
  37. }
  38. /**
  39. * 处理结果
  40. * */
  41. class Ret {
  42. private String code;
  43. private String msg;
  44. public String getCode() {
  45. return code;
  46. }
  47. public void setCode(String code) {
  48. this.code = code;
  49. }
  50. public String getMsg() {
  51. return msg;
  52. }
  53. public void setMsg(String msg) {
  54. this.msg = msg;
  55. }
  56. }
  57. public String getAppid() {
  58. return appid;
  59. }
  60. public void setAppid(String appid) {
  61. this.appid = appid;
  62. }
  63. public String getTranscode() {
  64. return transcode;
  65. }
  66. public void setTranscode(String transcode) {
  67. this.transcode = transcode;
  68. }
  69. public String getSeqno() {
  70. return seqno;
  71. }
  72. public void setSeqno(String seqno) {
  73. this.seqno = seqno;
  74. }
  75. public User getUser() {
  76. return user;
  77. }
  78. public void setUser(User user) {
  79. this.user = user;
  80. }
  81. public Ret getRet() {
  82. return ret;
  83. }
  84. public void setRet(Ret ret) {
  85. this.ret = ret;
  86. }
  87. public void setRetCode(String code) {
  88. this.ret.code = code;
  89. }
  90. public void setRetMsg(String msg) {
  91. this.ret.msg = msg;
  92. }
  93. }
  94. /**
  95. * Body
  96. * */
  97. @SuppressWarnings("rawtypes")
  98. class Body {
  99. private Param param = new Param();
  100. private DataSet dataset = new DataSet();
  101. public Body() {
  102. }
  103. /**
  104. * 参数
  105. * */
  106. class Param {
  107. private Limit limit = new Limit();
  108. private Map<String, String> form = new HashMap<String, String>();
  109. class Limit {
  110. private String start;
  111. private String size;
  112. private String total;
  113. public String getStart() {
  114. return start;
  115. }
  116. public void setStart(String start) {
  117. this.start = start;
  118. }
  119. public String getSize() {
  120. return size;
  121. }
  122. public void setSize(String size) {
  123. this.size = size;
  124. }
  125. public String getTotal() {
  126. return total;
  127. }
  128. public void setTotal(String total) {
  129. this.total = total;
  130. }
  131. }
  132. public Limit getLimit() {
  133. return limit;
  134. }
  135. public void setLimit(Limit limit) {
  136. this.limit = limit;
  137. }
  138. public Map<String, String> getForm() {
  139. return form;
  140. }
  141. public void setForm(Map<String, String> form) {
  142. this.form = form;
  143. }
  144. }
  145. /**
  146. * 数据集
  147. * */
  148. class DataSet {
  149. private String total;
  150. private List rows = new ArrayList();
  151. public String getTotal() {
  152. return total;
  153. }
  154. public void setTotal(String total) {
  155. this.total = total;
  156. }
  157. public List getRows() {
  158. return rows;
  159. }
  160. public void setRows(List rows) {
  161. this.rows = rows;
  162. }
  163. }
  164. public Param getParam() {
  165. return param;
  166. }
  167. public void setParam(Param param) {
  168. this.param = param;
  169. }
  170. public DataSet getDataset() {
  171. return dataset;
  172. }
  173. public void setDataset(DataSet dataset) {
  174. this.dataset = dataset;
  175. }
  176. public void setDataset(String total, List rows) {
  177. DataSet ds = new DataSet();
  178. ds.setTotal(total);
  179. ds.setRows(rows);
  180. this.dataset = ds;
  181. }
  182. }
  183. public Head getHead() {
  184. return head;
  185. }
  186. public Body getBody() {
  187. return body;
  188. }
  189. public void setHead(Head head) {
  190. this.head = head;
  191. }
  192. public void setBody(Body body) {
  193. this.body = body;
  194. }
  195. /**
  196. * 设置返回的消息信息
  197. * */
  198. public void setRetMsgCode(String code, String msg) {
  199. this.head.setRetCode(code);
  200. this.head.setRetMsg(msg);
  201. }
  202. public void setRetMsgCode(String code) {
  203. this.setRetMsgCode(code, null);
  204. }
  205. /**
  206. * 设置返回的结果集
  207. **/
  208. @SuppressWarnings("rawtypes")
  209. public void setDataSet(String total, List rows) {
  210. this.body.setDataset(total, rows);
  211. }
  212. public static DataTransaction2 fromJSON(String jsonString) {
  213. return JSON.parseObject(jsonString, DataTransaction2.class);
  214. }
  215. public String toJSON() {
  216. return JSON.toJSONString(this);
  217. }
  218. public static void main(String args[]) {
  219. String jsonString = "{'head' : {'appid':'epas','transcode' : '000000','seqno' : '111111111', 'user' : { 'id' : '00000'}, 'ret' : { 'code' : '1', 'msg' : 'txt'} }, 'body' : { param : { form:{ name : '111', sex : '1', address : 'street1', array : [ { id : '1', name : 'tom1' }, { id : '2', name : 'tom2' } ]}, limit : { start : 1, size : 25, total : 100} }, dataset : { total : 1000, rows : [ { id : 'id', name : 'name' }, { id : 'id', name : 'name' } ] } }}";
  220. DataTransaction2 dt = DataTransaction2.fromJSON(jsonString);
  221. System.out.println(dt.toJSON());
  222. DataTransaction2 dt1 = JSON.parseObject(dt.toJSON(), DataTransaction2.class);
  223. System.out.println(dt1.toJSON());
  224. Assert.assertEquals(dt.toJSON(), dt1.toJSON());
  225. System.out.println("=================");
  226. System.out.println(dt.toJSON());
  227. dt.setRetMsgCode("-1", "错误");
  228. dt.setDataSet("1000", new ArrayList<Map<String, Object>>());
  229. System.out.println(dt.toJSON());
  230. String text = dt.toJSON();
  231. System.out.println(text);
  232. DataTransaction2 dt2 = JSON.parseObject(text, DataTransaction2.class);
  233. System.out.println(JSON.toJSONString(dt2));
  234. Assert.assertEquals(dt.toJSON(), dt2.toJSON());
  235. }
  236. }