Package.com.google.gson API 详细 学习(2)

com.google.gson
Class Gson
  • com.google.gson.Gson

public final class Gson
extends Object
This is the main class for using Gson. Gson is typically used by first constructing a Gson instance and then invoking toJson(Object) or fromJson(String, Class) methods on it. Gson instances are Thread-safe so you can reuse them freely across multiple threads.
You can create a Gson instance by invoking new Gson() if the default configuration is all you need. You can also use GsonBuilder to build a Gson instance with various configuration options such as versioning support, pretty printing, custom JsonSerializers, JsonDeserializers, and InstanceCreators.
介绍:
这个是Gson的主要课程。Gson通常先创建一个Gson实例,然后调用它的 toJson(Object) 或者 fromJson(String,Class)方法。Gson实例是线程安全的,所以你可以在多个线程中只有的重用他们。
如果默认值配置都是你所需要的,那么你可以调用new Gson()来创建 Gson的实例。您还可以使用GsonBuilder创建一个有各种配置项的Gson实例,比如版本支持,漂亮的打印,自定义json序列化,json反序列化以及实例化器。

Here is an example of how Gson is used for a simple Class:
这里有有一个案例,关于如何使用Gson用于简单的类:

代码案例:toJson(Object),fromJson(String,Class);
--------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- ---
Gson gson = new Gson(); // Or use new GsonBuilder().create();
MyType target = new MyType();
String json = gson. toJson(target); // serializes target to Json
MyType target2 = gson. fromJson(json, MyType.class); // deserializes json into target2
--------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- ---
If the object that your are serializing/deserializing is a ParameterizedType (i.e. contains at least one type parameter and may be an array) then you must use the toJson(Object, Type) or fromJson(String, Type) method. Here is an example for serializing and deserializing a ParameterizedType:
如果对象是你序列化/反序列化是一个 "参数类型" 数据(至少包含一个参数类型,也可能是一个数组类型) ,那么你必须使用 toJson(Object,Type) 或者fromJson(String,Type)方法。
以下是一个序列化/反序列化参数类型的案例:

代码案例:toJson(Object,Type),fromJson(String,Type);
--------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- ---
Type listType = new TypeToken<List<String>>() {}.getType();
List<String> target = new LinkedList<String>();
target.add("blah");

Gson gson = new Gson();
String json = gson.toJson(target, listType);
List<String> target2 = gson.fromJson(json, listType);
--------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- --------- ---

作者: Inderjeet Singh, Joel Leitch, Jesse Wilson
Author: Inderjeet Singh, Joel Leitch, Jesse Wilson
或者你可以可以参考这个: http://blog.csdn.net/gtkknd/article/details/50890341

关于TypeToken的理解
Type listType = new TypeToken<LinkedList<User>>(){}.getType();Type是java里的reflect包的Type ,TypeToken 是google提供的一个解析Json数据的类库中一个类问题1:如果我没有写大括号{};就是TypeToken<LinkedList<User>>()后面的这个{},eclipse会报出 构造函数不可见,加上就好了,可是我在自己创建的2个测试类里这样做就不行,如果构造是private的,加不加大括号都不行这是为什么
问题2:通常情况下,不加大括号是什么情况?加上又是什么情况,他们的含义是什么,作用是什么,
不加大括号表示很普通的new一个对象。。如果那个类的构造方法是私有的。。肯定不能直接new了。。就会编译报错。。加上大括号表示你 new了一个匿名内部类的对象。。比如new TypeToken<LinkedList<User>>(){}。。表示你new的是一个匿名内部类的对象。。这个匿名类继承自TypeToken类。。你可以在大括号里面像写其他普通类代码一样随意写代码。。你可以在里面定义个方法等等。。主要你不能理解的原因我想是你不了解内部类。。多看内部类的知识就会明白了。。new TypeToken<LinkedList<User>>(){}匿名内部类常用在监听里面。。比如我们给一个按钮加监听。。比如JButton btn = new JButton("test"); btn.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {}}); btn.addActionListener()方法接受的是一个ActionListener类型的对象。。而ActionListener是一个接口。。不能直接new。。所以本来我们应该写一个类实现ActionListener接口。。然后这里给他传一个那个实现类的对象。。但是。。我们不想那么麻烦了。。因为这种情况太多。。所以我们直接在这里创建了那个类。。之所以叫匿名。。就是我们没有给他取名字就让他叫ActionListener接口的名字。。然后在这里实现那个方法。。


