Files
hutool/hutool-json/src/main/java/cn/hutool/json/convert/JSONDeserializerConverter.java
2022-09-08 01:46:07 +08:00

34 lines
1.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cn.hutool.json.convert;
import cn.hutool.core.convert.AbstractConverter;
import cn.hutool.core.convert.ConvertException;
import cn.hutool.core.reflect.ConstructorUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.serialize.JSONDeserializer;
/**
* 实现了{@link JSONDeserializer}接口的Bean对象转换器用于将指定JSON转换为JSONDeserializer子对象。
*
* @author looly
* @since 6.0.0
*/
public class JSONDeserializerConverter extends AbstractConverter {
private static final long serialVersionUID = 1L;
public static final JSONDeserializerConverter INSTANCE = new JSONDeserializerConverter();
@Override
protected Object convertInternal(final Class<?> targetClass, final Object value) {
// 自定义反序列化
if (value instanceof JSON) {
final JSONDeserializer<?> target = (JSONDeserializer<?>) ConstructorUtil.newInstanceIfPossible(targetClass);
if (null == target) {
throw new ConvertException("Can not instance target: [{}]", targetClass);
}
return target.deserialize((JSON) value);
}
throw new ConvertException("JSONDeserializer bean must be convert from JSON!");
}
}