Gson 是否必须使用默认的无参数构造函数? [英] Is default no-args constructor mandatory for Gson?

查看:27
本文介绍了Gson 是否必须使用默认的无参数构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Gson 用户指南指出我们应该为任何类定义默认的无参数构造函数正确使用 Gson.甚至更多,在 Gson 的 javadocInstanceCreator 类表示如果我们尝试反序列化缺少默认构造函数的类的实例,则会抛出异常,在这种情况下我们应该使用 InstanceCreator.但是,我尝试使用 Gson 测试缺少默认构造函数的类,并且序列化和反序列化都没有任何问题.

Gson user guide states that we should define default no-args constructor for any class to work with Gson properly. Even more, in the javadoc on Gson's InstanceCreator class said that exception will be thrown if we try to deserialize instance of class missing default constructor and we should use InstanceCreator in such cases. However, I've tried to test use Gson with class lacking default constructor and both serialization and deserialization work without any trouble.

这是反序列化的一段代码.没有非参数构造函数的类:

Here is the piece of code for deserializaiton. A class without non-args constructor:

public class Mushroom {
    private String name;
    private double diameter;

    public Mushroom(String name, double diameter) {
        this.name = name;
        this.diameter = diameter;
    }

    //equals(), hashCode(), etc.
}

和测试:

@Test
public void deserializeMushroom() {
    assertEquals(
            new Mushroom("Fly agaric", 4.0),
            new Gson().fromJson(
                    "{name:"Fly agaric", diameter:4.0}", Mushroom.class));
}

效果很好.

所以我的问题是:我真的可以在不需要默认构造函数的情况下使用 Gson 吗?或者在任何情况下它都不起作用?

推荐答案

从 Gson 2.3.1 开始.

As of Gson 2.3.1.

不管 Gson 文档怎么说,如果您的类没有无参数构造函数并且您没有注册任何 InstanceCreater 对象,那么它将创建一个 ObjectConstructor(它构造你的对象)和一个 UnsafeAllocator,它使用反射来获取 sun.misc.Unsafe 类的 allocateInstance 方法来创建你的类的实例.

Regardless of what the Gson documentation says, if your class doesn't have an no-args constructor and you have not registered any InstanceCreater objects, then it will create an ObjectConstructor (which constructs your Object) with an UnsafeAllocator which uses Reflection to get the allocateInstance method of the class sun.misc.Unsafe to create your class' instance.

这个不安全 class 解决了缺少无参数构造函数的问题,并且具有 许多其他危险使用.allocateInstance 状态

This Unsafe class goes around the lack of no-args constructor and has many other dangerous uses. allocateInstance states

分配一个实例但不运行任何构造函数.初始化如果还没有上课.

Allocate an instance but do not run any constructor. Initializes the class if it has not yet been.

所以它实际上并不需要一个构造函数,而是会绕过你的两个参数构造函数.查看一些示例此处.

So it doesn't actually need a constructor and will go around your two argument constructor. See some examples here.

如果您确实有一个无参数构造函数,Gson 将使用一个 ObjectConstructor,它通过调用

If you do have a no-args constructor, Gson will use an ObjectConstructor which uses that default Constructor by calling

yourClassType.getDeclaredConstructor(); // ie. empty, no-args

我的 2 美分:按照 Gson 所说的,使用无参数构造函数创建您的类或注册一个 InstanceCreator.使用 Unsafe 时,您可能会发现自己处于不利的位置.

My 2 cents: Follow what Gson says and create your classes with a no-arg constructor or register an InstanceCreator. You might find yourself in a bad position using Unsafe.

这篇关于Gson 是否必须使用默认的无参数构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