/src/test/java/com/alibaba/json/bvt/NumberFieldTest.java

https://github.com/alibaba/fastjson · Java · 192 lines · 126 code · 66 blank · 0 comment · 0 complexity · a4f1358a885016f5007d6507854bb0d8 MD5 · raw file

  1. package com.alibaba.json.bvt;
  2. import java.math.BigDecimal;
  3. import org.junit.Assert;
  4. import junit.framework.TestCase;
  5. import com.alibaba.fastjson.JSON;
  6. import com.alibaba.fastjson.serializer.SerializeConfig;
  7. import com.alibaba.fastjson.serializer.SerializerFeature;
  8. public class NumberFieldTest extends TestCase {
  9. public void test_codec() throws Exception {
  10. V0 v = new V0();
  11. v.setValue(1001L);
  12. String text = JSON.toJSONString(v);
  13. V0 v1 = JSON.parseObject(text, V0.class);
  14. Assert.assertEquals(v1.getValue().intValue(), v.getValue().intValue());
  15. }
  16. public void test_codec_no_asm() throws Exception {
  17. V0 v = new V0();
  18. v.setValue(1001L);
  19. SerializeConfig mapping = new SerializeConfig();
  20. mapping.setAsmEnable(false);
  21. String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
  22. Assert.assertEquals("{\"value\":1001}", text);
  23. V0 v1 = JSON.parseObject(text, V0.class);
  24. Assert.assertEquals(Integer.valueOf(1001), v1.getValue());
  25. }
  26. public void test_codec_null() throws Exception {
  27. V0 v = new V0();
  28. SerializeConfig mapping = new SerializeConfig();
  29. mapping.setAsmEnable(false);
  30. String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
  31. Assert.assertEquals("{\"value\":null}", text);
  32. V0 v1 = JSON.parseObject(text, V0.class);
  33. Assert.assertEquals(v1.getValue(), v.getValue());
  34. }
  35. public void test_codec_2_no_asm() throws Exception {
  36. V0 v = new V0();
  37. v.setValue(Long.MAX_VALUE);
  38. SerializeConfig mapping = new SerializeConfig();
  39. mapping.setAsmEnable(false);
  40. String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
  41. Assert.assertEquals("{\"value\":" + Long.MAX_VALUE + "}", text);
  42. V0 v1 = JSON.parseObject(text, V0.class);
  43. Assert.assertEquals(new Long(Long.MAX_VALUE), v1.getValue());
  44. }
  45. public void test_codec_2_asm() throws Exception {
  46. V0 v = new V0();
  47. v.setValue(Long.MAX_VALUE);
  48. SerializeConfig mapping = new SerializeConfig();
  49. mapping.setAsmEnable(true);
  50. String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
  51. Assert.assertEquals("{\"value\":" + Long.MAX_VALUE + "}", text);
  52. V0 v1 = JSON.parseObject(text, V0.class);
  53. Assert.assertEquals(new Long(Long.MAX_VALUE), v1.getValue());
  54. }
  55. public void test_codec_3_no_asm() throws Exception {
  56. V0 v = new V0();
  57. v.setValue(new BigDecimal("3.2"));
  58. SerializeConfig mapping = new SerializeConfig();
  59. mapping.setAsmEnable(false);
  60. String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
  61. Assert.assertEquals("{\"value\":3.2}", text);
  62. V0 v1 = JSON.parseObject(text, V0.class);
  63. Assert.assertEquals(new BigDecimal("3.2"), v1.getValue());
  64. }
  65. public void test_codec_3_asm() throws Exception {
  66. V0 v = new V0();
  67. v.setValue(new BigDecimal("3.2"));
  68. SerializeConfig mapping = new SerializeConfig();
  69. mapping.setAsmEnable(true);
  70. String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
  71. Assert.assertEquals("{\"value\":3.2}", text);
  72. V0 v1 = JSON.parseObject(text, V0.class);
  73. Assert.assertEquals(new BigDecimal("3.2"), v1.getValue());
  74. }
  75. public void test_codec_min_no_asm() throws Exception {
  76. V0 v = new V0();
  77. v.setValue(Long.MIN_VALUE);
  78. SerializeConfig mapping = new SerializeConfig();
  79. mapping.setAsmEnable(false);
  80. String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
  81. Assert.assertEquals("{\"value\":" + Long.MIN_VALUE + "}", text);
  82. V0 v1 = JSON.parseObject(text, V0.class);
  83. Assert.assertEquals(new Long(Long.MIN_VALUE), v1.getValue());
  84. }
  85. public void test_codec_min_asm() throws Exception {
  86. V0 v = new V0();
  87. v.setValue(Long.MIN_VALUE);
  88. SerializeConfig mapping = new SerializeConfig();
  89. mapping.setAsmEnable(true);
  90. String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
  91. Assert.assertEquals("{\"value\":" + Long.MIN_VALUE + "}", text);
  92. V0 v1 = JSON.parseObject(text, V0.class);
  93. Assert.assertEquals(new Long(Long.MIN_VALUE), v1.getValue());
  94. }
  95. public void test_codec_null_1() throws Exception {
  96. V0 v = new V0();
  97. SerializeConfig mapping = new SerializeConfig();
  98. mapping.setAsmEnable(false);
  99. String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullNumberAsZero);
  100. Assert.assertEquals("{\"value\":0}", text);
  101. V0 v1 = JSON.parseObject(text, V0.class);
  102. Assert.assertEquals(Integer.valueOf(0), v1.getValue());
  103. }
  104. public void test_codec_null_1_asm() throws Exception {
  105. V0 v = new V0();
  106. SerializeConfig mapping = new SerializeConfig();
  107. mapping.setAsmEnable(true);
  108. String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullNumberAsZero);
  109. Assert.assertEquals("{\"value\":0}", text);
  110. V0 v1 = JSON.parseObject(text, V0.class);
  111. Assert.assertEquals(Integer.valueOf(0), v1.getValue());
  112. }
  113. public void test_codec_cast() throws Exception {
  114. V0 v1 = JSON.parseObject("{\"value\":\"12.3\"}", V0.class);
  115. Assert.assertEquals(new BigDecimal("12.3"), v1.getValue());
  116. }
  117. public static class V0 {
  118. private Number value;
  119. public Number getValue() {
  120. return value;
  121. }
  122. public void setValue(Number value) {
  123. this.value = value;
  124. }
  125. }
  126. }