From f85d2a0a1786bcc3cd6be319359baa073a94ff18 Mon Sep 17 00:00:00 2001 From: Dariasc Date: Sun, 11 Mar 2018 22:29:02 -0300 Subject: [PATCH] Fix GSON Map Serialization (#1081) * Add backwards compatibility for permissions that were placed in dev builds. --- .../java/com/massivecraft/factions/P.java | 2 +- .../util/PermissionsMapTypeAdapter.java | 55 ++++++++++++++----- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/massivecraft/factions/P.java b/src/main/java/com/massivecraft/factions/P.java index 081a3548..d4dd7446 100644 --- a/src/main/java/com/massivecraft/factions/P.java +++ b/src/main/java/com/massivecraft/factions/P.java @@ -174,7 +174,7 @@ public class P extends MPlugin { Type accessTypeAdatper = new TypeToken>>() { }.getType(); - return new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE).registerTypeAdapter(accessTypeAdatper, new PermissionsMapTypeAdapter()).registerTypeAdapter(LazyLocation.class, new MyLocationTypeAdapter()).registerTypeAdapter(mapFLocToStringSetType, new MapFLocToStringSetTypeAdapter()).registerTypeAdapterFactory(EnumTypeAdapter.ENUM_FACTORY); + return new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().enableComplexMapKeySerialization().excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE).registerTypeAdapter(accessTypeAdatper, new PermissionsMapTypeAdapter()).registerTypeAdapter(LazyLocation.class, new MyLocationTypeAdapter()).registerTypeAdapter(mapFLocToStringSetType, new MapFLocToStringSetTypeAdapter()).registerTypeAdapterFactory(EnumTypeAdapter.ENUM_FACTORY); } @Override diff --git a/src/main/java/com/massivecraft/factions/util/PermissionsMapTypeAdapter.java b/src/main/java/com/massivecraft/factions/util/PermissionsMapTypeAdapter.java index 1a8b1b56..2b384688 100644 --- a/src/main/java/com/massivecraft/factions/util/PermissionsMapTypeAdapter.java +++ b/src/main/java/com/massivecraft/factions/util/PermissionsMapTypeAdapter.java @@ -7,6 +7,7 @@ import com.massivecraft.factions.struct.Role; import com.massivecraft.factions.zcore.fperms.Access; import com.massivecraft.factions.zcore.fperms.Permissable; import com.massivecraft.factions.zcore.fperms.PermissableAction; +import com.massivecraft.factions.zcore.util.TL; import java.lang.reflect.Type; import java.util.HashMap; @@ -14,6 +15,8 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; +import static com.massivecraft.factions.zcore.util.TL.ROLE_NORMAL; + public class PermissionsMapTypeAdapter implements JsonDeserializer>> { @Override @@ -39,6 +42,19 @@ public class PermissionsMapTypeAdapter implements JsonDeserializer accessMap = new HashMap<>(); for (Map.Entry entry2 : entry.getValue().getAsJsonObject().entrySet()) { PermissableAction permissableAction = PermissableAction.fromString(entry2.getKey()); + if (permissableAction == null) { + switch (entry2.getKey()) { + case "frostwalk": + permissableAction = PermissableAction.FROST_WALK; + break; + case "painbuild": + permissableAction = PermissableAction.PAIN_BUILD; + break; + case "items": + permissableAction = PermissableAction.ITEM; + break; + } + } Access access = Access.fromString(entry2.getValue().getAsString()); accessMap.put(permissableAction, access); } @@ -54,19 +70,32 @@ public class PermissionsMapTypeAdapter implements JsonDeserializer