Jackson 解串器未触发

问题描述 投票:0回答:1

我有一个可以轻松序列化的 SessionFlowModel 类。然而,由于使用了 ObservableLists,它是 JavaFX tableView 的模型,用户可以在其中选择某些单元格

public class SessionFlowModel {

    @JsonDeserialize(using = JsonObservableListAdapter.class)
    protected ObservableList<ObservableList<Boolean>> selectableMatrix; // matrix indicating whether a transition between two exercises is possible or not
    protected ObservableList<Exercise> exercises;                           // the list of exercises for the x-axis and y-axis
    @JsonIgnore
    protected DynamicSessionCtrl ctrl;                                      // reference to the UI controller to inform the UI on necessary updates

    // getter and setter here
}

在我定义的管理持久性的类中

    public void saveFlow(File file, SessionFlowModel flowModel) {
        
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.writeValue(file, flowModel);
        } catch (Exception e) {
            debugLogger.fine("Cannot save flowModel to " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }


    public void loadFlow(File file, SessionFlowModel flowModel) {

        try {
            ObjectMapper objectMapper = new ObjectMapper();
            SessionFlowModel readModel = objectMapper.readValue(file,SessionFlowModel.class);
            flowModel.setExercises(readModel.getExercises());
            flowModel.setObservableSelectableMatrix(readModel.getSelectableMatrix());

        } catch (Exception e) {
            debugLogger.fine("Cannot load flowModel from " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }

虽然方法 saveFlow 工作正常,但 loadFlow 方法不会触发下面的 JsonObservableListAdapter 类反序列化方法,如 SessionFlowModel selectableMatrix 属性中注释的那样(见上文)。

public class JsonObservableListAdapter extends JsonDeserializer<ObservableList<ObservableList<Boolean>>> {
    
    
    public JsonObservableListAdapter() {
    }
    
    
    @Override
    public ObservableList<ObservableList<Boolean>> deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException, JacksonException {

        ObservableList<ObservableList<Boolean>> matrix = FXCollections.observableArrayList();
        
        ObjectMapper objectMapper = (ObjectMapper) parser.getCodec();
        JsonNode node = objectMapper.readTree(parser);
        
        JsonNode childList = node.get("selectableMatrix");
        
        for (Iterator<JsonNode> i = childList.iterator(); i.hasNext();) {
            JsonNode childNode = i.next();
        }
       
        return matrix;
    }
}

我错过了什么?

java jackson deserialization
1个回答
0
投票

如果使用注释

@JsonDeserialize
的方法不起作用,您可以尝试使用类
SimpleModule
向 ObjectMapper 注册自定义反序列化器 (
JsonObservableListAdapter
) 的其他方法。 开始在 Jackson 中进行自定义反序列化

    public void loadFlow(File file, SessionFlowModel flowModel) {

        try {
            ObjectMapper objectMapper = new ObjectMapper();

            // Register the custom deserializer
            SimpleModule module = new SimpleModule();
            //module.addDeserializer(ObservableList.class, new JsonObservableListAdapter());
            module.addDeserializer(new TypeReference<ObservableList<ObservableList<Boolean>>>() {}, new JsonObservableListAdapter());
            objectMapper.registerModule(module);

            SessionFlowModel readModel = objectMapper.readValue(file,SessionFlowModel.class);
            flowModel.setExercises(readModel.getExercises());
            flowModel.setObservableSelectableMatrix(readModel.getSelectableMatrix());

        } catch (Exception e) {
            debugLogger.fine("Cannot load flowModel from " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.