#####运行结果:
Gson 库中的注解:有五种注解
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); } }
#####运行结果:
替换冗余、难看的 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); } }
#####运行结果:
**注解作用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(””); } }
#####运行结果:
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); } }
#####运行结果:
其余三种情况请查看原文
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); } }
#####运行结果:
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); } }
#####运行结果:
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); } }
#####运行结果:
接下来就是容错机制
为什么要容错了?
在 javaBean 中编号 per_id 声明的事 int 类,如果服务端返回的是""空字符串,那么客户端该怎么办?崩溃吗?
这时候就需要容错机制啦,容错的实现方式:
-
创建 Gson 的方式
-
使用 JsonReader
-
自定义 TypeAdapter
-
使用注解 JsonAdapter,其实也是自定义 Adapter
方式1和2可以归为一类 由框架实现,基本 json 大格式规范,键值对不标准,多引号的问题等等,而不报错停止解析,但是功能相对较弱,能解决 bug
方式1:Gson 的创建方式
gson = new GsonBuilder() .setLenient()// json宽松 .create();
方式2:使用 JsonReader
JsonReader jsonReader = gson.newJsonReader(value.charStream()); jsonReader.setLenient(true);
方式 3和4也可以归为一类,都属于自定义 adapter,但是方式3与 gson 绑定,方式4使用注解和字段绑定
代码示例:(这种方式比较倾向于整体)
public class FaultToleranceTest { public static class Person { private int per_id; private String name; private String sex; private boolean state; public Person() { } 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 class PersonTypeAdapter extends TypeAdapter { @Override public Person read(JsonReader in) throws IOException { Person person = new Person(); in.beginObject(); while (in.hasNext()) { switch (in.nextName()) { case “per_id”: try { String str = in.nextString(); person.per_id = Integer.valueOf(str); } catch (Exception e) { } break; case “name”: person.name = in.nextString(); break; case “sex”: person.sex = in.nextString(); break; case “state”: person.state = in.nextBoolean(); break; } } in.endObject(); return person; } @Override public void write(JsonWriter out, Person person) throws IOException { out.beginObject(); out.name(“per_id”).value(person.per_id); out.name(“name”).value(person.name); out.name(“sex”).value(person.sex); out.name(“state”).value(person.state); out.endObject(); } } public static void main(String[] args) { Gson gson = new Gson(); String json_str = “{“per_id”:”“,“name”:“layne”,“sex”:“man”,“state”:true}”; System.out.println(“服务端发送:” + json_str); try { Person person = gson.fromJson(json_str, Person.class); System.out.println(“默认Gson解析:” + person); } catch (JsonParseException e) {// java.lang.NumberFormatException: // empty String System.out.println(“默认Gson解析异常:” + e); } Gson gson2 = new GsonBuilder().registerTypeAdapter(Person.class, new PersonTypeAdapter()).create(); try { Person person2 = gson2.fromJson(json_str, Person.class); System.out.println(“自定义PersonTypeAdapter解析:” + person2); } catch (JsonParseException e) {// java.lang.NumberFormatException: // empty String System.out.println(“自定义PersonTypeAdapter解析异常:” + e); } try { PersonTypeAdapter personTypeAdapter = new PersonTypeAdapter(); Person person3 = personTypeAdapter.fromJson(json_str); System.out.println(“自定义PersonTypeAdapter解析2:” + person3); } catch (Exception e) { System.out.println(“自定义PersonTypeAdapter解析异常2:” + e); } } }
#####运行结果:
3. 自定义 TypeAdapter
上面的方式是倾向于整体的,下面是注解的方式,比较倾向于字段
代码演示:
public class FaultToleranceTest1 { public static class Person { @JsonAdapter(IntegerTypeAdapter.class) private int per_id; private String name; private String sex; private boolean state; public Person() { } 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 class Person1 { private int per_id; private String name; private String sex; private boolean state; public Person1() { } public Person1(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 “Person1–>[per_id=” + per_id + “, name=” + name + “, sex=” + sex + “, state=” + state + “]”; } } public static class IntegerTypeAdapter extends TypeAdapter { @Override public void write(JsonWriter out, Integer value) throws IOException { out.value(value); } @Override public Integer read(JsonReader in) throws IOException { int i = 0; try { String str = in.nextString(); i = Integer.valueOf(str); } catch (Exception e) { } return i; } } public static void main(String[] args) { Gson gson = new Gson(); String json_str = “{“per_id”:”“,“name”:“layne”,“sex”:“man”,“state”:true}”; System.out.println(“服务器发送:” + json_str); try { Person1 person1 = gson.fromJson(json_str, Person1.class); System.out.println(“gson解析:” + person1); } catch (Exception e) { System.out.println(“gson解析异常:” + e); } try { Person person = gson.fromJson(json_str, Person.class); System.out.println(“JsonAdapter注解解析:” + person); } catch (JsonParseException e) {// java.lang.NumberFormatException: // empty String System.out.println(“JsonAdapter注解异常:” + e); } } }
#####运行结果: