Minor code cleanup in BukkitObjectTypeAdapter

This commit is contained in:
Florian CUNY 2019-05-11 20:17:02 +02:00
parent d631d2cb86
commit ba7c118266

View File

@ -1,24 +1,24 @@
package world.bentobox.bentobox.database.json.adapters;
import static org.bukkit.configuration.serialization.ConfigurationSerialization.SERIALIZED_TYPE_KEY;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import static org.bukkit.configuration.serialization.ConfigurationSerialization.SERIALIZED_TYPE_KEY;
/**
* Handles {@link ConfigurationSerializable} types
* @author tastybento
* @since 1.5.0
*/
public class BukkitObjectTypeAdapter extends TypeAdapter<ConfigurationSerializable> {
@SuppressWarnings("rawtypes")
@ -29,9 +29,8 @@ public class BukkitObjectTypeAdapter extends TypeAdapter<ConfigurationSerializab
this.map = mapAdapter;
}
public static Map<String, Object> serializeObject(ConfigurationSerializable serializable) {
Map<String, Object> serialized = new HashMap<>();
serialized.putAll(serializable.serialize());
public static Map<String, Object> serializeObject(@NonNull ConfigurationSerializable serializable) {
Map<String, Object> serialized = new HashMap<>(serializable.serialize());
serialized.entrySet().stream()
.filter(e -> e.getValue() instanceof ConfigurationSerializable)
.forEach(e -> serialized.put(e.getKey(), serializeObject((ConfigurationSerializable) e.getValue())));
@ -39,6 +38,7 @@ public class BukkitObjectTypeAdapter extends TypeAdapter<ConfigurationSerializab
return serialized;
}
@Nullable
@SuppressWarnings("unchecked")
public ConfigurationSerializable deserializeObject(Map<String, Object> map) {
if (map == null) {
@ -58,6 +58,7 @@ public class BukkitObjectTypeAdapter extends TypeAdapter<ConfigurationSerializab
return ConfigurationSerialization.deserializeObject(map);
}
@Nullable
@SuppressWarnings("unchecked")
@Override
public ConfigurationSerializable read(JsonReader in) throws IOException {
@ -68,7 +69,8 @@ public class BukkitObjectTypeAdapter extends TypeAdapter<ConfigurationSerializab
public void write(JsonWriter out, ConfigurationSerializable value) throws IOException {
if (value == null) {
out.nullValue();
return;
}
map.write(out, serializeObject(value));
}
}
}