feat: removed ObjectMapper in deserializers, and replaced it with context-based mapping

This commit is contained in:
Yannik Bretschneider 2021-05-30 17:22:02 +02:00
parent 9192677ca1
commit d2216f190a
4 changed files with 40 additions and 56 deletions

View File

@ -1,17 +1,17 @@
package uulm.teamname.marvelous.gamelibrary.json; package uulm.teamname.marvelous.gamelibrary.json;
import com.fasterxml.jackson.core.JsonProcessingException; 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.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonNodeUnwrapper { public class JsonNodeUnwrapper {
private static final ObjectMapper mapper = new ObjectMapper(); public static <T> T unwrap(JsonNode subNode, Class<T> type, ObjectCodec codec)
throws JsonProcessingException {
public static <T> T unwrap (JsonNode rootNode, String key, Class<T> type) throws JsonProcessingException { if (subNode != null) {
JsonNode subNode; return codec.treeToValue(subNode, type);
if ((subNode = rootNode.get(key)) != null) {
return mapper.readValue(subNode.toString(), type);
} }
return null; return null;
} }

View File

@ -21,9 +21,6 @@ public class EntityDeserializer extends JsonDeserializer<Entity> {
Rock Rock
} }
// static so that no reinitializations are needed
private static final ObjectMapper mapper = new ObjectMapper();
@Override @Override
public Entity deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { public Entity deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
ObjectCodec codec = p.getCodec(); ObjectCodec codec = p.getCodec();
@ -40,7 +37,7 @@ public class EntityDeserializer extends JsonDeserializer<Entity> {
node.get("ID").intValue()); node.get("ID").intValue());
result = new Character( result = new Character(
id, id,
mapper.readValue(node.get("position").toString(), IntVector2.class), codec.treeToValue(node.get("position"), IntVector2.class),
node.get("name").asText(), node.get("name").asText(),
node.get("HP").asInt(), node.get("HP").asInt(),
node.get("MP").asInt(), node.get("MP").asInt(),
@ -50,21 +47,21 @@ public class EntityDeserializer extends JsonDeserializer<Entity> {
-1 -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)); ((Character) result).inventory.addStone(StoneType.valueOf(i));
} }
} }
case InfinityStone -> { case InfinityStone -> {
result = new InfinityStone( result = new InfinityStone(
new EntityID(EntityType.InfinityStones, node.get("ID").asInt()), 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()) StoneType.valueOf(node.get("ID").asInt())
); );
} }
case Rock -> { case Rock -> {
result = new Rock( result = new Rock(
new EntityID(EntityType.Rocks, node.get("ID").asInt()), 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() node.get("HP").asInt()
); );
} }

View File

