feat: implemented proper deserialization for EntityID and IntVector2

This commit is contained in:
Yannik Bretschneider 2021-05-30 17:00:59 +02:00
parent 42e57daa54
commit d0829f3948
4 changed files with 70 additions and 0 deletions

View File

@ -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;

View File

@ -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 */

View File

@ -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<IntVector2> {
public IntVector2Serializer() {
this(null);
}
protected IntVector2Serializer(Class<IntVector2> 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();
}
}

View File

@ -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<IntVector2> positions() {
return Arbitraries.integers()
.tuple2()
.map(pos -> new IntVector2(pos.get1(), pos.get2()));
}
}