更换至 Gradle
This commit is contained in:
350
src/main/scala/me/skymc/taboolib/common/json/TJsonArray.java
Normal file
350
src/main/scala/me/skymc/taboolib/common/json/TJsonArray.java
Normal file
@@ -0,0 +1,350 @@
|
||||
package me.skymc.taboolib.common.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Spliterator;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* 让 JsonObject 有更加人性化的操作,目前版本只能获取数据。
|
||||
*
|
||||
* @Author 坏黑
|
||||
* @Since 2018-10-27 23:46
|
||||
*/
|
||||
public class TJsonArray implements Iterable<TJsonObject> {
|
||||
|
||||
private JsonArray jsonArray;
|
||||
|
||||
TJsonArray(JsonArray jsonArray) {
|
||||
this.jsonArray = jsonArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 Json 原代码中获取
|
||||
*
|
||||
* @param json 源代码
|
||||
* @return {@link TJsonArray}
|
||||
* @throws JsonParseException if the specified text is not valid JSON
|
||||
* @throws IllegalStateException This is not a JSON Array.
|
||||
*/
|
||||
public static TJsonArray fromJson(String json) throws JsonParseException, IllegalStateException {
|
||||
return new TJsonArray(new JsonParser().parse(json).getAsJsonArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 JsonArray 创建
|
||||
*
|
||||
* @param jsonArray JsonArray 对象
|
||||
* @return {@link TJsonObject}
|
||||
*/
|
||||
public static TJsonArray fromJsonArray(JsonArray jsonArray) {
|
||||
return new TJsonArray(jsonArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加成员
|
||||
*
|
||||
* @param obj 成员
|
||||
*/
|
||||
public void add(TJsonObject obj) {
|
||||
jsonArray.add(obj.asOriginJsonElement());
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加成员
|
||||
*
|
||||
* @param obj 成员
|
||||
*/
|
||||
public void add(Boolean obj) {
|
||||
jsonArray.add(new JsonPrimitive(obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加成员
|
||||
*
|
||||
* @param obj 成员
|
||||
*/
|
||||
public void add(Character obj) {
|
||||
jsonArray.add(new JsonPrimitive(obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加成员
|
||||
*
|
||||
* @param obj 成员
|
||||
*/
|
||||
public void add(Number obj) {
|
||||
jsonArray.add(new JsonPrimitive(obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加成员
|
||||
*
|
||||
* @param obj 成员
|
||||
*/
|
||||
public void add(String obj) {
|
||||
jsonArray.add(new JsonPrimitive(obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加所有成员
|
||||
*
|
||||
* @param array 成员集合
|
||||
*/
|
||||
public void addAll(TJsonArray array) {
|
||||
jsonArray.add(array.asOriginJsonArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置成员
|
||||
*
|
||||
* @param index 位置
|
||||
* @param obj 成员
|
||||
*/
|
||||
public void set(int index, TJsonObject obj) {
|
||||
jsonArray.set(index, obj.asOriginJsonElement());
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置成员
|
||||
*
|
||||
* @param index 位置
|
||||
* @param obj 成员
|
||||
*/
|
||||
public void set(int index, Boolean obj) {
|
||||
jsonArray.set(index, new JsonPrimitive(obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置成员
|
||||
*
|
||||
* @param index 位置
|
||||
* @param obj 成员
|
||||
*/
|
||||
public void set(int index, Number obj) {
|
||||
jsonArray.set(index, new JsonPrimitive(obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置成员
|
||||
*
|
||||
* @param index 位置
|
||||
* @param obj 成员
|
||||
*/
|
||||
public void set(int index, String obj) {
|
||||
jsonArray.set(index, new JsonPrimitive(obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除成员
|
||||
*
|
||||
* @param obj 成员
|
||||
*/
|
||||
public void remove(JsonElement obj) {
|
||||
jsonArray.remove(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除成员
|
||||
*
|
||||
* @param obj 成员
|
||||
*/
|
||||
public void remove(Boolean obj) {
|
||||
jsonArray.remove(new JsonPrimitive(obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除成员
|
||||
*
|
||||
* @param obj 成员
|
||||
*/
|
||||
public void remove(Number obj) {
|
||||
jsonArray.remove(new JsonPrimitive(obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除成员
|
||||
*
|
||||
* @param obj 成员
|
||||
*/
|
||||
public void remove(String obj) {
|
||||
jsonArray.remove(new JsonPrimitive(obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* 含有成员
|
||||
*
|
||||
* @param obj 成员
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean contains(JsonElement obj) {
|
||||
return jsonArray.contains(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* 含有成员
|
||||
*
|
||||
* @param obj 成员
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean contains(Boolean obj) {
|
||||
return jsonArray.contains(new JsonPrimitive(obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* 含有成员
|
||||
*
|
||||
* @param obj 成员
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean contains(Number obj) {
|
||||
return jsonArray.contains(new JsonPrimitive(obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* 含有成员
|
||||
*
|
||||
* @param obj 成员
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean contains(String obj) {
|
||||
return jsonArray.contains(new JsonPrimitive(obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取成员,默认值:null
|
||||
*
|
||||
* @param index 序号
|
||||
* @return {@link TJsonObject}
|
||||
*/
|
||||
public TJsonObject getJsonObject(int index) {
|
||||
return jsonArray.get(index).isJsonObject() ? TJsonObject.fromJsonObject(jsonArray.get(index).getAsJsonObject()) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取成员,默认值:null
|
||||
*
|
||||
* @param index 序号
|
||||
* @return {@link TJsonArray}
|
||||
*/
|
||||
public TJsonArray getJsonArray(int index) {
|
||||
return jsonArray.get(index).isJsonArray() ? TJsonArray.fromJsonArray(jsonArray.get(index).getAsJsonArray()) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取成员,默认值:false
|
||||
*
|
||||
* @param index 序号
|
||||
* @return boolean
|
||||
*/
|
||||
public Boolean getBoolean(int index) {
|
||||
return jsonArray.get(index).isJsonPrimitive() && jsonArray.get(index).getAsBoolean();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取成员
|
||||
*
|
||||
* @param index 序号
|
||||
* @param def 默认值
|
||||
* @return boolean
|
||||
*/
|
||||
public Boolean getBoolean(int index, boolean def) {
|
||||
return jsonArray.get(index).isJsonPrimitive() ? jsonArray.get(index).getAsBoolean() : def;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取成员,默认值:0
|
||||
*
|
||||
* @param index 序号
|
||||
* @return number
|
||||
*/
|
||||
public Number getNumber(int index) {
|
||||
return jsonArray.get(index).isJsonPrimitive() ? jsonArray.get(index).getAsNumber() : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取成员
|
||||
*
|
||||
* @param index 序号
|
||||
* @param def 默认值
|
||||
* @return number
|
||||
*/
|
||||
public Number getNumber(int index, Number def) {
|
||||
return jsonArray.get(index).isJsonPrimitive() ? jsonArray.get(index).getAsNumber() : def;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取成员,默认值:null
|
||||
*
|
||||
* @param index 序号
|
||||
* @return string
|
||||
*/
|
||||
public String getString(int index) {
|
||||
return jsonArray.get(index).isJsonPrimitive() ? jsonArray.get(index).getAsString() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取成员
|
||||
*
|
||||
* @param index 序号
|
||||
* @param def 默认值
|
||||
* @return string
|
||||
*/
|
||||
public String getString(int index, String def) {
|
||||
return jsonArray.get(index).isJsonPrimitive() ? jsonArray.get(index).getAsString() : def;
|
||||
}
|
||||
|
||||
/**
|
||||
* 成员数量
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public int size() {
|
||||
return jsonArray.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为 JsonArray 类型
|
||||
*
|
||||
* @return {@link JsonArray}
|
||||
*/
|
||||
public JsonArray asOriginJsonArray() {
|
||||
return jsonArray;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<TJsonObject> iterator() {
|
||||
List<TJsonObject> jsonObjectList = new ArrayList<>();
|
||||
for (JsonElement jsonElement : jsonArray) {
|
||||
jsonObjectList.add(TJsonObject.fromJsonObject(jsonElement));
|
||||
}
|
||||
return jsonObjectList.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(Consumer<? super TJsonObject> action) {
|
||||
List<TJsonObject> jsonObjectList = new ArrayList<>();
|
||||
for (JsonElement jsonElement : jsonArray) {
|
||||
jsonObjectList.add(TJsonObject.fromJsonObject(jsonElement));
|
||||
}
|
||||
jsonObjectList.forEach(action);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Spliterator<TJsonObject> spliterator() {
|
||||
List<TJsonObject> jsonObjectList = new ArrayList<>();
|
||||
for (JsonElement jsonElement : jsonArray) {
|
||||
jsonObjectList.add(TJsonObject.fromJsonObject(jsonElement));
|
||||
}
|
||||
return jsonObjectList.spliterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return jsonArray.toString();
|
||||
}
|
||||
|
||||
}
|
||||
253
src/main/scala/me/skymc/taboolib/common/json/TJsonObject.java
Normal file
253
src/main/scala/me/skymc/taboolib/common/json/TJsonObject.java
Normal file
@@ -0,0 +1,253 @@
|
||||
package me.skymc.taboolib.common.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 让 JsonObject 有更加人性化的操作,目前版本只能获取数据。
|
||||
*
|
||||
* @Author 坏黑
|
||||
* @Since 2018-10-27 23:06
|
||||
*/
|
||||
public class TJsonObject {
|
||||
|
||||
private JsonElement jsonObject;
|
||||
|
||||
TJsonObject(JsonElement jsonObject) {
|
||||
this.jsonObject = jsonObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 Json 原代码中获取
|
||||
*
|
||||
* @param json 源代码
|
||||
* @return {@link TJsonObject}
|
||||
* @throws JsonParseException if the specified text is not valid JSON
|
||||
*/
|
||||
public static TJsonObject fromJson(String json) throws JsonParseException {
|
||||
return new TJsonObject(new JsonParser().parse(json));
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 JsonElement 创建
|
||||
*
|
||||
* @param jsonElement JsonElement 对象
|
||||
* @return {@link TJsonObject}
|
||||
*/
|
||||
public static TJsonObject fromJsonObject(JsonElement jsonElement) {
|
||||
return new TJsonObject(jsonElement);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否含有该节点
|
||||
*
|
||||
* @param path 地址
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean contains(String path) {
|
||||
return get(path) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文本,默认值:空
|
||||
*
|
||||
* @param path 地址
|
||||
* @return String
|
||||
*/
|
||||
public String getString(String path) {
|
||||
return getString(path, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文本
|
||||
*
|
||||
* @param path 地址
|
||||
* @param def 默认值
|
||||
* @return String
|
||||
*/
|
||||
public String getString(String path, String def) {
|
||||
JsonElement jsonElement = get(path);
|
||||
return !(jsonElement instanceof JsonPrimitive) || jsonElement == null ? def : jsonElement.getAsString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数字,默认值:0
|
||||
*
|
||||
* @param path 地址
|
||||
* @return Number
|
||||
*/
|
||||
public Number getNumber(String path) {
|
||||
return getNumber(path, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数字
|
||||
*
|
||||
* @param path 地址
|
||||
* @param def 默认值
|
||||
* @return Number
|
||||
*/
|
||||
public Number getNumber(String path, Number def) {
|
||||
JsonElement jsonElement = get(path);
|
||||
return !(jsonElement instanceof JsonPrimitive) || jsonElement == null ? def : jsonElement.getAsNumber();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取布尔值,默认值:false
|
||||
*
|
||||
* @param path 地址
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean getBoolean(String path) {
|
||||
return getBoolean(path, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取布尔值
|
||||
*
|
||||
* @param path 地址
|
||||
* @param def 默认值
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean getBoolean(String path, boolean def) {
|
||||
JsonElement jsonElement = get(path);
|
||||
return !(jsonElement instanceof JsonPrimitive) || jsonElement == null ? def : jsonElement.getAsBoolean();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 TJsonObject 对象,默认值:null
|
||||
*
|
||||
* @param path 地址
|
||||
* @return {@link TJsonObject}
|
||||
*/
|
||||
public TJsonObject getJsonObject(String path) {
|
||||
JsonElement jsonElement = get(path);
|
||||
return !(jsonElement instanceof JsonObject) || jsonElement == null ? null : TJsonObject.fromJsonObject(jsonElement);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 TJsonArray 对象,默认值:null
|
||||
*
|
||||
* @param path 地址
|
||||
* @return {@link TJsonArray}
|
||||
*/
|
||||
public TJsonArray getJsonArray(String path) {
|
||||
JsonElement jsonElement = get(path);
|
||||
return !(jsonElement instanceof JsonArray) || jsonElement == null ? null : TJsonArray.fromJsonArray((JsonArray) jsonElement);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有成员
|
||||
*
|
||||
* @return {@link Map.Entry}
|
||||
*/
|
||||
public Set<Map.Entry<String, TJsonObject>> entrySet() {
|
||||
return !(jsonObject instanceof JsonObject) ? new HashSet<>() : ((JsonObject) jsonObject).entrySet().stream().map(jsonElementEntry -> new HashMap.SimpleEntry<>(jsonElementEntry.getKey(), fromJsonObject(jsonElementEntry.getValue()))).collect(Collectors.toCollection(HashSet::new));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有键
|
||||
*
|
||||
* @return {@link Set}
|
||||
*/
|
||||
public Set<String> keySet() {
|
||||
return !(jsonObject instanceof JsonObject) ? new HashSet<>() : ((JsonObject) jsonObject).entrySet().stream().map(Map.Entry::getKey).collect(Collectors.toCollection(HashSet::new));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有值
|
||||
*
|
||||
* @return {@link Collection}
|
||||
*/
|
||||
public Collection<TJsonObject> values() {
|
||||
return !(jsonObject instanceof JsonObject) ? new ArrayList<>() : ((JsonObject) jsonObject).entrySet().stream().map(jsonElementEntry -> new TJsonObject(jsonElementEntry.getValue())).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为 JsonObject 类型
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isJsonObject() {
|
||||
return jsonObject instanceof JsonObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为 JsonArray 类型
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isJsonArray() {
|
||||
return jsonObject instanceof JsonArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为 JsonPrimitive 类型
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isJsonPrimitive() {
|
||||
return jsonObject instanceof JsonPrimitive;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为 JsonObject 类型
|
||||
*
|
||||
* @return {@link JsonObject}
|
||||
*/
|
||||
public JsonObject asOriginJsonObject() {
|
||||
return (JsonObject) jsonObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为 JsonArray 类型
|
||||
*
|
||||
* @return {@link JsonArray}
|
||||
*/
|
||||
public JsonArray asOriginJsonArray() {
|
||||
return (JsonArray) jsonObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为 JsonElement 类型
|
||||
*
|
||||
* @return {@link JsonElement}
|
||||
*/
|
||||
public JsonElement asOriginJsonElement() {
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为 JsonPrimitive 类型
|
||||
*
|
||||
* @return {@link JsonPrimitive}
|
||||
*/
|
||||
public JsonPrimitive asOriginJsonPrimitive() {
|
||||
return (JsonPrimitive) jsonObject;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return jsonObject.toString();
|
||||
}
|
||||
|
||||
// *********************************
|
||||
//
|
||||
// Private Methods
|
||||
//
|
||||
// *********************************
|
||||
|
||||
private JsonElement get(String path) {
|
||||
JsonElement subElement = jsonObject;
|
||||
for (String p : path.split("/")) {
|
||||
if (subElement instanceof JsonObject && ((JsonObject) subElement).has(p)) {
|
||||
subElement = ((JsonObject) subElement).get(p);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return subElement;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user