Constructor Summary
Constructor and Description 构造器描述
Gson()
Constructs a Gson object with default configuration.
带有一个缺省配置的Gson构造器。

Method Summary
Modifier and Type Method and Description
<T> T fromJson(JsonElement json, Class<T> classOfT)
This method deserializes the Json read from the specified parse tree into an object of the specified type.
这个方法将从指定的解析树种读取到的json数据反序列化成指定类型的对象。

<T> T fromJson(JsonElement json, Type typeOfT)
This method deserializes the Json read from the specified parse tree into an object of the specified type.
这个方法将从指定的解析树种读取到的json数据反序列化成指定类型的对象。【注意,这个数据类型不一样,上面是 Class!】

<T> T fromJson(JsonReader reader, Type typeOfT)
Reads the next JSON value from reader and convert it to an object of type typeOfT.
从读取器中读取下一个json值,将其转化成 typeOfT类型的对象。

<T> T fromJson(Reader json, Class<T> classOfT)
This method deserializes the Json read from the specified reader into an object of the specified class.
这个方法 将 指定类型读取器中读取到的 json 反序列化成指定类的对象。

<T> T fromJson(Reader json, Type typeOfT)
This method deserializes the Json read from the specified reader into an object of the specified type.
这个方法 将 从指定读取器中读取的json反序列化成指定类型的对象。

<T> T fromJson(String json, Class<T> classOfT)
This method deserializes the specified Json into an object of the specified class.
这个方法将指定的json反序列化成指定的类。

<T> T fromJson(String json, Type typeOfT)
This method deserializes the specified Json into an object of the specified type.
这个方法将指定类型的json反序列化成指定类型的对象。

<T> TypeAdapter<T> getAdapter(Class<T> type)
Returns the type adapter for type.
为类型返回一个类型适配器。

<T> TypeAdapter<T> getAdapter(TypeToken<T> type)
Returns the type adapter for type.
为类型返回一个类型适配器。

<T> TypeAdapter<T> getDelegateAdapter(TypeAdapterFactory skipPast, TypeToken<T> type)
This method is used to get an alternate type adapter for the specified type.
这个方法为指定类型获取一个备用类型适配器。

JsonReader newJsonReader(Reader reader)
Returns a new JSON writer configured for the settings on this Gson instance.
为了Gson实例的设置返回一个新的JSON写入器。

JsonWriter newJsonWriter(Writer writer)
Returns a new JSON writer configured for the settings on this Gson instance.
为了Gson实例的设置返回一个新的JSON写入器。

String toJson(JsonElement jsonElement)
Converts a tree of JsonElements into its equivalent JSON representation.
将JSON树转化成等价的JSON表现。

void toJson(JsonElement jsonElement, Appendable writer)
Writes out the equivalent JSON for a tree of JsonElements.
为了JSON树编写等价的JSON数据。

void toJson(JsonElement jsonElement, JsonWriter writer)
Writes the JSON for jsonElement to writer.
将jsonElement写入到JSON中。

String toJson(Object src)
This method serializes the specified object into its equivalent Json representation.
这个方法将指定的对象序列化成等价的JSON表现。

void toJson(Object src, Appendable writer)
This method serializes the specified object into its equivalent Json representation.
这个方法将指定的对象序列化成等价的JSON表现。

String toJson(Object src, Type typeOfSrc)
This method serializes the specified object, including those of generic types, into its equivalent Json representation.
这个方法将指定对象序列化成等价的JSON表现,包括泛型。

void toJson(Object src, Type typeOfSrc, Appendable writer)
This method serializes the specified object, including those of generic types, into its equivalent Json representation.
这个方法将指定对象序列化成等价的JSON表现,包括泛型。

void toJson(Object src, Type typeOfSrc, JsonWriter writer)
Writes the JSON representation of src of type typeOfSrc to writer.
将typeOfSrc类型的JSON表现写入writer。

