
com.sghd.common.utils.converter.JsonToObjectConverter Maven / Gradle / Ivy
The newest version!
package com.sghd.common.utils.converter;
import java.util.Collections;
import java.util.Set;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import com.sghd.common.utils.json.JsonObject;
import com.sghd.common.utils.json.JsonUtils;
/**
* 将json格式的array字符串转换成对应的数组实例
* @author frank
*/
public class JsonToObjectConverter implements ConditionalGenericConverter {
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
if (sourceType.getType() != String.class) {
return false;
}
if (!JsonObject.class.isAssignableFrom(targetType.getType())) {
return false;
}
return true;
}
@Override
public Set getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(String.class, Object.class));
}
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
String content = (String) source;
if (targetType.isArray()) {
return JsonUtils.string2Array(content, targetType.getElementTypeDescriptor().getType());
} else {
return JsonUtils.string2Object(content, targetType.getObjectType());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy