本文共 26996 字,大约阅读时间需要 89 分钟。
JSON解析类库之Gson(3) --- Gson注解
---Gson类库学习, 生成与解析json数据,json字符串与对象互转
{"id":"100",name":"chunlynn","emailAddress":"chunlynn@example.com","title":"engineer"}
{"id":"100",name":"chunlynn","email_address":"chunlynn@example.com","title":"engineer"}
@SerializedName( "email_address" ) private String emailAddress;
{"id":"100",name":"chunlynn","emailAddress":"chunlynn@example.com","title":"engineer"} {"id":"100",name":"chunlynn","email_address":"chunlynn@example.com","title":"engineer"}{"id":"100",name":"chunlynn","email":"chunlynn@example.com","title":"engineer"}
@SerializedName(value = "emailAddress", alternate = { "email", "email_address" }) private String emailAddress;
import com.google.gson.annotations.SerializedName; public class Employee { private String id; private String name; @SerializedName(value = "emailAddress", alternate = { "email", "email_address" }) private String emailAddress; //为了代码简洁,这里移除了getter和setter方法、toString方法等 }
package com.chunlynn.gson; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class GsonTest12 { public static void main(String[] args) { Gson gson = new GsonBuilder()// //.setPrettyPrinting()//格式化输出(序列化) .enableComplexMapKeySerialization() 支持Map的key为复杂对象的形式 .create(); /* {\"id\":\"100\",\"name\":\"chunlynn\",\"emailAddress\":\"chunlynn@example.com\"} {\"id\":\"100\",\"name\":\"chunlynn\",\"email_address\":\"chunlynn@example.com\"} {\"id\":\"100\",\"name\":\"chunlynn\",\"email\":\"chunlynn@example.com\"} */ String json1 = "{\"id\":\"100\",\"name\":\"chunlynn\",\"email_address\":\"chunlynn@example.com\"}"; String json2 = "{\"id\":\"100\",\"name\":\"chunlynn\",\"emailAddress\":\"chunlynn@example.com\",\"email_address\":\"chunlynn@example.com\",\"email\":\"chunlynn@example.com\"}"; Employee employee1 = gson.fromJson(json1, Employee.class); System.out.println("email_address字段反序列化 ===> " + employee1); //email_address字段反序列化 ===> Employee [id=100, name=chunlynn, emailAddress=chunlynn@example.com] Employee employee2 = gson.fromJson(json2, Employee.class); System.out.println("多种格式字段反序列化 ===> " + employee2); // 多种格式字段反序列化 ===> Employee [id=100, name=chunlynn, emailAddress=chunlynn@example.com] } }
public class Employee { private String id; private String name; //序列化后就变成了email (json串中的字段名) @SerializedName(value = "email", alternate = { "emailAddress", "email_address" }) private String emailAddress; //为了代码简洁,这里移除了getter和setter方法、toString方法、构造方法等 }
package com.chunlynn.gson; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class GsonTest13 { public static void main(String[] args) { Gson gson = new GsonBuilder()// //.setPrettyPrinting()//格式化输出(序列化) .enableComplexMapKeySerialization() 支持Map的key为复杂对象的形式 .create(); Employee empyee = new Employee("1001", "jeffchen", "jeff@126.com"); String jsonString = gson.toJson(empyee); System.out.println("使用注解后的序列化==" + jsonString); // 使用注解后的序列化=={"id":"1001","name":"jeffchen","email":"jeff@126.com"} Employee employee3 = gson.fromJson(jsonString, Employee.class); System.out.println("使用注解后的反序列化==" + employee3); // 使用注解后的反序列化==Employee [id=1001, name=jeffchen, emailAddress=jeff@126.com] } }
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Expose { public boolean serialize() default true; public boolean deserialize() default true; }
import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; public class Employee2 { private String id; @Expose //等同于@Expose(deserialize = true,serialize = true) private String name; @ Expose //序列化后就变成了email (json串中的字段名) @SerializedName(value = "email", alternate = { "emailAddress", "email_address" }) private String emailAddress; private Date birthday; @ Expose private String title; //为了代码简洁,这里移除了getter和setter方法、toString方法、构造方法等 }
package com.chunlynn.gson; import java.util.Date; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class GsonTest14 { public static void main(String[] args) { Gson gson = new GsonBuilder()// //.setPrettyPrinting()//格式化输出(序列化) .excludeFieldsWithoutExposeAnnotation() // 不导出实体中没有用@Expose注解的属性。 .setDateFormat("yyyy-MM-dd HH:mm:ss") //序列化时间转化为特定格式 .create(); Employee2 employee2 = new Employee2("1002", "chunlynn", "chunlynn@163.com", new Date(), "engineer"); String jsonString = gson.toJson(employee2); System.out.println("使用了@Expose注解后的序列化输出 ===》 " + jsonString); //使用了@Expose注解后的序列化输出 ===》 {"name":"chunlynn","email":"chunlynn@163.com","title":"engineer"} Employee2 retEmployee = gson.fromJson(jsonString, Employee2.class); System.out.println("使用了@Expose注解后的反序列化解析==》" + retEmployee); //使用了@Expose注解后的反序列化解析==》Employee2 [id=null, name=chunlynn, emailAddress=chunlynn@163.com, birthday=null, title=engineer] String jsonString2 = "{\"id\":\"1007\",\"name\":\"jeffchen\",\"email\":\"jeff@163.com\",\"title\":\"boss\"}"; Employee2 reEmployee2 = gson.fromJson(jsonString2, Employee2.class); System.out.println("使用了@Expose注解后的反序列化解析2==》" + reEmployee2); // 使用了@Expose注解后的反序列化解析2==》Employee2 [id=null, name=jeffchen, emailAddress=jeff@163.com, birthday=null, title=boss] } }
public class Employee3 { private String id; @Expose //等同于 @Expose(deserialize = true,serialize = true) private String name; @Expose @SerializedName(value = "email", alternate = { "emailAddress", "email_address" }) private String emailAddress; @Expose(serialize = true, deserialize = false) private Date birthday; @Expose(serialize = false, deserialize = true) private String title; //为了代码简洁,这里移除了getter和setter方法、toString方法、构造方法等 }
package com.chunlynn.gson; import java.util.Date; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class GsonTest15 { public static void main(String[] args) { Gson gson = new GsonBuilder()// //.setPrettyPrinting()//格式化输出(序列化) .excludeFieldsWithoutExposeAnnotation() // 不导出实体中没有用@Expose注解的属性 .setDateFormat("yyyy-MM-dd HH:mm:ss") //序列化时间转化为特定格式 .create(); Employee3 employee3 = new Employee3("1005", "chunlynn", "chunlynn@163.com", new Date(), "engineer"); /** * 序列化 */ String jsonStr = gson.toJson(employee3); System.out.println("使用@Expose()注解后的序列化输出 ==》 " + jsonStr); //使用了@Expose注解后的序列化输出 ===》 //{"name":"chunlynn","email":"chunlynn@163.com","birthday":"2017-05-05 11:20:36"} /** * 反序列化[1] */ Employee3 retEmployee = gson.fromJson(jsonStr, Employee3.class); System.out.println("使用了@Expose()注解后的反序列化解析==》" + retEmployee); //使用了@Expose()注解后的反序列化解析==》 //Employee3 [id=null, name=chunlynn, emailAddress=chunlynn@163.com, birthday=null, title=null] /** * 反序列化[2] */ String jsonString2 = "{\"id\":\"1007\",\"name\":\"jeffchen\",\"email\":\"jeff@163.com\",\"title\":\"boss\"}"; Employee3 reEmployee2 = gson.fromJson(jsonString2, Employee3.class); System.out.println("使用了@Expose()注解后的反序列化解析2==》" + reEmployee2); // 使用了@Expose()注解后的反序列化解析2==》 //Employee3 [id=null, name=jeffchen, emailAddress=jeff@163.com, birthday=null, title=boss] } }
@Retention (RetentionPolicy.RUNTIME) @Target({ ElementType.FIELD, ElementType.TYPE}) public @interface Since { /** * the value indicating a version number since this member * or type has been present. */ double value(); } @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.FIELD, ElementType.TYPE}) public @interface Until { /** * the value indicating a version number until this member * or type should be ignored. */ double value(); }
@Since
和 @Until
都接收一个 Double
值。 public class Employee4 { private String id; @Expose //等同于 @Expose(deserialize = true,serialize = true) private String name; @Expose @SerializedName(value = "email", alternate = { "emailAddress", "email_address" }) private String emailAddress; @Expose(serialize = true, deserialize = false) private Date birthday; @Expose(serialize = false, deserialize = true) private String title; @Expose @Since(4.0) //表示该字段从4.0开始生效 private String phoneNum; @Expose @Until(2.0) //表示2.0及其以后该字段就失效了 private String habbit; //为了代码简洁,这里移除了getter和setter方法、toString方法、构造方法等 }
package com.chunlynn.gson; import java.util.Date; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class GsonTest16 { public static void main(String[] args) { Gson gson = new GsonBuilder()// //.setPrettyPrinting()//格式化输出(序列化) .excludeFieldsWithoutExposeAnnotation() // 不导出实体中没有用@Expose注解的属性 .setDateFormat("yyyy-MM-dd HH:mm:ss") //序列化时间转化为特定格式 .setVersion(5.0) //设置当前版本号 .create(); Employee4 employee = new Employee4("1005", "chunlynn", "chunlynn@163.com", new Date(), "engineer", "10086", "羽毛球"); /** * 序列化 */ String jsonStr = gson.toJson(employee); System.out.println("使用@Since()和@Until()注解后的序列化输出 ==》 " + jsonStr); //使用@Since()和@Until()注解后的序列化输出 ==》 //{"name":"chunlynn","email":"chunlynn@163.com","birthday":"2017-05-05 11:52:40","phoneNum":"10086"} /** * 反序列化[3] */ Employee4 retEmployee = gson.fromJson(jsonStr, Employee4.class); System.out.println("使用@Since()和@Until()注解后的反序列化解析==》" + retEmployee); //使用@Since()和@Until()注解后的反序列化解析==》 //Employee4 [id=null, name=chunlynn, emailAddress=chunlynn@163.com, birthday=null, title=null, phoneNum=10086, habbit=null] /** * 反序列化[4] */ String jsonString2 = "{\"id\":\"1007\",\"name\":\"jeffchen\",\"email\":\"jeff@163.com\",\"title\":\"boss\",\"phoneNum\":\"10010\",\"habbit\":\"游泳\"}"; Employee4 reEmployee2 = gson.fromJson(jsonString2, Employee4.class); System.out.println("使用@Since()和@Until()注解后的反序列化解析2==》" + reEmployee2); //使用@Since()和@Until()注解后的反序列化解析2==》 //Employee4 [id=null, name=jeffchen, emailAddress=jeff@163.com, birthday=null, title=boss, phoneNum=10010, habbit=null] } }