/src/test/java/com/alibaba/json/bvt/issue_2700/Issue2784.java

https://github.com/alibaba/fastjson · Java · 128 lines · 104 code · 24 blank · 0 comment · 0 complexity · d62999e140183205e58a42eaf8c9aac4 MD5 · raw file

  1. package com.alibaba.json.bvt.issue_2700;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.annotation.JSONField;
  4. import junit.framework.TestCase;
  5. import java.time.LocalDateTime;
  6. import java.time.ZonedDateTime;
  7. import java.util.Date;
  8. public class Issue2784 extends TestCase {
  9. public void test_for_issue() throws Exception {
  10. Model m = new Model();
  11. m.time = java.time.LocalDateTime.now();
  12. String str = JSON.toJSONString(m);
  13. assertEquals("{\"time\":"
  14. + m.time.atZone(JSON.defaultTimeZone.toZoneId()).toInstant().toEpochMilli()
  15. + "}", str);
  16. Model m1 = JSON.parseObject(str, Model.class);
  17. assertEquals(m.time, m1.time);
  18. }
  19. public void test_for_issue_1() throws Exception {
  20. Model m = new Model();
  21. m.ztime = ZonedDateTime.now();
  22. String str = JSON.toJSONString(m);
  23. assertEquals("{\"ztime\":"
  24. + m.ztime.toInstant().toEpochMilli()
  25. + "}", str);
  26. Model m1 = JSON.parseObject(str, Model.class);
  27. assertEquals(m.ztime.toInstant().toEpochMilli(), m1.ztime.toInstant().toEpochMilli());
  28. }
  29. public void test_for_issue_2() throws Exception {
  30. Model m = new Model();
  31. m.time1 = java.time.LocalDateTime.now();
  32. String str = JSON.toJSONString(m);
  33. assertEquals("{\"time1\":"
  34. + m.time1.atZone(JSON.defaultTimeZone.toZoneId()).toEpochSecond()
  35. + "}", str);
  36. Model m1 = JSON.parseObject(str, Model.class);
  37. assertEquals(m.time1.atZone(JSON.defaultTimeZone.toZoneId()).toEpochSecond()
  38. , m1.time1.atZone(JSON.defaultTimeZone.toZoneId()).toEpochSecond());
  39. }
  40. public void test_for_issue_3() throws Exception {
  41. Model m = new Model();
  42. m.ztime1 = ZonedDateTime.now();
  43. String str = JSON.toJSONString(m);
  44. assertEquals("{\"ztime1\":"
  45. + m.ztime1.toEpochSecond()
  46. + "}", str);
  47. Model m1 = JSON.parseObject(str, Model.class);
  48. assertEquals(m.ztime1.toEpochSecond()
  49. , m1.ztime1.toEpochSecond());
  50. }
  51. public void test_for_issue_4() throws Exception {
  52. Model m = new Model();
  53. m.date = new Date();
  54. String str = JSON.toJSONString(m);
  55. assertEquals("{\"date\":"
  56. + m.date.getTime()
  57. + "}", str);
  58. Model m1 = JSON.parseObject(str, Model.class);
  59. assertEquals(m.date.getTime()
  60. , m1.date.getTime());
  61. }
  62. public void test_for_issue_5() throws Exception {
  63. Model m = new Model();
  64. m.date1 = new Date();
  65. String str = JSON.toJSONString(m);
  66. assertEquals("{\"date1\":"
  67. + (m.date1.getTime() / 1000)
  68. + "}", str);
  69. Model m1 = JSON.parseObject(str, Model.class);
  70. assertEquals(m.date1.getTime() / 1000
  71. , m1.date1.getTime() / 1000);
  72. }
  73. public void test_for_issue_6() throws Exception {
  74. Model m = new Model();
  75. m.date1 = new Date();
  76. String str = JSON.toJSONString(m);
  77. assertEquals("{\"date1\":"
  78. + (m.date1.getTime() / 1000)
  79. + "}", str);
  80. Model m1 = JSON.parseObject(str, Model.class);
  81. assertEquals(m.date1.getTime() / 1000
  82. , m1.date1.getTime() / 1000);
  83. }
  84. public void test_for_issue_7() throws Exception {
  85. Model m = JSON.parseObject("{\"time2\":20190714121314}", Model.class);
  86. assertEquals(m.time2, LocalDateTime.of(2019, 7, 14, 12, 13, 14));
  87. }
  88. public static class Model {
  89. @JSONField(format = "millis")
  90. public LocalDateTime time;
  91. @JSONField(format = "millis")
  92. public ZonedDateTime ztime;
  93. @JSONField(format = "unixtime")
  94. public LocalDateTime time1;
  95. @JSONField(format = "unixtime")
  96. public ZonedDateTime ztime1;
  97. @JSONField(format = "millis")
  98. public Date date;
  99. @JSONField(format = "unixtime")
  100. public Date date1;
  101. @JSONField(format = "yyyyMMddHHmmss")
  102. public LocalDateTime time2;
  103. }
  104. }