Might be fun to clone GSON elements some time.

This commit is contained in:
Olof Larsson 2014-01-31 18:13:58 +01:00
parent 8ca4c999d9
commit ef7a4ef69c

View File

@ -0,0 +1,54 @@
package com.massivecraft.mcore.store;
import java.util.Iterator;
import java.util.Map.Entry;
import com.massivecraft.mcore.xlib.gson.JsonArray;
import com.massivecraft.mcore.xlib.gson.JsonElement;
import com.massivecraft.mcore.xlib.gson.JsonNull;
import com.massivecraft.mcore.xlib.gson.JsonObject;
public class GsonCloner
{
public static JsonElement clone(JsonElement element)
{
// null
if (element == null) return null;
// JsonNull
if (element.isJsonNull()) return JsonNull.INSTANCE;
// JsonPrimitive
if (element.isJsonPrimitive())
{
// TODO: This is actually not safe since JsonPrimitive is mutable.
// However there is no easy way to clone a JsonPrimitive and I thought they were mutable anyways.
return element;
}
// JsonObject
if (element.isJsonObject())
{
JsonObject ret = new JsonObject();
for (Entry<String, JsonElement> entry : ((JsonObject)element).entrySet())
{
ret.add(entry.getKey(), clone(entry.getValue()));
}
return ret;
}
// JsonArray
if (element.isJsonArray())
{
JsonArray ret = new JsonArray();
Iterator<JsonElement> iter = ((JsonArray)element).iterator();
while (iter.hasNext())
{
ret.add(clone(iter.next()));
}
return ret;
}
throw new RuntimeException("Unknown JsonElement class: " + element.getClass().getName());
}
}