Last active
August 29, 2015 14:11
-
-
Save cowwoc/e31e96e093a2981f90bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.annotation.JsonCreator; | |
import com.fasterxml.jackson.annotation.JsonIdentityInfo; | |
import com.fasterxml.jackson.annotation.JsonIgnore; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import com.fasterxml.jackson.annotation.JsonPropertyOrder; | |
import com.fasterxml.jackson.annotation.ObjectIdGenerators; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.SerializationFeature; | |
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.Optional; | |
public class Main { | |
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class) | |
public static interface Task<Parent extends Task<?, ?>, Child extends Task<?, ?>> { | |
Optional<Parent> getParent(); | |
Optional<Child> getChild(); | |
} | |
@JsonPropertyOrder( | |
{ | |
// Serializing "child" last improves readability | |
"@id", "child" | |
}) | |
public static abstract class AbstractTask<Parent extends Task<?, ?>, Child extends Task<?, ?>> | |
implements Task<Parent, Child> { | |
@JsonProperty | |
public Optional<Child> child = Optional.empty(); | |
/** | |
* Creates a new task. | |
*/ | |
protected AbstractTask() | |
throws NullPointerException { | |
} | |
@Override | |
public Optional<Child> getChild() { | |
return child; | |
} | |
} | |
public static final class Task1 extends AbstractTask<Task<?, ?>, Task2> { | |
@JsonCreator | |
public Task1() | |
throws NullPointerException { | |
} | |
@JsonIgnore | |
@Override | |
public Optional<Task<?, ?>> getParent() { | |
return Optional.empty(); | |
} | |
} | |
public static final class Task2 extends AbstractTask<Task1, Task3> { | |
@JsonProperty | |
private final Optional<Task1> parent; | |
@JsonCreator | |
public Task2(@JsonProperty("parent") Task1 parent) | |
throws NullPointerException, IllegalArgumentException { | |
this.parent = Optional.of(parent); | |
} | |
@Override | |
public Optional<Task1> getParent() { | |
return parent; | |
} | |
} | |
public static final class Task3 extends AbstractTask<Task2, Task<?, ?>> { | |
@JsonProperty | |
private final Optional<Task2> parent; | |
@JsonCreator | |
public Task3(@JsonProperty("parent") Task2 parent) | |
throws NullPointerException, IllegalArgumentException { | |
this.parent = Optional.of(parent); | |
} | |
@Override | |
public Optional<Task2> getParent() { | |
return parent; | |
} | |
} | |
public static void main(String[] args) throws IOException { | |
ObjectMapper mapper = new ObjectMapper(); | |
// Pretty-printing | |
mapper.enable(SerializationFeature.INDENT_OUTPUT); | |
// java.util.Optional | |
mapper.registerModule(new Jdk8Module()); | |
Task1 task1 = new Task1(); | |
Task2 task2 = new Task2(task1); | |
Task3 task3 = new Task3(task2); | |
task1.child = Optional.of(task2); | |
task2.child = Optional.of(task3); | |
File file = new File("target/test.xml"); | |
mapper.writeValue(file, task1); | |
// This fails with: com.fasterxml.jackson.databind.deser.UnresolvedForwardReference: Could not resolve Object Id [2] (for [simple type, class Task2]). | |
task1 = mapper.readValue(file, Task1.class); | |
System.out.println("Success: " + task1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment