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;
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> T unwrap (JsonNode rootNode, String key, Class<T> type) throws JsonProcessingException {
JsonNode subNode;
if ((subNode = rootNode.get(key)) != null) {
return mapper.readValue(subNode.toString(), type);
public static <T> T unwrap(JsonNode subNode, Class<T> type, ObjectCodec codec)
throws JsonProcessingException {
if (subNode != null) {
return codec.treeToValue(subNode, type);
}
return null;
}

View File

@ -21,9 +21,6 @@ public class EntityDeserializer extends JsonDeserializer<Entity> {
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<Entity> {
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<Entity> {
-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()
);
}

View File

@ -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<Event> {
// 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<Event> {
// }
// 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(); }

View File

@ -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<Request> {
// 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<Request> {
}
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));