diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JsonNodeUnwrapper.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JsonNodeUnwrapper.java index 80c803d..a0dea7f 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JsonNodeUnwrapper.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JsonNodeUnwrapper.java @@ -1,17 +1,17 @@ package uulm.teamname.marvelous.gamelibrary.json; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.ObjectCodec; +import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; public class JsonNodeUnwrapper { - private static final ObjectMapper mapper = new ObjectMapper(); - - public static T unwrap (JsonNode rootNode, String key, Class type) throws JsonProcessingException { - JsonNode subNode; - if ((subNode = rootNode.get(key)) != null) { - return mapper.readValue(subNode.toString(), type); + public static T unwrap(JsonNode subNode, Class type, ObjectCodec codec) + throws JsonProcessingException { + if (subNode != null) { + return codec.treeToValue(subNode, type); } return null; } 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 index 1c9b9a8..0335eac 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EntityDeserializer.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EntityDeserializer.java @@ -21,9 +21,6 @@ public class EntityDeserializer extends JsonDeserializer { Rock } - // static so that no reinitializations are needed - private static final ObjectMapper mapper = new ObjectMapper(); - @Override public Entity deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { ObjectCodec codec = p.getCodec(); @@ -40,7 +37,7 @@ public class EntityDeserializer extends JsonDeserializer { node.get("ID").intValue()); result = new Character( id, - mapper.readValue(node.get("position").toString(), IntVector2.class), + codec.treeToValue(node.get("position"), IntVector2.class), node.get("name").asText(), node.get("HP").asInt(), node.get("MP").asInt(), @@ -50,21 +47,21 @@ public class EntityDeserializer extends JsonDeserializer { -1 ); - for (var i: mapper.readValue(node.get("stones").toString(), Integer[].class)) { + for (var i: codec.treeToValue(node.get("stones"), Integer[].class)) { ((Character) result).inventory.addStone(StoneType.valueOf(i)); } } case InfinityStone -> { result = new InfinityStone( new EntityID(EntityType.InfinityStones, node.get("ID").asInt()), - mapper.readValue(node.get("position").toString(), IntVector2.class), + codec.treeToValue(node.get("position"), 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), + codec.treeToValue(node.get("position"), IntVector2.class), node.get("HP").asInt() ); } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EventDeserializer.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EventDeserializer.java index f86257f..6af5183 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EventDeserializer.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EventDeserializer.java @@ -1,12 +1,10 @@ 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.Entity; import uulm.teamname.marvelous.gamelibrary.entities.EntityID; @@ -14,19 +12,14 @@ import uulm.teamname.marvelous.gamelibrary.entities.StoneType; import uulm.teamname.marvelous.gamelibrary.events.Event; import uulm.teamname.marvelous.gamelibrary.events.EventBuilder; import uulm.teamname.marvelous.gamelibrary.events.EventType; -import uulm.teamname.marvelous.gamelibrary.events.GamestateEvent; import static uulm.teamname.marvelous.gamelibrary.json.JsonNodeUnwrapper.unwrap; import java.io.IOException; -import java.util.Arrays; import java.util.HashMap; public class EventDeserializer extends JsonDeserializer { - // static so that no reinitializations are needed - private static final ObjectMapper mapper = new ObjectMapper(); - @Override public Event deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { ObjectCodec codec = p.getCodec(); @@ -76,40 +69,40 @@ public class EventDeserializer extends JsonDeserializer { // } // necessary because of exact type mismatch - var stoneTypeID = unwrap(node, "stoneType", EntityID.class); + var stoneTypeID = unwrap(node.get("stoneType"), EntityID.class, codec); - builder.withTargetEntity(unwrap(node, "targetEntity", EntityID.class)) - .withTargetField(unwrap(node, "targetField", IntVector2.class)) - .withAmount(unwrap(node, "targetField", Integer.class)) - .withEntity(unwrap(node, "entity", Entity.class)) + builder.withTargetEntity(unwrap(node.get("targetEntity"), EntityID.class, codec)) + .withTargetField(unwrap(node.get("targetField"), IntVector2.class, codec)) + .withAmount(unwrap(node.get("targetField"), Integer.class, codec)) + .withEntity(unwrap(node.get("entity"), Entity.class, codec)) - .withOriginEntity(unwrap(node, "originEntity", EntityID.class)) - .withOriginField(unwrap(node, "originField", IntVector2.class)) + .withOriginEntity(unwrap(node.get("originEntity"), EntityID.class, codec)) + .withOriginField(unwrap(node.get("originField"), IntVector2.class, codec)) .withStoneType(stoneTypeID != null ? StoneType.valueOf(stoneTypeID.id) : null) - .withRoundCount(unwrap(node, "roundCount", Integer.class)) - .withTurnCount(unwrap(node, "turnCount", Integer.class)) - .withCharacterOrder(unwrap(node, "characterOrder", EntityID[].class)) - .withNextCharacter(unwrap(node, "nextCharacter", EntityID.class)) + .withRoundCount(unwrap(node.get("roundCount"), Integer.class, codec)) + .withTurnCount(unwrap(node.get("turnCount"), Integer.class, codec)) + .withCharacterOrder(unwrap(node.get("characterOrder"), EntityID[].class, codec)) + .withNextCharacter(unwrap(node.get("nextCharacter"), EntityID.class, codec)) - .withPlayerWon(unwrap(node, "playerWon", Integer.class)) + .withPlayerWon(unwrap(node.get("playerWon"), Integer.class, codec)) - .withMessage(unwrap(node, "message", String.class)) - .withTimeLeft(unwrap(node, "timeLeft", Integer.class)) + .withMessage(unwrap(node.get("message"), String.class, codec)) + .withTimeLeft(unwrap(node.get("timeLeft"), Integer.class, codec)) - .withEntities(unwrap(node, "entities", Entity[].class)) - .withTurnOrder(unwrap(node, "turnOrder", EntityID[].class)) - .withMapSize(unwrap(node, "mapSize", IntVector2.class)) - .withActiveCharacter(unwrap(node, "activeCharacter", EntityID.class)) - .withStoneCooldowns(unwrap(node, "stoneCooldowns",Integer[].class)) - .withWinCondition(unwrap(node, "winCondition",Boolean.class)) + .withEntities(unwrap(node.get("entities"), Entity[].class, codec)) + .withTurnOrder(unwrap(node.get("turnOrder"), EntityID[].class, codec)) + .withMapSize(unwrap(node.get("mapSize"), IntVector2.class, codec)) + .withActiveCharacter(unwrap(node.get("activeCharacter"), EntityID.class, codec)) + .withStoneCooldowns(unwrap(node.get("stoneCooldowns"), Integer[].class, codec)) + .withWinCondition(unwrap(node.get("winCondition"), Boolean.class, codec)) - .withTeamIdentifier(unwrap(node, "teamIdentifier", String.class)) - .withCustomContent(unwrap(node,"customContent", HashMap.class)); + .withTeamIdentifier(unwrap(node.get("teamIdentifier"), String.class, codec)) + .withCustomContent(unwrap(node.get("customContent"), HashMap.class, codec)); - builder.withTargetEntity(unwrap(node, "targetEntity", EntityID.class)); - builder.withTargetField(unwrap(node, "targetField", IntVector2.class)); + builder.withTargetEntity(unwrap(node.get("targetEntity"), EntityID.class, codec)); + builder.withTargetField(unwrap(node.get("targetField"), IntVector2.class, codec)); switch (eventType) { case Ack, Nack, GamestateEvent -> { return builder.buildGameStateEvent(); } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/RequestDeserializer.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/RequestDeserializer.java index 3dcccd8..5696e33 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/RequestDeserializer.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/RequestDeserializer.java @@ -6,12 +6,9 @@ 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.Entity; import uulm.teamname.marvelous.gamelibrary.entities.EntityID; import uulm.teamname.marvelous.gamelibrary.entities.StoneType; -import uulm.teamname.marvelous.gamelibrary.events.Event; import uulm.teamname.marvelous.gamelibrary.requests.*; import java.io.IOException; @@ -21,9 +18,6 @@ import static uulm.teamname.marvelous.gamelibrary.json.JsonNodeUnwrapper.unwrap; public class RequestDeserializer extends JsonDeserializer { - // static so that no reinitializations are needed - private static final ObjectMapper mapper = new ObjectMapper(); - @Override public Request deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectCodec codec = p.getCodec(); @@ -38,12 +32,12 @@ public class RequestDeserializer extends JsonDeserializer { } var requestBuilder = new RequestBuilder(requestType) - .withValue(unwrap(node, "value", int.class)) - .withOriginEntity(unwrap(node, "originEntity", EntityID.class)) - .withTargetEntity(unwrap(node, "targetEntity", EntityID.class)) - .withOriginField(unwrap(node, "originField", IntVector2.class)) - .withTargetField(unwrap(node, "targetField", IntVector2.class)) - .withStoneType(Optional.ofNullable(unwrap(node, "stoneType", EntityID.class)) + .withValue(unwrap(node.get("value"), int.class, codec)) + .withOriginEntity(unwrap(node.get("originEntity"), EntityID.class, codec)) + .withTargetEntity(unwrap(node.get("targetEntity"), EntityID.class, codec)) + .withOriginField(unwrap(node.get("originField"), IntVector2.class, codec)) + .withTargetField(unwrap(node.get("targetField"), IntVector2.class, codec)) + .withStoneType(Optional.ofNullable(unwrap(node.get("stoneType"), EntityID.class, codec)) .map(x -> StoneType.valueOf(x.id)) .orElseGet(() -> null));