简体   繁体   English

使用Jackson正确地序列化和反序列化Java类

[英]Properly serializing and deserializing a Java Class with Jackson

I've got a model RForm that needs to be serialized and deserialized using the Java JSON library Jackson . 我有一个RForm模型,需要使用Java JSON库Jackson进行序列化和反序列化。

The current RForm model is: 当前的RForm模型是:

public class RForm implements Serializable {

    private ArrayList<Element> pit;
    private ArrayList<Element> match;

    public RForm() {}

    public RForm(ArrayList<Element> pit, ArrayList<Element> match) {
        this.pit = pit;
        this.match = match;
    }
}

Note, I have two ArrayLists that contained Elements . 注意,我有两个包含Elements ArrayLists Element is an abstract class and has eight children, when these are being serialized, an object in the Element class could be any of these eight children. Element是一个抽象类,有八个子代,当对它们进行序列化时, Element类中的对象可以是这八个子代中的任何一个。 I've been running into a slew of problems trying to serialize and deserialize these arrays. 我在尝试序列化和反序列化这些数组时遇到了很多问题。 Here's the code to serialize: 这是要序列化的代码:
ObjectMapper mapper = new ObjectMapper();
String serialized = mapper.writeValueAsString(new RForm());
To deserialize: 反序列化:

RForm form = mapper.readValue(serialized, RForm.class);

Here's the error I'm getting: 这是我得到的错误:

error occured: Unexpected token (END_OBJECT), expected FIELD_NAME: missing 
property 'type' that is to contain type id  (for class 
com.cpjd.robluscouter.forms.elements.Element)
08-01 00:03:59.963 14143-14165/? I/System.out:     at [Source: 
java.io.StringReader@49aa883; line: 1, column: 186] (through reference 
chain: com.cpjd.robluscouter.models.RForm["match"])

Here's the Element class: 这是Element类:

@Data
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = 
JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
    @JsonSubTypes.Type(value = EBoolean.class, name = "EBoolean"),
    @JsonSubTypes.Type(value = ECheckbox.class, name = "ECheckbox"),
    @JsonSubTypes.Type(value = EChooser.class, name = "EChooser"),
    @JsonSubTypes.Type(value = ECounter.class, name = "ECounter"),
    @JsonSubTypes.Type(value = EGallery.class, name = "EGallery"),
    @JsonSubTypes.Type(value = ESlider.class, name = "ESlider"),
    @JsonSubTypes.Type(value = ESTextfield.class, name = "ESTextfield"),
    @JsonSubTypes.Type(value = EStopwatch.class, name = "EStopwatch"),
    @JsonSubTypes.Type(value = ETextfield.class, name = "ETextfield")
})
public abstract class Element implements Serializable {

private String title;
private int ID;
private boolean modified; // if this is false, we can safely override this value

public Element() {}

Element(String title) {
    this.title = title; modified = false;
}

public abstract String getSubtitle();

}

Steps that collectively fixed the problem: 共同解决问题的步骤:

  • Make sure any inner classes are static , or preferably, just put them in their own class 确保任何内部类都是static ,或者最好将它们放在自己的类中
  • Add default constructors to every class 向每个类添加默认构造函数
  • Add the annotations above each class 在每个课程上方添加注释
  • Tell the ObjectMapper to ignore any variables that it can't find 告诉ObjectMapper忽略它找不到的任何变量
  • Make sure every variable has a getter and a setter (I'm using lombok, so it was fine) 确保每个变量都有一个getter和setter(我正在使用lombok,所以很好)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM