From 81414f76b3e99fb318b0b139a611cf6941d7aa4a Mon Sep 17 00:00:00 2001 From: Rikako Wu <496063163@qq.com> Date: Tue, 23 Aug 2022 00:19:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=B8=B8=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + spring/json/gson/gson.md | 59 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 .gitignore create mode 100644 spring/json/gson/gson.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ diff --git a/spring/json/gson/gson.md b/spring/json/gson/gson.md new file mode 100644 index 0000000..29b228b --- /dev/null +++ b/spring/json/gson/gson.md @@ -0,0 +1,59 @@ +# gson +## gson简介 +gson是一个java库,通常用来将java对象转化为其json表示的字符串,或者将json格式的字符串转化为其等价的java对象。 +## gson使用 +在gson中,使用最频繁的类是Gson。可以通过new Gson()构造函数来创建Gson对象,也可以通过GsonBuilder来创建Gson对象,GsonBuidler在创建Gson对象时能够指定一些设置,如版本控制等。 +由于Gson对象在执行json操作时并不会保存任何状态,故而Gson对象是线程安全的,单一的Gson对象可以在多线程环境下被重复使用。 +### Gson库通过Maven引入 +```xml + + + + com.google.code.gson + gson + 2.9.1 + compile + + +``` +### 基本类型的序列化和反序列化 +```java +// Serialization +Gson gson = new Gson(); +gson.toJson(1); // ==> 1 +gson.toJson("abcd"); // ==> "abcd" +gson.toJson(new Long(10)); // ==> 10 +int[] values = { 1 }; +gson.toJson(values); // ==> [1] + +// Deserialization +int one = gson.fromJson("1", int.class); +Integer one = gson.fromJson("1", Integer.class); +Long one = gson.fromJson("1", Long.class); +Boolean false = gson.fromJson("false", Boolean.class); +String str = gson.fromJson("\"abc\"", String.class); +String[] anotherStr = gson.fromJson("[\"abc\"]", String[].class); +``` +### 对象的序列化和反序列化 +类似于java自带的序列化机制,当成员字段被transient修饰时,并不会序列化该字段 +```java +class BagOfPrimitives { + private int value1 = 1; + private String value2 = "abc"; + private transient int value3 = 3; + BagOfPrimitives() { + // no-args constructor + } +} + +// Serialization +BagOfPrimitives obj = new BagOfPrimitives(); +Gson gson = new Gson(); +String json = gson.toJson(obj); + +// ==> json is {"value1":1,"value2":"abc"} + +// Deserialization +BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class); +// ==> obj2 is just like obj +``` \ No newline at end of file