Jackson ObjectMapper for json Parsing

Source: Internet
Author: User

 

Fastjson and jackson are commonly used for Json parsing. There are a lot of comparisons on the performance of the Internet, saying that fastjson is better. Today we will sort out jackson's stuff and then send another fastjson one.

Jackson is a built-in json Conversion Tool of spring mvc, while fastjson is an open-source toolkit made by Alibaba.

Jackson serialization is as follows:

        String jsonSerialize(     ObjectMapper om =                    }             }

Jackson deserialization is as follows:

         Object jsonDeserialize( String json,  Class<?>     ObjectMapper om =                    }             }

Json deserialization may occur during use. When your object contains get ** (), it is treated as an attribute, so when you serialize the json, during deserialization, it is very likely that an exception occurs, because this *** definition is not available in your model.

What should we do?

Jackson provides his own solution (JacksonHowToIgnoreUnknow ):

1. Add a declaration to ignore unknown attributes on the class: @ JsonIgnoreProperties (ignoreUnknown = true)

2. Add ignore unknown attribute parsing in deserialization, as shown below:

         Object jsonDeserialize( String json,  Class<?>     ObjectMapper om =                        om.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,               }             }

3. Add "Any setter" to process unknown attributes

            }

4. register the issue handling handle

Register a DeserializationProblemHandler handle to call ObjectMapper. addHandler (). When a handle is added, the handleUnknownProperty method of the handle can be called once on each unknown property.

This method is useful when you want to add an unknown attribute to process logs: When the attribute is uncertain, it will not cause a property binding error.

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.