JsonElement toJsonTree(Object src)
This method serializes the specified object into its equivalent representation as a tree of JsonElements.
这个方法将指定的对象序列化成json树表的等价表现。

JsonElement toJsonTree(Object src, Type typeOfSrc)
This method serializes the specified object, including those of generic types, into its equivalent representation as a tree of JsonElements.
这个方法将指定的对象序列化成json树表的等价表现,包括泛型数据。

String toString()


Constructor Detail 构造函数细节
Gson
public Gson()
Constructs a Gson object with default configuration. The default configuration has the following settings:
构造一个带有默认(缺省值)配置的Gson对象。这个默认配置项有如下设置:
The JSON generated by toJson methods is in compact representation. This means that all the unneeded white-space is removed. You can change this behavior with GsonBuilder.setPrettyPrinting().
用toJson方法生成出来的json是紧凑的表现形式。这意味着所有不需要的空格都被移除。你可以在GsonBuilder.setPrettyPrinting()中改变这个行为。
The generated JSON omits all the fields that are null. Note that nulls in arrays are kept as is since an array is an ordered list. Moreover, if a field is not null, but its generated JSON is empty, the field is kept. You can configure Gson to serialize null values by setting GsonBuilder.serializeNulls().
生成的JSON会忽略所有空的字段。注意在数组中的nulls将被保留,因为数组是一个有序的集合。此外,如果一个字段不是空的,但是它生成的json是空的,这个字段保留。你可以通过设置GsonBuilder.serializeNulls()来配置Gson去序列化空值。

Gson provides default serialization and deserialization for Enums, Map, URL, URI, Locale, Date, BigDecimal, and BigInteger classes. If you would prefer to change the default representation, you can do so by registering a type adapter through GsonBuilder.registerTypeAdapter(Type, Object).
Gson为(Enums,Map, URL, URI, Locale, Date, BigDecimal, and BigInteger classes)提供了默认的序列化和反序列化。如果你想改变默认的表现,你可以通过GsonBuilder.registerTypeAdatpter(Type,Object)方法注册类型适配器。

The default Date format is same as DateFormat.DEFAULT. This format ignores the millisecond portion of the date during serialization. You can change this by invoking GsonBuilder.setDateFormat(int) or GsonBuilder.setDateFormat(String).
这个默认的时间格式化和DateFormat.DEFAULT一样。这种格式化在序列化期间忽略了毫秒部分。你可以通过调用GsonBuilder.setDateFormat(int)或者 GsonBuilder.setDateFormat(String)方法改变这个。

By default, Gson ignores the Expose annotation. You can enable Gson to serialize/deserialize only those fields marked with this annotation through GsonBuilder.excludeFieldsWithoutExposeAnnotation().
在默认情况下,Gson会忽略空开的注释。你可以通过GsonBuilder.excludeFieldsWithoutExposeAnnotation()方法让Gson序列化/反序列化这些字段的注释。

By default, Gson ignores the Since annotation. You can enable Gson to use this annotation through GsonBuilder.setVersion(double).
在默认情况下,Gson忽略了这一注释。你可以让Gson通过GsonBuilder.setVersion(double)来使用这个注释。

The default field naming policy for the output Json is same as in Java. So, a Java class field versionNumber will be output as "versionNumber" in Json. The same rules are applied for mapping incoming Json to the Java classes. You can change this policy through GsonBuilder.setFieldNamingPolicy(FieldNamingPolicy).
输出JSON的默认字段命名策略和Java是一样的。 因此,在Json中,Java类字段的版本号将被输出为“versionNumber”。同样的规则也适用于将传入的Json映射到Java类。你可以改变这一政策通过GsonBuilder.setFieldNamingPolicy(FieldNamingPolicy)。

By default, Gson excludes transient or static fields from consideration for serialization and deserialization. You can change this behavior through GsonBuilder.excludeFieldsWithModifiers(int...).
默认情况下,Gson不考虑序列化和反序列化的临时或静态字段。你可以改变这一行为通过GsonBuilder.excludeFieldsWithModifiers(int……)。



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值