1 year ago
#384595
Zera42
Using Lombok's @SuperBuilder with GSON's fromJson throws nullPointerException parsing JSON in a complex inheritance structure
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>
java
json
javafx
gson
lombok
0 Answers
Your Answer