读书人

Gson 应用简介

发布时间: 2012-09-16 17:33:17 作者: rapoo

Gson 使用简介
这个网页通过正常的手段是访问不到的, 为了大家能够方便的学习Gson。 因此将原文应用到此。

OverviewGson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson is an open-source project hosted at http://code.google.com/p/google-gson.

Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.

Goals for GsonProvide easy to use mechanisms like toString() and constructor (factory method) to convert Java to JSON and vice-versaAllow pre-existing unmodifiable objects to be converted to and from JSONAllow custom representations for objectsSupport arbitrarily complex objectGenerate compact and readability JSON output
Gson Performance and Scalability
Here are some metrics that we obtained on a desktop (dual opteron, 8GB RAM, 64-bit Ubuntu) running lots of other things along-with the tests. You can rerun these tests by using the class PerformanceTest.
Strings: Deserialized strings of over 25MB without any problems (see disabled_testStringDeserializationPerformance method in PerformanceTest)
Large collections:
Serialized a collection of 1.4 million objects (see disabled_testLargeCollectionSerialization method inPerformanceTest)
Deserialized a collection of 87,000 objects (see disabled_testLargeCollectionDeserialization in PerformanceTest)Gson 1.4 raised the deserialization limit for byte arrays and collection to over 11MB from 80KB.Note: Delete the disabled_ prefix to run these tests. We use this prefix to prevent running these tests every time we run junit tests.
Gson Users

Gson was originally created for use inside Google where it is currently used in a number of projects. It is now used by a number of public projects and companies. See details here.

Using GsonThe primary class to use is Gson which you can just create by calling new Gson(). There is also a class GsonBuilder available that can be used to create a Gson instance with various settings like version control and so on.

The Gson instance does not maintain any state while invoking Json operations. So, you are free to reuse the same object for multiple Json serialization and deserialization operations.
Primitives Examples(Serialization)
Gson gson = new Gson();
gson.toJson(1); ==> prints 1
gson.toJson("abcd"); ==> prints "abcd"
gson.toJson(new Long(10)); ==> prints 10
int[] values = { 1 };
gson.toJson(values); ==> prints [1]

(Deserialization)
int one = gson.fromJson("1", int.class);
Integer one = gson.fromJson("1", Integer.class);
Long one = gson.fromJson("1", Long.class);
Boolean false = gson.fromJson("false", Boolean.class);
String str = gson.fromJson("\"abc\"", String.class);
String anotherStr = gson.fromJson("[\"abc\"]", String.class);

Object Examplesclass BagOfPrimitives {
private int value1 = 1;
private String value2 = "abc";
private transient int value3 = 3;
BagOfPrimitives() {
// no-args constructor
}
}

(Serialization)
BagOfPrimitives obj = new BagOfPrimitives();
Gson gson = new Gson();
String json = gson.toJson(obj);
==> json is {"value1":1,"value2":"abc"}

Note that you can not serialize objects with circular references since that will result in infinite recursion.

(Deserialization)
BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);
==> obj2 is just like obj

Finer Points with Objects
读书人网 >其他相关

热点推荐