From d97c7fb4a9e373f51f6a5c3b861aeb4c71847e3a Mon Sep 17 00:00:00 2001 From: NekokeCore Date: Fri, 2 Aug 2024 13:57:01 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Feat:=20Add=20method=20to=20convert?= =?UTF-8?q?=20objects=20to=20JsonPrimitive.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a new method `convertToJsonPrimitive` in `AlkaidFastConfigCenter` that converts given objects to `JsonPrimitive`. This method supports Boolean, Number, String, andCharacter types. If the object's type is not supported, it throws an IllegalArgumentException. This feature simplifies working with JSON primitives in plugin configurations. It ensures that the caller is decoupled from the GSON library. --- .../config/gson/AlkaidFastConfigCenter.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/alkaid-bukkit/src/main/java/com/alkaidmc/alkaid/bukkit/config/gson/AlkaidFastConfigCenter.java b/alkaid-bukkit/src/main/java/com/alkaidmc/alkaid/bukkit/config/gson/AlkaidFastConfigCenter.java index 0bda048..ac34a69 100644 --- a/alkaid-bukkit/src/main/java/com/alkaidmc/alkaid/bukkit/config/gson/AlkaidFastConfigCenter.java +++ b/alkaid-bukkit/src/main/java/com/alkaidmc/alkaid/bukkit/config/gson/AlkaidFastConfigCenter.java @@ -19,6 +19,7 @@ import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; @@ -155,6 +156,34 @@ public void setValue(String fileName, String key, JsonElement value) { save(fileName); } + /** + *

zh

+ * 将给定对象转换为JsonPrimitive对象,支持的数据类型包括Boolean、Number、String和Character。 + * 如果传入的对象类型不在此范围内,将抛出IllegalArgumentException异常。 + * + *

en

+ * Converts the given object to a JsonPrimitive instance, supporting types such as Boolean, + * Number, String, and Character. An IllegalArgumentException is thrown if the object's type + * does not fall within these supported categories. + * + * @param value 待转换的对象 / the object to be converted + * @return 转换后的JsonPrimitive对象 / the resulting JsonPrimitive instance + * @throws IllegalArgumentException 如果value的类型不受支持 / if the type of value is unsupported + */ + public JsonPrimitive convertToJsonPrimitive(Object value) { + if (value instanceof Boolean) { + return new JsonPrimitive((Boolean) value); + } else if (value instanceof Number) { + return new JsonPrimitive((Number) value); + } else if (value instanceof String) { + return new JsonPrimitive((String) value); + } else if (value instanceof Character) { + return new JsonPrimitive((Character) value); + } else { + throw new IllegalArgumentException("Unsupported type: " + value.getClass().getName()); + } + } + /** *

zh

* 保存所有配置文件