feat: partial rewrite of JSON wit support for CharacterConfig
This commit is contained in:
parent
c8d34fcc97
commit
852c85df08
@ -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);
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user