优化JsonArray

This commit is contained in:
redkale
2024-02-20 11:38:42 +08:00
parent f5c412c286
commit 2b6e44015b
3 changed files with 12 additions and 1 deletions

View File

@@ -3,6 +3,7 @@
*/
package org.redkale.convert.json;
import java.lang.reflect.Type;
import java.math.*;
import java.util.*;
import org.redkale.annotation.Nullable;
@@ -47,6 +48,11 @@ public class JsonArray extends ArrayList<Object> implements JsonElement {
return new JsonArray();
}
public <T> List<T> toList(Type componentType) {
Type listType = TypeToken.createParameterizedType(null, ArrayList.class, componentType);
return (List) JsonConvert.root().convertFrom(listType, JsonConvert.root().convertTo(this));
}
public JsonArray append(Object value) {
super.add(value);
return this;

View File

@@ -3,6 +3,7 @@
*/
package org.redkale.convert.json;
import java.lang.reflect.Type;
import java.math.*;
import java.util.*;
import org.redkale.annotation.Nullable;
@@ -56,6 +57,10 @@ public class JsonObject extends LinkedHashMap<String, Object> implements JsonEle
return convertFrom(JsonConvert.root().convertTo(bean));
}
public <T> T toObject(Type type) {
return (T) JsonConvert.root().convertFrom(type, JsonConvert.root().convertTo(this));
}
public JsonObject append(String key, Object value) {
super.put(key, value);
return this;

View File

@@ -568,7 +568,7 @@ public abstract class TypeToken<T> {
/**
* 动态创建 ParameterizedType
*
* @param ownerType0 ParameterizedType 的 ownerType
* @param ownerType0 ParameterizedType 的 ownerType, 一般为null
* @param rawType0 ParameterizedType 的 rawType
* @param actualTypeArguments0 ParameterizedType 的 actualTypeArguments
*