mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix toBean
This commit is contained in:
@@ -1,17 +1,19 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import cn.hutool.core.bean.BeanPath;
|
||||
import cn.hutool.core.lang.TypeReference;
|
||||
|
||||
/**
|
||||
* JSON接口
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public interface JSON extends Cloneable, Serializable{
|
||||
public interface JSON extends Cloneable, Serializable {
|
||||
|
||||
/**
|
||||
* 通过表达式获取JSON中嵌套的对象<br>
|
||||
@@ -19,16 +21,16 @@ public interface JSON extends Cloneable, Serializable{
|
||||
* <li>.表达式,可以获取Bean对象中的属性(字段)值或者Map中key对应的值</li>
|
||||
* <li>[]表达式,可以获取集合等对象中对应index的值</li>
|
||||
* </ol>
|
||||
*
|
||||
* <p>
|
||||
* 表达式栗子:
|
||||
*
|
||||
*
|
||||
* <pre>
|
||||
* persion
|
||||
* persion.name
|
||||
* persons[3]
|
||||
* person.friends[5].name
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @param expression 表达式
|
||||
* @return 对象
|
||||
* @see BeanPath#get(Object)
|
||||
@@ -44,20 +46,20 @@ public interface JSON extends Cloneable, Serializable{
|
||||
* <li>.表达式,可以获取Bean对象中的属性(字段)值或者Map中key对应的值</li>
|
||||
* <li>[]表达式,可以获取集合等对象中对应index的值</li>
|
||||
* </ol>
|
||||
*
|
||||
* <p>
|
||||
* 表达式栗子:
|
||||
*
|
||||
*
|
||||
* <pre>
|
||||
* persion
|
||||
* persion.name
|
||||
* persons[3]
|
||||
* person.friends[5].name
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @param expression 表达式
|
||||
* @param value 值
|
||||
* @param value 值
|
||||
*/
|
||||
public void putByPath(String expression, Object value);
|
||||
void putByPath(String expression, Object value);
|
||||
|
||||
/**
|
||||
* 通过表达式获取JSON中嵌套的对象<br>
|
||||
@@ -65,19 +67,19 @@ public interface JSON extends Cloneable, Serializable{
|
||||
* <li>.表达式,可以获取Bean对象中的属性(字段)值或者Map中key对应的值</li>
|
||||
* <li>[]表达式,可以获取集合等对象中对应index的值</li>
|
||||
* </ol>
|
||||
*
|
||||
* <p>
|
||||
* 表达式栗子:
|
||||
*
|
||||
*
|
||||
* <pre>
|
||||
* persion
|
||||
* persion.name
|
||||
* persons[3]
|
||||
* person.friends[5].name
|
||||
* </pre>
|
||||
*
|
||||
* <p>
|
||||
* 获取表达式对应值后转换为对应类型的值
|
||||
*
|
||||
* @param <T> 返回值类型
|
||||
*
|
||||
* @param <T> 返回值类型
|
||||
* @param expression 表达式
|
||||
* @param resultType 返回值类型
|
||||
* @return 对象
|
||||
@@ -86,43 +88,100 @@ public interface JSON extends Cloneable, Serializable{
|
||||
*/
|
||||
<T> T getByPath(String expression, Class<T> resultType);
|
||||
|
||||
/**
|
||||
* 格式化打印JSON,缩进为4个空格
|
||||
*
|
||||
* @return 格式化后的JSON字符串
|
||||
* @throws JSONException 包含非法数抛出此异常
|
||||
* @since 3.0.9
|
||||
*/
|
||||
default String toStringPretty() throws JSONException {
|
||||
return this.toJSONString(4);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化输出JSON字符串
|
||||
*
|
||||
* @param indentFactor 每层缩进空格数
|
||||
* @return JSON字符串
|
||||
* @throws JSONException 包含非法数抛出此异常
|
||||
*/
|
||||
default String toJSONString(int indentFactor) throws JSONException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
synchronized (sw.getBuffer()) {
|
||||
return this.write(sw, indentFactor, 0).toString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将JSON内容写入Writer,无缩进<br>
|
||||
* Warning: This method assumes that the data structure is acyclical.
|
||||
*
|
||||
*
|
||||
* @param writer Writer
|
||||
* @return Writer
|
||||
* @throws JSONException JSON相关异常
|
||||
*/
|
||||
Writer write(Writer writer) throws JSONException;
|
||||
default Writer write(Writer writer) throws JSONException {
|
||||
return this.write(writer, 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将JSON内容写入Writer<br>
|
||||
* Warning: This method assumes that the data structure is acyclical.
|
||||
*
|
||||
* @param writer writer
|
||||
*
|
||||
* @param writer writer
|
||||
* @param indentFactor 缩进因子,定义每一级别增加的缩进量
|
||||
* @param indent 本级别缩进量
|
||||
* @param indent 本级别缩进量
|
||||
* @return Writer
|
||||
* @throws JSONException JSON相关异常
|
||||
*/
|
||||
Writer write(Writer writer, int indentFactor, int indent) throws JSONException;
|
||||
|
||||
/**
|
||||
* 转换为JSON字符串
|
||||
*
|
||||
* @param indentFactor 缩进因子,定义每一级别增加的缩进量
|
||||
* @return JSON字符串
|
||||
* @throws JSONException JSON相关异常
|
||||
* 转为实体类对象,转换异常将被抛出
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param clazz 实体类
|
||||
* @return 实体类对象
|
||||
*/
|
||||
String toJSONString(int indentFactor) throws JSONException;
|
||||
default <T> T toBean(Class<T> clazz) {
|
||||
return toBean((Type) clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化打印JSON,缩进为4个空格
|
||||
*
|
||||
* @return 格式化后的JSON字符串
|
||||
* @throws JSONException 包含非法数抛出此异常
|
||||
* @since 3.0.9
|
||||
* 转为实体类对象,转换异常将被抛出
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param reference {@link TypeReference}类型参考子类,可以获取其泛型参数中的Type类型
|
||||
* @return 实体类对象
|
||||
* @since 4.2.2
|
||||
*/
|
||||
String toStringPretty() throws JSONException;
|
||||
default <T> T toBean(TypeReference<T> reference) {
|
||||
return toBean(reference.getType());
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为实体类对象
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param type {@link Type}
|
||||
* @return 实体类对象
|
||||
* @since 3.0.8
|
||||
*/
|
||||
default <T> T toBean(Type type) {
|
||||
return toBean(type, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为实体类对象
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param type {@link Type}
|
||||
* @param ignoreError 是否忽略转换错误
|
||||
* @return 实体类对象
|
||||
* @since 4.3.2
|
||||
*/
|
||||
default <T> T toBean(Type type, boolean ignoreError) {
|
||||
return JSONConverter.jsonConvert(type, this, ignoreError);
|
||||
}
|
||||
}
|
||||
|
@@ -1,15 +1,5 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.RandomAccess;
|
||||
|
||||
import cn.hutool.core.bean.BeanPath;
|
||||
import cn.hutool.core.collection.ArrayIter;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
@@ -20,6 +10,15 @@ import cn.hutool.core.util.TypeUtil;
|
||||
import cn.hutool.json.serialize.GlobalSerializeMapping;
|
||||
import cn.hutool.json.serialize.JSONSerializer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.RandomAccess;
|
||||
|
||||
/**
|
||||
* JSON数组<br>
|
||||
* JSON数组是表示中括号括住的数据表现形式<br>
|
||||
@@ -498,38 +497,6 @@ public class JSONArray extends JSONGetter<Integer> implements JSON, List<Object>
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化打印JSON,缩进为4个空格
|
||||
*
|
||||
* @return 格式化后的JSON字符串
|
||||
* @throws JSONException 包含非法数抛出此异常
|
||||
* @since 3.0.9
|
||||
*/
|
||||
@Override
|
||||
public String toStringPretty() throws JSONException {
|
||||
return this.toJSONString(4);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为JSON字符串,指定缩进值
|
||||
*
|
||||
* @param indentFactor 缩进值,即缩进空格数
|
||||
* @return JSON字符串
|
||||
* @throws JSONException JSON写入异常
|
||||
*/
|
||||
@Override
|
||||
public String toJSONString(int indentFactor) throws JSONException {
|
||||
StringWriter sw = new StringWriter();
|
||||
synchronized (sw.getBuffer()) {
|
||||
return this.write(sw, indentFactor, 0).toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Writer write(Writer writer) throws JSONException {
|
||||
return this.write(writer, 0, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Writer write(Writer writer, int indentFactor, int indent) throws JSONException {
|
||||
try {
|
||||
|
@@ -77,7 +77,7 @@ public class JSONConverter implements Converter<JSON> {
|
||||
}
|
||||
}
|
||||
|
||||
Object targetValue = null;
|
||||
Object targetValue;
|
||||
try {
|
||||
targetValue = Convert.convert(targetType, value);
|
||||
} catch (ConvertException e) {
|
||||
|
@@ -1,24 +1,10 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Type;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import cn.hutool.core.bean.BeanDesc.PropDesc;
|
||||
import cn.hutool.core.bean.BeanPath;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.lang.TypeReference;
|
||||
import cn.hutool.core.map.CaseInsensitiveLinkedMap;
|
||||
import cn.hutool.core.map.CaseInsensitiveMap;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
@@ -30,6 +16,18 @@ import cn.hutool.json.serialize.GlobalSerializeMapping;
|
||||
import cn.hutool.json.serialize.JSONObjectSerializer;
|
||||
import cn.hutool.json.serialize.JSONSerializer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.Method;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* JSON对象<br>
|
||||
* 例:<br>
|
||||
@@ -111,9 +109,9 @@ public class JSONObject extends JSONGetter<String> implements JSON, Map<String,
|
||||
*/
|
||||
public JSONObject(int capacity, JSONConfig config) {
|
||||
if (config.isIgnoreCase()) {
|
||||
this.rawHashMap = config.isOrder() ? new CaseInsensitiveLinkedMap<String, Object>(capacity) : new CaseInsensitiveMap<String, Object>(capacity);
|
||||
this.rawHashMap = config.isOrder() ? new CaseInsensitiveLinkedMap<>(capacity) : new CaseInsensitiveMap<>(capacity);
|
||||
} else {
|
||||
this.rawHashMap = config.isOrder() ? new LinkedHashMap<String, Object>(capacity) : new HashMap<String, Object>(capacity);
|
||||
this.rawHashMap = config.isOrder() ? new LinkedHashMap<>(capacity) : new HashMap<>(capacity);
|
||||
}
|
||||
this.config = config;
|
||||
}
|
||||
@@ -288,54 +286,6 @@ public class JSONObject extends JSONGetter<String> implements JSON, Map<String,
|
||||
return ja;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为实体类对象,转换异常将被抛出
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param clazz 实体类
|
||||
* @return 实体类对象
|
||||
*/
|
||||
public <T> T toBean(Class<T> clazz) {
|
||||
return toBean((Type) clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为实体类对象,转换异常将被抛出
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param reference {@link TypeReference}类型参考子类,可以获取其泛型参数中的Type类型
|
||||
* @return 实体类对象
|
||||
* @since 4.2.2
|
||||
*/
|
||||
public <T> T toBean(TypeReference<T> reference) {
|
||||
return toBean(reference.getType());
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为实体类对象
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param type {@link Type}
|
||||
* @return 实体类对象
|
||||
* @since 3.0.8
|
||||
*/
|
||||
public <T> T toBean(Type type) {
|
||||
return toBean(type, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为实体类对象
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param type {@link Type}
|
||||
* @param ignoreError 是否忽略转换错误
|
||||
* @return 实体类对象
|
||||
* @since 4.3.2
|
||||
*/
|
||||
public <T> T toBean(Type type, boolean ignoreError) {
|
||||
return JSONConverter.jsonConvert(type, this, ignoreError);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return rawHashMap.size();
|
||||
@@ -586,36 +536,6 @@ public class JSONObject extends JSONGetter<String> implements JSON, Map<String,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化打印JSON,缩进为4个空格
|
||||
*
|
||||
* @return 格式化后的JSON字符串
|
||||
* @throws JSONException 包含非法数抛出此异常
|
||||
* @since 3.0.9
|
||||
*/
|
||||
@Override
|
||||
public String toStringPretty() throws JSONException {
|
||||
return this.toJSONString(4);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化输出JSON字符串
|
||||
*
|
||||
* @param indentFactor 每层缩进空格数
|
||||
* @return JSON字符串
|
||||
* @throws JSONException 包含非法数抛出此异常
|
||||
*/
|
||||
@Override
|
||||
public String toJSONString(int indentFactor) throws JSONException {
|
||||
final StringWriter w = new StringWriter();
|
||||
return this.write(w, indentFactor, 0).toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Writer write(Writer writer) throws JSONException {
|
||||
return this.write(writer, 0, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Writer write(Writer writer, int indentFactor, int indent) throws JSONException {
|
||||
try {
|
||||
|
@@ -378,7 +378,7 @@ public final class JSONUtil {
|
||||
* @since 4.3.2
|
||||
*/
|
||||
public static <T> T toBean(String jsonString, Type beanType, boolean ignoreError) {
|
||||
return toBean(parseObj(jsonString), beanType, ignoreError);
|
||||
return toBean(parse(jsonString), beanType, ignoreError);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -391,7 +391,7 @@ public final class JSONUtil {
|
||||
* @return 实体类对象
|
||||
* @since 4.6.2
|
||||
*/
|
||||
public static <T> T toBean(JSONObject json, TypeReference<T> typeReference, boolean ignoreError) {
|
||||
public static <T> T toBean(JSON json, TypeReference<T> typeReference, boolean ignoreError) {
|
||||
return toBean(json, typeReference.getType(), ignoreError);
|
||||
}
|
||||
|
||||
@@ -405,7 +405,7 @@ public final class JSONUtil {
|
||||
* @return 实体类对象
|
||||
* @since 4.3.2
|
||||
*/
|
||||
public static <T> T toBean(JSONObject json, Type beanType, boolean ignoreError) {
|
||||
public static <T> T toBean(JSON json, Type beanType, boolean ignoreError) {
|
||||
if (null == json) {
|
||||
return null;
|
||||
}
|
||||
|
@@ -9,4 +9,5 @@ import cn.hutool.json.JSONArray;
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface JSONArraySerializer<V> extends JSONSerializer<JSONArray, V>{}
|
||||
|
@@ -9,6 +9,7 @@ import cn.hutool.json.JSON;
|
||||
*
|
||||
* @param <T> 反序列化后的类型
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface JSONDeserializer<T> {
|
||||
|
||||
/**
|
||||
|
@@ -8,4 +8,5 @@ import cn.hutool.json.JSONObject;
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface JSONObjectSerializer<V> extends JSONSerializer<JSONObject, V>{}
|
||||
|
@@ -9,6 +9,7 @@ import cn.hutool.json.JSON;
|
||||
* @param <V> 对象类型
|
||||
* @author Looly
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface JSONSerializer<T extends JSON, V> {
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user