After parsing JSON.toJSONString(), "$ref": "$[x].xxx" appears

Reason: When JSON is processing data, the same data appears, and JSON automatically replaces the data of the same node with a reference.

Solution:

String jsonString = JSON.toJSONString(params, SerializerFeature.DisableCircularReferenceDetect);

 SerializerFeature.DisableCircularReferenceDetect: Eliminate the problem of circular references to the same object

Extensions:

 

name meaning
QuoteFieldNames Whether to use double quotes when outputting the key, the default is true
UseSingleQuotes Use single quotes instead of double quotes, defaults to false
WriteMapNullValue Whether to output fields with null values, the default is false
WriteEnumUsingToString

Enum outputs name() or original, the default is false

 
1. The current version of fastjon uses the WriteEnumUsingName attribute for enum objects by default, so the enum value will be serialized into its Name.
2. Use the WriteEnumUsingToString method to convert Enum to the return value of toString() during serialization; at the same time, the override toString function can output the enum value in the required form. However, this will cause a problem. The static method valueof of Enum used in the corresponding deserialization may not recognize the self-generated toString(), resulting in deserialization errors.
3. If you want to save the size of the enum after serialization, you can serialize the ordinal value of the enum and save it as an int type. When fastJson is deserializing, if the value is int, it can use ordinal value matching to find the appropriate object.
To serialize enum to ordinal, fastjson only needs to disable the WriteEnumUsingName feature.
First exclude WriteEnumUsingName based on the default features, and then use the new features for serialization.

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

UseISO8601DateFormat Date is output in ISO8601 format, the default is false
WriteNullListAsEmpty If the List field is null, the output is [] instead of null.
WriteNullStringAsEmpty If the character type field is null, the output is "", not null.
WriteNullNumberAsZero If the numeric field is null, the output is 0 instead of null.
WriteNullBooleanAsFalse If the Boolean field is null, the output is false, not null.
SkipTransientField If it is true, the Field corresponding to the Get method in the class is transient and will be ignored during serialization.
Default is true
SortField Output after sorting by field name. Default is false
WriteTabAsSpecial Use \t to escape the output, the default is false
PrettyFormat Whether the result is formatted, the default is false
WriteClassName Write type information during serialization, default is false. Deserialization is required
DisableCircularReferenceDetect

Eliminate the problem of circular references to the same object. The default is false.
When toJSONString is performed, by default, if the object is reused, the object will be referenced by reference.

 [  
      {  
        "$ref": "$.itemSkuList[0].itemSpecificationList[0]"  
      },   
      {  
        "$ref": "$.itemSkuList[1].itemSpecificationList[0]"  
      }  
    ]  
 

Circular references
In many scenarios, there are circular references in the objects we need to serialize. In many json libraries, this will lead to stackoverflow. In the powerful fastjson, you don't need to worry about this problem. For example:

A a = new A();  
B b = new B(a);  
a.setB(b);  
String text = JSON.toJSONString(a); //{"b":{"a":{"$ref":".."}}}  
A a1 = JSON.parseObject(text, A.class);  
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输出

Guess you like

Origin blog.csdn.net/weixin_51689532/article/details/133393204