使用 GSON 将具有 JavaFX CheckBox 属性的对象转换为 JSON 字符串时出错

Error in converting an object with JavaFX CheckBox attribute to a JSON string using GSON

为了简要说明,我有一个 Task 对象,我想使用 GSON 库将其转换为 JSON 字符串。该对象本身具有 String 和 int 属性,我可以将其转换为 JSON 就好了,但是当我将 CheckBox 属性添加到 class 时,会出现此错误消息

Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private javafx.beans.property.BooleanProperty javafx.scene.control.CheckBox.indeterminate accessible: module javafx.controls does not "opens javafx.scene.control" to module gson at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:349) at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:289) at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:174) at java.base/java.lang.reflect.Field.setAccessible(Field.java:168) at gson@2.8.2/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:157) at gson@2.8.2/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100) at gson@2.8.2/com.google.gson.Gson.getAdapter(Gson.java:423) at gson@2.8.2/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115) at gson@2.8.2/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164) at gson@2.8.2/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100) at gson@2.8.2/com.google.gson.Gson.getAdapter(Gson.java:423) at gson@2.8.2/com.google.gson.Gson.toJson(Gson.java:661) at gson@2.8.2/com.google.gson.Gson.toJson(Gson.java:648) at gson@2.8.2/com.google.gson.Gson.toJson(Gson.java:603) at gson@2.8.2/com.google.gson.Gson.toJson(Gson.java:583) at IAssessment/application.AddTaskController.addTask(AddTaskController.java:45)

我之前有过类似的事情,我在这里问过(错误消息以不导出到 gson 结尾),解决方案是创建一个 class 模块信息,如下所示

exports snippet;
exports application;
requires gson;
requires java.sql;
requires javafx.base;
requires javafx.controls;
requires javafx.fxml;
requires javafx.graphics;
opens application to gson, javafx.fxml;

这解决了我之前的问题,但这似乎是一个类似的问题,我不知道该怎么做。那么有办法解决吗?感谢所有帮助。

想通了

(tablecolumnname).setCellFactory(new Callback<TableColumn<(object), Boolean>, TableCell<(object), Boolean>>() {

        public TableCell<(object), Boolean> call(TableColumn<(object), Boolean> p) {
            return new CheckBoxTableCell<(object), Boolean>();
        }

    });