這幾天,由於項目的須要,接觸了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