feat: implemented still-missing JSON ser/deser for portalEntities

This commit is contained in:
Yannik Bretschneider 2021-07-12 21:16:02 +02:00
parent 518e458c5f
commit e9b4bc5f58
5 changed files with 59 additions and 1 deletions

View File

@ -9,7 +9,8 @@ public enum SerializedEntityType {
NPC,
Character,
InfinityStone,
Rock;
Rock,
Portal;
private static final Map<EntityType, SerializedEntityType> entityTypeToDeserialized;
@ -21,6 +22,7 @@ public enum SerializedEntityType {
entityTypeToDeserialized.put(EntityType.P2, Character);
entityTypeToDeserialized.put(EntityType.InfinityStones, InfinityStone);
entityTypeToDeserialized.put(EntityType.Rocks, Rock);
entityTypeToDeserialized.put(EntityType.Portals, Portal);
}
public static SerializedEntityType valueOf(EntityType entityType) {

View File

@ -75,6 +75,10 @@ public class EntityDeserializer extends JsonDeserializer<Entity> {
codec.treeToValue(node.get("position"), IntVector2.class),
node.get("HP").asInt()
);
case Portal -> result = new Portal(
new EntityID(EntityType.Portals, node.get("ID").asInt()),
codec.treeToValue(node.get("position"), IntVector2.class)
);
}
return result;
}

View File

@ -32,6 +32,8 @@ public class EntitySerializer extends StdSerializer<Entity> {
serializeRock((Rock) value, gen, provider);
} else if (value instanceof InfinityStone) {
serializeInfinityStone((InfinityStone) value, gen, provider);
} else if (value instanceof Portal) {
serializePortal((Portal) value, gen, provider);
}
gen.writeObjectField("position", value.getPosition());
@ -72,6 +74,10 @@ public class EntitySerializer extends StdSerializer<Entity> {
gen.writeNumberField("ID", value.id.id);
}
private void serializePortal(Portal value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeNumberField("ID", value.id.id);
}
/** Returns an int[] with the sorted integer representations of the stones in the given Inventory */
private int[] inventoryToIntArray(Inventory inventory) {
return Arrays.stream(inventory.getStonesAsArray())

View File

@ -271,12 +271,33 @@ class EntityDeserializerTest {
rock.getPosition().getY()
);
// === RANDOM PORTAL ===
Portal portal = new Portal(
new EntityID(EntityType.Portals, Math.abs(randomIntegers.next())),
new IntVector2(Math.abs(randomIntegers.next()), Math.abs(randomIntegers.next()))
);
String jsonRepresentingPortal = """
{
"entityType": "Portal",
"ID": %d,
"position": [%d, %d]
}
""".formatted(// formatting the string with the required arguments
portal.id.id,
portal.getPosition().getX(),
portal.getPosition().getY()
);
// System.out.println(character);
// System.out.println(jsonRepresentingCharacter);
// System.out.println(stone);
// System.out.println(jsonRepresentingStone);
// System.out.println(rock);
// System.out.println(jsonRepresentingRock);
// System.out.println(portal);
// System.out.println(jsonRepresentingPortal);
// // Assertions
assertThat((Character) (mapper.readValue(jsonRepresentingCharacter, Entity.class)))
@ -288,6 +309,9 @@ class EntityDeserializerTest {
assertThat((Rock) (mapper.readValue(jsonRepresentingRock, Entity.class)))
.isEqualTo(rock);
assertThat((Portal) mapper.readValue(jsonRepresentingPortal, Entity.class))
.isEqualTo(portal);

View File

@ -243,4 +243,26 @@ class EntitySerializerTest {
}
@Test
void serializePortal() throws JsonProcessingException {
var mapper = new ObjectMapper();
Portal portal = new Portal(
new EntityID(EntityType.Portals, 15),
new IntVector2(25, 35)
);
String jsonRepresentingPortal = """
{
"entityType":"Portal",
"ID":15,
"position":[25,35]
}
""".replace("\n", "");
assertThat(mapper.writeValueAsString(portal))
.isEqualTo(jsonRepresentingPortal);
}
}