Saber-Factions/src/main/java/com/massivecraft/factions/util/MapFLocToStringSetTypeAdapter.java

129 lines
3.5 KiB
Java
Raw Normal View History

2011-07-31 03:17:00 +02:00
package com.massivecraft.factions.util;
import java.lang.reflect.Type;
import java.util.concurrent.ConcurrentHashMap;
2011-07-31 03:17:00 +02:00
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.Map.Entry;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonArray;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonDeserializationContext;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonDeserializer;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonElement;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonObject;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonParseException;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonPrimitive;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonSerializationContext;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonSerializer;
2011-07-31 03:17:00 +02:00
import com.massivecraft.factions.FLocation;
import com.massivecraft.factions.P;
2011-07-31 03:17:00 +02:00
public class MapFLocToStringSetTypeAdapter implements JsonDeserializer<Map<FLocation, Set<String>>>, JsonSerializer<Map<FLocation, Set<String>>>
{
2011-07-31 03:17:00 +02:00
@Override
public Map<FLocation, Set<String>> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
2011-07-31 03:17:00 +02:00
try {
JsonObject obj = json.getAsJsonObject();
if (obj == null)
{
2011-07-31 03:17:00 +02:00
return null;
}
Map<FLocation, Set<String>> locationMap = new ConcurrentHashMap<FLocation, Set<String>>();
2011-07-31 03:17:00 +02:00
Set<String> nameSet;
Iterator<JsonElement> iter;
String worldName;
String[] coords;
int x, z;
for (Entry<String, JsonElement> entry : obj.entrySet())
{
2011-07-31 03:17:00 +02:00
worldName = entry.getKey();
for (Entry<String, JsonElement> entry2 : entry.getValue().getAsJsonObject().entrySet())
{
2011-07-31 03:17:00 +02:00
coords = entry2.getKey().trim().split("[,\\s]+");
x = Integer.parseInt(coords[0]);
z = Integer.parseInt(coords[1]);
nameSet = new HashSet<String>();
iter = entry2.getValue().getAsJsonArray().iterator();
while (iter.hasNext())
{
2011-07-31 03:17:00 +02:00
nameSet.add(iter.next().getAsString());
}
locationMap.put(new FLocation(worldName, x, z), nameSet);
}
}
return locationMap;
}
catch (Exception ex)
{
2011-07-31 03:17:00 +02:00
ex.printStackTrace();
P.p.log(Level.WARNING, "Error encountered while deserializing a Map of FLocations to String Sets.");
2011-07-31 03:17:00 +02:00
return null;
}
}
@Override
public JsonElement serialize(Map<FLocation, Set<String>> src, Type typeOfSrc, JsonSerializationContext context)
{
2011-07-31 03:17:00 +02:00
JsonObject obj = new JsonObject();
try {
if (src != null)
{
2011-07-31 03:17:00 +02:00
FLocation loc;
String locWorld;
Set<String> nameSet;
Iterator<String> iter;
JsonArray nameArray;
JsonPrimitive nameElement;
for (Entry<FLocation, Set<String>> entry : src.entrySet())
{
2011-07-31 03:17:00 +02:00
loc = entry.getKey();
locWorld = loc.getWorldName();
nameSet = entry.getValue();
if (nameSet == null || nameSet.isEmpty())
{
2011-07-31 03:17:00 +02:00
continue;
}
nameArray = new JsonArray();
iter = nameSet.iterator();
while (iter.hasNext())
{
2011-07-31 03:17:00 +02:00
nameElement = new JsonPrimitive(iter.next());
nameArray.add(nameElement);
}
if ( ! obj.has(locWorld))
{
2011-07-31 03:17:00 +02:00
obj.add(locWorld, new JsonObject());
}
obj.get(locWorld).getAsJsonObject().add(loc.getCoordString(), nameArray);
}
}
return obj;
}
catch (Exception ex)
{
2011-07-31 03:17:00 +02:00
ex.printStackTrace();
P.p.log(Level.WARNING, "Error encountered while serializing a Map of FLocations to String Sets.");
2011-07-31 03:17:00 +02:00
return obj;
}
}
}