mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-19 07:26:57 +00:00
ref[storage]: list convert
This commit is contained in:
@@ -20,7 +20,6 @@ import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.converter.ConditionalGenericConverter;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -49,16 +48,10 @@ public class JsonToArrayConverter implements ConditionalGenericConverter {
|
||||
}
|
||||
// 如果为json格式,则以json格式解析
|
||||
if (content.startsWith("[") || content.endsWith("]")) {
|
||||
return JsonUtils.string2Object(content, targetType.getType());
|
||||
return componentType.isPrimitive()
|
||||
? JsonUtils.string2Object(content, targetType.getObjectType())
|
||||
: JsonUtils.string2Array(content, componentType);
|
||||
}
|
||||
// 用普通的逗号分隔符解析
|
||||
var splits = content.split(StringUtils.COMMA_REGEX);
|
||||
var length = splits.length;
|
||||
Object array = Array.newInstance(componentType, length);
|
||||
for (var i = 0; i < length; i++) {
|
||||
Object value = ConvertUtils.convert(splits[i], componentType);
|
||||
Array.set(array, i, value);
|
||||
}
|
||||
return array;
|
||||
return ConvertUtils.convertToArray(content, componentType);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
package com.zfoo.storage.strategy;
|
||||
|
||||
import com.zfoo.protocol.util.JsonUtils;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
import com.zfoo.storage.util.ConvertUtils;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.converter.ConditionalGenericConverter;
|
||||
import org.springframework.lang.NonNull;
|
||||
@@ -42,19 +44,20 @@ public class JsonToListConverter implements ConditionalGenericConverter {
|
||||
|
||||
@Override
|
||||
public Object convert(Object source, @NonNull TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
// String content = (String) source;
|
||||
// return targetType.isPrimitive() ? JsonUtil.string2Object(content, targetType.getObjectType())
|
||||
// : JsonUtil.string2Array(content, targetType.getType());
|
||||
var content = StringUtils.trim((String) source);
|
||||
if (StringUtils.isEmpty(content)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
Class<?> clazz = null;
|
||||
|
||||
String content = (String) source;
|
||||
Type type = targetType.getResolvableType().getGeneric(0).getType();
|
||||
if (type instanceof Class) {
|
||||
clazz = (Class<?>) type;
|
||||
} else if (type instanceof ParameterizedType parameterizedType) {
|
||||
clazz = (Class<?>) parameterizedType.getRawType();
|
||||
}
|
||||
|
||||
return Collections.unmodifiableList(JsonUtils.string2List(content, clazz));
|
||||
if (content.startsWith("[") || content.endsWith("]")) {
|
||||
return Collections.unmodifiableList(JsonUtils.string2List(content, clazz));
|
||||
}
|
||||
return Collections.unmodifiableList(ConvertUtils.convertToList(content, clazz));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,12 +12,17 @@
|
||||
*/
|
||||
package com.zfoo.storage.util;
|
||||
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
import com.zfoo.storage.strategy.*;
|
||||
import org.springframework.context.support.ConversionServiceFactoryBean;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
@@ -51,4 +56,35 @@ public abstract class ConvertUtils {
|
||||
return conversionServiceFactoryBean.getObject().convert(content, TYPE_DESCRIPTOR, targetType);
|
||||
}
|
||||
|
||||
public static Object convertToArray(String content, Class<?> componentType) {
|
||||
content = StringUtils.trim(content);
|
||||
// null safe,content为空则返回长度为0的数组
|
||||
if (StringUtils.isEmpty(content)) {
|
||||
return Array.newInstance(componentType, 0);
|
||||
}
|
||||
// 用普通的逗号分隔符解析
|
||||
var splits = content.split(StringUtils.COMMA_REGEX);
|
||||
var length = splits.length;
|
||||
Object array = Array.newInstance(componentType, length);
|
||||
for (var i = 0; i < length; i++) {
|
||||
Object value = ConvertUtils.convert(StringUtils.trim(splits[i]), componentType);
|
||||
Array.set(array, i, value);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public static <T> List<T> convertToList(String content, Class<T> type) {
|
||||
content = StringUtils.trim(content);
|
||||
if (StringUtils.isEmpty(content)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
var splits = content.split(StringUtils.COMMA_REGEX);
|
||||
var length = splits.length;
|
||||
var list = new ArrayList<T>();
|
||||
for (var i = 0; i < length; i++) {
|
||||
var value = ConvertUtils.convert(StringUtils.trim(splits[i]), type);
|
||||
list.add(value);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user