Fix GSON Map Serialization (#1081)

* Add backwards compatibility for permissions that were placed in dev builds.
This commit is contained in:
Dariasc 2018-03-11 22:29:02 -03:00 committed by Trent Hensler
parent b78ff51e10
commit f85d2a0a17
2 changed files with 43 additions and 14 deletions

View File

@ -174,7 +174,7 @@ public class P extends MPlugin {
Type accessTypeAdatper = new TypeToken<Map<Permissable, Map<PermissableAction, Access>>>() {
}.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

View File

@ -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<Map<Permissable, Map<PermissableAction, Access>>> {
@Override
@ -39,6 +42,19 @@ public class PermissionsMapTypeAdapter implements JsonDeserializer<Map<Permissab
Map<PermissableAction, Access> accessMap = new HashMap<>();
for (Map.Entry<String, JsonElement> 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<Map<Permissab
}
}
private Permissable getPermissable(String s) {
try {
return Relation.fromString(s);
} catch (Exception e) {
e.printStackTrace();
private Permissable getPermissable(String name) {
// If name is uppercase then it is (probably, no way to completely know) valid if not begin conversion
if (name.equals(name.toUpperCase())) {
if (Role.fromString(name.toUpperCase()) != null) {
return Role.fromString(name.toUpperCase());
} else if (Relation.fromString(name.toUpperCase()) != null) {
return Relation.fromString(name.toUpperCase());
} else {
return null;
}
} else {
if (name.equals(TL.ROLE_RECRUIT.toString())) {
return Role.RECRUIT;
} else if (name.equals(TL.ROLE_NORMAL.toString())) {
return Role.NORMAL;
} else if (name.equals(TL.ROLE_MODERATOR.toString())) {
return Role.MODERATOR;
} else {
// If it is explicitly member and its old data then it refers to relation member not role, skip it
if (name.equals("member")) {
return null;
}
return Relation.fromString(name);
}
}
try {
return Role.fromString(s);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}