以下内容来之官网翻译,地址

1.Gson依赖

1.1.Gradle/Android

dependencies {
implementation 'com.google.code.gson:gson:2.9.0'
}

1.2.maven

<dependencies>
<!-- Gson: Java to Json conversion -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
<scope>compile</scope>
</dependency>
</dependencies>

1.2.Gson简单实用

1.2.1.基础类型

// 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);

1.2.2.对象

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

Notes 如果对象内存在循环引用,序列化时将导致死循环。

例如:


@Data
public class RecursionObject { private String name; private RecursionReferObject refer;
} @Data
public class RecursionReferObject { private String name; private RecursionObject refer;
} public class GsonRecursionTest { public static void main(String[] args) {
RecursionObject parent = new RecursionObject();
parent.setName("1"); RecursionReferObject son = new RecursionReferObject();
son.setName("2"); parent.setRefer(son);
son.setRefer(parent); Gson gson = new GsonBuilder().create();
String json=gson.toJson(parent);
System.out.println(json);
RecursionObject recursionObject=gson.fromJson(json,RecursionObject.class);
System.out.println(recursionObject);
}
} /***
Exception in thread "main" java.lang.StackOverflowError
at com.google.gson.stream.JsonWriter.string(JsonWriter.java:566)
at com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:402)
at com.google.gson.stream.JsonWriter.value(JsonWriter.java:417)
at com.google.gson.internal.bind.TypeAdapters$16.write(TypeAdapters.java:406)
at com.google.gson.internal.bind.TypeAdapters$16.write(TypeAdapters.java:390)
/**

说明:

  • 推荐对象字段使用基础类型
  • 不需要添加给字段添加注解来表示该字段需要序列化,因为当前类(所有父类)中的所有字段默认都会被序列化
  • 如果一个字段被标记为transient,默认它在序列化/反序列化时会被忽略
  • null的处理
    • 当序列化时,一个null字段会被省略
    • 当反序列化时,如果一个字段找不到,则对应的对象字段会被设置为以下默认值:对象类型为null,数值类型为0,boolean类型为false
  • 被synthetic 标记的字段,也会在序列化/反序列化过程中被忽略
  • 内部类、匿名类、本地类所对应的外部类字段,在序列化/反序列化过程中也将会忽略(这块没太理解)

1.2.3.内部类(没看太懂)

Gson can serialize static nested classes quite easily.

Gson can also deserialize static nested classes. However, Gson can not automatically deserialize the pure inner classes since their no-args constructor also need a reference to the containing Object which is not available at the time of deserialization. You can address this problem by either making the inner class static or by providing a custom InstanceCreator for it. Here is an example:

public class A {
public String a; class B { public String b; public B() {
// No args constructor for B
}
}
}

NOTE: The above class B can not (by default) be serialized with Gson.

Gson can not deserialize {"b":"abc"} into an instance of B since the class B is an inner class. If it was defined as static class B then Gson would have been able to deserialize the string. Another solution is to write a custom instance creator for B.

public class InstanceCreatorForB implements InstanceCreator<A.B> {
private final A a;
public InstanceCreatorForB(A a) {
this.a = a;
}
public A.B createInstance(Type type) {
return a.new B();
}
}

The above is possible, but not recommended.

1.2.4.Array

Gson gson = new Gson();
int[] ints = {1, 2, 3, 4, 5};
String[] strings = {"abc", "def", "ghi"}; // Serialization
gson.toJson(ints); // ==> [1,2,3,4,5]
gson.toJson(strings); // ==> ["abc", "def", "ghi"] // Deserialization
int[] ints2 = gson.fromJson("[1,2,3,4,5]", int[].class);
// ==> ints2 will be same as ints

支持多维。

1.2.5.集合

Gson gson = new Gson();
Collection<Integer> ints = Lists.immutableList(1,2,3,4,5); // Serialization
String json = gson.toJson(ints); // ==> json is [1,2,3,4,5] // Deserialization
Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);
// ==> ints2 is same as ints

限制:gson可以序列化任意对象的集合,但是反序列化时需要指定集合元素的类型。

1.2.泛型

1.2.1.TypeToken的使用

1.2.1.1.对象类型的泛型
class Foo<T> {
T value;
}
Gson gson = new Gson();
Foo<Bar> foo = new Foo<Bar>();
gson.toJson(foo); // May not serialize foo.value correctly gson.fromJson(json, foo.getClass()); // Fails to deserialize foo.value as Bar Type fooType = new TypeToken<Foo<Bar>>() {}.getType();
gson.toJson(foo, fooType);
gson.fromJson(json, fooType);

通过TypeToken来定义泛型类型。

1.2.1.2.集合类型的泛型

@Data
public class Bar { private String name;
} public class GsonListTest { public static void main(String[] args) {
Gson gson = new GsonBuilder().create(); List<Bar> bars = new ArrayList<>(); Bar bar = new Bar();
bar.setName("bar 1"); bars.add(bar);
String json = gson.toJson(bars);
System.out.println(json);
Type type = new TypeToken<List<Bar>>(){}.getType();
List<Bar> dbars = gson.fromJson(json,type);
System.out.println(dbars); }
} /**
[{"name":"bar 1"}]
[Bar(name=bar 1)]
***/

