1
mirror of https://github.com/CarmJos/BukkitJSONSerializer.git synced 2026-06-04 16:48:18 +08:00

5 Commits

3 changed files with 33 additions and 13 deletions
+7 -7
View File
@@ -19,23 +19,23 @@ First, we should get the serializer instance or create a new one.
BukkitJSONSerializer serializer = BukkitJSONSerializer.get();
```
Then, we cloud use `BukkitJSONSerializer#serializeToJSON(ConfigurationSerializable)` to serialize a object to JSON.
Then, we cloud use `serializeToJSON(ConfigurationSerializable)` to serialize a object to JSON.
```jave
Location location = new Location(Bukkit.getWorlds().get(0), -100.5, 100, 105.5);
String serialized = BukkitJSONSerializer.serializeToJSON(location);
// serialized -> {"world":"world","x":-100.5,"y":100,"z":105.5,"yaw":0.0,"pitch":0.0}
String serialized = serializer.serializeToJSON(location);
// -> {"world":"world","x":-100.5,"y":100,"z":105.5,"yaw":0.0,"pitch":0.0}
```
When we need to read the object, just use `BukkitJSONSerializer#deserializeSON(json,typeClass)` to deserialize the JSON
When we need to read the object, just use `deserializeJSON(json,typeClass)` to deserialize the JSON
string.
```java
Location location = serializer.deserializeSON(json, Location.class);
// deserialized -> Location{world=world, x=-100.5, y=100, z=105.5, pitch=0.0, yaw=0.0}
Location location = serializer.deserializeJSON(json, Location.class);
// Location{world=world, x=-100.5, y=100, z=105.5, pitch=0.0, yaw=0.0}
```
Or use `BukkitJSONSerializer#deserializeSON(json,typeClass,defaultValue)` if we need a default value.
Or use `deserializeSON(json,typeClass,defaultValue)` if we need a default value.
### JSONSerializable class
+1 -1
View File
@@ -13,7 +13,7 @@
</properties>
<groupId>cc.carm.lib</groupId>
<artifactId>bukkitjsonserializer</artifactId>
<version>1.0.0</version>
<version>1.0.2</version>
<name>BukkitJSONSerializer</name>
<description>A JSON serialize/deserialize util for bukkit's ConfigurationSerialization.</description>
@@ -10,6 +10,7 @@ import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
@@ -29,8 +30,13 @@ public class BukkitJSONSerializer {
public static final String TYPE_KEY = ConfigurationSerialization.SERIALIZED_TYPE_KEY;
public static final BukkitJSONSerializer INSTANCE = BukkitJSONSerializer.create();
public static @NotNull BukkitJSONSerializer create() {
return create(new GsonBuilder().disableHtmlEscaping().create(), new JsonParser());
return create(new GsonBuilder().enableComplexMapKeySerialization().serializeNulls().disableHtmlEscaping().create());
}
public static @NotNull BukkitJSONSerializer create(@NotNull Gson gson) {
return new BukkitJSONSerializer(gson, new JsonParser());
}
public static @NotNull BukkitJSONSerializer create(@NotNull Gson gson, @NotNull JsonParser parser) {
@@ -163,12 +169,26 @@ public class BukkitJSONSerializer {
} else {
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 {
args.put(key, v);
}
});
return args;
}
}