From f7b151449160408a308bf016209e3c8a9c1ea68c Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Tue, 11 May 2021 21:17:55 +0200 Subject: [PATCH] feat: wrote EntityDeserializer --- .../gamelibrary/entities/Entity.java | 3 + .../json/ingame/EntityDeserializer.java | 70 +++++++++++++++++++ .../json/ingame/EntityDeserializerTest.java | 56 +++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EntityDeserializer.java create mode 100644 src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EntityDeserializerTest.java diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Entity.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Entity.java index 9f2ad40..8e15a41 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Entity.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Entity.java @@ -1,10 +1,13 @@ package uulm.teamname.marvelous.gamelibrary.entities; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import uulm.teamname.marvelous.gamelibrary.IntVector2; +import uulm.teamname.marvelous.gamelibrary.json.ingame.EntityDeserializer; import java.util.Objects; /** Represents an abstract entity. */ +@JsonDeserialize(using = EntityDeserializer.class) public abstract class Entity { /** Whether or not the entity is currently active in the game */ protected boolean active = true; diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EntityDeserializer.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EntityDeserializer.java new file mode 100644 index 0000000..1f1ccd5 --- /dev/null +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EntityDeserializer.java @@ -0,0 +1,70 @@ +package uulm.teamname.marvelous.gamelibrary.json.ingame; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.ObjectCodec; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import uulm.teamname.marvelous.gamelibrary.IntVector2; +import uulm.teamname.marvelous.gamelibrary.entities.*; +import uulm.teamname.marvelous.gamelibrary.entities.Character; +import uulm.teamname.marvelous.gamelibrary.json.JSON; + +import java.io.IOException; + +public class EntityDeserializer extends JsonDeserializer { + + private enum DeserializedEntityType { + Character, + Stone, + Rock + } + + private final ObjectMapper mapper = new ObjectMapper(); + + @Override + public Entity deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { + ObjectCodec codec = p.getCodec(); + JsonNode node = codec.readTree(p); + + Entity result = null; + + DeserializedEntityType type = DeserializedEntityType.valueOf(node.get("entityType").asText()); + + switch (type) { + case Character -> { + EntityID id = new EntityID( + EntityType.valueOf("P" + node.get("PID").intValue()), + node.get("ID").intValue()); + result = new Character( + id, + mapper.readValue(node.get("position").toString(), IntVector2.class), + node.get("name").asText(), + node.get("HP").asInt(), + node.get("MP").asInt(), + node.get("AP").asInt(), + -1, + -1, + -1 + ); + } + case Stone -> { + result = new InfinityStone( + new EntityID(EntityType.InfinityStones, node.get("ID").asInt()), + mapper.readValue(node.get("position").toString(), IntVector2.class), + StoneType.valueOf(node.get("ID").asInt()) + ); + } + case Rock -> { + result = new Rock( + new EntityID(EntityType.Rocks, node.get("ID").asInt()), + mapper.readValue(node.get("position").toString(), IntVector2.class), + node.get("HP").asInt() + ); + } + } + return result; + } +} diff --git a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EntityDeserializerTest.java b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EntityDeserializerTest.java new file mode 100644 index 0000000..b40169f --- /dev/null +++ b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EntityDeserializerTest.java @@ -0,0 +1,56 @@ +package uulm.teamname.marvelous.gamelibrary.json.ingame; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.BeforeEach; +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; +import uulm.teamname.marvelous.gamelibrary.IntVector2; +import uulm.teamname.marvelous.gamelibrary.entities.Character; +import uulm.teamname.marvelous.gamelibrary.entities.Entity; +import uulm.teamname.marvelous.gamelibrary.entities.EntityID; +import uulm.teamname.marvelous.gamelibrary.entities.EntityType; + +import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.*; + +class EntityDeserializerTest { + + ObjectMapper mapper; + + @BeforeEach + void setUp() { + mapper = new ObjectMapper(); + } + + @Test + void simpleCharacterTest() throws JsonProcessingException { + var character = new Character( + new EntityID(EntityType.P1, 3), + new IntVector2(4, 2), + "God", + 255, + 65535, + 4, + -1, + -1, + -1 + ); + + var jsonRepresentingCharacter = """ +{ + "entityType": "Character", + "name": "God", + "PID": 1, + "ID": 3, + "HP": 255, + "MP": 65535, + "AP": 4, + "position": [4, 2] +} +"""; + assertThat(mapper.readValue(jsonRepresentingCharacter, Entity.class)) + .isEqualTo(character); + } +} \ No newline at end of file