Oh, so very much formatting and stuffs

This commit is contained in:
gomeow
2013-05-23 22:57:16 -07:00
parent e2c327bd46
commit e3516774f3
19 changed files with 519 additions and 428 deletions
@@ -17,17 +17,18 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Fancy JSON serialization mostly by evilmidget38.
* @author evilmidget38, gomeow
*
*/
public class Serialization {
/*
* All normal functions
*/
@SuppressWarnings("unchecked")
public static Map<String, Object> toMap(JSONObject object) throws JSONException {
Map<String, Object> map = new HashMap<String, Object>();
Iterator<String> keys = object.keys();
while (keys.hasNext()) {
while(keys.hasNext()) {
String key = (String) keys.next();
map.put(key, fromJson(object.get(key)));
}
@@ -35,11 +36,11 @@ public class Serialization {
}
private static Object fromJson(Object json) throws JSONException {
if(json == JSONObject.NULL) {
if (json == JSONObject.NULL) {
return null;
} else if(json instanceof JSONObject) {
} else if (json instanceof JSONObject) {
return toMap((JSONObject) json);
} else if(json instanceof JSONArray) {
} else if (json instanceof JSONArray) {
return toList((JSONArray) json);
} else {
return json;
@@ -61,7 +62,7 @@ public class Serialization {
items.add(is);
}
for(ConfigurationSerializable cs:items) {
if(cs == null) {
if (cs == null) {
result.add("null");
}
else {
@@ -75,7 +76,7 @@ public class Serialization {
Inventory inv = Bukkit.createInventory(null, 54, ChatColor.RED + "Vault #" + number);
List<ItemStack> contents = new ArrayList<ItemStack>();
for(String piece:stringItems) {
if(piece.equalsIgnoreCase("null")) {
if (piece.equalsIgnoreCase("null")) {
contents.add(null);
}
else {
@@ -97,7 +98,7 @@ public class Serialization {
public static Map<String, Object> serialize(ConfigurationSerializable cs) {
Map<String, Object> serialized = recreateMap(cs.serialize());
for(Entry<String, Object> entry:serialized.entrySet()) {
if(entry.getValue() instanceof ConfigurationSerializable) {
if (entry.getValue() instanceof ConfigurationSerializable) {
entry.setValue(serialize((ConfigurationSerializable) entry.getValue()));
}
}
@@ -113,20 +114,14 @@ public class Serialization {
return map;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({"unchecked", "rawtypes"})
public static ConfigurationSerializable deserialize(Map<String, Object> map) {
for(Entry<String, Object> entry:map.entrySet()) {
// Check if any of its sub-maps are ConfigurationSerializable. They need to be done
// first.
if(entry.getValue() instanceof Map && ((Map) entry.getValue()).containsKey(ConfigurationSerialization.SERIALIZED_TYPE_KEY)) {
if (entry.getValue() instanceof Map && ((Map) entry.getValue()).containsKey(ConfigurationSerialization.SERIALIZED_TYPE_KEY)) {
entry.setValue(deserialize((Map) entry.getValue()));
}
}
return ConfigurationSerialization.deserializeObject(map);
}
/*
* All old methods for transferring
*/
}