Gson使用二(GsonBuilder)

转载请出自出处:http://eksliang.iteye.com/blog/2175473

一.概述

    GsonBuilder用来定制java跟json之间的转换格式

 

二.基本使用

实体测试类:

温馨提示:默认情况下@Expose注解是不起作用的,除非你用GsonBuilder创建Gson的时候调用了GsonBuilder.excludeFieldsWithoutExposeAnnotation()方法

package com.ickes.json.daomain;

import java.util.Date;
import com.google.gson.annotations.Expose;
/**
 * @author Ickes
 */
public class User {
	private String  id;
	@Expose  //默认情况下序列化和反序列化都会使用
	private String  userName;
	@Expose(serialize=true,deserialize=false)//序列化时使用,反序列化不使用
	private String  userPwd;
	@Expose
	private Integer age;
	private Float   price;
	@Expose
	private Date    birthday;
	
	public User(String id, String userName, String userPwd, Integer age,
			Float price, Date birthday) {
		this.id = id;
		this.userName = userName;
		this.userPwd = userPwd;
		this.age = age;
		this.price = price;
		this.birthday = birthday;
	}
	get()和set()方法省略!
	@Override
	public String toString() {
		return "User [id=" + id + ", userName=" + userName + ", userPwd="
				+ userPwd + ", age=" + age + ", price=" + price + ", birthday="
				+ birthday + "]";
	}
	
}

 测试类:

package com.ickes.json;

import java.text.DateFormat;
import java.util.Date;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ickes.json.daomain.User;

public class GsonBuilderTest {
	public static void main(String[] args) {
		Gson gson = new GsonBuilder()
		.excludeFieldsWithoutExposeAnnotation() //不导出实体中没有用@Expose注解的属性
		.serializeNulls()                       //当需要序列化的值为空时,采用null映射,否则会把该字段省略
		.setDateFormat("yyyy-MM-dd HH:mm:ss")   //日期格式转换
		.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) //将属性的首字母大写
		.setPrettyPrinting()   //将结果进行格式化      
		.create();
		User user = new User("A001", "xl","xl_123",24,12000F,new Date());
		user.setAge(null); 
		String json = gson.toJson(user);
		System.out.println("序列化:"+json);
		
        user = gson.fromJson(json,User.class);
        System.out.println("反序列化:\n"+user);
	}

	
}

 打印信息如下:

序列化:{
  "UserName": "xl",
  "UserPwd": "xl_123",
  "Age": null,
  "Birthday": "2015-01-13 14:50:50"
}
反序列化:
User [id=null, userName=xl, userPwd=null, age=null, price=null, birthday=Tue Jan 13 14:50:50 CST 2015]

 

三、使用注解定制序列化字段

参考实例:

public class User {
	private String  id;
	@Expose
	@SerializedName("name") //序列化时会把userName字段名映射为name
	private String  userName;
        //序列化时使用,反序列化不使用该字段,默认都等于true
	@Expose(serialize=true,deserialize=false)
	private String  userPwd;
	@Expose
	private Integer age;
	private Float   price;
	@Expose
	private Date    birthday;
        get()和set()方法省略......!
}

 执行上面测类打印结果:

序列化:{
  "name": "xl", --序列化时,将userName转换为name
  "UserPwd": "xl_123",
  "Age": null,
  "Birthday": "2015-01-13 14:54:55"
}
反序列化:
User [id=null, userName=xl, userPwd=null, age=null, price=null, birthday=Tue Jan 13 14:54:55 CST 2015]
--返序列化后userName仍然能映射回来

四、使用注解根据“版本”进行序列化和返序列化

       有的字段不是一开始就有的,而是随着版本的升级添加进来,那么在进行序列化和返序列化的时候就会根据版本号来选择是否要序列化

参考实例:

public class User {
	private String  id;
	@Expose  
	private String  userName;
	@Expose(serialize=true,deserialize=false)//序列化时使用,反序列化不使用该字段
	private String  userPwd;
	@Expose
	@Since(1.1) //标记这个字段只有在1.1以上才会序列化和反序列化
	private Integer age;
	@Since(1.2) //标记这个字段只有在1.2以上才会序列化和反序列化
	@Expose
	private Float   price;
	@Expose
	private Date    birthday;
         get()和set()方法省略......!
}

 返回结果:

序列化:{ --序列化时可以明显看到price是1.2的版本,没有被序列化
  "UserName": "xl",
  "UserPwd": "xl_123",
  "Age": 24,
  "Birthday": "2015-01-13 15:27:26"
}
反序列化:
User [id=null, userName=xl, userPwd=null, age=24, price=null, birthday=Tue Jan 13 15:27:26 CST 2015]

 

猜你喜欢

转载自eksliang.iteye.com/blog/2175473