From cd992b0fa6c52a08ec828e4d44fd7676cd5cf7c9 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Fri, 4 Jun 2021 19:48:40 +0200 Subject: [PATCH] doc: added logging in case of failed de/serialization --- .../marvelous/gamelibrary/json/JSON.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java index d1db478..c0a7ca7 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java @@ -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 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(); }