Fastjson, Jackson, Gson the details of the Java Object Conversion JSON

Source: Internet
Author: User
Tags object object

Preface to the Java object in the JSON, if the object has a property value is null, then the JSON serialization should not be serialized out? Compare the following JSON conversion method one, Fastjson 1, Fastjson When converting Java object to JSON, the default is to not serialize the null value corresponding to the keyIn other words, when the property inside the object is empty, when converting to JSON, the properties of those null values are not serialized as follows: Autopartssearchrequest has the following properties:
Mainnew Autopartssearchrequest (); Request.setkeywords ("123"); Request.setsortingfield ("234242"); String str = jsonobject.tojsonstring (request); //fastjson Default conversion is SYSTEM.OUT.PRINTLN (str) that does not serialize the corresponding key of the null value;}   

Output: {"keywords": "123", "Sortingfield": "234242"}, no serialization of those value NULL attribute 2, but if you want to serialize the null corresponding key? Then take a closer look at the Fastjson to convert the Java object to JSON: This is the method: jsonobject.tojsonstring (Object object, Serializerfeature ... features)
Serializerfeature Serialization properties for Fastjson:


Quotefieldnames whether double quotation marks are used when the key is output ———-, the default is true writemapnullvalue--– if the output value is null, the default is false writenullnumberaszero--numeric field if NULL, the output is 0, and not the null writenulllistasempty-–list field if null, the output is [], not  Null writenullstringasempty-character Type field if null, the output is "" and not a null writenullbooleanasfalse–boolean field if  NULL, output is false, not null             
Combining the above, serializerfeature ... features is the array, then we can pass in the parameters we want, such as to serialize NULL, the case is as follows:
Mainnew Autopartssearchrequest (); Request.setkeywords ("123"); Request.setsortingfield ("234242"); String str = jsonobject.tojsonstring (request, Serializerfeature.writemapnullvalue); System.out.println (str);}  

The output results are as follows:

3. If you want the character type field to be null, the conversion output is "", NOT NULL, you need to add one more parameter: Writenullstringasempty, the case is as follows:
Mainnew Autopartssearchrequest (); Request.setkeywords ("123"); Request.setsortingfield ("234242"); String str = jsonobject.tojsonstring (Request, Serializerfeature.writemapnullvalue, Serializerfeature.writenullstringasempty); System.out.println (str);}  

The output is as follows: Two, Jackson 1, Jackson by default is the serialization of NULL corresponding to the key, that is, regardless of whether your object attributes have values, in the conversion of JSON will be serialized out
Mainnew Autopartssearchrequest (); Request.setkeywords ("123"); Request.setsortingfield (New Objectmapper (); String str = mapper.writevalueasstring (request); System.out.println (str); //output (not formatted here): {"Sortingfield": "234242", "Partsclassifyid": null, "Partssubclassifyid": null, " Sortingdirection ": null: ...}   

2, in the same vein, want to not serialize null is also possible, specifically as follows:

1. On-Entity @JsonInclude (include.non_null) //put the tag on the property, If the property is NULL then it does not participate in serialization //if placed on top of the class, then all properties of the class function // Include.Include.ALWAYS default //include.non_default property is not serialized by default / The/include.non_empty property is empty ("") or null is not serialized //include.non_null property is NULL not serialized 2. Code objectmapper mapper = new objectmapper (); Mapper.setserializationinclusion (Include.non_null); //the Mapper object is set by this method, all serialized objects will be serialized according to the rules of the change // Include.Include.ALWAYS default //include.non_default property is not serialized by default / The/include.non_empty property is null ("") or null is not serialized //include.non_null property is NULL not serialized   span>           


Note: only for VO function, Map list does not work, in addition to Jackson can also filter out the properties you set, specific to you to study the source

Or refer to: Jackson's explanation

Third, Gson
1, Gson and Fastjson, the default is to not serialize the null value corresponding to the key, the specific case is as follows:
Mainnew Autopartssearchrequest (); Request.setkeywords ("123"); Request.setsortingfield (New Gsonbuilder (). Create (); String str = G.tojson (request); System.out.println (str); //Output result: {"Sortingfield": "234242", "keywords": "123"}}   


2, if you want to serialize the null value corresponding to the key, only need to change the above creation code to the following code:
New Gsonbuilder (). Serializenulls (). Create ();
The case won't be written.

3, if you want to switch to NULL for the empty string "", you need to manually handle the specific reference: Gson convert null to an empty string from:http://www.voidcn.com/blog/shijing266/article/p-6126128.html

Fastjson, Jackson, Gson the details of the Java Object Conversion JSON

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.