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

https://github.com/flydream/fastjson · Java · 83 lines · 55 code · 28 blank · 0 comment · 0 complexity · 9781808e01ea8f7143bf76292f9bdaab MD5 · raw file

  1. package com.alibaba.json.bvt;
  2. import junit.framework.Assert;
  3. import junit.framework.TestCase;
  4. import com.alibaba.fastjson.JSON;
  5. import com.alibaba.fastjson.parser.ParserConfig;
  6. import com.alibaba.fastjson.serializer.SerializeConfig;
  7. import com.alibaba.fastjson.serializer.SerializerFeature;
  8. public class LongFieldTest 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. System.out.println(text);
  14. V0 v1 = JSON.parseObject(text, V0.class);
  15. Assert.assertEquals(v1.getValue(), v.getValue());
  16. }
  17. public void test_codec_null() throws Exception {
  18. V0 v = new V0();
  19. SerializeConfig mapping = new SerializeConfig();
  20. mapping.setAsmEnable(false);
  21. String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
  22. Assert.assertEquals("{\"value\":null}", text);
  23. V0 v1 = JSON.parseObject(text, V0.class);
  24. Assert.assertEquals(v1.getValue(), v.getValue());
  25. }
  26. public void test_codec_null_asm() throws Exception {
  27. V0 v = new V0();
  28. SerializeConfig mapping = new SerializeConfig();
  29. mapping.setAsmEnable(true);
  30. String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
  31. Assert.assertEquals("{\"value\":null}", text);
  32. ParserConfig config = new ParserConfig();
  33. config.setAsmEnable(false);
  34. V0 v1 = JSON.parseObject(text, V0.class, config, JSON.DEFAULT_PARSER_FEATURE);
  35. Assert.assertEquals(v1.getValue(), v.getValue());
  36. }
  37. public void test_codec_null_1() throws Exception {
  38. V0 v = new V0();
  39. SerializeConfig mapping = new SerializeConfig();
  40. mapping.setAsmEnable(false);
  41. String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullNumberAsZero);
  42. Assert.assertEquals("{\"value\":0}", text);
  43. V0 v1 = JSON.parseObject(text, V0.class);
  44. Assert.assertEquals(Long.valueOf(0), v1.getValue());
  45. }
  46. public static class V0 {
  47. private Long value;
  48. public Long getValue() {
  49. return value;
  50. }
  51. public void setValue(Long value) {
  52. this.value = value;
  53. }
  54. }
  55. }