mirror of
https://github.com/CarmJos/BukkitJSONSerializer.git
synced 2026-06-04 08:38:22 +08:00
fix(deserialize): try to fix gson number deserialize problem.
This commit is contained in:
@@ -13,7 +13,7 @@
|
|||||||
</properties>
|
</properties>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>bukkitjsonserializer</artifactId>
|
<artifactId>bukkitjsonserializer</artifactId>
|
||||||
<version>1.0.1</version>
|
<version>1.0.2</version>
|
||||||
|
|
||||||
<name>BukkitJSONSerializer</name>
|
<name>BukkitJSONSerializer</name>
|
||||||
<description>A JSON serialize/deserialize util for bukkit's ConfigurationSerialization.</description>
|
<description>A JSON serialize/deserialize util for bukkit's ConfigurationSerialization.</description>
|
||||||
|
|||||||
@@ -1,15 +1,17 @@
|
|||||||
package cc.carm.lib.bukkit.configuration;
|
package cc.carm.lib.bukkit.configuration;
|
||||||
|
|
||||||
import com.google.gson.*;
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonParser;
|
||||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||||
import org.bukkit.configuration.serialization.ConfigurationSerialization;
|
import org.bukkit.configuration.serialization.ConfigurationSerialization;
|
||||||
import org.jetbrains.annotations.Contract;
|
import org.jetbrains.annotations.Contract;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.text.NumberFormat;
|
import java.math.BigDecimal;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@@ -30,36 +32,7 @@ public class BukkitJSONSerializer {
|
|||||||
|
|
||||||
|
|
||||||
public static @NotNull BukkitJSONSerializer create() {
|
public static @NotNull BukkitJSONSerializer create() {
|
||||||
return create(new GsonBuilder().enableComplexMapKeySerialization().serializeNulls().disableHtmlEscaping()
|
return create(new GsonBuilder().enableComplexMapKeySerialization().serializeNulls().disableHtmlEscaping().create());
|
||||||
.registerTypeAdapter(Map.class, (JsonDeserializer<Map<?, ?>>) (json, typeOfT, context) -> {
|
|
||||||
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
|
|
||||||
JsonObject obj = json.getAsJsonObject();
|
|
||||||
for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
|
|
||||||
String key = entry.getKey();
|
|
||||||
JsonElement element = entry.getValue();
|
|
||||||
if (element.isJsonArray()) {
|
|
||||||
map.put(key, context.deserialize(element, List.class));
|
|
||||||
} else if (element.isJsonPrimitive()) {
|
|
||||||
Object value = element.getAsString();
|
|
||||||
try {
|
|
||||||
// try to fix number problems
|
|
||||||
Number num = NumberFormat.getInstance().parse((String) value);
|
|
||||||
if (num != null) {
|
|
||||||
if (num.toString().equals(value)) {
|
|
||||||
value = num.longValue() > Integer.MAX_VALUE ? num.longValue() : num.intValue();
|
|
||||||
} else {
|
|
||||||
value = num.doubleValue();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
|
||||||
map.put(key, value);
|
|
||||||
} else if (element.isJsonObject()) {
|
|
||||||
map.put(key, context.deserialize(element, Map.class));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return map;
|
|
||||||
}).create());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static @NotNull BukkitJSONSerializer create(@NotNull Gson gson) {
|
public static @NotNull BukkitJSONSerializer create(@NotNull Gson gson) {
|
||||||
@@ -196,9 +169,24 @@ public class BukkitJSONSerializer {
|
|||||||
} else {
|
} else {
|
||||||
args.put(key, sub);
|
args.put(key, sub);
|
||||||
}
|
}
|
||||||
|
} else if (v instanceof Double) {
|
||||||
|
double d = (Double) v;
|
||||||
|
BigDecimal num = new BigDecimal(d);
|
||||||
|
long longValue = num.longValue();
|
||||||
|
String decimalPart = String.valueOf(num.subtract(new BigDecimal(longValue)));
|
||||||
|
|
||||||
|
if (decimalPart.matches("^0+$")) {
|
||||||
|
if (longValue > Integer.MAX_VALUE || longValue < Integer.MIN_VALUE) {
|
||||||
|
args.put(key, num.longValue());
|
||||||
|
} else {
|
||||||
|
args.put(key, num.intValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
args.put(key, v);
|
args.put(key, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user