Android框架-Google官方Gson解析
Gson(又称Google Gson)是 Google 公司发布的一个开放源代码的 Java库,主要用途为序列化 Java 对象为 JSON 字符串,或反序列化 JSON 字符串成 Java 对象。
而 JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成,广泛应用于各种数据的交互中,尤其是服务器与客户端的交互。
GitHub中的源码地址:https://github.com/google/gson
AS 在项目的 app 目录下的 buile.gradle 添加 gson 依赖:
<figure style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-family: -apple-system-font, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 16px; color: rgb(62, 62, 62); line-height: inherit; background-color: rgb(255, 255, 255);"><figcaption style="margin: 10px 0px 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-size: 0.7em; color: rgb(153, 153, 153); line-height: inherit; text-align: center;"></figcaption>
</figure>
Gson 的实例化方式:
Gson gson = new Gson(); //可以通过GsonBuilder配置多种选项 gson = new GsonBuilder() .setLenient()// json宽松 .enableComplexMapKeySerialization()//支持Map的key为复杂对象的形式 .serializeNulls() //智能null .setPrettyPrinting()// 调教格式 .disableHtmlEscaping() //默认是GSON把HTML 转义的 .create();
接下来介绍 Gson 的集中基本用法:
1、对象和 json 字符串的相互转换(先准备一个 Person 类)
public class Person { private int per_id; private String name; private String sex; private int age; public Person(int per_id, String name, String sex, int age) { this.per_id = per_id; this.name = name; this.sex = sex; this.age = age; } //get、set和toString方法... }
测试代码:
public class GsonTest { public static void main(String[] args) { Gson gson = new Gson(); // 可以通过GsonBuilder配置多种选项 gson = new GsonBuilder() .setLenient()// json宽松 .enableComplexMapKeySerialization()// 支持Map的key为复杂对象的形式 .serializeNulls() // 智能null .setPrettyPrinting()// 调教格式 .disableHtmlEscaping() // 默认是GSON把HTML 转义的 .create(); Person person = new Person(10, "layne", "man", 18); // 对象转换为json字符串 String str = gson.toJson(person); System.out.println("对象转换为json字符串:" + str); // json字符串转化为对象 Person person1 = gson.fromJson(str, Person.class); System.out.println("json字符串转化为对象:" + person1.toString()); } }
运行结果:
<figure style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-family: -apple-system-font, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 16px; color: rgb(62, 62, 62); line-height: inherit; background-color: rgb(255, 255, 255);"><figcaption style="margin: 10px 0px 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-size: 0.7em; color: rgb(153, 153, 153); line-height: inherit; text-align: center;"></figcaption>
</figure>
2、集合和 json 字符串之间的转换:
public class CollectionTest { public static void main(String[] args) { Gson gson = new Gson(); // 1.1、List字符串集合转换为字符串 ArrayList<String> str_list = new ArrayList<>(); for (int i = 0; i < 5; i++) { str_list.add("这是字符串" + i); } String json_str = gson.toJson(str_list); System.out.println("这是list字符串集合转化为json:" + json_str); // 1.2、这是json集合转化为list字符集合 Type type = new TypeToken<ArrayList<String>>() { }.getType(); ArrayList<String> sList = gson.fromJson(json_str, type); System.out.println("这是json集合转化为list字符集合:" + sList); System.out.println("=========================================="); // 2.1、List对象集合转换为字符串 List<Person> list = new ArrayList<>(); for (int i = 0; i < 2; i++) { Person person = new Person(i, "layne", "man", 18); list.add(person); } String json_str1 = gson.toJson(list); System.out.println("这是list对象集合转化为json:" + json_str1); // 2.1、这是json转化为List对象集合 Type type1 = new TypeToken<ArrayList<Person>>() { }.getType(); ArrayList<String> sList1 = gson.fromJson(json_str1, type1); System.out.println("这是json转化为List对象集合:" + sList1); System.out.println("=========================================="); //3.1、这是map集合转化为json Map<String, Object> map = new HashMap<>(); map.put("per_id", 5); map.put("name", "layne"); map.put("sex", "man"); map.put("age", "18"); String json_str2 = gson.toJson(map); System.out.println("这是map集合转化为json:" + json_str2); //3.2、从json字符串转化为map集合 Type type2 = new TypeToken<Map<String, Object>>() { }.getType(); Map<String, Object> map1 = gson.fromJson(json_str2, type2); System.out.println("这是json转化为map集合:" + map1); } }
运行结果:
<figure style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-family: -apple-system-font, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 16px; color: rgb(62, 62, 62); line-height: inherit; background-color: rgb(255, 255, 255);"><figcaption style="margin: 10px 0px 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-size: 0.7em; color: rgb(153, 153, 153); line-height: inherit; text-align: center;"></figcaption>
</figure>
接下来介绍抽象基类 JsonElement 的继承体系类:
JSONObject、JSONArray 和 Android 自带的差不多,参见上篇
Android网络传输数据json解析的基本认识
JsonNull:实际上就是 null 的字符串字段
JsonPrimitive:这个其实挺有意思的,我们知道如果json转换成字符串会包含引号的转义,但是通过 JsonPrimative 我们可以获得为转义的字符串
public class JsonTest { public static void main(String[] args) { // JsonNull jsonNull = new JsonNull();// 构造方法过时,推荐INSTANCE JsonNull jsonNull = JsonNull.INSTANCE; System.out.println("JsonNull:" + jsonNull); Gson gson = new Gson(); Person person = new Person(1, "layne", "man", 18); String json_str = gson.toJson(person); System.out.println("json字符串:" + json_str); JsonPrimitive jsonPrimitive = new JsonPrimitive(json_str); System.out.println("JsonPrimitive字符串:" + jsonPrimitive);// 多了转义字符,以及头尾的双引号:"{\"per_id\":1,\"name\":\"layne\",\"sex\":\"man\",\"age\":18}" System.out.println("JsonPrimitive字符串:" + jsonPrimitive.getAsString()); } }
运行结果:
<figure style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-family: -apple-system-font, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 16px; color: rgb(62, 62, 62); line-height: inherit; background-color: rgb(255, 255, 255);"><figcaption style="margin: 10px 0px 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-size: 0.7em; color: rgb(153, 153, 153); line-height: inherit; text-align: center;"></figcaption>
</figure>
Gson 库中的注解:有五种注解
<figure style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-family: -apple-system-font, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 16px; color: rgb(62, 62, 62); line-height: inherit; background-color: rgb(255, 255, 255);"><figcaption style="margin: 10px 0px 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-size: 0.7em; color: rgb(153, 153, 153); line-height: inherit; text-align: center;"></figcaption>
</figure>
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); } }
运行结果:
<figure style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-family: -apple-system-font, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 16px; color: rgb(62, 62, 62); line-height: inherit; background-color: rgb(255, 255, 255);"><figcaption style="margin: 10px 0px 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-size: 0.7em; color: rgb(153, 153, 153); line-height: inherit; text-align: center;"></figcaption>
</figure>
替换冗余、难看的 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); } }
运行结果:
<figure style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-family: -apple-system-font, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 16px; color: rgb(62, 62, 62); line-height: inherit; background-color: rgb(255, 255, 255);"><figcaption style="margin: 10px 0px 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-size: 0.7em; color: rgb(153, 153, 153); line-height: inherit; text-align: center;"></figcaption>
</figure>
注解作用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("=========================="); } }
运行结果:
<figure style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-family: -apple-system-font, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 16px; color: rgb(62, 62, 62); line-height: inherit; background-color: rgb(255, 255, 255);"><figcaption style="margin: 10px 0px 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-size: 0.7em; color: rgb(153, 153, 153); line-height: inherit; text-align: center;"></figcaption>
</figure>
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); } }
运行结果:
<figure style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-family: -apple-system-font, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 16px; color: rgb(62, 62, 62); line-height: inherit; background-color: rgb(255, 255, 255);"><figcaption style="margin: 10px 0px 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-size: 0.7em; color: rgb(153, 153, 153); line-height: inherit; text-align: center;"></figcaption>
</figure>
其余三种情况请查看原文
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); } }
运行结果:
<figure style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-family: -apple-system-font, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 16px; color: rgb(62, 62, 62); line-height: inherit; background-color: rgb(255, 255, 255);"><figcaption style="margin: 10px 0px 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-size: 0.7em; color: rgb(153, 153, 153); line-height: inherit; text-align: center;"></figcaption>
</figure>
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); } }
运行结果:
<figure style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-family: -apple-system-font, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 16px; color: rgb(62, 62, 62); line-height: inherit; background-color: rgb(255, 255, 255);"><figcaption style="margin: 10px 0px 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-size: 0.7em; color: rgb(153, 153, 153); line-height: inherit; text-align: center;"></figcaption>
</figure>
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<Person> 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); } }
运行结果:
<figure style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-family: -apple-system-font, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 16px; color: rgb(62, 62, 62); line-height: inherit; background-color: rgb(255, 255, 255);"><figcaption style="margin: 10px 0px 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-size: 0.7em; color: rgb(153, 153, 153); line-height: inherit; text-align: center;"></figcaption>
</figure>
接下来就是容错机制
为什么要容错了?
在 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<Person> { @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); } } }
运行结果:
<figure style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-family: -apple-system-font, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 16px; color: rgb(62, 62, 62); line-height: inherit; background-color: rgb(255, 255, 255);"><figcaption style="margin: 10px 0px 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-size: 0.7em; color: rgb(153, 153, 153); line-height: inherit; text-align: center;"></figcaption>
</figure>
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<Integer> { @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); } } }
运行结果:
<figure style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-family: -apple-system-font, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 16px; color: rgb(62, 62, 62); line-height: inherit; background-color: rgb(255, 255, 255);"><figcaption style="margin: 10px 0px 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-size: 0.7em; color: rgb(153, 153, 153); line-height: inherit; text-align: center;"></figcaption>
</figure>
上面的两种方式保险是很保险的,但是代码量大,需要维护的地方也多,这里介绍另外一种方式: JsonSerializer 与
JsonDeserializer 之关系单方面的处理,可以全局注册某个类型的处理
代码演示:
public class FaultToleranceTest2 { 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 void main(String[] args) { JsonDeserializer<Integer> jsonDeserializer = new JsonDeserializer<Integer>() { @Override public Integer deserialize(JsonElement json, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { try { return json.getAsInt(); } catch (NumberFormatException e) { return 0; } } }; String json_str = "{\"per_id\":\"\",\"name\":\"layne\",\"sex\":\"man\",\"state\":true}"; System.out.println("服务器发送:" + json_str); Gson gson = new Gson(); try { Person person1 = gson.fromJson(json_str, Person.class); System.out.println("默认gson解析:" + person1); } catch (Exception e) { System.out.println("默认gson解析异常:" + e); } //方式1.创建Gson的方式 Gson gson1 = new GsonBuilder().registerTypeAdapter(int.class, jsonDeserializer).create(); try { Person person = gson1.fromJson(json_str, Person.class); System.out.println("JsonDeserializer<Integer>解析:" + person); } catch (Exception e) { System.out.println("JsonDeserializer<Integer>解析异常:" + e); } } }
运行结果:
<figure style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-family: -apple-system-font, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-size: 16px; color: rgb(62, 62, 62); line-height: inherit; background-color: rgb(255, 255, 255);"></figure>