From d74cd114cc12463616d11221cd83371c68868942 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Fri, 4 Jun 2021 18:44:54 +0200 Subject: [PATCH] feat: added static methods to JSON for configuration deserialization --- .../marvelous/gamelibrary/json/JSON.java | 66 +++++++++++++++++++ .../marvelous/gamelibrary/json/JSONTest.java | 59 +++++++++++++++++ 2 files changed, 125 insertions(+) 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 134056a..d1db478 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java @@ -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 parseCharacterConfig (String jsonRepresentingConfig) { + Optional result; + try { + result = Optional.of(staticMapper.readValue(jsonRepresentingConfig, CharacterConfig.class)); + } catch (JsonProcessingException e) { + result = Optional.empty(); + } + return result; + } + + public static Optional parseCharacterConfig (File jsonRepresentingConfig) { + Optional result; + try { + result = Optional.of(staticMapper.readValue(jsonRepresentingConfig, CharacterConfig.class)); + } catch (IOException e) { + result = Optional.empty(); + } + return result; + } + + public static Optional parsePartyConfig (String jsonRepresentingConfig) { + Optional result; + try { + result = Optional.of(staticMapper.readValue(jsonRepresentingConfig, PartyConfig.class)); + } catch (JsonProcessingException e) { + result = Optional.empty(); + } + return result; + } + + public static Optional parsePartyConfig (File jsonRepresentingConfig) { + Optional result; + try { + result = Optional.of(staticMapper.readValue(jsonRepresentingConfig, PartyConfig.class)); + } catch (IOException e) { + result = Optional.empty(); + } + return result; + } + + public static Optional parseScenarioConfig (String jsonRepresentingConfig) { + Optional result; + try { + result = Optional.of(staticMapper.readValue(jsonRepresentingConfig, ScenarioConfig.class)); + } catch (JsonProcessingException e) { + result = Optional.empty(); + } + return result; + } + + public static Optional parseScenarioConfig (File jsonRepresentingConfig) { + Optional 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) { diff --git a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/JSONTest.java b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/JSONTest.java index 75ebf64..106ef3f 100644 --- a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/JSONTest.java +++ b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/JSONTest.java @@ -56,6 +56,65 @@ class JSONTest { // >>>>>>> Deserialization tests <<<<<<< // ============================================================================= + @Test + void scenarioConfigDeserializationTest() { + var jsonRepresentingConfig = """ + { + "scenario":[ + ["GRASS", "GRASS", "GRASS", "GRASS", "GRASS"], + ["GRASS", "GRASS", "GRASS", "GRASS", "GRASS"], + ["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"], + ["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"], + ["GRASS", "GRASS", "ROCK", "ROCK", "GRASS"], + ["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"], + ["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"], + ["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"], + ["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"], + ["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"], + ["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"], + ["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"], + ["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"], + ["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"], + ["GRASS", "GRASS", "ROCK", "GRASS", "GRASS"], + ["ROCK", "GRASS", "ROCK", "GRASS", "GRASS"], + ["ROCK", "GRASS", "ROCK", "ROCK", "GRASS"], + ["ROCK", "ROCK", "ROCK", "ROCK", "GRASS"] + ], + "author": "jakobmh", + "name": "avengerstower" + } + """; + + var result = JSON.parseScenarioConfig(jsonRepresentingConfig).get(); + + var actual = new ScenarioConfig(); + actual.scenario = new FieldType[][] { + {FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS}, + {FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS}, + {FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS}, + {FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS}, + {FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.ROCK, FieldType.GRASS}, + {FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS}, + {FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS}, + {FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS}, + {FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS}, + {FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS}, + {FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS}, + {FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS}, + {FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS}, + {FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS}, + {FieldType.GRASS, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS}, + {FieldType.ROCK, FieldType.GRASS, FieldType.ROCK, FieldType.GRASS, FieldType.GRASS}, + {FieldType.ROCK, FieldType.GRASS, FieldType.ROCK, FieldType.ROCK, FieldType.GRASS}, + {FieldType.ROCK, FieldType.ROCK, FieldType.ROCK, FieldType.ROCK, FieldType.GRASS} + }; + + actual.author = "jakobmh"; + actual.name = "avengerstower"; + + assertThat(result).isEqualTo(actual); + } + @Test void characterSelectionDeserializationParseTest() throws JsonProcessingException { var jsonRepresentingMessage = """