Fastjson meet the problem or project to optimize the problem, see source code can be solved

Source: Internet
Author: User
Tags lexer tojson

1: It feels like hell. General storage jsonobject fields are missing?

        Jsonobject object=new jsonobject ();        Map fields = new HashMap ();        Fields.put ("1", "1");        Object.put ("Fields", fields);        System.out.println (Object.ToString ());        Jsonobject newfields = Object.getjsonobject ("Fields");        Newfields.put ("2", 2);        TODO  serialized string not 2?!!        //See the source code can be known, when we use map as Jsonobject parsing, Fastjson will return a new object        System.out.println (Object.ToString ());

  

Com.alibaba.fastjson.json#tojson (Java.lang.Object, com.alibaba.fastjson.serializer.SerializeConfig) Source:

public static object ToJSON (object javaobject, Serializeconfig config) {        if (Javaobject = = null) {            return Null;
   }        if (Javaobject instanceof JSON) {            return javaobject;        }        if (javaobject instanceof map) {            map<object, object> map = (Map<object, object>) javaobject; A new jsonobject was created to return the            jsonobject json = new Jsonobject (Map.size ());            For (Map.entry<object, object> entry:map.entrySet ()) {                Object key = Entry.getkey ();                String Jsonkey = typeutils.casttostring (key);                Object Jsonvalue = ToJSON (Entry.getvalue ());                Json.put (Jsonkey, Jsonvalue);            }            return JSON;        }

  

2: When we use Jsonarray, sometimes it is possible to store a larger amount of data, but some scenarios need to be at the specified index at the insert operation, because the Jsonarray default underlying use of ArrayList storage, so there is a performance problem, So is it possible to use LinkedList? The answer can be:

Jsonarray arr = new Jsonarray (new LinkedList ());

  

3: Can we replace the underlying stored ArrayList with LinkedList when we query the interface to get a jsonarray string to deserialize? Answer: Yes, modify the source code or add an overloaded method.

public static Jsonarray Parsearray (String text) {        if (text = = null) {            return null;        }        Defaultjsonparser parser = new Defaultjsonparser (text, parserconfig.getglobalinstance ());        Jsonarray Array;        Jsonlexer lexer = parser.lexer;        if (lexer.token () = = Jsontoken.null) {            lexer.nexttoken ();            Array = null;        } else if (lexer.token () = = jsontoken.eof) {            array = null;        } else {//            array = new Jsonarray (new ArrayList ());            array = new Jsonarray (new LinkedList ());            Parser.parsearray (array);            Parser.handleresovletask (array);        }        Parser.close ();        return array;    }

  

Fastjson meet the problem or project to optimize the problem, see source code can be solved

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.