From 68248a6689964555f62cbb42991260e2ea34e4c3 Mon Sep 17 00:00:00 2001 From: Jan Fic Date: Thu, 21 Apr 2022 03:16:15 -0400 Subject: [PATCH] removed known class names from json --- .../games/computercombat/model/Component.java | 8 +++---- .../games/computercombat/model/Player.java | 8 ++++--- .../model/match/MatchState.java | 22 +++++++++++++++---- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/computercombat/core/src/com/janfic/games/computercombat/model/Component.java b/computercombat/core/src/com/janfic/games/computercombat/model/Component.java index 3c6fcf1..033c476 100644 --- a/computercombat/core/src/com/janfic/games/computercombat/model/Component.java +++ b/computercombat/core/src/com/janfic/games/computercombat/model/Component.java @@ -234,15 +234,15 @@ public int hashCode() { @Override public void write(Json json) { json.writeValue("color", color); - json.writeValue("position", "" + x + " " + y); + json.writeValue("x", x); + json.writeValue("y", y); } @Override public void read(Json json, JsonValue jv) { this.color = json.readValue("color", Integer.class, jv); - String[] position = json.readValue("position", String.class, jv).split(" "); - this.x = Integer.parseInt(position[0]); - this.y = Integer.parseInt(position[1]); + this.x = json.readValue("x", Integer.class, jv); + this.y = json.readValue("y", Integer.class, jv); this.textureName = colorToTextureName.get(this.color); } diff --git a/computercombat/core/src/com/janfic/games/computercombat/model/Player.java b/computercombat/core/src/com/janfic/games/computercombat/model/Player.java index 02d1df5..5f2fad2 100644 --- a/computercombat/core/src/com/janfic/games/computercombat/model/Player.java +++ b/computercombat/core/src/com/janfic/games/computercombat/model/Player.java @@ -54,15 +54,17 @@ public Deck getActiveDeck() { @Override public void write(Json json) { + json.setTypeName("class"); json.writeType(this.getClass()); - json.writeValue("uid", uid); - json.writeValue("deck", deck); + json.writeValue("uid", uid, String.class); + //json.writeValue("deck", deck); + json.setTypeName(null); } @Override public void read(Json json, JsonValue jsonData) { this.uid = json.readValue("uid", String.class, jsonData); - this.deck = json.readValue("deck", Deck.class, jsonData); + //this.deck = json.readValue("deck", Deck.class, jsonData); } @Override diff --git a/computercombat/core/src/com/janfic/games/computercombat/model/match/MatchState.java b/computercombat/core/src/com/janfic/games/computercombat/model/match/MatchState.java index d15f94d..1ed906e 100644 --- a/computercombat/core/src/com/janfic/games/computercombat/model/match/MatchState.java +++ b/computercombat/core/src/com/janfic/games/computercombat/model/match/MatchState.java @@ -382,6 +382,7 @@ public List getComponentsAsList() { @Override public void write(Json json) { + json.setTypeName(null); json.writeValue("players", players, List.class); json.writeValue("currentPlayerMove", currentPlayerMove, Player.class); json.writeValue("activeEntities", activeEntities, Map.class); @@ -397,17 +398,29 @@ public void write(Json json) { } assert (board.length() == 64); json.writeValue("componentBoard", board); + json.setTypeName("class"); } @Override public void read(Json json, JsonValue jsonData) { - this.players = json.readValue("players", List.class, jsonData); - this.currentPlayerMove = json.readValue("currentPlayerMove", String.class, jsonData); + json.setTypeName("class"); + this.players = json.readValue("players", List.class, Player.class, jsonData); + json.setTypeName(null); this.isGameOver = json.readValue("isGameOver", boolean.class, jsonData); this.winner = json.readValue("winner", String.class, jsonData); - this.activeEntities = json.readValue("activeEntities", HashMap.class, List.class, jsonData); - this.computers = json.readValue("computers", HashMap.class, Card.class, jsonData); + this.currentPlayerMove = json.readValue("currentPlayerMove", String.class, jsonData); this.decks = json.readValue("decks", HashMap.class, Deck.class, jsonData); + HashMap> v = json.readValue("activeEntities", HashMap.class, List.class, jsonData); + this.activeEntities = new HashMap<>(); + for (String string : v.keySet()) { + List cards = new ArrayList<>(); + for (JsonValue jsonValue : v.get(string)) { + Card c = json.readValue(Card.class, jsonValue); + cards.add(c); + } + activeEntities.put(string, cards); + } + this.computers = json.readValue("computers", HashMap.class, Card.class, jsonData); String boardString = json.readValue("componentBoard", String.class, jsonData); componentBoard = new Component[8][8]; assert (boardString.length() == 64); @@ -420,6 +433,7 @@ public void read(Json json, JsonValue jsonData) { e.printStackTrace(); } } + json.setTypeName("class"); } public int countComponents(ComponentFilter filter, Move move) {