Json
JSON is called Javascriptobject Natation, which uses Key:value key-value pairs to store data, and JSON is a lightweight data interchange format compared to XML format; don't be fooled by the word JavaScript. JSON is actually a data format that has nothing to do with a specific language. JSON has been widely used in the industry, such as the current NoSQL database storage mostly using Key:value storage structure, taking MONGO as an example, its scripting syntax even directly using JavaScript, in the data transmission, the use of JSON format is also widely used, Most open APIs open JSON-mode data output, and JSON format is widely recommended when AJAX requests data. JSON for more information can be viewed on the official JSON website http://json.org.
Java transient keywords
Java specification original The transient marker is not fully specified by the Java languagespecification it is used in object serialization To mark member variables thatshould isn't be serialized. In order to facilitate storage and network transmission, Java has a serialized object mechanism, and transient can be used to specify the member objects that are not currently serialized. As an example of transient application, under the Mongo+morphia Open source project, if you specify transient for a member of the Java PO, the member data will not be stored in the MONGO database. Another application scenario is the JSON here, if the Java PO uses refrence (MONGO refrence) or lazyloading (which can be understood as Hibernate lazyloading concept), So most of the open source Java JSON related projects, will automatically load these refrence, lazyloading objects, if the PO formed a mutual reference, it will form a dead loop, even if there is no dead loop, a lot of unnecessary data is output to the client waste of resources can not be underestimated. Plus transient is a solution.
Java-based JSON main open source project and its comparison
There are a lot of JSON open source projects, such as Org.json, Json-lib, Jsontool, Jackson, Gson, Simplejson, and so on, and later specifically looked at several JSON open-source test data comparison, decided to adopt Fastjson. Show two sets of test data. First of all, the hero Wangym (former blog http://wangym.iteye.com/blog/738933) to Jackson, Json-lib, Gson test results
JSON to bean,5 threads concurrently, about 200-byte objects, 10 million conversions:
Jackson |
Json-lib |
Gson |
Throughput |
64113.7 |
8067.4 |
13952.8 |
Total time taken (seconds) |
155 |
1238 |
700 |
The bean goes json,5 threads concurrently, about 200 bytes of objects, 10 million conversions:
Jackson |
Json-lib |
Gson |
Throughput |
54802 |
15093.2 |
17308.2 |
Total time taken (seconds) |
181 |
661 |
560 |
Obviously, no matter what kind of conversion, Jackson > Gson > Json-lib.
Jackson's ability to deal with the json-lib is about 10 times times higher.
And then take the Fastjson with the Json-lib, Simple-json, Jackson performance test comparison data
Performance comparison
Test case |
Json-lib |
Simple-json |
Fastjson |
Jackson |
Intarray1000decode |
3,626 |
1,431 |
563 |
596 |
Stringarray1000decode |
2,698 |
2,283 |
677 |
774 |
Map100stringdecode |
515 |
597 |
208 |
230 |
function comparison
Characteristics |
Json-lib |
Simple-json |
Fastjson |
Jackson |
Serialization support arrays |
Not supported |
Not supported |
Support |
Support |
Serialization Support enum |
Not supported |
Not supported |
Support |
Support |
Support JavaBean |
Not directly supported |
Not directly supported |
Support |
Support |
You can see Fastjson in terms of performance, beyond all of the current Java JSON proccesor, including Jackson.
Fastjson Application Example 1, using jquery Ajax request Fastjson data to display the user list example implementation
Define a User Po object
Public class User implements serializable{
private static final long Serialversionuid = 1738399846398814044L;
Private String userid;
Private String username;
Note that refrence and lazyloading related references are used here.
@Refrence
Private Userdetail Userdeatil;
Public String GetUserid () {
return userid;
}
Public void Setuserid (String userid) {
this. userid = userid;
}
Public String GetUserName () {
return username;
}
Public void setusername (String username) {
this. Username = Username;
}
Public Userdetail Getuserdetail () {
return Userdetail;
}
Public void setuserdetail (userdetail userdetail) {
this. Userdetail = Userdetail;
}
}
Define a Userdetail Po object
Public class Userdetail implements serializable{
private static final long Serialversionuid = 1738399846398814045L;
Private String address;
Public String getaddress () {
return address;
}
Public void setaddress (String address) {
this. address = address;
}
}
Write action, output LIST<USER>, use pseudo code here
....
List<user> ls= userservice.getuserlist ();
Printwriterout = null;
Try {
out = GetResponse (). Getwriter ();
Out.write (json.tojsonstring (LS));
Out.flush ();
} catch (IOException e) {
E.printstacktrace ();
} finally {
Out.close ();
}
...
Write jquery Ajax request list of users
$.ajax ({
Type: "GET",
URL: "/user/getuserlist",//assuming this is your configured action address
DataType: "JSON",
Cache:false,
Success: function(users) {
var html= "";
if (users.length>0) {
for (var i in users) {
html=html+ "Username:" +users[i]+username+ "Address:" +users[i].userdetail.address;
}
alert (HTML);
}
});
2, how to solve the refrence and lazyloading caused by the death cycle problem?
From the above example, we can see that Fastjson will correctly take out the address data under Userdetail, and virtually all of the JSON open source projects support this association removal. But sometimes we do not need to userdetail the data, if automatically load a bunch of irrelevant data, even create a dead loop, how to solve it?
The first approach:
As already mentioned, plus transient keyword, such as the user PO's userdetail definition changed to
Private Transient Userdetailuserdeatil;
The second approach:
The first approach is a generic approach, using other JSON open source projects, can also achieve the effect, under Fastjson can also use @jsonfield (Serialize=false)
@JSONField (Serialize=false)
Private Transient Userdetailuserdeatil;
Of course Jsonfield There are other parameters that can be specified to implement member custom serialization, in general, if we determine that members can be non-serializable, we first recommend the use of transient. However, sometimes specifying the transient will cause other problems, if the user object has a long field remark, if the remark specified transient, then in the case of using MONGO database, will cause the page to submit the remark data can not be saved to the database , other fields without the transient keyword are saved normally. At this point, you can use @jsonfield to solve the problem.
The third approach:
If there is further optimization, such as scene A When you need serialization remark, and in the scene B when there is no need for serialization, then use Fastjson custom filter, Fastjson can be by name, property, value three kinds of filtering, to the property Example , rewrite get list<user> this pseudo code:
....
List<user> ls= userservice.getuserlist ();
PropertyFilter filter = new PropertyFilter () {
Publicboolean Apply (object source, String name, object value) {
if ("Remark". Equals (name)) {
return true;
}
Returnfalse;
}
};
serializewriter SW = new Serializewriter ();
Jsonserializer serializer = new Jsonserializer (SW);
Serializer.getpropertyfilters (). Add (filter);
Serializer.write (LS);
Printwriterout = null;
Try {
out = GetResponse (). Getwriter ();
Out.write (Sw.tostring ());
Out.flush ();
} catch (IOException e) {
E.printstacktrace ();
} finally {
Out.close ();
}
...
In this way, when encountering scene B, we use a third method to filter out the members of the remark, without adding filters in the case of scenario A.
Fastjson filtering properties that do not require serialization