diff --git a/storage/src/main/java/com/zfoo/storage/strategy/JsonToArrayConverter.java b/storage/src/main/java/com/zfoo/storage/strategy/JsonToArrayConverter.java index c9743500a..c9a6f62ea 100644 --- a/storage/src/main/java/com/zfoo/storage/strategy/JsonToArrayConverter.java +++ b/storage/src/main/java/com/zfoo/storage/strategy/JsonToArrayConverter.java @@ -20,7 +20,6 @@ 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 Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t } // 如果为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); } } diff --git a/storage/src/main/java/com/zfoo/storage/strategy/JsonToListConverter.java b/storage/src/main/java/com/zfoo/storage/strategy/JsonToListConverter.java index 9be8ea057..99942bf4f 100644 --- a/storage/src/main/java/com/zfoo/storage/strategy/JsonToListConverter.java +++ b/storage/src/main/java/com/zfoo/storage/strategy/JsonToListConverter.java @@ -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 Set getConvertibleTypes() { @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)); } } diff --git a/storage/src/main/java/com/zfoo/storage/util/ConvertUtils.java b/storage/src/main/java/com/zfoo/storage/util/ConvertUtils.java index eac806387..bb445c2e7 100644 --- a/storage/src/main/java/com/zfoo/storage/util/ConvertUtils.java +++ b/storage/src/main/java/com/zfoo/storage/util/ConvertUtils.java @@ -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 static Object convertField(String content, Field field) { 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 List convertToList(String content, Class 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(); + for (var i = 0; i < length; i++) { + var value = ConvertUtils.convert(StringUtils.trim(splits[i]), type); + list.add(value); + } + return list; + } }