1.2.2.自定义ParameterizedType

在实际项目中,如果使用大量使用TypeToken,定义起来会比较麻烦,查看TypeToken的底层源码,发现它也是通过ParameterizedType来实现的。(不懂ParameterizedType的可以先百度一下)


public class MyParameterizedType implements ParameterizedType { private Type[] args; private Class rawType; public MyParameterizedType( Class rawType,Type[] args) {
this.args = args;
this.rawType = rawType;
} @Override
public Type[] getActualTypeArguments() {
return args;
} @Override
public Type getRawType() {
return rawType;
} @Override
public Type getOwnerType() {
return null;
}
} //测试复杂泛型类型 public class ParameterizedTypeTest { public static void main(String[] args) {
Gson gson = new GsonBuilder().create();
Result<List<Bar>> result = new Result<>();
List<Bar> bars = new ArrayList<>();
Bar bar = new Bar();
bar.setName("bar 1");
bars.add(bar);
result.setData(bars); Type inner = new MyParameterizedType(List.class, new Class[]{Bar.class});
MyParameterizedType type = new MyParameterizedType(Result.class,new Type[]{inner}); String json = gson.toJson(result);
System.out.println(json);
Result<List<Bar>> result1=gson.fromJson(json,type);
System.out.println(result1); }
}

1.3.null值处理

Gson gson = new GsonBuilder().serializeNulls().create();

public class Foo {
private final String s;
private final int i; public Foo() {
this(null, 5);
} public Foo(String s, int i) {
this.s = s;
this.i = i;
}
} Gson gson = new GsonBuilder().serializeNulls().create();
Foo foo = new Foo();
String json = gson.toJson(foo);
System.out.println(json); json = gson.toJson(null);
System.out.println(json); {"s":null,"i":5}
null

java基础知识-序列化/反序列化-gson基础知识的更多相关文章

  1. java使用类序列化反序列化(读写文件)

    创建类:Role package com.wbg.springRedis.entity; import java.io.Serializable; public class Role implemen ...

  2. Java安全之SnakeYaml反序列化分析

    Java安全之SnakeYaml反序列化分析 目录 Java安全之SnakeYaml反序列化分析 写在前面 SnakeYaml简介 SnakeYaml序列化与反序列化 常用方法 序列化 反序列化 Sn ...

  3. 【Java基础】序列化与反序列化深入分析

    一.前言 复习Java基础知识点的序列化与反序列化过程,整理了如下学习笔记. 二.为什么需要序列化与反序列化 程序运行时,只要需要,对象可以一直存在,并且我们可以随时访问对象的一些状态信息,如果程序终 ...

  4. Java做acm所需要的基础知识之排序问题

    Java做acm所需要的基础知识. 以前做acm的题都是用C/C++来写代码的,在学习完Java之后突然感觉Java中的方法比C/C++丰富很多,所以就整理一下平时做题需要用到的Java基础知识. 1 ...

  5. JAVA基础之——序列化和反序列化

    1 概念 序列化,将java对象转换成字节序列的过程. 反序列化,将字节序列恢复成java对象的过程. 2 为什么要序列化? 2.1 实现数据持久化,当对象创建后,它就会一直在,但是在程序终止时,这个 ...

  6. Java基础系列——序列化(一)

    原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6797659.html 工作中发现,自己对Java的了解还很片面,没有深入的研究,有很多的J ...

  7. JAVA反序列化漏洞基础原理

    JAVA反序列化漏洞基础原理 1.1 什么是序列化和反序列化? Java序列化是指把Java对象转换为字节序列的过程: Java反序列化是指把字节序列恢复为Java对象的过程: 1.2 为什么要序列化 ...

  8. python 全栈开发,Day32(知识回顾,网络编程基础)

    一.知识回顾 正则模块 正则表达式 元字符 : . 匹配除了回车以外的所有字符 \w 数字字母下划线 \d 数字 \n \s \t 回车 空格 和 tab ^ 必须出现在一个正则表达式的最开始,匹配开 ...

  9. 【学习笔记】Linux基础(零):预备知识

    学习笔记(连载)之Linux系列 Note:本学习笔记源自<鸟哥的Linux私房菜(基础学习篇)>一书,为此书重要内容的摘要和总结,对于一些常识性的知识不再归纳 新型冠状病毒引发的肺炎战& ...

随机推荐

  1. Anaconda Navigator卡logo打不开闪退问题处理方案-更换阿里云镜像源

    镜像下载.域名解析.时间同步请点击阿里云开源镜像站 一.打开软件卡logo,点击图标后闪退 最近有同事使用anaconda时出现了卡logo,显示loading applications,点击图标时发 ...

  2. Centos7下开启防火墙,允许通过的端口

    1.查看防火墙状态 systemctl status firewalld 2.如果不是显示active状态,需要打开防火墙 systemctl start firewalld 3.# 查看所有已开放的 ...

  3. 开源电调blheli / blheli_s分析

    一. 启动阶段分析 启动阶段需完成24次换相,超过24次之后进入初始运行阶段,该阶段持续12次换相周期(每个周期6次换相),完成后进入正常运转阶段 二. 换相时间分析 总体思想是根据电机运行状态计算前 ...

  4. TTL、RS232、RS485、UART、串口的关系和常见半双工、全双工协议

    串口(UART口).COM口.USB口.DB9.DB25是指的物理接口形式(硬件) TTL.RS-232.RS-485是指的电平标准(电平信号)   我们单片机嵌入式常用的串口有三种(TTL/RS-2 ...

  5. 程序语言与编程实践7-> Java实操4 | 第三周作业及思路讲解 | 异常处理考察

    第三周作业,可能是异常那一章当时没怎么听,此前也不怎么接触,感觉还挺陌生的. 00 第1题 00-1 题目 /* * To change this license header, choose Lic ...

  6. vue渐进式?

    小到可以只使用核心功能,比如单文件组件作为一部分嵌入:大到使用整个工程,vue init webpack my-project来构建项目:VUE的核心库及其生态系统也可以满足你的各式需求(core+v ...

  7. 去掉一个Vector集合中重复的元素 ?

    Vector newVector = new Vector(); For (int i=0;i<vector.size();i++) { Object obj = vector.get(i); ...

  8. python 列表推导式,生成器推导式,集合推导式,字典推导式简介

    1.列表推导式multiples = [i for i in range(30) if i % 2 is 0]names = [[],[]]multiples = [name for lst in n ...

  9. mybatis学习一:基于xml与注解配置入门实例与问题

    注:本case参考自:http://www.cnblogs.com/ysocean/p/7277545.html 一:Mybatis的介绍: MyBatis 本是apache的一个开源项目iBatis ...

  10. vue-router的原理,例如hashhistory和History interface?

    vue-router用法:在router.js或者某一个路由分发页面配置path, name, component对应关系 每个按钮一个value, 在watch功能中使用this.$router.p ...