From d0829f3948f104abdf73972dc805e83638793ae1 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Sun, 30 May 2021 17:00:59 +0200 Subject: [PATCH] feat: implemented proper deserialization for EntityID and IntVector2 --- .../marvelous/gamelibrary/IntVector2.java | 3 ++ .../gamelibrary/entities/EntityID.java | 3 ++ .../json/ingame/IntVector2Serializer.java | 27 ++++++++++++++ .../json/ingame/IntVector2SerializerTest.java | 37 +++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/IntVector2Serializer.java create mode 100644 src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/IntVector2SerializerTest.java diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/IntVector2.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/IntVector2.java index 5ef49c9..ef4b951 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/IntVector2.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/IntVector2.java @@ -1,13 +1,16 @@ package uulm.teamname.marvelous.gamelibrary; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; import uulm.teamname.marvelous.gamelibrary.json.ingame.IntVector2Deserializer; +import uulm.teamname.marvelous.gamelibrary.json.ingame.IntVector2Serializer; import java.io.Serializable; import java.util.Objects; /** Represents a 2d vector of integers. */ @JsonDeserialize(using = IntVector2Deserializer.class) +@JsonSerialize(using = IntVector2Serializer.class) public class IntVector2 implements Serializable { private int x; private int y; diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/EntityID.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/EntityID.java index c6eb5f1..af7465b 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/EntityID.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/EntityID.java @@ -1,15 +1,18 @@ package uulm.teamname.marvelous.gamelibrary.entities; import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import uulm.teamname.marvelous.gamelibrary.json.ingame.EntityIDDeserializer; import java.util.Objects; /** Represents a distinct identification for every {@link Entity} in a game. */ +@JsonPropertyOrder({"entityID", "ID"}) @JsonDeserialize(using = EntityIDDeserializer.class) public class EntityID { /** The index of the entity */ + @JsonProperty("ID") public final int id; /** The type of the entity */ diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/IntVector2Serializer.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/IntVector2Serializer.java new file mode 100644 index 0000000..9d02f60 --- /dev/null +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/IntVector2Serializer.java @@ -0,0 +1,27 @@ +package uulm.teamname.marvelous.gamelibrary.json.ingame; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; +import uulm.teamname.marvelous.gamelibrary.IntVector2; + +import java.io.IOException; + +public class IntVector2Serializer extends StdSerializer { + + public IntVector2Serializer() { + this(null); + } + + protected IntVector2Serializer(Class t) { + super(t); + } + + @Override + public void serialize(IntVector2 value, JsonGenerator gen, SerializerProvider provider) throws IOException { + gen.writeStartArray(); + gen.writeNumber(value.getX()); + gen.writeNumber(value.getY()); + gen.writeEndArray(); + } +} diff --git a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/IntVector2SerializerTest.java b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/IntVector2SerializerTest.java new file mode 100644 index 0000000..2f530ff --- /dev/null +++ b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/IntVector2SerializerTest.java @@ -0,0 +1,37 @@ +package uulm.teamname.marvelous.gamelibrary.json.ingame; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import net.jqwik.api.*; +import net.jqwik.api.lifecycle.BeforeProperty; +import uulm.teamname.marvelous.gamelibrary.IntVector2; + +import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.*; +import static org.assertj.core.api.Assertions.*; + +class IntVector2SerializerTest { + + ObjectMapper mapper = new ObjectMapper(); + + @BeforeProperty + void setup() { + mapper = new ObjectMapper(); + } + + @Property + void vectorsGetProperlyDeserialized(@ForAll @From("positions") IntVector2 vector) throws JsonProcessingException { + var jsonRepresentingVector = String.format("[%d,%d]", vector.getX(), vector.getY()); + assertThat(mapper.writeValueAsString(vector)) + .isEqualTo(jsonRepresentingVector); + + } + + @Provide("positions") + Arbitrary positions() { + return Arbitraries.integers() + .tuple2() + .map(pos -> new IntVector2(pos.get1(), pos.get2())); + } + +} \ No newline at end of file