Gson 库中的注解:有五种注解
![](https://ucc.alicdn.com/images/user-upload-01/img_convert/ab3c59f391d986328e998c14e5752e5a.webp?x-oss-process=image/format,png)
1、首先着重的先说明重命名注解: SerializedName
**注解的作用:**转换 key 关键字,json 转换成对象是,json 字段的 key 默认必须和声明类的字段名称一样。但是如果服务器返回的数据中 key 是关键字,这该怎么办?例如 key 是 case、switch 等,我们在声明类的时候是不能用这些字段的。或许你会让服务端那边改动,那服务端可能要改动非常的大,但是实际情况是不太愿意去改的。而这时候重命名注解就派上用场了。
还有就是如果服务端返回的 json 的 key 太冗余、或是不直观,这是就可以简化一下,代码看起来比较的优雅。
#####替换关键字的 key: public class AnnotationTest { public class Person { private int per_id; private String name; private String sex; @SerializedName(“case”) private int case_num; public Person(int per_id, String name, String sex, int case_num) { super(); this.per_id = per_id; this.name = name; this.sex = sex; this.case_num = case_num; } @Override public String toString() { return “Person–>[per_id=” + per_id + “, name=” + name + “, sex=” + sex + “, case_num=” + case_num + “]”; } } public static void main(String[] args) { Gson gson = new Gson(); String json_str = “{“per_id”:1,“name”:“layne”,“sex”:“man”,“case”:18}”; Person person = gson.fromJson(json_str, Person.class); System.out.println(“服务端发送的情况:”+json_str); System.out.println(“移动端转换的情况:”+person); } } #####运行结果: ![](https://ucc.alicdn.com/images/user-upload-01/img_convert/14553d2032677b77411c1809258ab912.webp?x-oss-process=image/format,png)
替换冗余、难看的 key:
public class AnnotationTest1 { public class Person { private int per_id; private String name; private String sex; @SerializedName(“home_light_state”) private boolean state; public Person(int per_id, String name, String sex, boolean state) { super(); this.per_id = per_id; this.name = name; this.sex = sex; this.state = state; } @Override public String toString() { return “Person [per_id=” + per_id + “, name=” + name + “, sex=” + sex + “, state=” + state + “]”; } } public static void main(String[] args) { Gson gson = new Gson(); String json_str = “{“per_id”:1,“name”:“layne”,“sex”:“man”,“home_light_state”:true}”; Person person = gson.fromJson(json_str, Person.class); System.out.println(“服务端发送的情况:”+json_str); System.out.println(“移动端转换的情况:”+person); } } #####运行结果: ![](https://ucc.alicdn.com/images/user-upload-01/img_convert/41ad8bf66a65d934c37ffabb3992555f.webp?x-oss-process=image/format,png) **注解作用2:**结合 alternate 提供多种备用字段key来解析,@SerializedName(value = “state”, alternate = { “plus”, “all” }) 如果 json 中有 plus 就会解析成 state,如果有 all 也会解析成 state,当然 state 依旧不变的。 **注意1:**value 中的值不能出现在 alternate 中; **注意2:**alternate 的备选字段会后面的替换前面的。 #####实例代码: public class AnnotationTest2 { public class Person { private int per_id; private String name; private String sex; @SerializedName(value = “state”, alternate = { “plus”, “all” }) private String state; public Person(int per_id, String name, String sex, String state) { super(); this.per_id = per_id; this.name = name; this.sex = sex; this.state = state; } @Override public String toString() { return “Person [per_id=” + per_id + “, name=” + name + “, sex=” + sex + “, state=” + state + “]”; } } public static void main(String[] args) { Gson gson = new Gson(); String json_str = “{“per_id”:1,“name”:“layne”,“sex”:“man”,“all”:“广东广州”}”; Person person = gson.fromJson(json_str, Person.class); System.out.println(“服务端发送:”+json_str); System.out.println(“转换成:” + person); System.out.println(““); String json_str1 = “{“per_id”:1,“name”:“layne”,“sex”:“man”,“plus”:“广东广州”}”; Person person1 = gson.fromJson(json_str1, Person.class); System.out.println(“服务端发送:”+json_str1); System.out.println(“转换成:” + person1); System.out.println(””); //all在state之后,所以all会解析成state,值则是all的原先的值 String json_str2 = “{“per_id”:1,“name”:“layne”,“sex”:“man”,“state”:“state广东广州”,“all”:“all广东广州”}”; Person person2 = gson.fromJson(json_str2, Person.class); System.out.println(“服务端发送:”+json_str2); System.out.println(“转换成:” + person2); System.out.println(““); //state在最后,不用解析,解析后的值也是state原先的 String json_str3 = “{“per_id”:1,“name”:“layne”,“sex”:“man”,“plus”:“plus广东广州”,“state”:“all广东广州”}”; Person person3 = gson.fromJson(json_str3, Person.class); System.out.println(“服务端发送:”+json_str3); System.out.println(“转换成:” + person3); System.out.println(””); } } #####运行结果: ![](https://ucc.alicdn.com/images/user-upload-01/img_convert/e150bcc4447cc6f9763236867a276eba.webp?x-oss-process=image/format,png)
2、过滤注解:Expose
源码:默认既可序列化又可反序列化
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Expose { public boolean serialize() default true; public boolean deserialize() default true; } 可以排除不需要序列化的字段,需要配合 GsonBuilder 使用 Gson gson = new GsonBuilder() .excludeFieldsWithoutExposeAnnotation() .create();
不添加 @Expose 注解的字段将不会解析,分为以下几种情况:
**1、**不添加 @Expose 注解等同于 @Expose(deserialize = false,serialize = false) 不做任何解析
2、@Expose(deserialize = true,serialize = false) 只解析用用,也就是反序列化可以,序列化不可以
3、@Expose(deserialize = false,serialize = true) 序列化可以,反序列化不行
4、@Expose(deserialize = true,serialize = true) 既可以序列化,也可以反序列化
#####实例代码:
不添加 @Expose 注解等同于 @Expose(deserialize = false,serialize = false) 不做任何解析
public class ExposeTest { public static class Person { private int per_id; private String name; private String sex; private boolean state; public Person(int per_id, String name, String sex, boolean state) { this.per_id = per_id; this.name = name; this.sex = sex; this.state = state; } @Override public String toString() { return “Person–>[per_id=” + per_id + “, name=” + name + “, sex=” + sex + “, state=” + state + “]”; } } public static void main(String[] args) { String json_str = “{“per_id”:1,“name”:“layne”,“sex”:“man”,“state”:true}”; Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation() .create(); Person person = gson.fromJson(json_str, Person.class); System.out.println(“反序列化:” + person); Person person1 = new Person(2, “layne”, “man”, true); String json_str1 = gson.toJson(person1); System.out.println(“序列化:”+json_str1); } } #####运行结果: ![](https://ucc.alicdn.com/images/user-upload-01/img_convert/c82b6e35e7fc22f5a4a4f3bb67e386a0.webp?x-oss-process=image/format,png) 其余三种情况请查看原文
3、版本控制注解:Since、Util
Since 注解:Gson 实例配置 GsonBuilder.setVersion(n) 使用,当 n>=v 时,才会序列化解析
#####实例代码:
public class SinceTest { public static class Person { @Since(2) private int per_id; @Since(2) private String name; @Since(2) private String sex; @Since(2) private boolean state; public Person(int per_id, String name, String sex, boolean state) { this.per_id = per_id; this.name = name; this.sex = sex; this.state = state; } @Override public String toString() { return “Person–>[per_id=” + per_id + “, name=” + name + “, sex=” + sex + “, state=” + state + “]”; } } public static void main(String[] args) { String json_str = “{“per_id”:1,“name”:“layne”,“sex”:“man”,“state”:true}”; Gson gson = new GsonBuilder().setVersion(1)//版本为1 .create(); Person person = gson.fromJson(json_str, Person.class); System.out.println(“反序列化v=1:” + person); Person person1 = new Person(2, “layne”, “man”, true); String json_str1 = gson.toJson(person1); System.out.println(“序列化v=1:”+json_str1); System.out.println(“=“); Gson gson1 = new GsonBuilder().setVersion(2)//版本为2 .create(); Person person2 = gson1.fromJson(json_str, Person.class); System.out.println(“反序列化v=2:” + person2); Person person3 = new Person(2, “layne”, “man”, true); String json_str2 = gson1.toJson(person3); System.out.println(“序列化v=2:”+json_str2); System.out.println(”=”); Gson gson2 = new GsonBuilder().setVersion(3)//版本为3 .create(); Person person4 = gson2.fromJson(json_str, Person.class); System.out.println(“反序列化v=3:” + person4); Person person5 = new Person(2, “layne”, “man”, true); String json_str3 = gson2.toJson(person5); System.out.println(“序列化v=3:”+json_str3); } } #####运行结果: ![](https://ucc.alicdn.com/images/user-upload-01/img_convert/2e59d4cd11297e9de65e1c0ef007ddd4.webp?x-oss-process=image/format,png)
Util 注解:Gson 实例配置 GsonBuilder.setVersion(n) 使用,当 n < v 时,才会序列化解析
#####实例代码:
public class UtilTest { public static class Person { @Until(2) private int per_id; @Until(2) private String name; @Until(2) private String sex; @Until(2) private boolean state; public Person(int per_id, String name, String sex, boolean state) { this.per_id = per_id; this.name = name; this.sex = sex; this.state = state; } @Override public String toString() { return “Person–>[per_id=” + per_id + “, name=” + name + “, sex=” + sex + “, state=” + state + “]”; } } public static void main(String[] args) { String json_str = “{“per_id”:1,“name”:“layne”,“sex”:“man”,“state”:true}”; Gson gson = new GsonBuilder().setVersion(1)// 版本为1 .create(); Person person = gson.fromJson(json_str, Person.class); System.out.println(“反序列化v=1:” + person); Person person1 = new Person(2, “layne”, “man”, true); String json_str1 = gson.toJson(person1); System.out.println(“序列化v=1:” + json_str1); System.out.println(“=“); Gson gson1 = new GsonBuilder().setVersion(2)// 版本为2 .create(); Person person2 = gson1.fromJson(json_str, Person.class); System.out.println(“反序列化v=2:” + person2); Person person3 = new Person(2, “layne”, “man”, true); String json_str2 = gson1.toJson(person3); System.out.println(“序列化v=2:” + json_str2); System.out.println(”=”); Gson gson2 = new GsonBuilder().setVersion(3)// 版本为3 .create(); Person person4 = gson2.fromJson(json_str, Person.class); System.out.println(“反序列化v=3:” + person4); Person person5 = new Person(2, “layne”, “man”, true); String json_str3 = gson2.toJson(person5); System.out.println(“序列化v=3:” + json_str3); } } #####运行结果: ![](https://ucc.alicdn.com/images/user-upload-01/img_convert/abc037b6da840c5a1f92d7111c0a5bfa.webp?x-oss-process=image/format,png)
Gson 还有一种更高级的手法进行序列化和反序列化,那就是 TypeAdapter ,就是就是对象 json 之间的互相转换 接替了T 泛型类的序列化和反序列化的逻辑,大家如果有兴趣可以去看一下源码,2.1版本之前后的用法是不一样的,2.1版本之前可以自定义 adapter,在2.1版本之后更推荐直接插入泛型就使用。在这里演示泛型的
代码演示:
public class TypeAdapterTest { public static class Person { private int per_id; private String name; private String sex; private boolean state; public Person(int per_id, String name, String sex, boolean state) { this.per_id = per_id; this.name = name; this.sex = sex; this.state = state; } @Override public String toString() { return “Person–>[per_id=” + per_id + “, name=” + name + “, sex=” + sex + “, state=” + state + “]”; } } public static void main(String[] args) throws Exception { Gson gson = new Gson(); TypeAdapter personTypeAdapter = gson.getAdapter(Person.class); Person person = new Person(1, “layne”, “man”, true); String json_str = personTypeAdapter.toJson(person); System.out.println(“序列化结果:” + json_str); Person person1 = personTypeAdapter.fromJson(json_str); System.out.println(“反序列化结果:” + person1); } } #####运行结果: ![](https://ucc.alicdn.com/images/user-upload-01/img_convert/4a2ad0c0b427e215f87bcc62d2ffd4e4.webp?x-oss-process=image/format,png)
接下来就是容错机制
为什么要容错了?
在 javaBean 中编号 per_id 声明的事 int 类,如果服务端返回的是""空字符串,那么客户端该怎么办?崩溃吗?
这时候就需要容错机制啦,容错的实现方式: