gson README

The project's README from github.com/google/gson. As one markdown file: README.md.

Gson

Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.

There are a few open-source projects that can convert Java objects to JSON. However, most of them require that you place Java annotations in your classes; something that you can not do if you do not have access to the source-code. Most also do not fully support the use of Java Generics. Gson considers both of these as very important design goals.

[!NOTE]
Gson is currently in maintenance mode; existing bugs will be fixed, but large new features will likely not be added. If you want to add a new feature, please first search for existing GitHub issues, or create a new one to discuss the feature and get feedback.

[!IMPORTANT]
Gson's main focus is on Java. Using it with other JVM languages such as Kotlin or Scala might work fine in many cases, but language-specific features such as Kotlin's non-null types or constructors with default arguments are not supported. This can lead to confusing and incorrect behavior.
When using languages other than Java, prefer a JSON library with explicit support for that language.

[!IMPORTANT]
Gson is not a recommended library for interacting with JSON on Android. The open-ended reflection in the Gson runtime doesn't play nicely with shrinking/optimization/obfuscation passes that Android release apps should perform.
If your app or library may be running on Android, consider using Kotlin Serialization, which uses code generation instead of reflection. This avoids Gson's runtime crashes when optimizations are applied (usually due to the fields missing or being obfuscated), and results in faster performance on Android devices. If you still want to use Gson and attempt to avoid these crashes, you can see how to do so here.

Goals

Download

Gradle:

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

Maven:

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.14.0</version>
</dependency>

Gson jar downloads are available from Maven Central.

Build Status

Requirements

Minimum Java version

Despite supporting older Java versions, Gson also provides a JPMS module descriptor (module name com.google.gson) for users of Java 9 or newer.

JPMS dependencies (Java 9+)

These are the optional Java Platform Module System (JPMS) JDK modules which Gson depends on. This only applies when running Java 9 or newer.

Minimum Android API level

Older Gson versions may also support lower API levels, however this has not been verified.

Documentation

Please use the 'gson' tag on StackOverflow, GitHub Discussions or the google-gson Google group to discuss Gson or to post questions.

ProGuard / R8

See the details in the related section in the Troubleshooting guide.

Building

Gson uses Maven to build the project:

mvn clean verify

JDK 17 or newer is required for building, JDK 21 is recommended. JDK 25 is supported, but only runs a subset of all tests if JDK jmod files are missing. Newer JDKs are currently not supported for building (but are supported when using Gson).

Contributing

See the contributing guide.
Please perform a quick search to check if there are already existing issues or pull requests related to your contribution.

Keep in mind that Gson is in maintenance mode. If you want to add a new feature, please first search for existing GitHub issues, or create a new one to discuss the feature and get feedback.

License

Gson is released under the Apache 2.0 license.

Copyright 2008 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Disclaimer

This is not an officially supported Google product.

gson (google/gson)

Searching Database…

24.2K ·GitHub
这是什么东西?Gson 是一个 Java 库,可以把 Java 对象序列化成 JSON,也能把 JSON 反序列化回 Java 对象,而且适用于任意类——哪怕你没有源代码的类也行。
它和其他 Java 的 JSON 库有什么不同?大多数 Java JSON 库需要在你的类上加注解,而且对泛型的支持不完整。Gson 两者都不需要:它能转换任意对象,包括那些你无法修改的已有类,并且对泛型有很好的支持。
我怎么把它加到我的项目里?把 Maven Central 上的 com.google.gson:gson 构件作为 Gradle 或 Maven 依赖添加即可——它是一个库,不是独立工具。仓库里还提供了带示例的用户指南和已发布的 Javadoc。
我需要哪个 Java 版本?Gson 2.12.0 及更新版本需要 Java 8;更早的版本最低支持到 Java 7,甚至 Java 6。在 Java 9 及以上,它还附带一个名为 com.google.gson 的 JPMS 模块。
它还在积极开发吗?Gson 目前处于维护模式:现有的 bug 会继续修复,但不太可能新增大型功能,新功能的提议必须先在 GitHub issue 里讨论。
怎么把对象转成 JSON 再转回来?两个方法:gson.toJson(obj) 返回 JSON 字符串,gson.fromJson(json, MyClass.class) 把它解析回对象。还可以为特定类型注册自定义的表示方式。
它能处理复杂的对象图和我无法控制的类吗?可以——这正是它明确的设计目标:支持任意复杂的对象,包括深层继承结构和大量使用泛型类型的情况,还支持转换那些无法加注解的已有不可修改对象。
如果我的类没有无参构造函数怎么办?在 Java 9 及以上,当相应的 JDK 模块可用时,Gson 可以退而使用 sun.misc.Unsafe 来创建这类实例,但 README 警告说它并非在所有环境都可用,而且有一些坑;你可以用 GsonBuilder.disableJdkUnsafe() 把它禁用。
Gson 是谷歌的官方产品吗?它位于 google 这个 GitHub 组织下,但 README 的免责声明说明它不是谷歌官方支持的产品——它是一个由社区维护的开源项目,采用 Apache 2.0 许可证。
What are similar projects?fastjson, auto, javapoet, butterknife, hutool, protobuf, moshi, protobuf
Creator
google
Size
23 MB
Stars
24.2K
Age
11y
Commits · 90 Days