2021-04-29 14:45:18 +00:00
|
|
|
package uulm.teamname.marvelous.gamelibrary.json;
|
2021-04-29 14:40:35 +00:00
|
|
|
|
2021-05-11 04:13:21 +00:00
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
2021-05-31 21:24:22 +00:00
|
|
|
import com.fasterxml.jackson.databind.InjectableValues;
|
2021-05-11 04:13:21 +00:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2021-06-03 01:46:58 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.config.CharacterConfig;
|
2021-06-02 14:28:19 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage;
|
2021-06-03 20:22:23 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.messages.server.EventMessage;
|
2021-04-29 14:40:35 +00:00
|
|
|
|
2021-06-03 21:08:01 +00:00
|
|
|
import java.util.Optional;
|
|
|
|
|
2021-05-31 21:24:22 +00:00
|
|
|
/**
|
|
|
|
* Class that contains JSON encoding and decoding. It is initiated with the Character configuration.
|
2021-04-29 14:40:35 +00:00
|
|
|
*/
|
|
|
|
public class JSON {
|
2021-05-30 16:06:18 +00:00
|
|
|
|
2021-06-03 01:46:58 +00:00
|
|
|
private final ObjectMapper mapper = new ObjectMapper();
|
2021-05-31 21:24:22 +00:00
|
|
|
|
|
|
|
public JSON (CharacterConfig config) {
|
|
|
|
// add the config to the mappers InjectableValues, where it is later accessed by the EntityDeserializer
|
|
|
|
this.mapper.setInjectableValues(new InjectableValues
|
|
|
|
.Std()
|
|
|
|
.addValue("CharacterConfig", config));
|
|
|
|
}
|
2021-05-30 16:06:18 +00:00
|
|
|
|
2021-05-28 12:51:17 +00:00
|
|
|
/** Deserializes an incoming network message into a {@link EventMessage}.
|
2021-06-03 01:46:58 +00:00
|
|
|
* @param input The JSON to deserialize
|
2021-06-03 21:08:01 +00:00
|
|
|
* @return An {@link Optional} containing the parsed message (as {@link BasicMessage}, so typecasting is necessary)
|
|
|
|
* or an empty {@link Optional} if the deserialization failed
|
2021-05-31 21:24:22 +00:00
|
|
|
*/
|
2021-06-03 21:08:01 +00:00
|
|
|
public Optional<BasicMessage> parse(String input) {
|
2021-05-11 04:13:21 +00:00
|
|
|
try {
|
2021-06-03 21:08:01 +00:00
|
|
|
return Optional.of(mapper.readValue(input, BasicMessage.class));
|
2021-05-11 04:13:21 +00:00
|
|
|
} catch (JsonProcessingException e) {
|
2021-06-03 21:08:01 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
return Optional.empty();
|
2021-05-11 04:13:21 +00:00
|
|
|
}
|
2021-04-29 14:40:35 +00:00
|
|
|
}
|
|
|
|
|
2021-05-28 12:51:17 +00:00
|
|
|
/** Serializes a {@link EventMessage} into a JSON string.
|
2021-06-03 01:46:58 +00:00
|
|
|
* @param input The message to serialize
|
2021-06-03 21:08:01 +00:00
|
|
|
* @return An {@link Optional}<{@link String}> of the message as JSON or
|
|
|
|
* an empty {@link Optional} if the serialization failed
|
2021-05-31 21:24:22 +00:00
|
|
|
*/
|
2021-06-03 21:08:01 +00:00
|
|
|
public Optional<String> stringify(BasicMessage input) {
|
2021-05-30 16:06:18 +00:00
|
|
|
try {
|
2021-06-03 21:08:01 +00:00
|
|
|
return Optional.of(mapper.writeValueAsString(input));
|
2021-05-30 16:06:18 +00:00
|
|
|
} catch (JsonProcessingException e) {
|
2021-06-03 21:08:01 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
return Optional.empty();
|
2021-05-30 16:06:18 +00:00
|
|
|
}
|
2021-04-29 14:40:35 +00:00
|
|
|
}
|
|
|
|
}
|