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.['hello',5,{name:'GREETINGS',source:'guest'}]Collection collection = new ArrayList();collection.add("hello");collection.add(5);collection.add(new Event("GREETINGS", "guest"));Where the Event class is defined as:class Event { private String name; private String source; private Event(String name, String source) { this.name = name; this.source = source; }}toJson(collection) would write out the desired output.However, deserialization with fromJson(json, Collection.class) will not work since Gson has no way of knowing how to map the input to the types. Gson requires that you provide a genericised version of collection type in fromJson. So, you have three options:Deserialization is very similar but not exactly the same
Need to call "new Id(Class<T>, String)" which returns an instance of Id<T>Gson supports numerous mechanisms for excluding top-level classes, fields and field types. Below are pluggable mechanism that allow field and class exclusion. If none of the below mechanism satisfy your needs then you can always use custom serializers and deserializers.