From 852c85df08f3a759d364307ca260530254b0d956 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Mon, 31 May 2021 23:24:22 +0200 Subject: [PATCH] feat: partial rewrite of JSON wit support for CharacterConfig --- .../marvelous/gamelibrary/json/JSON.java | 34 +++++++++++++++---- .../marvelous/gamelibrary/json/JSONTest.java | 10 ++++-- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java index 8428d54..5829813 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java @@ -1,22 +1,43 @@ package uulm.teamname.marvelous.gamelibrary.json; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.InjectableValues; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; import jdk.jshell.spi.ExecutionControl; +import uulm.teamname.marvelous.gamelibrary.entities.Entity; +import uulm.teamname.marvelous.gamelibrary.entities.Character; +import uulm.teamname.marvelous.gamelibrary.events.EntityEvent; +import uulm.teamname.marvelous.gamelibrary.events.Event; +import uulm.teamname.marvelous.gamelibrary.events.GamestateEvent; import uulm.teamname.marvelous.gamelibrary.json.basic.BasicMessage; import uulm.teamname.marvelous.gamelibrary.json.basic.EventMessage; +import uulm.teamname.marvelous.gamelibrary.json.config.CharacterConfig; +import uulm.teamname.marvelous.gamelibrary.json.ingame.EntityDeserializer; -/** Contains JSON encoding and decoding. +/** + * Class that contains JSON encoding and decoding. It is initiated with the Character configuration. */ public class JSON { - private static final ObjectMapper mapper = new ObjectMapper(); + private final ObjectMapper mapper; + + public JSON (CharacterConfig config) { + this.mapper = new ObjectMapper(); + + // add the config to the mappers InjectableValues, where it is later accessed by the EntityDeserializer + this.mapper.setInjectableValues(new InjectableValues + .Std() + .addValue("CharacterConfig", config)); + + } /** Deserializes an incoming network message into a {@link EventMessage}. * @param input The JSON to deserialize. - * @return The parsed message. */ - public static EventMessage parse(String input) { + * @return The parsed message. + */ + public EventMessage parse(String input) { EventMessage result = null; try { result = mapper.readValue(input, EventMessage.class); @@ -28,8 +49,9 @@ public class JSON { /** Serializes a {@link EventMessage} into a JSON string. * @param input The message to serialize. - * @return The message as JSON. */ - public static String stringify(BasicMessage input) throws ExecutionControl.NotImplementedException { + * @return The message as JSON. + */ + public String stringify(BasicMessage input) throws ExecutionControl.NotImplementedException { String result = null; try { result = mapper.writeValueAsString(input); diff --git a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/JSONTest.java b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/JSONTest.java index 13f45ff..f731683 100644 --- a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/JSONTest.java +++ b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/JSONTest.java @@ -34,6 +34,9 @@ class JSONTest { /** Still need to add: messages, messageType */ EventMessage target; + JSON json; + + // TODO: add proper CharacterConfig, otherwise Character events won't be deserializable @BeforeEach void setUp() { messageStructureStart = """ @@ -69,6 +72,9 @@ class JSONTest { target.customContent.put("customKey", "customResult"); target.customContent.put("customNumber", 15); target.customContent.put("customProperty", true); + + this.json = new JSON(null); + // FIXME: Temporary hotfix, nothing valid yet } @AfterEach @@ -91,7 +97,7 @@ class JSONTest { target.messageType = MessageType.EVENTS; target.messages = targetEvents; - assertThat(JSON.parse(completeMessageStructure)).isEqualTo(target); + assertThat(json.parse(completeMessageStructure)).isEqualTo(target); } @@ -113,7 +119,7 @@ class JSONTest { """; String completeMessageStructure = messageStructureStart + eventRepresentation + messageStructureEnd[1]; - System.out.println(JSON.parse(completeMessageStructure)); + System.out.println(json.parse(completeMessageStructure)); } @Test