feat: partial rewrite of JSON wit support for CharacterConfig

This commit is contained in:
Yannik Bretschneider 2021-05-31 23:24:22 +02:00
parent c8d34fcc97
commit 852c85df08
2 changed files with 36 additions and 8 deletions

View File

@ -1,22 +1,43 @@
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.databind.InjectableValues;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import jdk.jshell.spi.ExecutionControl; 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.BasicMessage;
import uulm.teamname.marvelous.gamelibrary.json.basic.EventMessage; 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 { 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}. /** Deserializes an incoming network message into a {@link EventMessage}.
* @param input The JSON to deserialize. * @param input The JSON to deserialize.
* @return The parsed message. */ * @return The parsed message.
public static EventMessage parse(String input) { */
public EventMessage parse(String input) {
EventMessage result = null; EventMessage result = null;
try { try {
result = mapper.readValue(input, EventMessage.class); result = mapper.readValue(input, EventMessage.class);
@ -28,8 +49,9 @@ public class JSON {
/** Serializes a {@link EventMessage} into a JSON string. /** Serializes a {@link EventMessage} into a JSON string.
* @param input The message to serialize. * @param input The message to serialize.
* @return The message as JSON. */ * @return The message as JSON.
public static String stringify(BasicMessage input) throws ExecutionControl.NotImplementedException { */
public String stringify(BasicMessage input) throws ExecutionControl.NotImplementedException {
String result = null; String result = null;
try { try {
result = mapper.writeValueAsString(input); result = mapper.writeValueAsString(input);

View File

@ -34,6 +34,9 @@ class JSONTest {
/** Still need to add: messages, messageType */ /** Still need to add: messages, messageType */
EventMessage target; EventMessage target;
JSON json;
// TODO: add proper CharacterConfig, otherwise Character events won't be deserializable
@BeforeEach @BeforeEach
void setUp() { void setUp() {
messageStructureStart = """ messageStructureStart = """
@ -69,6 +72,9 @@ class JSONTest {
target.customContent.put("customKey", "customResult"); target.customContent.put("customKey", "customResult");
target.customContent.put("customNumber", 15); target.customContent.put("customNumber", 15);
target.customContent.put("customProperty", true); target.customContent.put("customProperty", true);
this.json = new JSON(null);
// FIXME: Temporary hotfix, nothing valid yet
} }
@AfterEach @AfterEach
@ -91,7 +97,7 @@ class JSONTest {
target.messageType = MessageType.EVENTS; target.messageType = MessageType.EVENTS;
target.messages = targetEvents; 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]; String completeMessageStructure = messageStructureStart + eventRepresentation + messageStructureEnd[1];
System.out.println(JSON.parse(completeMessageStructure)); System.out.println(json.parse(completeMessageStructure));
} }
@Test @Test