Jackson entity to JSON null or NULL to not participate in serialization

Source: Internet
Author: User
Tags serialization

Http://www.cnblogs.com/yangy608/p/3936848.html

**********************************************************

1. On-Entity

@JsonInclude (Include.non_null)

Place the tag on the property, if the property is null, does not participate in serialization
If placed on top of a class, it works for all properties of the class
Include.Include.ALWAYS Default
The Include.non_default property is not serialized as the default value
Include.non_empty property is empty ("") or null is not serialized
Include.non_null property is NULL not serialized


2. On the Code
Objectmapper mapper = new Objectmapper ();

Mapper.setserializationinclusion (Include.non_null);

The Mapper object is set by this method, and all serialized objects are serialized according to the rules.
Include.Include.ALWAYS Default
The Include.non_default property is not serialized as the default value
Include.non_empty property is empty ("") or null is not serialized
Include.non_null property is NULL not serialized

User user = new User (1, "", null);
String Outjson = mapper.writevalueasstring (user);
System.out.println (Outjson);

Note: The Map list does not work for VO only

For example

 objectmapper mapper = new   Objectmapper (); Mapper.setserializationinclusion (Include.non_null); Map map  = new   HashMap (); Map.put ( "a" , null   "B", "B" ); String ret_val  = mapper.writevalueasstring (map); System.err.println (Ret_val); Map m  = Mapper.readvalue (ret_val, Map. Class   "a") + "|" + M.get ("B"  "B": "B", "a": null  }  null  |b 
New VO (); Vo.seta (null); Vo.setb (          "B"); == Mapper.readvalue (Ret_val1, VO.class+ "|" + V.GETB ());<br> output {"B": "B" }|b

Jackson entity to JSON null or NULL to not participate in serialization

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.