Gson是默认的无参数构造函数吗? [英] Is default no-args constructor mandatory for Gson?

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

问题描述

Gson用户指南指出,我们应该为任何类定义默认的无参数构造函数与Gson合作。更甚者,在Gson's的 javadoc 上, InstanceCreator 类表示如果我们尝试反序列化缺少默认构造函数的类的实例并且应该使用 InstanceCreator在这种情况下。但是,我试过测试使用Gson的类缺乏默认构造函数,并且序列化和反序列化都没有任何问题。



这是deserializaiton的一段代码。一个没有非参数构造函数的类:
$ b

  public class Mushroom {
private String name ;
私人双倍直径;

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

// equals(),hashCode()等
}

和一个测试:
$ b

  @Test 
public void deserializeMushroom(){
assertEquals(
new Mushroom(Fly agaric,4.0),
new Gson()。fromJson(
{name:\木耳,直径:4.0},Mushroom.class));
}

正常工作。



所以我的问题是:我可以在不需要默认构造函数的情况下实际使用Gson吗?或者在任何情况下无法使用Gson?

InstanceCreater 对象,那么它会创建一个 ObjectConstructor )与 UnsafeAllocator ,它使用Reflection获取类 sun.misc的 allocateInstance 方法.Unsafe 来创建您的类的实例。



Unsafe class 围绕缺少无参数构造函数和有其他危险用途 allocateInstance states


分配一个实例但不运行任何构造函数。初始化
类,如果还没有的话。


所以它实际上并不需要构造函数,你的两个参数构造函数。请参阅此处的一些示例。

如果你有一个无参数构造函数,Gson将使用一个 ObjectConstructor ,它使用默认的 $构造函数通过调用

  yourClassType.getDeclaredConstructor(); // ie。空,无参数

我的2分:按照Gson的说法和使用无参数构造函数创建类或注册 InstanceCreator 。使用 Unsafe 可能会发现自己处于不利位置。


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.
}

and a test:

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

which works fine.

So my question is: could I actually use Gson without need to have default constructor or there is any circumstances when it will not work?

解决方案

As of Gson 2.3.1.

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.

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.

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

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天全站免登陆