Skip to content

Commit

Permalink
✨ Feat: Add method to convert objects to JsonPrimitive.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Core2002 committed Aug 2, 2024
1 parent 0aca7c3 commit d97c7fb
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -155,6 +156,34 @@ public void setValue(String fileName, String key, JsonElement value) {
save(fileName);
}

/**
* <p> zh </p>
* 将给定对象转换为JsonPrimitive对象,支持的数据类型包括Boolean、Number、String和Character。
* 如果传入的对象类型不在此范围内,将抛出IllegalArgumentException异常。
*
* <p> en </p>
* 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());
}
}

/**
* <p> zh </p>
* 保存所有配置文件
Expand Down

0 comments on commit d97c7fb

Please sign in to comment.