mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add JSONBeanParser
This commit is contained in:
18
hutool-json/src/main/java/cn/hutool/json/JSONBeanParser.java
Executable file
18
hutool-json/src/main/java/cn/hutool/json/JSONBeanParser.java
Executable file
@@ -0,0 +1,18 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
/**
|
||||
* 实现此接口的类可以通过实现{@code parse(value)}方法来将JSON中的值解析为此对象的值
|
||||
*
|
||||
* @author Looly
|
||||
* @since 5.7.8
|
||||
*/
|
||||
public interface JSONBeanParser<T> {
|
||||
|
||||
/**
|
||||
* value转Bean<br>
|
||||
* 通过实现此接口,将JSON中的值填充到当前对象的字段值中,即对象自行实现JSON反序列化逻辑
|
||||
*
|
||||
* @param value 被解析的对象类型,可能为JSON或者普通String、Number等
|
||||
*/
|
||||
void parse(T value);
|
||||
}
|
@@ -6,6 +6,7 @@ import cn.hutool.core.convert.Converter;
|
||||
import cn.hutool.core.convert.ConverterRegistry;
|
||||
import cn.hutool.core.convert.impl.ArrayConverter;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.core.util.TypeUtil;
|
||||
import cn.hutool.json.serialize.GlobalSerializeMapping;
|
||||
@@ -16,7 +17,7 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* JSON转换器
|
||||
*
|
||||
*
|
||||
* @author looly
|
||||
* @since 4.2.2
|
||||
*/
|
||||
@@ -32,7 +33,7 @@ public class JSONConverter implements Converter<JSON> {
|
||||
|
||||
/**
|
||||
* JSONArray转数组
|
||||
*
|
||||
*
|
||||
* @param jsonArray JSONArray
|
||||
* @param arrayClass 数组元素类型
|
||||
* @return 数组对象
|
||||
@@ -43,7 +44,7 @@ public class JSONConverter implements Converter<JSON> {
|
||||
|
||||
/**
|
||||
* 将JSONArray转换为指定类型的对量列表
|
||||
*
|
||||
*
|
||||
* @param <T> 元素类型
|
||||
* @param jsonArray JSONArray
|
||||
* @param elementType 对象元素类型
|
||||
@@ -69,7 +70,21 @@ public class JSONConverter implements Converter<JSON> {
|
||||
if (JSONUtil.isNull(value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
// since 5.7.8,增加自定义Bean反序列化接口
|
||||
if(targetType instanceof Class){
|
||||
final Class<?> clazz = (Class<?>) targetType;
|
||||
if (JSONBeanParser.class.isAssignableFrom(clazz)){
|
||||
@SuppressWarnings("rawtypes")
|
||||
JSONBeanParser target = (JSONBeanParser) ReflectUtil.newInstanceIfPossible(clazz);
|
||||
if(null == target){
|
||||
throw new ConvertException("Can not instance [{}]", targetType);
|
||||
}
|
||||
target.parse(value);
|
||||
return (T) target;
|
||||
}
|
||||
}
|
||||
|
||||
if(value instanceof JSON) {
|
||||
final JSONDeserializer<?> deserializer = GlobalSerializeMapping.getDeserializer(targetType);
|
||||
if(null != deserializer) {
|
||||
@@ -85,7 +100,7 @@ public class JSONConverter implements Converter<JSON> {
|
||||
// 此处特殊处理,认为返回null属于正常情况
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
throw new ConvertException("Can not convert {} to type {}", value, ObjectUtil.defaultIfNull(TypeUtil.getClass(targetType), targetType));
|
||||
}
|
||||
|
||||
|
@@ -3,7 +3,7 @@ package cn.hutool.json;
|
||||
/**
|
||||
* {@code JSONString}接口定义了一个{@code toJSONString()}<br>
|
||||
* 实现此接口的类可以通过实现{@code toJSONString()}方法来改变转JSON字符串的方式。
|
||||
*
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
@@ -11,7 +11,7 @@ public interface JSONString {
|
||||
|
||||
/**
|
||||
* 自定义转JSON字符串的方法
|
||||
*
|
||||
*
|
||||
* @return JSON字符串
|
||||
*/
|
||||
String toJSONString();
|
||||
|
@@ -1,20 +1,33 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
|
||||
/**
|
||||
* JSON支持<br>
|
||||
* 继承此类实现实体类与JSON的相互转换
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class JSONSupport implements JSONString{
|
||||
|
||||
public class JSONSupport implements JSONString, JSONBeanParser<JSON> {
|
||||
|
||||
/**
|
||||
* JSON String转Bean
|
||||
*
|
||||
* @param jsonString JSON String
|
||||
*/
|
||||
public void parse(String jsonString){
|
||||
new JSONObject(jsonString).toBean(this.getClass());
|
||||
public void parse(String jsonString) {
|
||||
parse(new JSONObject(jsonString));
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON String转Bean
|
||||
*
|
||||
* @param json JSON String
|
||||
*/
|
||||
@Override
|
||||
public void parse(JSON json) {
|
||||
final JSONSupport support = json.toBean(this.getClass());
|
||||
BeanUtil.copyProperties(support, this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -23,7 +36,7 @@ public class JSONSupport implements JSONString{
|
||||
public JSONObject toJSON() {
|
||||
return new JSONObject(this);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toJSONString() {
|
||||
return toJSON().toString();
|
||||
@@ -31,7 +44,7 @@ public class JSONSupport implements JSONString{
|
||||
|
||||
/**
|
||||
* 美化的JSON(使用回车缩进显示JSON),用于打印输出debug
|
||||
*
|
||||
*
|
||||
* @return 美化的JSON
|
||||
*/
|
||||
public String toPrettyString() {
|
||||
|
Reference in New Issue
Block a user