1
mirror of https://github.com/CarmJos/BukkitJSONSerializer.git synced 2024-09-19 21:35:57 +00:00

feat(all): use instance instead of static methods.

This commit is contained in:
Carm Jos 2022-06-07 07:20:37 +08:00
parent a1d6485e4c
commit ea54bc7af7
4 changed files with 46 additions and 26 deletions

View File

@ -13,7 +13,13 @@ A JSON serialize/deserialize util for bukkit's ConfigurationSerialization.
### Basic usage ### Basic usage
We cloud use `BukkitJSONSerializer#serializeToJSON(ConfigurationSerializable)` to serialize a object to JSON. First, we should get the serializer instance or create a new one.
```java
BukkitJSONSerializer serializer=BukkitJSONSerializer.get();
```
Then, we cloud use `BukkitJSONSerializer#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);
@ -25,12 +31,14 @@ When we need to read the object, just use `BukkitJSONSerializer#deserializeSON(j
string. string.
```java ```java
Location deserialized=BukkitJSONSerializer.deserializeJSON(serialized,Location.class); 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}
``` ```
Or use `BukkitJSONSerializer#deserializeSON(json,typeClass,defaultValue)` if we need a default value. Or use `BukkitJSONSerializer#deserializeSON(json,typeClass,defaultValue)` if we need a default value.
### JSONSerializable class ### JSONSerializable class
This project provided an interface `JSONSerializable` which provided a default method to serialize itself to JSON. This project provided an interface `JSONSerializable` which provided a default method to serialize itself to JSON.
```java ```java

View File

