JSONObject 项目启动时初始化fastjson的Provider,添加SerializerFeature的策略为WriteMapNullValue

fastjson:SerializerFeature属性使用

  1. 源码
    1. //
    2. // Source code recreated from a .class file by IntelliJ IDEA
    3. // (powered by Fernflower decompiler)
    4. //
    5. package com.alibaba.fastjson.serializer;
    6. public enum SerializerFeature {
    7. QuoteFieldNames,
    8. UseSingleQuotes,
    9. WriteMapNullValue,
    10. WriteEnumUsingToString,
    11. WriteEnumUsingName,
    12. UseISO8601DateFormat,
    13. WriteNullListAsEmpty,
    14. WriteNullStringAsEmpty,
    15. WriteNullNumberAsZero,
    16. WriteNullBooleanAsFalse,
    17. SkipTransientField,
    18. SortField,
    19. /** @deprecated */
    20. @Deprecated
    21. WriteTabAsSpecial,
    22. PrettyFormat,
    23. WriteClassName,
    24. DisableCircularReferenceDetect,
    25. WriteSlashAsSpecial,
    26. BrowserCompatible,
    27. WriteDateUseDateFormat,
    28. NotWriteRootClassName,
    29. DisableCheckSpecialChar,
    30. BeanToArray,
    31. WriteNonStringKeyAsString,
    32. NotWriteDefaultValue,
    33. BrowserSecure,
    34. IgnoreNonFieldGetter;
    35. private final int mask = 1 << this.ordinal();
    36. private SerializerFeature() {
    37. }
    38. public final int getMask() {
    39. return this.mask;
    40. }
    41. public static boolean isEnabled(int features, SerializerFeature feature) {
    42. return (features & feature.getMask()) != 0;
    43. }
    44. public static boolean isEnabled(int features, int fieaturesB, SerializerFeature feature) {
    45. int mask = feature.getMask();
    46. return (features & mask) != 0 || (fieaturesB & mask) != 0;
    47. }
    48. public static int config(int features, SerializerFeature feature, boolean state) {
    49. if(state) {
    50. features |= feature.getMask();
    51. } else {
    52. features &= ~feature.getMask();
    53. }
    54. return features;
    55. }
    56. public static int of(SerializerFeature[] features) {
    57. if(features == null) {
    58. return 0;
    59. } else {
    60. int value = 0;
    61. SerializerFeature[] var2 = features;
    62. int var3 = features.length;
    63. for( int var4 = 0; var4 < var3; ++var4) {
    64. SerializerFeature feature = var2[var4];
    65. value |= feature.getMask();
    66. }
    67. return value;
    68. }
    69. }
    70. }

  2. SerializerFeature属性解释
  3. 名称含义备注
    QuoteFieldNames输出key时是否使用双引号,默认为true 
    UseSingleQuotes使用单引号而不是双引号,默认为false 
    WriteMapNullValue是否输出值为null的字段,默认为false 
    WriteEnumUsingToString
    Enum输出name()或者original,默认为false


    1. 目前版本的fastjon默认对enum对象使用WriteEnumUsingName属性,因此会将enum值序列化为其Name。
    2. 使用WriteEnumUsingToString方法可以序列化时将Enum转换为toString()的返回值;同时override toString函数能够将enum值输出需要的形式。但是这样做会带来一个问题,对应的反序列化使用的Enum的静态方法valueof可能无法识别自行生成的toString(),导致反序列化出错。
    3. 如果将节省enum序列化后的大小,可以将enum序列化其ordinal值,保存为int类型。fastJson在反序列化时,如果值为int,则能够使用ordinal值匹配,找到合适的对象。
      fastjson要将enum序列化为ordinal只需要禁止WriteEnumUsingName feature。
      首先根据默认的features排除WriteEnumUsingName,然后使用新的features序列化即可。

      int features=SerializerFeature.config(JSON.DEFAULT_GENERATE_FEATURE, SerializerFeature.WriteEnumUsingName, false)
      JSON.toJSONString(obj,features,SerializerFeature.EMPTY);



     
    WriteEnumUsingName  
    UseISO8601DateFormatDate使用ISO8601格式输出,默认为false 
    WriteNullListAsEmptyList字段如果为null,输出为[],而非null 
    WriteNullStringAsEmpty字符类型字段如果为null,输出为”“,而非null 
    WriteNullNumberAsZero数值字段如果为null,输出为0,而非null 
    WriteNullBooleanAsFalseBoolean字段如果为null,输出为false,而非null 
    SkipTransientField如果是true,类中的Get方法对应的Field是transient,序列化时将会被忽略。
    默认为true
     
    SortField按字段名称排序后输出。默认为false 
    WriteTabAsSpecial把\t做转义输出,默认为false不推荐
    PrettyFormat结果是否格式化,默认为false不推荐
    WriteClassName序列化时写入类型信息,默认为false。反序列化是需用到不推荐
    DisableCircularReferenceDetect消除对同一对象循环引用的问题,默认为false
    当进行toJSONString的时候,默认如果重用对象的话,会使用引用的方式进行引用对象。
    1.  [  
    2.       {  
    3.         "$ref""$.itemSkuList[0].itemSpecificationList[0]"  
    4.       },   
    5.       {  
    6.         "$ref""$.itemSkuList[1].itemSpecificationList[0]"  
    7.       }  
    8.     ]  

    循环引用

    很多场景中,我们需要序列化的对象中存在循环引用,在许多的json库中,这会导致stackoverflow。在功能强大的fastjson中,你不需要担心这个问题。例如:


    1. A a = new A();  
    2. B b = new B(a);  
    3. a.setB(b);  
    4. String text = JSON.toJSONString(a); //{"b":{"a":{"$ref":".."}}}  
    5. A a1 = JSON.parseObject(text, A.class);  
    6. Assert.assertTrue(a1 == a1.getB().getA());  

    引用是通过"$ref"来表示的

    引用描述
    • "$ref":".."  上一级
    • "$ref":"@"   当前对象,也就是自引用
    • "$ref":"$"   根对象
    • "$ref":"$.children.0"   基于路径的引用,相当于 root.getChildren().get(0)



    不推荐
    WriteSlashAsSpecial对斜杠’/’进行转义不推荐
    BrowserCompatible将中文都会序列化为\uXXXX格式,字节数会多一些,但是能兼容IE 6,默认为false不推荐
    WriteDateUseDateFormat全局修改日期格式,默认为false。
    JSON.DEFFAULT_DATE_FORMAT = “yyyy-MM-dd”;
    JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat);
    不推荐
    DisableCheckSpecialChar一个对象的字符串属性中如果有特殊字符如双引号,将会在转成json时带有反斜杠转移符。如果不需要转义,可以使用这个属性。默认为false不推荐
    NotWriteRootClassName含义不推荐
    BeanToArray将对象转为array输出不推荐
    WriteNonStringKeyAsString 不推荐
    NotWriteDefaultValue 不推荐
    BrowserSecure 不推荐
    IgnoreNonFieldGetter 不推荐
    使用
    JSONObject.toJSONString(实体, SerializerFeature.WriteMapNullValue))


  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值