Gamelib/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java

141 lines
5.7 KiB
Java
Raw Normal View History

2021-04-29 14:45:18 +00:00
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 org.tinylog.Logger;
2021-06-03 01:46:58 +00:00
import uulm.teamname.marvelous.gamelibrary.config.CharacterConfig;
import uulm.teamname.marvelous.gamelibrary.config.PartyConfig;
import uulm.teamname.marvelous.gamelibrary.config.ScenarioConfig;
import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage;
import uulm.teamname.marvelous.gamelibrary.messages.server.EventMessage;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
/**
* Class that contains JSON encoding and decoding. It is initiated with the Character configuration.
*/
public class JSON {
// ===================>>>>>> STATIC <<<<<<======================
private static final ObjectMapper staticMapper = new ObjectMapper();
public static Optional<CharacterConfig> parseCharacterConfig (String jsonRepresentingConfig) {
Optional<CharacterConfig> result;
try {
result = Optional.of(staticMapper.readValue(jsonRepresentingConfig, CharacterConfig.class));
} catch (JsonProcessingException e) {
Logger.debug("JSON deserialization of CharacterConfig failed. Exception: {}", e.toString());
result = Optional.empty();
}
return result;
}
public static Optional<CharacterConfig> parseCharacterConfig (File jsonRepresentingConfig) {
Optional<CharacterConfig> result;
try {
result = Optional.of(staticMapper.readValue(jsonRepresentingConfig, CharacterConfig.class));
} catch (IOException e) {
Logger.debug("JSON deserialization of CharacterConfig failed. Exception: {}", e.toString());
result = Optional.empty();
}
return result;
}
public static Optional<PartyConfig> parsePartyConfig (String jsonRepresentingConfig) {
Optional<PartyConfig> result;
try {
result = Optional.of(staticMapper.readValue(jsonRepresentingConfig, PartyConfig.class));
} catch (JsonProcessingException e) {
Logger.debug("JSON deserialization of PartyConfig failed. Exception: {}", e.toString());
result = Optional.empty();
}
return result;
}
public static Optional<PartyConfig> parsePartyConfig (File jsonRepresentingConfig) {
Optional<PartyConfig> result;
try {
result = Optional.of(staticMapper.readValue(jsonRepresentingConfig, PartyConfig.class));
} catch (IOException e) {
Logger.debug("JSON deserialization of PartyConfig failed. Exception: {}", e.toString());
result = Optional.empty();
}
return result;
}
public static Optional<ScenarioConfig> parseScenarioConfig (String jsonRepresentingConfig) {
Optional<ScenarioConfig> result;
try {
result = Optional.of(staticMapper.readValue(jsonRepresentingConfig, ScenarioConfig.class));
} catch (JsonProcessingException e) {
Logger.debug("JSON deserialization of ScenarioConfig failed. Exception: {}", e.toString());
result = Optional.empty();
}
return result;
}
public static Optional<ScenarioConfig> parseScenarioConfig (File jsonRepresentingConfig) {
Optional<ScenarioConfig> result;
try {
result = Optional.of(staticMapper.readValue(jsonRepresentingConfig, ScenarioConfig.class));
} catch (IOException e) {
Logger.debug("JSON deserialization of ScenarioConfig failed. Exception: {}", e.toString());
result = Optional.empty();
}
return result;
}
// =================>>>>>> NON-STATIC <<<<<<====================
2021-06-03 01:46:58 +00:00
private final ObjectMapper mapper = new ObjectMapper();
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));
}
/** Deserializes an incoming network message into a {@link EventMessage}.
2021-06-03 01:46:58 +00:00
* @param input The JSON to deserialize
* @return An {@link Optional} containing the parsed message (as {@link BasicMessage}, so typecasting is necessary)
* or an empty {@link Optional} if the deserialization failed
*/
public Optional<BasicMessage> parse(String input) {
try {
return Optional.of(mapper.readValue(input, BasicMessage.class));
} catch (JsonProcessingException e) {
Logger.debug("JSON deserialization of Message failed. Exception: {}",
e.toString());
return Optional.empty();
}
}
/** Serializes a {@link EventMessage} into a JSON string.
2021-06-03 01:46:58 +00:00
* @param input The message to serialize
* @return An {@link Optional}<{@link String}> of the message as JSON or
* an empty {@link Optional} if the serialization failed
*/
public Optional<String> stringify(BasicMessage input) {
try {
return Optional.of(mapper.writeValueAsString(input));
} catch (JsonProcessingException e) {
Logger.debug("JSON serialization of Message of type {} failed. Exception: {}",
input.messageType.toString(),
e.toString());
// e.printStackTrace();
return Optional.empty();
}
}
public void setCharacterConfig(CharacterConfig config) {
this.mapper.setInjectableValues(new InjectableValues
.Std()
.addValue("CharacterConfig", config));
}
}