cleanup: improve JSON class code

This commit is contained in:
punchready 2021-06-03 03:46:58 +02:00
parent 09d798f37f
commit f323873b39

View File

@ -3,54 +3,48 @@ 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.InjectableValues;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import jdk.jshell.spi.ExecutionControl; import uulm.teamname.marvelous.gamelibrary.config.CharacterConfig;
import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage; import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage;
import uulm.teamname.marvelous.gamelibrary.messages.EventMessage; import uulm.teamname.marvelous.gamelibrary.messages.EventMessage;
import uulm.teamname.marvelous.gamelibrary.config.CharacterConfig;
/** /**
* Class that contains JSON encoding and decoding. It is initiated with the Character configuration. * Class that contains JSON encoding and decoding. It is initiated with the Character configuration.
*/ */
public class JSON { public class JSON {
private final ObjectMapper mapper; private final ObjectMapper mapper = new ObjectMapper();
public JSON (CharacterConfig config) { public JSON (CharacterConfig config) {
this.mapper = new ObjectMapper();
// add the config to the mappers InjectableValues, where it is later accessed by the EntityDeserializer // add the config to the mappers InjectableValues, where it is later accessed by the EntityDeserializer
this.mapper.setInjectableValues(new InjectableValues this.mapper.setInjectableValues(new InjectableValues
.Std() .Std()
.addValue("CharacterConfig", config)); .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 or {@code null} if the deserialization failed
*/ */
public EventMessage parse(String input) { public EventMessage parse(String input) {
EventMessage result = null; EventMessage result = null;
try { try {
result = mapper.readValue(input, EventMessage.class); return mapper.readValue(input, EventMessage.class);
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
e.printStackTrace(); e.printStackTrace();
return null;
} }
return result;
} }
/** 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 or {@code null} if the serialization failed
*/ */
public String stringify(BasicMessage input) throws ExecutionControl.NotImplementedException { public String stringify(BasicMessage input) {
String result = null;
try { try {
result = mapper.writeValueAsString(input); return mapper.writeValueAsString(input);
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
e.printStackTrace(); e.printStackTrace();
} return null;
return result; }
} }
} }