Attempted itemstack json serialization fixes

Signed-off-by: Irmo van den Berge <irmo.vandenberge@ziggo.nl>
This commit is contained in:
Irmo van den Berge committed 2024-08-07 22:50:47 +02:00
1 parent 31b6e17257
commit 6b6b698eb3
4 files changed
+43 -6

No files matched your search

@@ -1,8 +1,10 @@
package com.bergerkiller.bukkit.common.config;
import java.util.Collections;
import java.util.Map;
import com.bergerkiller.bukkit.common.config.yaml.YamlDeserializer;
import com.bergerkiller.bukkit.common.map.gson.EmptyMapSerializer;
import com.google.gson.GsonBuilder;
import org.bukkit.inventory.ItemStack;
@@ -14,7 +16,9 @@ import com.bergerkiller.bukkit.common.utils.LogicUtil;
* Can serialize and deserialize various objects from/to a JSON String.
*/
public class JsonSerializer {
private final com.google.gson.Gson gson = new GsonBuilder().disableHtmlEscaping().create();
private final com.google.gson.Gson gson = new GsonBuilder().disableHtmlEscaping()
.registerTypeAdapter(Collections.emptyMap().getClass(), new EmptyMapSerializer())
.create();
public ItemStack fromJsonToItemStack(String json) throws JsonSyntaxException {
Map<String, Object> mapping = jsonToMap(json);
@@ -0,0 +1,17 @@
package com.bergerkiller.bukkit.common.map.gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import java.util.Map;
public class EmptyMapSerializer implements JsonSerializer<Map<?, ?>> {
@Override
public JsonElement serialize(Map<?, ?> o, Type type, JsonSerializationContext jsonSerializationContext) {
return new JsonObject();
}
}
@@ -1183,17 +1183,22 @@ public class LogicUtil {
}
}
boolean cloned = false;
for (Map.Entry<String, Object> entry : values.entrySet()) {
Object value = entry.getValue();
if (value instanceof ConfigurationSerializable) {
Map<String, Object> serialized = serializeDeep((ConfigurationSerializable) value);
try {
entry.setValue(serialized);
} catch (UnsupportedOperationException ex) {
// This shouldn't ever be hit, but kept it for safety
if (!cloned) {
try {
entry.setValue(serialized);
continue;
} catch (UnsupportedOperationException ex) { /* Shouldn't happen but just in case */ }
values = new LinkedHashMap<>(values);
values.put(entry.getKey(), serialized);
cloned = true;
}
values.put(entry.getKey(), serialized);
}
}
return values;
@@ -9,6 +9,8 @@ import com.bergerkiller.generated.com.mojang.authlib.properties.PropertyHandle;
import com.bergerkiller.generated.org.bukkit.craftbukkit.util.CraftMagicNumbersHandle;
import org.junit.Test;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
@@ -17,6 +19,15 @@ import static org.junit.Assert.*;
public class JsonSerializerTest {
final int dataVersion = CraftMagicNumbersHandle.getDataVersion();
@Test
public void testEmptyMapToJson() {
JsonSerializer serializer = new JsonSerializer();
Map<String, Object> jsonMap = new LinkedHashMap<>();
jsonMap.put("key", Collections.emptyMap());
String json = serializer.toJson(jsonMap);
assertEquals("{\"key\":{}}", json);
}
@Test
public void testItemStackToJsonStick() throws JsonSerializer.JsonSyntaxException {
JsonSerializer serializer = new JsonSerializer();