-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add code to enable deserializing tagged union using sealed class
- Loading branch information
Showing
4 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package app.work; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
|
||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type", visible = true) | ||
@JsonSubTypes({ | ||
@JsonSubTypes.Type(value = Dog.class, name = "dog"), | ||
@JsonSubTypes.Type(value = Cat.class, name = "cat") | ||
}) | ||
public abstract sealed class Animal { | ||
@JsonProperty | ||
String type; | ||
} | ||
|
||
final class Dog extends Animal { | ||
@JsonProperty | ||
String bone; | ||
public Dog() { | ||
this.type = "dog"; | ||
} | ||
} | ||
final class Cat extends Animal { | ||
@JsonProperty | ||
String milk; | ||
public Cat() { | ||
this.type = "cat"; | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package app.work; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import org.junit.Test; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
public class TestAnimal { | ||
@Test | ||
public void deserialize() throws IOException { | ||
var file = new File("src/test/resources/dog.json"); | ||
Animal dog = new ObjectMapper().readerFor(Animal.class).readValue(file); | ||
|
||
assertEquals("dog", dog.type); | ||
assertEquals(Dog.class, dog.getClass()); | ||
} | ||
|
||
@Test | ||
public void testDeserializeDog() throws Exception { | ||
String json = "{\"type\":\"dog\", \"bone\":\"chew toy\"}"; | ||
|
||
Animal animal = new ObjectMapper().readValue(json, Animal.class); | ||
assertEquals(Dog.class, animal.getClass()); | ||
Dog dog = (Dog) animal; | ||
assertEquals("chew toy", dog.bone); | ||
} | ||
|
||
@Test | ||
public void testDeserializeCat() throws Exception { | ||
String json = "{\"type\":\"cat\", \"milk\":\"almond milk\"}"; | ||
|
||
Animal animal = new ObjectMapper().readValue(json, Animal.class); | ||
assertEquals(Cat.class, animal.getClass()); | ||
Cat cat = (Cat) animal; | ||
assertEquals("almond milk", cat.milk); | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"type": "dog", | ||
"bone": "beef" | ||
} |