feat: added static methods to JSON for configuration deserialization
This commit is contained in:
@ -4,9 +4,13 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.InjectableValues;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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;
|
||||
|
||||
/**
|
||||
@ -14,6 +18,68 @@ import java.util.Optional;
|
||||
*/
|
||||
public class JSON {
|
||||
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
result = Optional.empty();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
public JSON (CharacterConfig config) {
|
||||
|
Reference in New Issue
Block a user