@ -26,17 +26,27 @@ import java.util.Optional;
*/ */
public class BukkitJSONSerializer { public class BukkitJSONSerializer {
protected 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();
protected static Gson gson = new GsonBuilder().disableHtmlEscaping().create(); public static @NotNull BukkitJSONSerializer create() {
protected static JsonParser parser = new JsonParser(); return create(new GsonBuilder().disableHtmlEscaping().create(), new JsonParser());
public static void setGson(Gson gson) {
BukkitJSONSerializer.gson = gson;
} }
public static void setParser(JsonParser parser) { public static @NotNull BukkitJSONSerializer create(@NotNull Gson gson, @NotNull JsonParser parser) {
BukkitJSONSerializer.parser = parser; return new BukkitJSONSerializer(gson, parser);
}
public static @NotNull BukkitJSONSerializer get() {
return BukkitJSONSerializer.INSTANCE;
}
protected final @NotNull Gson gson;
protected final @NotNull JsonParser parser;
public BukkitJSONSerializer(@NotNull Gson gson, @NotNull JsonParser parser) {
this.gson = gson;
this.parser = parser;
} }
/** /**
@ -46,7 +56,7 @@ public class BukkitJSONSerializer {
* @param <T> {@link ConfigurationSerializable} object type. * @param <T> {@link ConfigurationSerializable} object type.
* @return Map containing serialized data * @return Map containing serialized data
*/ */
public static <T extends ConfigurationSerializable> Map<String, Object> serializeToMap(T value) { public <T extends ConfigurationSerializable> Map<String, Object> serializeToMap(T value) {
Map<String, Object> values = new LinkedHashMap<>(); Map<String, Object> values = new LinkedHashMap<>();
// First, put tye type key; // First, put tye type key;
values.put(TYPE_KEY, ConfigurationSerialization.getAlias(value.getClass())); values.put(TYPE_KEY, ConfigurationSerialization.getAlias(value.getClass()));
@ -68,7 +78,7 @@ public class BukkitJSONSerializer {
* @param <T> {@link ConfigurationSerializable} object type. * @param <T> {@link ConfigurationSerializable} object type.
* @return JSON string containing serialized data. * @return JSON string containing serialized data.
*/ */
public static <T extends ConfigurationSerializable> String serializeToJSON(T value) { public <T extends ConfigurationSerializable> String serializeToJSON(T value) {
return gson.toJson(serializeToMap(value)); return gson.toJson(serializeToMap(value));
} }
@ -79,7 +89,7 @@ public class BukkitJSONSerializer {
* @return Deserialized object. * @return Deserialized object.
*/ */
@Contract("null->null") @Contract("null->null")
public static Object deserializeJSON(@Nullable String json) { public Object deserializeJSON(@Nullable String json) {
return deserializeJSON(json, (ConfigurationSerializable) null); return deserializeJSON(json, (ConfigurationSerializable) null);
} }
@ -91,8 +101,8 @@ public class BukkitJSONSerializer {
* @return Deserialized object. * @return Deserialized object.
*/ */
@Contract("_,!null->!null; null,null->null") @Contract("_,!null->!null; null,null->null")
public static ConfigurationSerializable deserializeJSON(@Nullable String json, public ConfigurationSerializable deserializeJSON(@Nullable String json,
@Nullable ConfigurationSerializable defaultValue) { @Nullable ConfigurationSerializable defaultValue) {
if (json == null) return defaultValue; if (json == null) return defaultValue;
Map<String, Object> args = jsonToMap(json); Map<String, Object> args = jsonToMap(json);
@ -110,8 +120,8 @@ public class BukkitJSONSerializer {
* @param <T> {@link ConfigurationSerializable} object type. * @param <T> {@link ConfigurationSerializable} object type.
* @return Deserialized object. * @return Deserialized object.
*/ */
public static <T extends ConfigurationSerializable> @Nullable T deserializeJSON(@Nullable String json, public <T extends ConfigurationSerializable> @Nullable T deserializeJSON(@Nullable String json,
@NotNull Class<T> typeClazz) { @NotNull Class<T> typeClazz) {
return deserializeJSON(json, typeClazz, null); return deserializeJSON(json, typeClazz, null);
} }
@ -125,24 +135,24 @@ public class BukkitJSONSerializer {
* @return Deserialized object. * @return Deserialized object.
*/ */
@Contract("_,_,!null->!null; null,_,null->null") @Contract("_,_,!null->!null; null,_,null->null")
public static <T extends ConfigurationSerializable> T deserializeJSON(@Nullable String json, public <T extends ConfigurationSerializable> T deserializeJSON(@Nullable String json,
@NotNull Class<T> typeClazz, @NotNull Class<T> typeClazz,
@Nullable T defaultValue) { @Nullable T defaultValue) {
Object value = deserializeJSON(json, defaultValue); Object value = deserializeJSON(json, defaultValue);
if (!typeClazz.isInstance(value)) return defaultValue; if (!typeClazz.isInstance(value)) return defaultValue;
return typeClazz.cast(value); return typeClazz.cast(value);
} }
protected static Map<String, Object> jsonToMap(String json) { protected Map<String, Object> jsonToMap(String json) {
return jsonToMap(parser.parse(json).getAsJsonObject()); return jsonToMap(parser.parse(json).getAsJsonObject());
} }
protected static Map<String, Object> jsonToMap(JsonObject object) { protected Map<String, Object> jsonToMap(JsonObject object) {
return parseMap(gson.fromJson(object, Map.class)); return parseMap(gson.fromJson(object, Map.class));
} }
protected static Map<String, Object> parseMap(Map<?, ?> map) { protected Map<String, Object> parseMap(Map<?, ?> map) {
Map<String, Object> args = new LinkedHashMap<>(); Map<String, Object> args = new LinkedHashMap<>();
map.forEach((k, v) -> { map.forEach((k, v) -> {
String key = (String) k; String key = (String) k;

View File

@ -11,7 +11,7 @@ public interface JSONSerializable extends ConfigurationSerializable {
* @return JSON string contains serialized data. * @return JSON string contains serialized data.
*/ */
default @NotNull String serializeToJSON() { default @NotNull String serializeToJSON() {
return BukkitJSONSerializer.serializeToJSON(this); return BukkitJSONSerializer.get().serializeToJSON(this);
} }
} }

View File

@ -10,13 +10,15 @@ import java.util.UUID;
public class demo { public class demo {
public static void demoUsage() { public static void demoUsage() {
BukkitJSONSerializer serializer = BukkitJSONSerializer.create();
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} // serialized -> {"world":"world","x":-100.5,"y":100,"z":105.5,"yaw":0.0,"pitch":0.0}
Location deserialized = BukkitJSONSerializer.deserializeJSON(serialized, Location.class); Location deserialized = serializer.deserializeJSON(serialized, Location.class);
// deserialized -> Location{world=world, x=-100.5, y=100, z=105.5, pitch=0.0, yaw=0.0}
} }