Gson - 反序列化对象时的异常

时间:2018-02-16 14:21:26

标签: java json gson

我有以下对象,我正在尝试使用Gson序列化/反序列化:

public class ReportTemplate {

    private SimpleStringProperty templateName = new SimpleStringProperty("");
    private SimpleObjectProperty<ReportTemplate> destinationTemplate = new SimpleObjectProperty<>();

    // The list of ReportColumns; holds the format as well
    private ArrayList<ReportColumn> columns;

    public ReportTemplate() {
    }

    public ReportTemplate(ArrayList<ReportColumn> columns) {
        this.columns = columns;
    }

    public String getTemplateName() {
        return templateName.get();
    }

    public SimpleStringProperty templateNameProperty() {
        return templateName;
    }

    public void setTemplateName(String templateName) {
        this.templateName.set(templateName);
    }

    public ReportTemplate getDestinationTemplate() {
        return destinationTemplate.get();
    }

    public SimpleObjectProperty<ReportTemplate> destinationTemplateProperty() {
        return destinationTemplate;
    }

    public void setDestinationTemplate(ReportTemplate destinationTemplate) {
        this.destinationTemplate.set(destinationTemplate);
    }

    public List<ReportColumn> getColumns() {
        return columns;
    }

    public void setColumns(ArrayList<ReportColumn> columns) {
        this.columns = columns;
    }

    public ReportColumn getReportColumn(int index) {
        return columns.get(index);
    }

    @Override
    public String toString() {
        return templateName.get();
    }
}

运行以下测试代码非常合适:

class Test {

    private static ReportTemplate destinationFormat;
    private static ReportTemplate sourceFormat;
    private static HashMap<Integer, Integer> mappedColumns;

    public static void main(String[] args) {

        // Template Lists
        ArrayList<ReportTemplate> sources = new ArrayList<>();
        ArrayList<ReportTemplate> dests = new ArrayList<>();

        ReportTemplate hiscox = new ReportTemplate(getSource());
        hiscox.setTemplateName("Hiscox");

        ReportTemplate agm = new ReportTemplate(getDestination());
        agm.setTemplateName("Monthly AGM");

        hiscox.setDestinationTemplate(agm);
        dests.add(agm);
        sources.add(hiscox);

        Global.setSourceTemplates(sources);
        Global.setDestinationTemplates(dests);

        TemplatesDatasource.saveTemplates(Global.getSourceTemplates());
        TemplatesDatasource.saveTemplates(Global.getDestinationTemplates());

        TemplatesDatasource.loadTemplates();

    }

    private static ArrayList<ReportColumn> getSource() {
        // Hiscox source format
        ArrayList<ReportColumn> sourceColumns = new ArrayList<>();
        sourceColumns.add(new ReportColumn("Policy No.", 0, ColumnFormat.TEXT));
        sourceColumns.add(new ReportColumn("Event Type", 1, ColumnFormat.TEXT));
        sourceColumns.add(new ReportColumn("New or Renewal", 2, ColumnFormat.TEXT));
        sourceColumns.add(new ReportColumn("Insured Name", 3, ColumnFormat.TEXT));
        sourceColumns.add(new ReportColumn("Display Date", 4, ColumnFormat.TEXT));
        sourceColumns.add(new ReportColumn("Processed Date", 5, ColumnFormat.TEXT));
        sourceColumns.add(new ReportColumn("Policy Effective Date", 6, ColumnFormat.TEXT));
        sourceColumns.add(new ReportColumn("Policy Expiration Date", 7, ColumnFormat.TEXT));
        sourceColumns.add(new ReportColumn("Premium", 8, ColumnFormat.CURRENCY));
        sourceColumns.add(new ReportColumn("Commission Rate", 9, ColumnFormat.TEXT));
        sourceColumns.add(new ReportColumn("Commission", 10, ColumnFormat.CURRENCY));
        sourceColumns.add(new ReportColumn("State", 11, ColumnFormat.TEXT));
        sourceColumns.add(new ReportColumn("Partner", 12, ColumnFormat.TEXT));
        sourceColumns.add(new ReportColumn("Partner Agent Name", 13, ColumnFormat.TEXT));
        sourceColumns.add(new ReportColumn("Partner Agency Name", 14, ColumnFormat.TEXT));
        sourceColumns.add(new ReportColumn("Partner Agent", 15, ColumnFormat.TEXT));
        sourceColumns.add(new ReportColumn("Partner Agency Phone Number", 16, ColumnFormat.TEXT));
        sourceColumns.add(new ReportColumn("Partner Agent Email", 17, ColumnFormat.TEXT));
        sourceColumns.add(new ReportColumn("Partner Agent Representative", 18, ColumnFormat.TEXT));
        return sourceColumns;
    }

    private static ArrayList<ReportColumn> getDestination() {
        // Destination format
        ArrayList<ReportColumn> destinationColumns = new ArrayList<>();
        destinationColumns.add(new ReportColumn("Agent Code", 0, ColumnFormat.TEXT));
        destinationColumns.add(new ReportColumn("Agent Name", 1, ColumnFormat.TEXT));
        destinationColumns.add(new ReportColumn("Agent Zip Code", 2, ColumnFormat.TEXT));
        destinationColumns.add(new ReportColumn("Agent Email Address", 3, ColumnFormat.TEXT));
        destinationColumns.add(new ReportColumn("Line of Business", 4, ColumnFormat.TEXT));
        destinationColumns.add(new ReportColumn("Policy Number", 5, ColumnFormat.TEXT));
        destinationColumns.add(new ReportColumn("Event Type", 6, ColumnFormat.TEXT));
        destinationColumns.add(new ReportColumn("Insured Name", 7, ColumnFormat.TEXT));
        destinationColumns.add(new ReportColumn("Premium", 8, ColumnFormat.CURRENCY));
        destinationColumns.add(new ReportColumn("Effective Date", 9, ColumnFormat.TEXT));
        return destinationColumns;
    }
}

现在,在我的实际应用程序中,我使用ReportTemplate对象来填充各种JavaFX控件(通过在运行ObservableArrayList方法时将它们转换为setItems();我不会更改原始文件以任何方式列出。

在我的应用程序中将ReportTemplate对象序列化回JSON时,输出包括&#34; helper&#34;和#34;可观察的&#34;键。当尝试再次加载这些输出文件时,我得到以下两个例外:

java.lang.UnsupportedOperationException: Abstract class can't be instantiated! Class name: com.sun.javafx.binding.ExpressionHelper
java.lang.RuntimeException: Unable to invoke no-args constructor for com.sun.javafx.binding.ExpressionHelper<java.lang.String>. Registering an InstanceCreator with Gson for this type may fix this problem.

以下是我用来从我的应用程序中保存它们的代码(与上面相同):

// save the Templates
        TemplatesDatasource.saveTemplates(Global.getSourceTemplates());
        TemplatesDatasource.saveTemplates(Global.getDestinationTemplates());

Global中,原始源和目标模板列表在使用Gson中的fromJson()方法创建后不会以任何方式更改。

我花了几个小时查找各种类似的问题,但似乎没有解决为什么Gson在输出Json中包含额外键时它应该与测试输出相同。

有人可以建议我的问题所在吗?我无法在较小的测试项目中复制错误,因此它显然与我的完整应用程序有关,其代码太大而无法在此处发布。

0 个答案:

没有答案