doc: added logging in case of failed de/serialization

This commit is contained in:
Yannik Bretschneider 2021-06-04 19:48:40 +02:00
parent fd3e2636e3
commit cd992b0fa6

View File

@ -3,6 +3,7 @@ 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;
import uulm.teamname.marvelous.gamelibrary.config.CharacterConfig;
import uulm.teamname.marvelous.gamelibrary.config.PartyConfig;
import uulm.teamname.marvelous.gamelibrary.config.ScenarioConfig;
@ -18,6 +19,8 @@ import java.util.Optional;
*/
public class JSON {
// ===================>>>>>> STATIC <<<<<<======================
private static final ObjectMapper staticMapper = new ObjectMapper();
public static Optional<CharacterConfig> parseCharacterConfig (String jsonRepresentingConfig) {
@ -25,6 +28,7 @@ public class JSON {
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;
@ -35,6 +39,7 @@ public class JSON {
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;
@ -45,6 +50,7 @@ public class JSON {
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;
@ -55,6 +61,7 @@ public class JSON {
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;
@ -65,6 +72,7 @@ public class JSON {
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;
@ -75,11 +83,14 @@ public class JSON {
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 <<<<<<====================
private final ObjectMapper mapper = new ObjectMapper();
public JSON (CharacterConfig config) {
@ -98,7 +109,8 @@ public class JSON {
try {
return Optional.of(mapper.readValue(input, BasicMessage.class));
} catch (JsonProcessingException e) {
// e.printStackTrace();
Logger.debug("JSON deserialization of Message failed. Exception: {}",
e.toString());
return Optional.empty();
}
}
@ -112,6 +124,9 @@ public class JSON {
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();
}