mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-09-07 12:58:02 +02:00
Minor code cleanup in BukkitObjectTypeAdapter
This commit is contained in:
parent
d631d2cb86
commit
ba7c118266
@ -1,24 +1,24 @@
|
|||||||
package world.bentobox.bentobox.database.json.adapters;
|
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.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
import static org.bukkit.configuration.serialization.ConfigurationSerialization.SERIALIZED_TYPE_KEY;
|
||||||
import org.bukkit.configuration.serialization.ConfigurationSerialization;
|
|
||||||
|
|
||||||
import com.google.gson.TypeAdapter;
|
|
||||||
import com.google.gson.stream.JsonReader;
|
|
||||||
import com.google.gson.stream.JsonWriter;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles {@link ConfigurationSerializable} types
|
* Handles {@link ConfigurationSerializable} types
|
||||||
* @author tastybento
|
* @author tastybento
|
||||||
* @since 1.5.0
|
* @since 1.5.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class BukkitObjectTypeAdapter extends TypeAdapter<ConfigurationSerializable> {
|
public class BukkitObjectTypeAdapter extends TypeAdapter<ConfigurationSerializable> {
|
||||||
|
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
@ -29,9 +29,8 @@ public class BukkitObjectTypeAdapter extends TypeAdapter<ConfigurationSerializab
|
|||||||
this.map = mapAdapter;
|
this.map = mapAdapter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Map<String, Object> serializeObject(ConfigurationSerializable serializable) {
|
public static Map<String, Object> serializeObject(@NonNull ConfigurationSerializable serializable) {
|
||||||
Map<String, Object> serialized = new HashMap<>();
|
Map<String, Object> serialized = new HashMap<>(serializable.serialize());
|
||||||
serialized.putAll(serializable.serialize());
|
|
||||||
serialized.entrySet().stream()
|
serialized.entrySet().stream()
|
||||||
.filter(e -> e.getValue() instanceof ConfigurationSerializable)
|
.filter(e -> e.getValue() instanceof ConfigurationSerializable)
|
||||||
.forEach(e -> serialized.put(e.getKey(), serializeObject((ConfigurationSerializable) e.getValue())));
|
.forEach(e -> serialized.put(e.getKey(), serializeObject((ConfigurationSerializable) e.getValue())));
|
||||||
@ -39,6 +38,7 @@ public class BukkitObjectTypeAdapter extends TypeAdapter<ConfigurationSerializab
|
|||||||
return serialized;
|
return serialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public ConfigurationSerializable deserializeObject(Map<String, Object> map) {
|
public ConfigurationSerializable deserializeObject(Map<String, Object> map) {
|
||||||
if (map == null) {
|
if (map == null) {
|
||||||
@ -58,6 +58,7 @@ public class BukkitObjectTypeAdapter extends TypeAdapter<ConfigurationSerializab
|
|||||||
return ConfigurationSerialization.deserializeObject(map);
|
return ConfigurationSerialization.deserializeObject(map);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public ConfigurationSerializable read(JsonReader in) throws IOException {
|
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 {
|
public void write(JsonWriter out, ConfigurationSerializable value) throws IOException {
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
out.nullValue();
|
out.nullValue();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
map.write(out, serializeObject(value));
|
map.write(out, serializeObject(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user