将 Lombok 的 @SuperBuilder 与 GSON 的 fromJson 结合使用会在复杂的继承结构中解析 JSON 时抛出 nullPointerException

发布于 2025-01-19 12:23:12 字数 5093 浏览 1 评论 0原文

继承结构如下: 孩子>父> SuperParentBuilderEnabler > (第3方)RecursiveTreeObject

使用Gson的fromJson(obj, Child.class); 创建对象时函数工作正常,但是添加到 ObservableList 会导致错误,我不认为 RecursiveTreeObject 构造函数已正确初始化,因此当我将它与 TreeItem使用时root = new RecursiveTreeItem<>(tree, RecursiveTreeObject::getChildren); 我收到 NullPointerException。

此时,我确定 RecursiveTreeObject没有正确扩展,但我不确定 Gson.fromJson(obj, Child.class) 是否有限制 功能。

Test1:我希望能够执行此操作,但它给了我错误:

        Child child = gson.fromJson(jsonObject, Child.class);
        tree.add(child);

所有字段

        Child child2 = Child.builder().field1(jsonObject.get("field1")
                .getAsString()).field3(jsonObject.get("field3").getAsString())
                .build().init();
        tree.add(child2);

Test2:这将运行而不会出现错误,但需要我指定我希望能够通过 Test1 执行此操作的 。

所有类和依赖项如下。

SuperParentBuilderEnabler 类

import com.jfoenix.controls.datamodels.treetable.RecursiveTreeObject;

public abstract class SuperParentBuilderEnabler extends RecursiveTreeObject<Parent> {
    public static abstract class SuperParentBuilderEnablerBuilder<
            C extends SuperParentBuilderEnabler,
            B extends SuperParentBuilderEnablerBuilder<C, B>
            > {
        protected abstract B self();
        public abstract C build();
    }

    protected SuperParentBuilderEnabler(SuperParentBuilderEnablerBuilder<?, ?> b) {
        super();
    }
}

父类

import com.google.gson.annotations.Expose;
import javafx.beans.property.SimpleStringProperty;
import lombok.Data;
import lombok.experimental.SuperBuilder;

@SuperBuilder()
@Data
public class Parent extends SuperParentBuilderEnabler {
    @Expose
    protected String field1;
    protected SimpleStringProperty field2;

    public Parent init(){
        setField2(new SimpleStringProperty(field1));
        return this;
    }
}

子类

import com.google.gson.annotations.Expose;
import lombok.Data;
import lombok.experimental.SuperBuilder;

@SuperBuilder()
@Data
public class Child extends Parent{
    @Expose
    protected String field3;

    public Child init(){
        super.init();
        return this;
    }
}

主类

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.jfoenix.controls.RecursiveTreeItem;
import com.jfoenix.controls.datamodels.treetable.RecursiveTreeObject;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TreeItem;

public class Main {
    public static final String JSON = "{\"field1\": \"test1\", \"field3\": \"test3\"}";

    public static void main(String[] args) {
        JsonParser parser = new JsonParser();
        JsonObject jsonObject = parser.parse(JSON).getAsJsonObject();
        Gson gson = new Gson();

        ObservableList<Parent> tree = FXCollections.observableArrayList();
        TreeItem<Parent> root = new RecursiveTreeItem<>(tree, RecursiveTreeObject::getChildren);

        Child child = gson.fromJson(jsonObject, Child.class);
        tree.add(child);

        Child child2 = Child.builder().field1(jsonObject.get("field1")
                .getAsString()).field3(jsonObject.get("field3").getAsString())
                .build().init();
        tree.add(child2);
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>StackOverflow</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.jfoenix</groupId>
            <artifactId>jfoenix</artifactId>
            <version>8.0.10</version>
        </dependency>
        <dependency>
            <groupId>org.hildan.fxgson</groupId>
            <artifactId>fx-gson</artifactId>
            <version>3.1.2</version>
        </dependency>
    </dependencies>
</project>

Inheritance structure is as follows:
Child > Parent > SuperParentBuilderEnabler > (3rd party) RecursiveTreeObject<Parent>

Using Gson's fromJson(obj, Child.class);
function works fine when creating the object however adding to ObservableList<Parent> it causes an error, I don't think the
RecursiveTreeObject<Parent> constructor is initialized properly and as a result of this when I use it with TreeItem<Parent> root = new RecursiveTreeItem<>(tree, RecursiveTreeObject::getChildren);
I get a NullPointerException.

At this point I'm certain that the RecursiveTreeObject<Parent> isn't getting extended properly but I'm not sure if there's limitations to the Gson.fromJson(obj, Child.class) functionality.

Test1: I like to be able to do this but it gives me errors:

        Child child = gson.fromJson(jsonObject, Child.class);
        tree.add(child);

Test2: This will run without errors but requires me to specify the all the fields

        Child child2 = Child.builder().field1(jsonObject.get("field1")
                .getAsString()).field3(jsonObject.get("field3").getAsString())
                .build().init();
        tree.add(child2);

I would like to be able to do this via Test1.

All the classes and dependencies are below.

SuperParentBuilderEnabler class

import com.jfoenix.controls.datamodels.treetable.RecursiveTreeObject;

public abstract class SuperParentBuilderEnabler extends RecursiveTreeObject<Parent> {
    public static abstract class SuperParentBuilderEnablerBuilder<
            C extends SuperParentBuilderEnabler,
            B extends SuperParentBuilderEnablerBuilder<C, B>
            > {
        protected abstract B self();
        public abstract C build();
    }

    protected SuperParentBuilderEnabler(SuperParentBuilderEnablerBuilder<?, ?> b) {
        super();
    }
}

Parent class

import com.google.gson.annotations.Expose;
import javafx.beans.property.SimpleStringProperty;
import lombok.Data;
import lombok.experimental.SuperBuilder;

@SuperBuilder()
@Data
public class Parent extends SuperParentBuilderEnabler {
    @Expose
    protected String field1;
    protected SimpleStringProperty field2;

    public Parent init(){
        setField2(new SimpleStringProperty(field1));
        return this;
    }
}

Child class

import com.google.gson.annotations.Expose;
import lombok.Data;
import lombok.experimental.SuperBuilder;

@SuperBuilder()
@Data
public class Child extends Parent{
    @Expose
    protected String field3;

    public Child init(){
        super.init();
        return this;
    }
}

Main class

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.jfoenix.controls.RecursiveTreeItem;
import com.jfoenix.controls.datamodels.treetable.RecursiveTreeObject;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TreeItem;

public class Main {
    public static final String JSON = "{\"field1\": \"test1\", \"field3\": \"test3\"}";

    public static void main(String[] args) {
        JsonParser parser = new JsonParser();
        JsonObject jsonObject = parser.parse(JSON).getAsJsonObject();
        Gson gson = new Gson();

        ObservableList<Parent> tree = FXCollections.observableArrayList();
        TreeItem<Parent> root = new RecursiveTreeItem<>(tree, RecursiveTreeObject::getChildren);

        Child child = gson.fromJson(jsonObject, Child.class);
        tree.add(child);

        Child child2 = Child.builder().field1(jsonObject.get("field1")
                .getAsString()).field3(jsonObject.get("field3").getAsString())
                .build().init();
        tree.add(child2);
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>StackOverflow</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.jfoenix</groupId>
            <artifactId>jfoenix</artifactId>
            <version>8.0.10</version>
        </dependency>
        <dependency>
            <groupId>org.hildan.fxgson</groupId>
            <artifactId>fx-gson</artifactId>
            <version>3.1.2</version>
        </dependency>
    </dependencies>
</project>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文