这几天,由于项目的须要,接触了Google的Gson库,发现这个东西很好用,遂记下简单的笔记,供之后参考。至于Gson是干什么的,有什么优势,请各位同窗自行百度。话很少说,切入正题:html
1. 下载Gson的jar包,拷贝到项目的lib文件夹中,并将其加入到buildPath中。使用maven的同窗,直接在pom中加入如下依赖便可:java
1
2
3
4
5
|
<
dependency
>
<
groupId
>com.google.code.gson</
groupId
>
<
artifactId
>gson</
artifactId
>
<
version
>2.2.4</
version
>
</
dependency
>
|
2. 编写实体类:json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
public
class
People {
String name;
int
age;
boolean
setName;
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
int
getAge() {
return
age;
}
public
void
setAge(
int
age) {
this
.age = age;
}
public
boolean
getSetName() {
return
setName;
}
public
void
setSetName(
boolean
setName) {
this
.setName = setName;
}
@Override
public
String toString() {
return
"name="
+ name +
" age="
+ age +
" setName="
+setName;
}
}
|
3. 编写测试类GsonTestmaven
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import
com.google.gson.ExclusionStrategy;
import
com.google.gson.FieldAttributes;
import
com.google.gson.Gson;
import
com.google.gson.GsonBuilder;
/**
* Convert java object to json.
*/
public
class
GsonTest {
public
static
void
main(String[] args) {
People p =
new
People();
p.setAge(
20
);
p.setName(
"People"
);
p.setSetName(
true
);
Gson gson =
new
Gson();
System.out.println(gson.toJson(p));
}
}
|
4. 输出结果:ide
1
|
{
"name"
:
"People"
,
"age"
:
20
,
"setName"
:
true
}
|
5. 这只是最简单的Gson的使用。若是咱们须要将bool类型的属性setName在转换成json的时候不转换,怎么实现呢? svn
在Gson的包中找半天,发现com.google.gson包下面有这么一个接口:ExclusionStrategy ,虽然不清楚是干什么的,可是根据名字,能够推断,这个接口是用来设置Gson转换的排除策略的,因而在官网http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/index.html查了一下这个接口,发现只要实现这个接口,并将实现类的对象塞给Gson,在转换成json的时候,Gson就会过滤掉指定的类或者属性。因而有了下面的代码:测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
import
com.google.gson.ExclusionStrategy;
import
com.google.gson.FieldAttributes;
import
com.google.gson.Gson;
import
com.google.gson.GsonBuilder;
/**
* Convert java object to json, skip specific fileds.
*/
public
class
GsonTest {
public
static
void
main(String[] args) {
People p =
new
People();
p.setAge(
20
);
p.setName(
"People"
);
p.setSetName(
true
);
ExclusionStrategy excludeStrategy =
new
SetterExclusionStrategy();
Gson gson1 =
new
GsonBuilder()
.setExclusionStrategies(excludeStrategy)
.create();
Gson gson2 =
new
Gson();
String json1 = gson1.toJson(p);
String json2 = gson2.toJson(p);
System.out.println(json1);
System.out.println(json2);
People p1 = gson1.fromJson(json1, People.
class
);
People p2 = gson2.fromJson(json2, People.
class
);
System.out.println(p1);
System.out.println(p2);
}
private
static
class
SetterExclusionStrategy
implements
ExclusionStrategy {
public
boolean
shouldSkipClass(Class<?> clazz) {
return
false
;
}
public
boolean
shouldSkipField(FieldAttributes f) {
return
f.getName().startsWith(
"set"
);
}
}
}
|
原来,Gson对象的建立有两种方式:new Gson()表示使用默认的配置建立一个Gson对象,而若是使用GsonBuilder.create()方法建立,则能够自定义一些设置,这主要是为了使建立的Gson更适合于某些特定的状况。上例中第一段蓝色的代码建立了一个Gson对象,这个对象拥有对以“set”字样开头的属性的过滤的配置(若是须要过滤掉某种类型,则重写ExclusionStrategy接口的shouldSkipClass(Class<?> clazz)方法便可,若是须要过滤掉多种状况,则能够多建立几个ExclusionStrategy的实现类对象,并在建立Gson对象的时候设置进去便可),所以在本例中,将People对象转换成Json的时候,属性setName将被过滤掉。因为json1中没有属性setName,因此将json1反序列化成People对象的时候,boolean类型的setName就没有了值,因此打印的时候取了boolean类型的默认值。因而有了如下结果:ui
1
2
3
4
|
{
"name"
:
"People"
,
"age"
:
20
}
{
"name"
:
"People"
,
"age"
:
20
,
"setName"
:
true
}
name=People age=
20
setName=
false
name=People age=
20
setName=
true
|
6. Gson还支持使用注解,在com.google.gson.annotation包中,有几个注解Expose, SerializedName, Since和Until,他们各有各的做用,下面使用官方例子介绍经常使用的注解: this
6.1 Exposegoogle
此注解做用在属性上,代表当序列化和反序列化的时候,这个属性将会暴露给Gson对象。这个注解只有当建立Gson对象时使用GsonBuilder方式建立并调用了GsonBuilder.excludeFieldsWithoutExposeAnnotation() 方法的时候才有效,不然无效。下面是一个介绍@Expose注解如何使用的例子:
1
2
3
4
5
6
|
public
class
User {
@Expose
private
String firstName;
@Expose
(serialize =
false
)
private
String lastName;
@Expose
(serialize =
false
, deserialize =
false
)
private
String emailAddress;
private
String password;
}
|
若是你以new Gson()的方式建立Gson对象,toJson()方法和fromJson() 方法在序列化和反序列化的时候将会操做这4个属性。然而,若是你使用 Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()来建立Gson对象,Gson 的 toJson() 和 fromJson() 方法将会排除掉 password 字段,这是由于 password 字段没有被注解 @Expose 所标记。 这个 Gson 对象一样会排除 lastName 和 emailAddress 字段,由于注解@Expose的属性 serialize 被设置成了 false。相似的,Gson 将会在反序列化时排除掉 emailAddress 字段,由于 deserialize被设置成了 false。
6.2 SerializedName
此注解做用在属性上,代表这个属性在序列化成Json的时候,须要将名字序列化成注解的value属性指定的值。
这个注解将会覆盖任何的FieldNamingPolicy, 包括默认的命名策略。下面是一个介绍@SerializedName注解如何使用的例子: ,
1
2
3
4
5
6
7
8
|
public
class
SomeClassWithFields {
@SerializedName
(
"name"
)
private
final
String someField;
private
final
String someOtherField;
public
SomeClassWithFields(String a, String b) {
this
.someField = a;
this
.someOtherField = b;
}
}
|
下面的代码展现了序列化上面这个测试类的结果:
1
2
3
4
|
SomeClassWithFields objectToSerialize =
new
SomeClassWithFields(
"a"
,
"b"
);
Gson gson =
new
Gson();
String jsonRepresentation = gson.toJson(objectToSerialize);
System.out.println(jsonRepresentation);
|
执行结果是:
1
2
|
===== OUTPUT =====
{
"name"
:
"a"
,
"someOtherField"
:
"b"
}
|
因而可知,属性"someField"已经被序列化成了"name"。
注意:在@SerializedName的value中指定的属性名必须为有效的Json属性名。
6.3 Since和Until至关,请同窗们自行查看官网的API文档。
转自:http://my.oschina.net/itblog/blog/204120