[英]binding a child object from JSON to parent attribute java
I have a class called Component
that contains some attributes including an object of Device
class which is a base class for some classes such as Resistance
and M1
and I have to read a JSON file for components and check the type of the device Resistance
or M1
then map it to the right class I tried to use JSON annotation but I'm still getting errors! I have a class called
Component
that contains some attributes including an object of Device
class which is a base class for some classes such as Resistance
and M1
and I have to read a JSON file for components and check the type of the device Resistance
or M1
then map 向右 class 我尝试使用 JSON 注释,但我仍然遇到错误!
Here are my classes Component Class:这是我的课程组件 Class:
public class Component {
private String type;
private String id;
@JsonProperty
private Device device;
@JsonProperty("netlist")
private HashMap<String,String> netlist;
public Component() {
}
public Component(String type, String id, Device device, HashMap<String, String> netList) {
this.type = type;
this.id = id;
this.device = device;
this.netlist = netList;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Device getDevice() {
return device;
}
public void setDevice(Device device) {
this.device = device;
}
public HashMap<String, String> getNetlist() {
return netlist;
}
public void setNetlist(HashMap<String, String> netlist) {
this.netlist = netlist;
}
@Override
public String toString() {
return "type='" + type + '\'' +
", id='" + id + '\'' +
", "+device.toString()+
", netList=" + netlist ;
}
}
Device Class:设备 Class:
public abstract class Device {
@JsonProperty("default")
protected int defaultValue;
protected int min;
protected int max;
public Device() {
}
public Device(int defaultValue, int min, int max) {
this.defaultValue = defaultValue;
this.min = min;
this.max = max;
}
public int getDefaultValue() {
return defaultValue;
}
public void setDefaultValue(int defaultValue) {
this.defaultValue = defaultValue;
}
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
}
Resistance:反抗:
@JsonTypeName("resistance")
public class Resistance extends Device {
public Resistance() {
}
@Override
public String toString() {
return "resistance{" +
"default=" + defaultValue +
", min=" + min +
", max=" + max +
'}';
}
}
M1 Class: M1 Class:
@JsonTypeName(value = "m(1)")
public class M1 extends Device {
@Override
public String toString() {
return "m(1){" +
"default=" + defaultValue +
", min=" + min +
", max=" + max +
'}';
}
}
and this is a simple JSON file:这是一个简单的 JSON 文件:
"components": [
{
"type": "resistor",
"id": "res1",
"resistance": {
"default": 100,
"min": 10,
"max": 1000
},
"netlist": {
"t1": "vdd",
"t2": "n1"
}
},
{
"type": "nmos",
"id": "m1",
"m(l)": {
"deafult": 1.5,
"min": 1,
"max": 2
},
"netlist": {
"drain": "n1",
"gate": "vin",
"source": "vss"
}
}
]
Thanks in advance提前致谢
@JsonTypeName
does not do what you think it does. @JsonTypeName
没有做你认为的事情。 In its reference documentation you can read:在其参考文档中,您可以阅读:
Annotation used for binding logical name that the annotated class has.
用于绑定带注释的 class 具有的逻辑名称的注释。 Used with JsonTypeInfo (and specifically its JsonTypeInfo.use() property) to establish relationship between type names and types.
与 JsonTypeInfo(特别是其 JsonTypeInfo.use() 属性)一起使用以建立类型名称和类型之间的关系。
This means that you are missing key annotations in Device
( @JsonTypeInfo
and @JsonSubTypes
) so that Jackson knows how to read the JSON you are providing:这意味着您缺少
Device
( @JsonTypeInfo
和@JsonSubTypes
)中的关键注释,因此 Jackson 知道如何阅读您提供的 JSON :
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
@Type(value = Resistance.class, name = "resistance"),
@Type(value = M1.class, name = "m1")
})
public abstract class Device {
(...)
}
With this you can get rid of @JsonTypeName
in Resistance
and M1
.有了这个,您可以摆脱
Resistance
和M1
中的@JsonTypeName
。
However, this is not enough.然而,这还不够。 You also need to change your JSON to match
Component
properties ( resistance
and m(1)
must be renamed to device
) and you also need to add a type
to this device
property to match @JsonTypeInfo
and @JsonSubTypes
:您还需要更改 JSON 以匹配
Component
属性( resistance
和m(1)
必须重命名为device
),您还需要向此device
属性添加一个type
以匹配@JsonTypeInfo
和@JsonSubTypes
:
"components": [
{
"type": "resistor",
"id": "res1",
"device": {
"type": "resistance",
"default": 100,
"min": 10,
"max": 1000
},
"netlist": {
"t1": "vdd",
"t2": "n1"
}
},
{
"type": "nmos",
"id": "m1",
"device": {
"type": "m1",
"deafult": 1.5,
"min": 1,
"max": 2
},
"netlist": {
"drain": "n1",
"gate": "vin",
"source": "vss"
}
}
]
You can read more about this in the following online resources:您可以在以下在线资源中阅读更多相关信息:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.