From 3ffa768cfc082b61a75733207de1f3c0a7f539e4 Mon Sep 17 00:00:00 2001 From: Anuken Date: Mon, 14 Oct 2019 17:36:58 -0400 Subject: [PATCH] Json cleanup --- core/src/io/anuke/mindustry/io/JsonIO.java | 59 +++++++--------------- gradle.properties | 2 +- 2 files changed, 19 insertions(+), 42 deletions(-) diff --git a/core/src/io/anuke/mindustry/io/JsonIO.java b/core/src/io/anuke/mindustry/io/JsonIO.java index 7e0b8f640c..c9adc28482 100644 --- a/core/src/io/anuke/mindustry/io/JsonIO.java +++ b/core/src/io/anuke/mindustry/io/JsonIO.java @@ -8,12 +8,27 @@ import io.anuke.mindustry.game.*; import io.anuke.mindustry.type.*; import io.anuke.mindustry.world.*; +import java.io.*; + @SuppressWarnings("unchecked") public class JsonIO{ private static CustomJson jsonBase = new CustomJson(); - private static Json json = new Json(){{ - apply(this); - }}; + private static Json json = new Json(){ + { apply(this); } + + @Override + public void writeValue(Object value, Class knownType, Class elementType){ + if(value instanceof MappableContent){ + try{ + getWriter().value(((MappableContent)value).name); + }catch(IOException e){ + throw new RuntimeException(e); + } + }else{ + super.writeValue(value, knownType, elementType); + } + } + }; public static String write(Object object){ return json.toJson(object, object.getClass()); @@ -66,24 +81,6 @@ public class JsonIO{ } }); - //TODO extremely hacky and disgusting - for(Block block : Vars.content.blocks()){ - Class type = block.getClass(); - if(type.isAnonymousClass()) type = type.getSuperclass(); - - json.setSerializer(type, new Serializer(){ - @Override - public void write(Json json, Block object, Class knownType){ - json.writeValue(object.name); - } - - @Override - public Block read(Json json, JsonValue jsonData, Class type){ - return Vars.content.getByName(ContentType.block, jsonData.asString()); - } - }); - } - json.setSerializer(Block.class, new Serializer(){ @Override public void write(Json json, Block object, Class knownType){ @@ -96,26 +93,6 @@ public class JsonIO{ } }); - /* - json.setSerializer(TeamData.class, new Serializer(){ - @Override - public void write(Json json, TeamData object, Class knownType){ - json.writeObjectStart(); - json.writeValue("brokenBlocks", object.brokenBlocks.toArray()); - json.writeValue("team", object.team.ordinal()); - json.writeObjectEnd(); - } - - @Override - public TeamData read(Json json, JsonValue jsonData, Class type){ - long[] blocks = jsonData.get("brokenBlocks").asLongArray(); - Team team = Team.all[jsonData.getInt("team", 0)]; - TeamData out = new TeamData(team, EnumSet.of(new Team[]{})); - out.brokenBlocks = new LongQueue(blocks); - return out; - } - });*/ - json.setSerializer(ItemStack.class, new Serializer(){ @Override public void write(Json json, ItemStack object, Class knownType){ diff --git a/gradle.properties b/gradle.properties index 312603e215..4863d8326d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ org.gradle.daemon=true org.gradle.jvmargs=-Xms256m -Xmx1024m -archash=9ed2c7bd64741309e2ece3fdc15f169862922ef3 +archash=737a74af85af88ee003a6e5a5c4cbc6f0f375a44