mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-15 16:15:27 +00:00
ref[storage]: list convert
This commit is contained in:
1 parent
db0d91e002
commit
9d069cd3da
3 files changed
+50
-18
No files matched your search
@@ -20,7 +20,6 @@ import org.springframework.core.convert.TypeDescriptor;
|
|||||||
import org.springframework.core.convert.converter.ConditionalGenericConverter;
|
import org.springframework.core.convert.converter.ConditionalGenericConverter;
|
||||||
|
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -49,16 +48,10 @@ public class JsonToArrayConverter implements ConditionalGenericConverter {
|
|||||||
}
|
}
|
||||||
// 如果为json格式,则以json格式解析
|
// 如果为json格式,则以json格式解析
|
||||||
if (content.startsWith("[") || content.endsWith("]")) {
|
if (content.startsWith("[") || content.endsWith("]")) {
|
||||||
return JsonUtils.string2Object(content, targetType.getType());
|
return componentType.isPrimitive()
|
||||||
|
? JsonUtils.string2Object(content, targetType.getObjectType())
|
||||||
|
: JsonUtils.string2Array(content, componentType);
|
||||||
}
|
}
|
||||||
// 用普通的逗号分隔符解析
|
return ConvertUtils.convertToArray(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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -14,6 +14,8 @@
|
|||||||
package com.zfoo.storage.strategy;
|
package com.zfoo.storage.strategy;
|
||||||
|
|
||||||
import com.zfoo.protocol.util.JsonUtils;
|
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.TypeDescriptor;
|
||||||
import org.springframework.core.convert.converter.ConditionalGenericConverter;
|
import org.springframework.core.convert.converter.ConditionalGenericConverter;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
@@ -42,19 +44,20 @@ public class JsonToListConverter implements ConditionalGenericConverter {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object convert(Object source, @NonNull TypeDescriptor sourceType, TypeDescriptor targetType) {
|
public Object convert(Object source, @NonNull TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||||
// String content = (String) source;
|
var content = StringUtils.trim((String) source);
|
||||||
// return targetType.isPrimitive() ? JsonUtil.string2Object(content, targetType.getObjectType())
|
if (StringUtils.isEmpty(content)) {
|
||||||
// : JsonUtil.string2Array(content, targetType.getType());
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
Class<?> clazz = null;
|
Class<?> clazz = null;
|
||||||
|
|
||||||
String content = (String) source;
|
|
||||||
Type type = targetType.getResolvableType().getGeneric(0).getType();
|
Type type = targetType.getResolvableType().getGeneric(0).getType();
|
||||||
if (type instanceof Class) {
|
if (type instanceof Class) {
|
||||||
clazz = (Class<?>) type;
|
clazz = (Class<?>) type;
|
||||||
} else if (type instanceof ParameterizedType parameterizedType) {
|
} else if (type instanceof ParameterizedType parameterizedType) {
|
||||||
clazz = (Class<?>) parameterizedType.getRawType();
|
clazz = (Class<?>) parameterizedType.getRawType();
|
||||||
}
|
}
|
||||||
|
if (content.startsWith("[") || content.endsWith("]")) {
|
||||||
return Collections.unmodifiableList(JsonUtils.string2List(content, clazz));
|
return Collections.unmodifiableList(JsonUtils.string2List(content, clazz));
|
||||||
|
}
|
||||||
|
return Collections.unmodifiableList(ConvertUtils.convertToList(content, clazz));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,12 +12,17 @@
|
|||||||
*/
|
*/
|
||||||
package com.zfoo.storage.util;
|
package com.zfoo.storage.util;
|
||||||
|
|
||||||
|
import com.zfoo.protocol.util.StringUtils;
|
||||||
import com.zfoo.storage.strategy.*;
|
import com.zfoo.storage.strategy.*;
|
||||||
import org.springframework.context.support.ConversionServiceFactoryBean;
|
import org.springframework.context.support.ConversionServiceFactoryBean;
|
||||||
import org.springframework.core.convert.TypeDescriptor;
|
import org.springframework.core.convert.TypeDescriptor;
|
||||||
|
|
||||||
|
import java.lang.reflect.Array;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author godotg
|
* @author godotg
|
||||||
@@ -51,4 +56,35 @@ public abstract class ConvertUtils {
|
|||||||
return conversionServiceFactoryBean.getObject().convert(content, TYPE_DESCRIPTOR, targetType);
|
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