@ -1,12 +1,10 @@
package uulm.teamname.marvelous.gamelibrary.json.ingame; package uulm.teamname.marvelous.gamelibrary.json.ingame;
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec; import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import uulm.teamname.marvelous.gamelibrary.IntVector2; import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.entities.Entity; import uulm.teamname.marvelous.gamelibrary.entities.Entity;
import uulm.teamname.marvelous.gamelibrary.entities.EntityID; 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.Event;
import uulm.teamname.marvelous.gamelibrary.events.EventBuilder; import uulm.teamname.marvelous.gamelibrary.events.EventBuilder;
import uulm.teamname.marvelous.gamelibrary.events.EventType; import uulm.teamname.marvelous.gamelibrary.events.EventType;
import uulm.teamname.marvelous.gamelibrary.events.GamestateEvent;
import static uulm.teamname.marvelous.gamelibrary.json.JsonNodeUnwrapper.unwrap; import static uulm.teamname.marvelous.gamelibrary.json.JsonNodeUnwrapper.unwrap;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
public class EventDeserializer extends JsonDeserializer<Event> { public class EventDeserializer extends JsonDeserializer<Event> {
// static so that no reinitializations are needed
private static final ObjectMapper mapper = new ObjectMapper();
@Override @Override
public Event deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { public Event deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
ObjectCodec codec = p.getCodec(); ObjectCodec codec = p.getCodec();
@ -76,40 +69,40 @@ public class EventDeserializer extends JsonDeserializer<Event> {
// } // }
// necessary because of exact type mismatch // 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)) builder.withTargetEntity(unwrap(node.get("targetEntity"), EntityID.class, codec))
.withTargetField(unwrap(node, "targetField", IntVector2.class)) .withTargetField(unwrap(node.get("targetField"), IntVector2.class, codec))
.withAmount(unwrap(node, "targetField", Integer.class)) .withAmount(unwrap(node.get("targetField"), Integer.class, codec))
.withEntity(unwrap(node, "entity", Entity.class)) .withEntity(unwrap(node.get("entity"), Entity.class, codec))
.withOriginEntity(unwrap(node, "originEntity", EntityID.class)) .withOriginEntity(unwrap(node.get("originEntity"), EntityID.class, codec))
.withOriginField(unwrap(node, "originField", IntVector2.class)) .withOriginField(unwrap(node.get("originField"), IntVector2.class, codec))
.withStoneType(stoneTypeID != null ? StoneType.valueOf(stoneTypeID.id) : null) .withStoneType(stoneTypeID != null ? StoneType.valueOf(stoneTypeID.id) : null)
.withRoundCount(unwrap(node, "roundCount", Integer.class)) .withRoundCount(unwrap(node.get("roundCount"), Integer.class, codec))
.withTurnCount(unwrap(node, "turnCount", Integer.class)) .withTurnCount(unwrap(node.get("turnCount"), Integer.class, codec))
.withCharacterOrder(unwrap(node, "characterOrder", EntityID[].class)) .withCharacterOrder(unwrap(node.get("characterOrder"), EntityID[].class, codec))
.withNextCharacter(unwrap(node, "nextCharacter", EntityID.class)) .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)) .withMessage(unwrap(node.get("message"), String.class, codec))
.withTimeLeft(unwrap(node, "timeLeft", Integer.class)) .withTimeLeft(unwrap(node.get("timeLeft"), Integer.class, codec))
.withEntities(unwrap(node, "entities", Entity[].class)) .withEntities(unwrap(node.get("entities"), Entity[].class, codec))
.withTurnOrder(unwrap(node, "turnOrder", EntityID[].class)) .withTurnOrder(unwrap(node.get("turnOrder"), EntityID[].class, codec))
.withMapSize(unwrap(node, "mapSize", IntVector2.class)) .withMapSize(unwrap(node.get("mapSize"), IntVector2.class, codec))
.withActiveCharacter(unwrap(node, "activeCharacter", EntityID.class)) .withActiveCharacter(unwrap(node.get("activeCharacter"), EntityID.class, codec))
.withStoneCooldowns(unwrap(node, "stoneCooldowns",Integer[].class)) .withStoneCooldowns(unwrap(node.get("stoneCooldowns"), Integer[].class, codec))
.withWinCondition(unwrap(node, "winCondition",Boolean.class)) .withWinCondition(unwrap(node.get("winCondition"), Boolean.class, codec))
.withTeamIdentifier(unwrap(node, "teamIdentifier", String.class)) .withTeamIdentifier(unwrap(node.get("teamIdentifier"), String.class, codec))
.withCustomContent(unwrap(node,"customContent", HashMap.class)); .withCustomContent(unwrap(node.get("customContent"), HashMap.class, codec));
builder.withTargetEntity(unwrap(node, "targetEntity", EntityID.class)); builder.withTargetEntity(unwrap(node.get("targetEntity"), EntityID.class, codec));
builder.withTargetField(unwrap(node, "targetField", IntVector2.class)); builder.withTargetField(unwrap(node.get("targetField"), IntVector2.class, codec));
switch (eventType) { switch (eventType) {
case Ack, Nack, GamestateEvent -> { return builder.buildGameStateEvent(); } case Ack, Nack, GamestateEvent -> { return builder.buildGameStateEvent(); }

View File

@ -6,12 +6,9 @@ import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import uulm.teamname.marvelous.gamelibrary.IntVector2; 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.EntityID;
import uulm.teamname.marvelous.gamelibrary.entities.StoneType; import uulm.teamname.marvelous.gamelibrary.entities.StoneType;
import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.requests.*; import uulm.teamname.marvelous.gamelibrary.requests.*;
import java.io.IOException; import java.io.IOException;
@ -21,9 +18,6 @@ import static uulm.teamname.marvelous.gamelibrary.json.JsonNodeUnwrapper.unwrap;
public class RequestDeserializer extends JsonDeserializer<Request> { public class RequestDeserializer extends JsonDeserializer<Request> {
// static so that no reinitializations are needed
private static final ObjectMapper mapper = new ObjectMapper();
@Override @Override
public Request deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { public Request deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectCodec codec = p.getCodec(); ObjectCodec codec = p.getCodec();
@ -38,12 +32,12 @@ public class RequestDeserializer extends JsonDeserializer<Request> {
} }
var requestBuilder = new RequestBuilder(requestType) var requestBuilder = new RequestBuilder(requestType)
.withValue(unwrap(node, "value", int.class)) .withValue(unwrap(node.get("value"), int.class, codec))
.withOriginEntity(unwrap(node, "originEntity", EntityID.class)) .withOriginEntity(unwrap(node.get("originEntity"), EntityID.class, codec))
.withTargetEntity(unwrap(node, "targetEntity", EntityID.class)) .withTargetEntity(unwrap(node.get("targetEntity"), EntityID.class, codec))
.withOriginField(unwrap(node, "originField", IntVector2.class)) .withOriginField(unwrap(node.get("originField"), IntVector2.class, codec))
.withTargetField(unwrap(node, "targetField", IntVector2.class)) .withTargetField(unwrap(node.get("targetField"), IntVector2.class, codec))
.withStoneType(Optional.ofNullable(unwrap(node, "stoneType", EntityID.class)) .withStoneType(Optional.ofNullable(unwrap(node.get("stoneType"), EntityID.class, codec))
.map(x -> StoneType.valueOf(x.id)) .map(x -> StoneType.valueOf(x.id))
.orElseGet(() -> null)); .orElseGet(() -> null));