Talk about Fastjson inverse sequence method Json.parseobject (String text, class<t> clazz)--from production practice

Source: Internet
Author: User
Tags object serialization

Fastjson This toolkit helps us to convert between Java objects and JSON-formatted strings. The process of object-to-string, which we call serialization, is, conversely, called deserialization.

Now let's talk about the deserialization method provided by Fastjson, this article only discusses the deserialization method of returning the corresponding object according to the specified bytecode, which has many overloaded forms, which are designed according to the overlapping pattern. Common entries are: Json.parseobject (String text, class<t> clazz), whose invocation chain is:

Json.parseobject (string text, class<t> clazz)--Parseobject (string text, class<t> clazz, Feature ... Features)--parseobject (String input, Type clazz, parserconfig config, int featurevalues, Feature ... features)- Parseobject (String input, Type clazz, Parserconfig config, parseprocess processor, int featurevalues, Feature ... features )

The method that is finally actually called and its parameter values are: Parseobject (input, Clazz, parserconfig.getglobalinstance (), NULL, default_parser_feature, new Feature[0]).

We focus on parserconfig.getglobalinstance (), each time the call returns the same Parserconfig object. This in fact guarantees the Json.parseobject (String text, class<t> clazz) as the entrance of the scene, the Parserconfig object is globally unique, that is, the so-called Singleton.

Let's look at the role of the Parserconfig object in the Fastjson deserialization process:

Function One: maintains the correspondence between the common type and the Deserializer, stored in the Identityhashmap<type, objectdeserializer>, and can be passed Getdeserializer (type type) The Objectdeserializer method obtains the object Deserializer, and for the non-predefined good type, obtains the type of the Deserializer, and establishes the corresponding relationship between the type and the corresponding Deserializer, and stores it in the Identityhashmap<type, Objectdeserializer> for subsequent direct use;

Role two: Create field Deserializer Fielddeserializer, and these fielddeserializer will be maintained to Objectdeserializer identityhashmap<string, Fielddeserializer>, where key is the field name.

Focus on the generation of Fielddeserializer, through the source analysis, usually will call Asmdeserializerfactory.getinstance (). Createfielddeserializer ( Parserconfig, Clazz, fieldInfo) generates a field deserializer.

if (Fieldclass = = Int.class | | fieldclass = Long.class | | fieldclass = string.class) {
return Createstringfielddeserializer (mapping, Clazz, FieldInfo);
}

Through the source code in the above Createfielddeserializer can be seen, for the int, long and string types have done special processing, further analysis found its internal use of ASM bytecode increase technology to Integerfielddeserializer, Longfielddeserializer and Stringfielddeserializer have been expanded to generate new classes dynamically.

The class name is:String name = "Fastjson_asm__field_" + clazz.getsimplename ();
name + = "_" + fieldinfo.getname () + "_" + seed.incrementandget (); ( note that seed is a singleton in this scenario )

This class is primarily a new SetValue () method, which should be used to perform assignment operations on fields (PS: Internal processing logic for object serialization and field serializers to have a chance to further analyze the study)

All in all: for a JVM that retains a permanent generation, for three types of fields, a new class is dynamically generated when the Fielddeserializer is created, resulting in a rise in the number of classes loaded by the JVM and an increase in permanent memory. Of course, the classes that need to be deserialized in a project are limited, and because Parseconfig is a singleton in the common case, the field serializer class that corresponds to the corresponding field is generated after a copy is not duplicated, and the permanent generation of memory does not normally overflow.

Json.parseobject (Reqmsg, Reqmsgdto.class, New Parserconfig (), jsonobject.default_parser_feature)

However, if the API is invoked as above, in which case the parserconfig is not globally unique, the field serializer class may be generated continuously, resulting in java.lang.OutOfMemoryError:PermGen space. In this scenario, the instance of Parserconfig should be used as a class variable or as a member variable to avoid creating a new field serialization class for each invocation.

Remark: Fastjson source analysis based on 1.1.37 version

Talk about Fastjson inverse sequence method Json.parseobject (String text, class<t> clazz)--from production practice

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.