gpt4 book ai didi

java - 当类包含接口(interface)类型的变量时,如何通过 jackson 序列化/反序列化?

转载 作者:行者123 更新时间:2023-12-02 08:51:03 24 4
gpt4 key购买 nike

有一个接口(interface),假设是 A。我有多个实现该接口(interface) A 的类。这些类还包含 A 类型的类变量。所以,就像:

@JsonTypeInfo(use = JsonTypeInfo.Id.Class, include = JsonTypeInfo.As.Property, property = "className")

@JsonSubType({
@Type(value = abstractClass.class, name = "abstractClass"),
@Type(value = subClass1.class, name = "subClass1"),
@Type(value = subClass2.class, name = "subClass2"),
'
'
'
})
interface A {
func1();
func2();
}

abstract class abstractClass implements A {
int abstractvar1;
func1(){//code}
}

class subClass1 extends abstractClass {
int var1;
int var2;
A var3;
A var4;
}

class subClass2 extends abstractClass {
int var1;
int var2;
A var3;
}

class subClass3 extends abstractClass {
float var1;
int var2;
A var3;
}

and more classes defined trying to extend abstractClass..

构造函数、getter 和 setter 已经定义。

由所有变量组成的类

class Implementor {
int implementorVar1;
String implementorVar2;
A implementorVar3;
int implementorVar4;
}

所以,我想将 Implementor 类序列化为 JSON。我用 jackson 也是为了同样的目的。因此,我将 @jsonTypeInfo 和 @type 添加到接口(interface)中,以便它们有一个可以使用的具体类。但是当我尝试序列化子类时,只有 int 类型的 var1 和 var2 被序列化,而不是 A 类型的 var3/var4 被序列化。我怎样才能序列化这些变量?

如果我尝试序列化 Implementor,我会得到 Json:

{
"implementorVar1": 1,
"implementorVar2": "hello",
"implementorVar3": {
"className": "subClass2",
"abstractVar1": 45,
},
"implementorVar4": 1000
}

我期待的 Json:

{
"implementorVar1": 1,
"implementorVar2": "hello",
"implementorVar3": {
"className": "subClass2",
"abstractVar1" : 45,
"var1": 45,
"var2": 56,
"var3": {
"className": "subClass3",
"var1": 2,
"var2": 5,
"var3" : {
"className" : "" ...
}
}
},
"implementorVar4": 1000
}

最佳答案

我通过一些更改复制了您的代码,当按如下方式实现(序列化和反序列化)时,它对我有用,所以请告诉我这是否符合您的期望。需要注意的要点是对注释的一些小修正,我发现在默认配置下,拥有正确的 getter 和 setter 绝对至关重要,否则属性将不会被序列化 - 这似乎是最有可能的问题。

我个人会考虑使用配置来允许 Jackson 直接使用属性,因为我讨厌一揽子 getters 和 setters 公开泄漏你所有的内部状态,而不是封装它并暴露特定的行为,但这只是意见 - 与你的问题无关!

输出:

{
"implementorVar1" : 1,
"implementorVar2" : "hello",
"implementorVar3" : {
"className" : "subClass2",
"var1" : 1,
"var2" : 2,
"var3" : {
"className" : "subClass3",
"var1" : 1.0,
"var2" : 2,
"var3" : {
"className" : "subClass1",
"var1" : 1,
"var2" : 2
}
}
},
"implementorVar4" : 1000
}

代码片段:

public static void main(String[] args) {
Implementor target = new Implementor(1, "hello",
new SubClass2(1, 2,
new SubClass3(1F, 2,
new SubClass1(1, 2))),
1000);
try {
ObjectMapper mapper = new ObjectMapper();
String json = mapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(target);
Implementor deserialised = mapper.readValue(json, Implementor.class);
System.out.println(json);
System.out.println(deserialised);
} catch (Exception e) {
e.printStackTrace();
}
}


class Implementor {
private int implementorVar1;
private String implementorVar2;
private A implementorVar3;
private int implementorVar4;

public Implementor() {}

public Implementor(int implementorVar1, String implementorVar2, A implementorVar3, int implementorVar4) {
this.implementorVar1 = implementorVar1;
this.implementorVar2 = implementorVar2;
this.implementorVar3 = implementorVar3;
this.implementorVar4 = implementorVar4;
}

public int getImplementorVar1() {
return implementorVar1;
}

public void setImplementorVar1(int implementorVar1) {
this.implementorVar1 = implementorVar1;
}
// Other getters/setters omitted
// Default configuration ABSOLUTELY requires getters and setters for all properties in all serialised classes
}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "className")
@JsonSubTypes({
@JsonSubTypes.Type(value = SubClass1.class, name = "subClass1"),
@JsonSubTypes.Type(value = SubClass2.class, name = "subClass2"),
@JsonSubTypes.Type(value = SubClass3.class, name = "subClass3")
})
interface A {
int func1();
int func2();
}


class SubClass1 extends AbstractClass {
private int var1;
private int var2;

public SubClass1() {}

public SubClass1(int var1, int var2) {
this.var1 = var1;
this.var2 = var2;
}

@Override
public int func1() {
return 0;
}

@Override
public int func2() {
return 0;
}

// getters and setters omitted but they HAVE to be there
}

class SubClass2 extends AbstractClass {
private int var1;
private int var2;
private A var3;

public SubClass2() {}

public SubClass2(int var1, int var2, A var3) {
this.var1 = var1;
this.var2 = var2;
this.var3 = var3;
}
// getters and setters omitted but they HAVE to be there
}

class SubClass3 extends AbstractClass {
private float var1;
private int var2;
private A var3;

public SubClass3() {}

public SubClass3(float var1, int var2, A var3) {
this.var1 = var1;
this.var2 = var2;
this.var3 = var3;
}

@Override
public int func1() {
return 0;
}

@Override
public int func2() {
return 0;
}
// getters and setters omitted but they HAVE to be there
}

关于java - 当类包含接口(interface)类型的变量时,如何通过 jackson 序列化/反序列化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60765284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com