json在日常開(kāi)發(fā)中有2種情況是經(jīng)常要處理的旱物。一種是json字段與類屬性不一致蘸朋。比如蛇形轉(zhuǎn)駝峰捉片,另一種情況部分屬性不序列化
SerializedName注解
SerializedName 用于json屬性與對(duì)象屬性名映射關(guān)系;
SerializedName 有2個(gè)屬性艺骂。value诸老,alternate 。
value:序列化或反序列化時(shí)字段的期望名稱钳恕。
alternate:該字段在反序列化時(shí)的其他名稱别伏。
這2個(gè)屬性什么意識(shí)呢?其實(shí)就是我們一般起別名就是因?yàn)樵搄son是由其他語(yǔ)言規(guī)范序列化的來(lái)的忧额,但是如果這個(gè)對(duì)象我們也要序列化返回給其他人使用直接使用默認(rèn)的SerializedName明顯不合適因?yàn)樾蛄谢闹祵⑹瞧渌Z(yǔ)言的規(guī)范所以設(shè)置value為java的規(guī)范厘肮,alternate為其他語(yǔ)言的規(guī)范。這樣反序列使用其他語(yǔ)言宙址。
ps:注意如果json中同事出現(xiàn)value 和alternate 包含的值轴脐,反序列的結(jié)果將是json串中后出現(xiàn)的值
Expose注解
Expose 用戶具體屬性的顯示调卑;
Expose有2個(gè)屬性 serialize, deserialize 這2個(gè)屬性默認(rèn)都是true;如果有需求可以自定義抡砂;
不過(guò)要該注解生效大咱。Gson對(duì)象不能使用new Gson()來(lái)創(chuàng)建對(duì)象。應(yīng)該使用new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); 來(lái)構(gòu)建
Gson excludeGson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
示例代碼
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class OrderRequest {
/**
* 用戶標(biāo)識(shí)
*/
@Expose(serialize = false)
@SerializedName("user_id")
private String userId;
/**
* 商品標(biāo)識(shí)
*/
@Expose(deserialize = false)
@SerializedName("goods_id")
private String goodsId;
/**
* 價(jià)格
*/
private int price;
@Expose
@SerializedName("order_no")
private String orderNo;
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getGoodsId() {
return goodsId;
}
public void setGoodsId(String goodsId) {
this.goodsId = goodsId;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return String.format("userId:%s ,goodsId:%s,price:%d,orderNo:%s", userId, goodsId, price, orderNo);
}
}
測(cè)試用例
對(duì)象序列化json
@Test
public void testSerialization() {
OrderRequest request = new OrderRequest();
request.setPrice(10);
request.setGoodsId("g01");
request.setUserId("1");
request.setOrderNo("20180331100001");
Gson gson = new Gson();
System.out.println("Gson:" + gson.toJson(request));
Gson excludeGson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
System.out.println("ExcludeGson:" + excludeGson.toJson(request));
}
輸出結(jié)果
Gson:{"user_id":"1","goods_id":"g01","price":10,"order_no":"20180331100001"}
ExcludeGson:{"goods_id":"g01","order_no":"20180331100001"}
對(duì)象反序列化
@Test
public void testDeserialization() {
String json = "{\"user_id\":\"1\",\"goods_id\":\"goods\",\"price\":10,\"order_no\":\"20180331100001\"}";
Gson gson = new Gson();
System.out.println(gson.fromJson(json, OrderRequest.class));
Gson excludeGson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
System.out.println(excludeGson.fromJson(json, OrderRequest.class));
}
輸出結(jié)果
userId:1 ,goodsId:goods,price:10,orderNo:20180331100001
userId:1 ,goodsId:null,price:0,orderNo:20180331100001