Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将Gson扩展到构造函数并为任何类型生成默认对象?_Java_Json_Reflection_Gson - Fatal编程技术网

Java 如何将Gson扩展到构造函数并为任何类型生成默认对象?

Java 如何将Gson扩展到构造函数并为任何类型生成默认对象?,java,json,reflection,gson,Java,Json,Reflection,Gson,全部, Gson给了我很多启发,现在我想把Gson扩展到任何其他类型的具有自定义默认值的对象。我应该重写格森的哪部分?我真的很想重用Gson反射和支持的基本类型。但我发现,在审查了Gson的源代码之后,基于当前Gson的设计,要做到这一点有一些困难 现在,我的要求可以表示如下: 我定义了一个POJO类,例如: TestInputParam.class: public class TestInputParam{ private Date startTime; private Str

全部,

Gson给了我很多启发,现在我想把Gson扩展到任何其他类型的具有自定义默认值的对象。我应该重写格森的哪部分?我真的很想重用Gson反射和支持的基本类型。但我发现,在审查了Gson的源代码之后,基于当前Gson的设计,要做到这一点有一些困难

现在,我的要求可以表示如下:

我定义了一个POJO类,例如:

TestInputParam.class:

public class TestInputParam{
    private Date startTime;
    private String name;
    private int num;

    //setters and gettters
}
GsonEx<TestInputParam> gsonEx = new GsonEx<TestInputParam>();
TestInputParam defaultParam = gsonEx.build(TestInputParam.class) 

System.out.println(new Gson().toJson(defaultParam));
It should output this object default value .
要求:

public class TestInputParam{
    private Date startTime;
    private String name;
    private int num;

    //setters and gettters
}
GsonEx<TestInputParam> gsonEx = new GsonEx<TestInputParam>();
TestInputParam defaultParam = gsonEx.build(TestInputParam.class) 

System.out.println(new Gson().toJson(defaultParam));
It should output this object default value .
注意事项:

public class TestInputParam{
    private Date startTime;
    private String name;
    private int num;

    //setters and gettters
}
GsonEx<TestInputParam> gsonEx = new GsonEx<TestInputParam>();
TestInputParam defaultParam = gsonEx.build(TestInputParam.class) 

System.out.println(new Gson().toJson(defaultParam));
It should output this object default value .

我的理解是:new Gson().fromJson(stringContent,type)通过JsonReader用stringContent值构建其对应的对象,只需扩展它就可以通过一些默认值或随机值构建其对应的对象。不要让它的字段值来自stringContent。

如果我理解您的问题,您可能正在寻找Gson的类型适配器。这些允许您创建

假设您有以下JSON:

{
  "foo": ...,
  "bar": ...,

  "testInputParam": {
    "startTime": {...},
    "name": "SomeName",
    "num": 1
  },

  "someArray": [...]    
}
例如,您可以编写如下自定义反序列化程序:

private class TestInputParamDeserializer implements JsonDeserializer<TestInputParam> {

  public TestInputParam deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
      throws JsonParseException {

    //This is the actual "testInputParam" JSON object...
    JsonObject object = json.getAsJsonObject();

    //This is the custom object you will return...
    TestInputParam result = new TestInputParam();

    //Fill in the "startDate" field with the current Date instead of the actual value in the JSON...
    result.setStartDate = new Date(); 

    //Use the actual "name" found in the JSON...
    result.name = object.get("name").getAsJsonString();

    //Fill in the "num" with a random value...
    result.setNum = new Random().nextInt();

    //Finally return your custom object...
    return result;
  }
}
现在,每当您使用此
gson
对象来反序列化JSON字符串时,每次它找到表示
TestInputParam
类的JSON对象时,它都将使用自定义反序列化,但您仍然可以对JSON字符串的其余部分使用默认反序列化



编辑:使用这种方法,您必须为要进行自定义序列化/反序列化的每个类编写自定义序列化程序/反序列化程序。还有一个名为
TypeAdapterFactory
的类,它允许您为一组相关类型创建类型适配器。您可以找到信息和示例。

对不起,我不明白。如果JSON字符串为空,是否要求返回具有默认值的对象?否,我的意思是如何将Gson扩展为基于其类型的对象生成器。这意味着自动填充一种类型的对象作为我们通过值工厂自定义的一些值。或者一些随机值。是的,你明白我的想法,但是你的解决方案意味着对于每个TestInputParam,我们需要一个接一个地编写它们?我想要实现的是一个通用类型的通用解决方案。它就像对象工厂一样,可以创建任何对象,其基本字段具有一些随机值或自定义值。不仅仅针对特殊的POJO,例如:TestInputParam