mirror of
https://github.com/CarmJos/BukkitJSONSerializer.git
synced 2026-06-05 09:01:57 +08:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2264c4af48 | |||
| f4e391f1d0 | |||
| 3e1399ebf6 | |||
| b5ad3541b8 |
@@ -16,26 +16,26 @@ A JSON serialize/deserialize util for bukkit's ConfigurationSerialization.
|
|||||||
First, we should get the serializer instance or create a new one.
|
First, we should get the serializer instance or create a new one.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
BukkitJSONSerializer serializer=BukkitJSONSerializer.get();
|
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
|
```jave
|
||||||
Location location = new Location(Bukkit.getWorlds().get(0), -100.5, 100, 105.5);
|
Location location = new Location(Bukkit.getWorlds().get(0), -100.5, 100, 105.5);
|
||||||
String serialized = BukkitJSONSerializer.serializeToJSON(location);
|
String serialized = serializer.serializeToJSON(location);
|
||||||
// serialized -> {"world":"world","x":-100.5,"y":100,"z":105.5,"yaw":0.0,"pitch":0.0}
|
// -> {"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.
|
string.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
Location location = serializer.deserializeSON(json, Location.class);
|
Location location = serializer.deserializeJSON(json, Location.class);
|
||||||
// deserialized -> Location{world=world, x=-100.5, y=100, z=105.5, pitch=0.0, yaw=0.0}
|
// 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
|
### JSONSerializable class
|
||||||
|
|
||||||
|
|||||||
@@ -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.0</version>
|
<version>1.0.1</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,16 +1,15 @@
|
|||||||
package cc.carm.lib.bukkit.configuration;
|
package cc.carm.lib.bukkit.configuration;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.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.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;
|
||||||
|
|
||||||
@@ -29,8 +28,42 @@ public class BukkitJSONSerializer {
|
|||||||
public static final String TYPE_KEY = ConfigurationSerialization.SERIALIZED_TYPE_KEY;
|
public static final String TYPE_KEY = ConfigurationSerialization.SERIALIZED_TYPE_KEY;
|
||||||
public static final BukkitJSONSerializer INSTANCE = BukkitJSONSerializer.create();
|
public static final BukkitJSONSerializer INSTANCE = BukkitJSONSerializer.create();
|
||||||
|
|
||||||
|
|
||||||
public static @NotNull BukkitJSONSerializer create() {
|
public static @NotNull BukkitJSONSerializer create() {
|
||||||
return create(new GsonBuilder().disableHtmlEscaping().create(), new JsonParser());
|
return create(new GsonBuilder().enableComplexMapKeySerialization().serializeNulls().disableHtmlEscaping()
|
||||||
|
.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) {
|
||||||
|
return new BukkitJSONSerializer(gson, new JsonParser());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static @NotNull BukkitJSONSerializer create(@NotNull Gson gson, @NotNull JsonParser parser) {
|
public static @NotNull BukkitJSONSerializer create(@NotNull Gson gson, @NotNull JsonParser parser) {
|
||||||
@@ -170,5 +203,4 @@ public class BukkitJSONSerializer {
|
|||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user