feat: implemented proper stringify in the JSON class, and changed ObjectMapper to static
This commit is contained in:
parent
d2216f190a
commit
2ff309500e
@ -3,17 +3,21 @@ package uulm.teamname.marvelous.gamelibrary.json;
|
|||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import jdk.jshell.spi.ExecutionControl;
|
import jdk.jshell.spi.ExecutionControl;
|
||||||
|
import uulm.teamname.marvelous.gamelibrary.json.basic.BasicMessage;
|
||||||
import uulm.teamname.marvelous.gamelibrary.json.basic.EventMessage;
|
import uulm.teamname.marvelous.gamelibrary.json.basic.EventMessage;
|
||||||
|
|
||||||
/** Contains JSON encoding and decoding.
|
/** Contains JSON encoding and decoding.
|
||||||
*/
|
*/
|
||||||
public class JSON {
|
public class JSON {
|
||||||
|
|
||||||
|
private static final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
|
||||||
/** Deserializes an incoming network message into a {@link EventMessage}.
|
/** Deserializes an incoming network message into a {@link EventMessage}.
|
||||||
* @param input The JSON to deserialize.
|
* @param input The JSON to deserialize.
|
||||||
* @return The parsed message. */
|
* @return The parsed message. */
|
||||||
public static EventMessage parse(String input) {
|
public static EventMessage parse(String input) {
|
||||||
EventMessage result = null;
|
EventMessage result = null;
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
|
||||||
try {
|
try {
|
||||||
result = mapper.readValue(input, EventMessage.class);
|
result = mapper.readValue(input, EventMessage.class);
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
@ -25,8 +29,13 @@ public class JSON {
|
|||||||
/** Serializes a {@link EventMessage} into a JSON string.
|
/** Serializes a {@link EventMessage} into a JSON string.
|
||||||
* @param input The message to serialize.
|
* @param input The message to serialize.
|
||||||
* @return The message as JSON. */
|
* @return The message as JSON. */
|
||||||
public static String stringify(EventMessage input) throws ExecutionControl.NotImplementedException {
|
public static String stringify(BasicMessage input) throws ExecutionControl.NotImplementedException {
|
||||||
//TODO: implement JSON.stringify
|
String result = null;
|
||||||
throw new ExecutionControl.NotImplementedException("JSON.stringify is not implemented");
|
try {
|
||||||
|
result = mapper.writeValueAsString(input);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user