`
abeisnow
  • 浏览: 5490 次
  • 来自: ...
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

Gson Design Document

阅读更多

Gson Design Document


Inderjeet Singh, Joel Leitch

This document presents issues that we faced while designing Gson. It is meant for advanced users or developers working on Gson. If you are interested in learning how to use Gson, see its user guide.

Navigating the Json tree or the target Type Tree while deserializing

When you are deserializing a Json string into an object of desired type, you can either navigate the tree of the input, or the type tree of the desired type. Gson uses the latter approach of navigating the type of the target object. This keeps you in tight control of instantiating only the type of objects that you are expecting (essentially validating the input against the expected "schema"). By doing this, you also ignore any extra fields that the Json input has but were not expected.

As part of Gson, we wrote a general purpose ObjectNavigator that can take any object and navigate through its fields calling a visitor of your choice.

Supporting richer serialization semantics than deserialization semantics

Gson supports serialization of arbitrary collections, but can only deserialize genericized collections. this means that Gson can, in some cases, fail to deserialize Json that it wrote. This is primarily a limitation of the Java type system since when you encounter a Json array of arbitrary types there is no way to detect the types of individual elements. We could have chosen to restrict the serialization to support only generic collections, but chose not to.This is because often the user of the library are concerned with either serialization or deserialization, but not both. In such cases, there is no need to artificially restrict the serialization capabilities.

Supporting serialization and deserialization of classes that are not under your control and hence can not be modified

Some Json libraries use annotations on fields or methods to indicate which fields should be used for Json serialization. That approach essentially precludes the use of classes from JDK or third-party libraries. We solved this problem by defining the notion of Custom serializers and deserializers. This approach is not new, and was used by the JAX-RPC technology to solve essentially the same problem.

Using Checked vs Unchecked exceptions to indicate a parsing error

We chose to use unchecked exceptions to indicate a parsing failure. This is primarily done because usually the client can not recover from bad input, and hence forcing them to catch a checked exception results in sloppy code in the catch() block.

Creating class instances for deserialization

Gson needs to create a dummy class instance before it can deserialize Json data into its fields. We could have used Guice to get such an instance, but that would have resulted in a dependency on Guice. Moreover, it probably would have done the wrong thing since Guice is expected to return a valid instance, whereas we need to create a dummy one. Worse, Gson would overwrite the fields of that instance with the incoming data there by modifying the instance for all subsequent Guice injections. This is clearly not a desired behavior. Hence, we create class instances by invoking the parameterless constructor. We also handle the primitive types, enums, collections, sets, maps and trees as a special case.

To solve the problem of supporting unmodifiable types, we use custom instance creators. So, if you want to use a library types that does not define a default constructor (for example, Money class), then you can register an instance creator that returns a dummy instance when asked.

Using fields vs getters to indicate Json elements

Some Json libraries use the getters of a type to deduce the Json elements. We chose to use all fields (up the inheritance hierarchy) that are not transient, static, or synthetic. We did this because not all classes are written with suitably named getters. Moreover, getXXX or isXXX might be semantic rather than indicating properties.

However, there are good arguments to support properties as well. We intend to enhance Gson in a latter version to support properties as an alternate mapping for indicating Json fields. For now, Gson is fields-based.

Why are most classes in Gson marked as final?

While Gson provides a fairly extensible architecture by providing pluggable serializers and deserializers, Gson classes were not specifically designed to be extensible. Providing non-final classes would have allowed a user to legitimately extend Gson classes, and then expect that behavior to work in all subsequent revisions. We chose to limit such use-cases by marking classes as final, and waiting until a good use-case emerges to allow extensibility. Marking a class final also has a minor benefit of providing additional optimization opportunities to Java compiler and virtual machine.

Why are inner interfaces and classes used heavily in Gson?

Gson uses inner classes substantially. Many of the public interfaces are inner interfaces too (see JsonSerializer.Context or JsonDeserializer.Context as an example). These are primarily done as a matter of style. For example, we could have moved JsonSerializer.Context to be a top-level class JsonSerializerContext, but chose not to do so. However, if you can give us good reasons to rename it alternately, we are open to changing this philosophy.

Why do you provide two ways of constructing Gson?

Gson can be constructed in two ways: by invoking new Gson() or by using a GsonBuilder. We chose to provide a simple no-args constructor to handle simple use-cases for Gson where you want to use default options, and quickly want to get going with writing code. For all other situations, where you need to configure Gson with options such as formatters, version controls etc, we use a builder pattern. The builder pattern allows a user to specify multiple optional settings for what essentially become constructor parameters for Gson.

Comparing Gson with Alternate Approaches

Comparing Gson with org.json library

org.json is a much lower-level library that can be used to write a toJson() method in a class. If you can not use Gson directly (may be because of platform restrictions regarding reflection), you could use org.json to hand-code a toJson method in each object.

Comparing Gson with org.json.simple library

org.json.simple library is very similar to org.json library and hence fairly low level. The key issue with using this library is that it silently ignores errors so doesn't feel production-ready.

分享到:
评论

相关推荐

    om.google.gson.Gson Gson maven依赖

    Gson是Google开发的一款Java语言编写的库,用于在Java对象和JSON数据之间进行映射。这个库使得Java对象可以轻松地序列化成JSON格式,同时也可以从JSON字符串反序列化回Java对象。在Java开发中,Gson库非常常见,特别...

    GSON JAR包 最新包和历史包 gson-2.10.1

    介绍: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. 压缩包里有以下文件: ...

    com.google.gson.Gson 2.8.1 2.8.2 jar包 gson

    Gson是Google开发的一款强大的Java库,用于在Java对象和JSON数据之间进行映射。它的全称是Google Gson,主要用于将Java对象转换为JSON格式的字符串,也可以将JSON字符串反序列化为对应的Java对象。在Java开发中,...

    gson jar包下载

    Gson,全称为Google Simple JSON,是Google提供的一款开源库,用于在Java对象和JSON数据之间进行映射。这个库使得Java开发者能够轻松地将JSON字符串转换为等效的Java对象,反之亦然。在Java应用程序中,Gson库特别...

    json解析 gson fastjson

    使用Gson,你可以通过`Gson().toJson()`方法将Java对象转化为JSON字符串,通过`new Gson().fromJson()`方法将JSON字符串反序列化为Java对象。 例如: ```java Person person = new Person("John", "Doe"); String ...

    gson-2.8.5版本的jar包

    GSON简介 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。 Gson是Google提供的用来在Java对象和JSON数据之间进行映射的Java类库。可以将一个JSON字符串...

    googleGson JsonObject json转换包

    包含以下java源文件: com.google.gson.DefaultDateTypeAdapter.class com.google.gson.ExclusionStrategy.class com.google.gson.FieldAttributes.class com.google.gson.FieldNamingPolicy.class ...

    Gson的2.2.4jar

    Gson是Google推出的一款强大的Java库,主要用于在Java对象与JSON数据之间进行相互转换。它的全称是Google Simple JSON,自2008年发布以来,已经成为了Java开发者处理JSON数据的一个常用工具,尤其是在Android开发中...

    com.google.gson.Gson.jar

    json我们在网络请求中经常用到,最近最火的微信小程序开发中... 就会报错,因为对于嵌套类型的json它是解释不出来的,这时就需要用到com.google.gson.Gson.jar了。资源中提供了jar包与代码示例,一句代码解决您的问题。

    Gson-2.8.1的官方jar包合集【gson-2.8.1.jar,gson-2.8.1-sources.jar】

    Gson是Google开发的一款强大的Java库,用于将Java对象转换为JSON格式的字符串,以及将JSON数据解析回等效的Java对象。这个压缩包包含了Gson库的版本2.8.1,它提供了两个主要的组件:`gson-2.8.1.jar`和`gson-2.8.1-...

    gson-2.8.0-API文档-中文版.zip

    赠送jar包:gson-2.8.0.jar; 赠送原API文档:gson-2.8.0-javadoc.jar; 赠送源代码:gson-2.8.0-sources.jar; 赠送Maven依赖信息文件:gson-2.8.0.pom; 包含翻译后的API文档:gson-2.8.0-javadoc-API文档-中文...

    gson-2.8.6-API文档-中文版.zip

    赠送jar包:gson-2.8.6.jar; 赠送原API文档:gson-2.8.6-javadoc.jar; 赠送源代码:gson-2.8.6-sources.jar; 赠送Maven依赖信息文件:gson-2.8.6.pom; 包含翻译后的API文档:gson-2.8.6-javadoc-API文档-中文...

    gson各个版本 jar 包下载

    Gson JAR包是Google推出的一款功能强大的Java库,它主要用于在Java对象和JSON格式数据之间进行转换。Gson库提供了简单而直观的API,使得开发者可以轻松地将Java对象序列化为JSON格式的字符串,或者将JSON字符串反...

    gson 使用Gson将Java对象转换为JSON

    Gson是Google的一个开源项目,可以将Java对象转换成JSON,也可能将JSON转换成Java对象。 Gson里最重要的对象有2个Gson 和 GsonBuilder Gson有2个最基本的方法 1) toJson() – 转换java 对象到JSON 2) from...

    gson-2.8.6.jar下载

    gson-2.8.6.jar下载,gson是Google开发的一款优秀的Java JSON解析库。它可以将Java对象转换成JSON字符串,也可以将JSON字符串转换成Java对象。gson库简单易用,性能良好,所以很受欢迎,被广泛应用于Android和Java项目中...

    com.google.gson.Gson 2.8.5 jar包

    截止至2018-11-27,github上com.google.gson.Gson 最新的2.8.5jar包。强大的json字符串解析功能及将字符串转换为json格式。我16年上传的免费下载的2.8.0版本不知道为什么变成下载需要50分了

    gson-2.8.9-API文档-中文版.zip

    赠送jar包:gson-2.8.9.jar; 赠送原API文档:gson-2.8.9-javadoc.jar; 赠送源代码:gson-2.8.9-sources.jar; 赠送Maven依赖信息文件:gson-2.8.9.pom; 包含翻译后的API文档:gson-2.8.9-javadoc-API文档-中文...

    Google gson jar包

    标题"Google gson jar包"暗示了我们讨论的是Google Gson库的Java档案文件(JAR),它包含了所有必要的类和资源,使开发者能够在项目中直接使用Gson功能。JAR文件通常在Java开发中用于封装库,以便在应用程序中作为...

    Google-Gson 2.2.4

    **Gson库详解** Google-Gson 2.2.4是Google公司开发的一个Java库,其主要功能是将Java对象转换为JSON字符串,同时也可以将JSON数据解析为等效的Java对象。这个版本(2.2.4)是Gson的一个稳定版本,广泛应用于...

    gson-2.6.2.jar包(com.google.code.gson:gson:2.6.2)

    Gson库是Google开发的一个Java库,用于在Java对象和JSON数据之间进行映射。它的全名是Google Simple JSON,其版本2.6.2在Android开发中被广泛使用,尤其是在处理JSON序列化和反序列化任务时。Gson库通过提供`Gson`类...

Global site tag (gtag.js) - Google Analytics