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