From 2ff309500e97132fc2b92fc91abdf740ee7fc779 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Sun, 30 May 2021 18:06:18 +0200 Subject: [PATCH] feat: implemented proper stringify in the JSON class, and changed ObjectMapper to static --- .../marvelous/gamelibrary/json/JSON.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) 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 0d75cf6..8428d54 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java @@ -3,17 +3,21 @@ package uulm.teamname.marvelous.gamelibrary.json; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import jdk.jshell.spi.ExecutionControl; +import uulm.teamname.marvelous.gamelibrary.json.basic.BasicMessage; import uulm.teamname.marvelous.gamelibrary.json.basic.EventMessage; /** Contains JSON encoding and decoding. */ public class JSON { + + private static final ObjectMapper mapper = new ObjectMapper(); + + /** Deserializes an incoming network message into a {@link EventMessage}. * @param input The JSON to deserialize. * @return The parsed message. */ public static EventMessage parse(String input) { EventMessage result = null; - ObjectMapper mapper = new ObjectMapper(); try { result = mapper.readValue(input, EventMessage.class); } catch (JsonProcessingException e) { @@ -25,8 +29,13 @@ public class JSON { /** Serializes a {@link EventMessage} into a JSON string. * @param input The message to serialize. * @return The message as JSON. */ - public static String stringify(EventMessage input) throws ExecutionControl.NotImplementedException { - //TODO: implement JSON.stringify - throw new ExecutionControl.NotImplementedException("JSON.stringify is not implemented"); + public static String stringify(BasicMessage input) throws ExecutionControl.NotImplementedException { + String result = null; + try { + result = mapper.writeValueAsString(input); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + return result; } }