mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
fix code
This commit is contained in:
@@ -15,6 +15,14 @@ import java.lang.reflect.Type;
|
||||
*/
|
||||
public interface JSON extends Cloneable, Serializable {
|
||||
|
||||
/**
|
||||
* 获取JSON配置
|
||||
*
|
||||
* @return {@link JSONConfig}
|
||||
* @since 5.3.0
|
||||
*/
|
||||
JSONConfig getConfig();
|
||||
|
||||
/**
|
||||
* 通过表达式获取JSON中嵌套的对象<br>
|
||||
* <ol>
|
||||
@@ -36,7 +44,7 @@ public interface JSON extends Cloneable, Serializable {
|
||||
* @see BeanPath#get(Object)
|
||||
* @since 4.0.6
|
||||
*/
|
||||
default Object getByPath(String expression){
|
||||
default Object getByPath(final String expression){
|
||||
return BeanPath.of(expression).get(this);
|
||||
}
|
||||
|
||||
@@ -61,7 +69,7 @@ public interface JSON extends Cloneable, Serializable {
|
||||
* @param expression 表达式
|
||||
* @param value 值
|
||||
*/
|
||||
default void putByPath(String expression, Object value){
|
||||
default void putByPath(final String expression, final Object value){
|
||||
BeanPath.of(expression).set(this, value);
|
||||
}
|
||||
|
||||
@@ -152,6 +160,18 @@ public interface JSON extends Cloneable, Serializable {
|
||||
return toBean((Type) clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为实体类对象
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param type {@link Type}
|
||||
* @return 实体类对象
|
||||
* @since 4.3.2
|
||||
*/
|
||||
default <T> T toBean(final Type type) {
|
||||
return JSONConverter.jsonConvert(type, this, getConfig());
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为实体类对象,转换异常将被抛出
|
||||
*
|
||||
@@ -163,29 +183,4 @@ public interface JSON extends Cloneable, Serializable {
|
||||
default <T> T toBean(final TypeReference<T> reference) {
|
||||
return toBean(reference.getType());
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为实体类对象
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param type {@link Type}
|
||||
* @return 实体类对象
|
||||
* @since 3.0.8
|
||||
*/
|
||||
default <T> T toBean(final Type type) {
|
||||
return toBean(type, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为实体类对象
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param type {@link Type}
|
||||
* @param ignoreError 是否忽略转换错误
|
||||
* @return 实体类对象
|
||||
* @since 4.3.2
|
||||
*/
|
||||
default <T> T toBean(final Type type, final boolean ignoreError) {
|
||||
return JSONConverter.jsonConvert(type, this, ignoreError);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,7 +193,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
|
||||
@Override
|
||||
public <T> T getByPath(final String expression, final Class<T> resultType) {
|
||||
return JSONConverter.jsonConvert(resultType, getByPath(expression), this.config.isIgnoreError());
|
||||
return JSONConverter.jsonConvert(resultType, getByPath(expression), getConfig());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -237,7 +237,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
|
||||
@Override
|
||||
public <T> T toBean(final Type type) {
|
||||
return JSON.super.toBean(type, config.isIgnoreError());
|
||||
return JSON.super.toBean(type);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
/**
|
||||
* 实现此接口的类可以通过实现{@code parse(value)}方法来将JSON中的值解析为此对象的值
|
||||
* 实现此接口的类可以通过实现{@code parse(value)}方法来将JSON中的值解析为此对象的值<br>
|
||||
* 此接口主要用于可以自定义的Bean对象,而无需全局配置转换器。
|
||||
*
|
||||
* @author Looly
|
||||
* @since 5.7.8
|
||||
|
||||
@@ -7,6 +7,8 @@ import cn.hutool.core.convert.ConvertException;
|
||||
import cn.hutool.core.convert.Converter;
|
||||
import cn.hutool.core.convert.ConverterRegistry;
|
||||
import cn.hutool.core.convert.impl.BeanConverter;
|
||||
import cn.hutool.core.convert.impl.DateConverter;
|
||||
import cn.hutool.core.convert.impl.TemporalAccessorConverter;
|
||||
import cn.hutool.core.reflect.ConstructorUtil;
|
||||
import cn.hutool.core.reflect.TypeUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
@@ -15,6 +17,8 @@ import cn.hutool.json.serialize.GlobalSerializeMapping;
|
||||
import cn.hutool.json.serialize.JSONDeserializer;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* JSON转换器
|
||||
@@ -35,7 +39,7 @@ public class JSONConverter implements Converter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Type targetType, Object value) throws ConvertException {
|
||||
public Object convert(final Type targetType, final Object value) throws ConvertException {
|
||||
return JSONUtil.parse(value);
|
||||
}
|
||||
|
||||
@@ -47,38 +51,60 @@ public class JSONConverter implements Converter {
|
||||
* @param <T> 转换后的对象类型
|
||||
* @param targetType 目标类型
|
||||
* @param value 值
|
||||
* @param ignoreError 是否忽略转换错误
|
||||
* @param config JSON配置项
|
||||
* @return 目标类型的值
|
||||
* @throws ConvertException 转换失败
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected static <T> T jsonConvert(final Type targetType, final Object value, final boolean ignoreError) throws ConvertException {
|
||||
protected static <T> T jsonConvert(final Type targetType, final Object value, final JSONConfig config) throws ConvertException {
|
||||
if (null == value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// since 5.7.8,增加自定义Bean反序列化接口
|
||||
// since 5.7.8,增加自定义Bean反序列化接口和特殊对象的自定义转换
|
||||
if(targetType instanceof Class){
|
||||
final Class<?> clazz = (Class<?>) targetType;
|
||||
if (JSONBeanParser.class.isAssignableFrom(clazz)){
|
||||
final Class<?> targetClass = (Class<?>) targetType;
|
||||
if(targetClass.isInstance(value)){
|
||||
return (T) value;
|
||||
}
|
||||
|
||||
if (JSONBeanParser.class.isAssignableFrom(targetClass)){
|
||||
// 自定义对象转换
|
||||
@SuppressWarnings("rawtypes")
|
||||
final JSONBeanParser target = (JSONBeanParser) ConstructorUtil.newInstanceIfPossible(clazz);
|
||||
final JSONBeanParser target = (JSONBeanParser) ConstructorUtil.newInstanceIfPossible(targetClass);
|
||||
if(null == target){
|
||||
throw new ConvertException("Can not instance [{}]", targetType);
|
||||
}
|
||||
target.parse(value);
|
||||
return (T) target;
|
||||
} else if(targetType == byte[].class && value instanceof CharSequence){
|
||||
} else if(targetClass == byte[].class && value instanceof CharSequence){
|
||||
// bytes二进制反序列化,默认按照Base64对待
|
||||
// issue#I59LW4
|
||||
return (T) Base64.decode((CharSequence) value);
|
||||
}else if(targetClass.isAssignableFrom(Date.class) || targetClass.isAssignableFrom(TemporalAccessor.class)){
|
||||
// 用户指定了日期格式,获取日期属性时使用对应格式
|
||||
final String valueStr = Convert.toStr(value);
|
||||
if (null == valueStr) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 日期转换,支持自定义日期格式
|
||||
final String format = getDateFormat(config);
|
||||
if(null != format){
|
||||
if(targetClass.isAssignableFrom(Date.class)){
|
||||
return (T) new DateConverter(format).convert(targetClass, valueStr);
|
||||
}else{
|
||||
return (T) new TemporalAccessorConverter(format).convert(targetClass, valueStr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return jsonToBean(targetType, value, ignoreError);
|
||||
return jsonToBean(targetType, value, null != config && config.isIgnoreError());
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON递归转换<br>
|
||||
* JSON递归转换为Bean<br>
|
||||
* 首先尝试JDK类型转换,如果失败尝试JSON转Bean
|
||||
*
|
||||
* @param <T> 转换后的对象类型
|
||||
@@ -103,9 +129,8 @@ public class JSONConverter implements Converter {
|
||||
|
||||
// issue#2212@Github
|
||||
// 在JSONObject转Bean时,读取JSONObject本身的配置文件
|
||||
if(value instanceof JSONGetter
|
||||
&& targetType instanceof Class && BeanUtil.hasSetter((Class<?>) targetType)){
|
||||
final JSONConfig config = ((JSONGetter<?>) value).getConfig();
|
||||
if(targetType instanceof Class && BeanUtil.hasSetter((Class<?>) targetType)){
|
||||
final JSONConfig config = ((JSON) value).getConfig();
|
||||
final Converter converter = new BeanConverter(InternalJSONUtil.toCopyOptions(config).setIgnoreError(ignoreError));
|
||||
return ignoreError ? converter.convert(targetType, value, null)
|
||||
: (T) converter.convert(targetType, value);
|
||||
@@ -126,4 +151,19 @@ public class JSONConverter implements Converter {
|
||||
|
||||
return targetValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置文件中的日期格式,无格式返回{@code null}
|
||||
* @param config JSON配置
|
||||
* @return 日期格式,无返回{@code null}
|
||||
*/
|
||||
private static String getDateFormat(final JSONConfig config){
|
||||
if(null != config){
|
||||
final String format = config.getDateFormat();
|
||||
if (StrUtil.isNotBlank(format)) {
|
||||
return format;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,12 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.convert.ConvertException;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.date.LocalDateTimeUtil;
|
||||
import cn.hutool.core.lang.getter.OptNullBasicTypeFromObjectGetter;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 用于JSON的Getter类,提供各种类型的Getter方法
|
||||
@@ -132,29 +127,7 @@ public interface JSONGetter<K> extends OptNullBasicTypeFromObjectGetter<K> {
|
||||
|
||||
@Override
|
||||
default Date getDate(final K key, final Date defaultValue) {
|
||||
// 默认转换
|
||||
final Object obj = getObj(key);
|
||||
if (ObjUtil.isNull(obj)) {
|
||||
return defaultValue;
|
||||
}
|
||||
if (obj instanceof Date) {
|
||||
return (Date) obj;
|
||||
}
|
||||
|
||||
final Optional<String> formatOps = Optional.ofNullable(getConfig()).map(JSONConfig::getDateFormat);
|
||||
if (formatOps.isPresent()) {
|
||||
final String format = formatOps.get();
|
||||
if (StrUtil.isNotBlank(format)) {
|
||||
// 用户指定了日期格式,获取日期属性时使用对应格式
|
||||
final String str = Convert.toStr(obj);
|
||||
if (null == str) {
|
||||
return defaultValue;
|
||||
}
|
||||
return DateUtil.parse(str, format);
|
||||
}
|
||||
}
|
||||
|
||||
return Convert.toDate(obj, defaultValue);
|
||||
return get(key, Date.class);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -166,29 +139,7 @@ public interface JSONGetter<K> extends OptNullBasicTypeFromObjectGetter<K> {
|
||||
* @since 5.7.7
|
||||
*/
|
||||
default LocalDateTime getLocalDateTime(final K key, final LocalDateTime defaultValue) {
|
||||
// 默认转换
|
||||
final Object obj = getObj(key);
|
||||
if (ObjUtil.isNull(obj)) {
|
||||
return defaultValue;
|
||||
}
|
||||
if (obj instanceof LocalDateTime) {
|
||||
return (LocalDateTime) obj;
|
||||
}
|
||||
|
||||
final Optional<String> formatOps = Optional.ofNullable(getConfig()).map(JSONConfig::getDateFormat);
|
||||
if (formatOps.isPresent()) {
|
||||
final String format = formatOps.get();
|
||||
if (StrUtil.isNotBlank(format)) {
|
||||
// 用户指定了日期格式,获取日期属性时使用对应格式
|
||||
final String str = Convert.toStr(obj);
|
||||
if (null == str) {
|
||||
return defaultValue;
|
||||
}
|
||||
return LocalDateTimeUtil.parse(str, format);
|
||||
}
|
||||
}
|
||||
|
||||
return Convert.toLocalDateTime(obj, defaultValue);
|
||||
return ObjUtil.defaultIfNull(get(key, LocalDateTime.class), defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -201,37 +152,22 @@ public interface JSONGetter<K> extends OptNullBasicTypeFromObjectGetter<K> {
|
||||
return get(key, byte[].class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定类型的对象<br>
|
||||
* 转换失败或抛出异常
|
||||
*
|
||||
* @param <T> 获取的对象类型
|
||||
* @param key 键
|
||||
* @param type 获取对象类型
|
||||
* @return 对象
|
||||
* @throws ConvertException 转换异常
|
||||
* @since 3.0.8
|
||||
*/
|
||||
default <T> T get(final K key, final Class<T> type) throws ConvertException {
|
||||
return get(key, type, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定类型的对象
|
||||
*
|
||||
* @param <T> 获取的对象类型
|
||||
* @param key 键
|
||||
* @param type 获取对象类型
|
||||
* @param ignoreError 是否跳过转换失败的对象或值
|
||||
* @return 对象
|
||||
* @throws ConvertException 转换异常
|
||||
* @since 3.0.8
|
||||
*/
|
||||
default <T> T get(final K key, final Class<T> type, final boolean ignoreError) throws ConvertException {
|
||||
default <T> T get(final K key, final Class<T> type) throws ConvertException {
|
||||
final Object value = this.getObj(key);
|
||||
if (ObjUtil.isNull(value)) {
|
||||
return null;
|
||||
}
|
||||
return JSONConverter.jsonConvert(type, value, ignoreError);
|
||||
|
||||
return JSONConverter.jsonConvert(type, value, getConfig());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,7 +151,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
|
||||
@Override
|
||||
public <T> T toBean(final Type type) {
|
||||
return JSON.super.toBean(type, this.config.isIgnoreError());
|
||||
return JSON.super.toBean(type);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,7 +183,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
|
||||
@Override
|
||||
public <T> T getByPath(final String expression, final Class<T> resultType) {
|
||||
return JSONConverter.jsonConvert(resultType, getByPath(expression), this.config.isIgnoreError());
|
||||
return JSONConverter.jsonConvert(resultType, getByPath(expression), getConfig());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -382,7 +382,7 @@ public class JSONUtil {
|
||||
* @since 3.1.2
|
||||
*/
|
||||
public static <T> T toBean(final String jsonString, final Class<T> beanClass) {
|
||||
return toBean(parseObj(jsonString), beanClass);
|
||||
return toBean(parse(jsonString), beanClass);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -397,7 +397,21 @@ public class JSONUtil {
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public static <T> T toBean(final String jsonString, final JSONConfig config, final Class<T> beanClass) {
|
||||
return toBean(parseObj(jsonString, config), beanClass);
|
||||
return toBean(parse(jsonString, config), beanClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON字符串转为实体类对象,转换异常将被抛出<br>
|
||||
* 通过{@link JSONConfig}可选是否忽略大小写、忽略null等配置
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param jsonString JSON字符串
|
||||
* @param config JSON配置
|
||||
* @param type Bean类型
|
||||
* @return 实体类对象
|
||||
*/
|
||||
public static <T> T toBean(final String jsonString, final JSONConfig config, final Type type) {
|
||||
return toBean(parse(jsonString, config), type);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -412,63 +426,33 @@ public class JSONUtil {
|
||||
return null == json ? null : json.toBean(beanClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON字符串转为实体类对象,转换异常将被抛出
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param jsonString JSON字符串
|
||||
* @param typeReference {@link TypeReference}类型参考子类,可以获取其泛型参数中的Type类型
|
||||
* @param ignoreError 是否忽略错误
|
||||
* @return 实体类对象
|
||||
* @since 4.3.2
|
||||
*/
|
||||
public static <T> T toBean(final String jsonString, final TypeReference<T> typeReference, final boolean ignoreError) {
|
||||
return toBean(jsonString, typeReference.getType(), ignoreError);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON字符串转为实体类对象,转换异常将被抛出
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param jsonString JSON字符串
|
||||
* @param beanType 实体类对象类型
|
||||
* @param ignoreError 是否忽略错误
|
||||
* @return 实体类对象
|
||||
* @since 4.3.2
|
||||
*/
|
||||
public static <T> T toBean(final String jsonString, final Type beanType, final boolean ignoreError) {
|
||||
return parse(jsonString, JSONConfig.of().setIgnoreError(ignoreError)).toBean(beanType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为实体类对象
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param json JSONObject
|
||||
* @param typeReference {@link TypeReference}类型参考子类,可以获取其泛型参数中的Type类型
|
||||
* @param ignoreError 是否忽略转换错误
|
||||
* @return 实体类对象
|
||||
* @since 4.6.2
|
||||
*/
|
||||
public static <T> T toBean(final JSON json, final TypeReference<T> typeReference, final boolean ignoreError) {
|
||||
return toBean(json, typeReference.getType(), ignoreError);
|
||||
public static <T> T toBean(final JSON json, final TypeReference<T> typeReference) {
|
||||
return toBean(json, typeReference.getType());
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为实体类对象
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param json JSONObject
|
||||
* @param beanType 实体类对象类型
|
||||
* @param ignoreError 是否忽略转换错误
|
||||
* @param <T> Bean类型
|
||||
* @param json JSONObject
|
||||
* @param beanType 实体类对象类型
|
||||
* @return 实体类对象
|
||||
* @since 4.3.2
|
||||
*/
|
||||
public static <T> T toBean(final JSON json, final Type beanType, final boolean ignoreError) {
|
||||
public static <T> T toBean(final JSON json, final Type beanType) {
|
||||
if (null == json) {
|
||||
return null;
|
||||
}
|
||||
return json.toBean(beanType, ignoreError);
|
||||
return json.toBean(beanType);
|
||||
}
|
||||
// -------------------------------------------------------------------- toBean end
|
||||
|
||||
|
||||
@@ -32,8 +32,8 @@ public class Claims implements Serializable {
|
||||
* @param value 属性值
|
||||
*/
|
||||
protected void setClaim(final String name, final Object value) {
|
||||
init();
|
||||
Assert.notNull(name, "Name must be not null!");
|
||||
init();
|
||||
if (value == null) {
|
||||
claimJSON.remove(name);
|
||||
return;
|
||||
|
||||
@@ -5,9 +5,9 @@ import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.exceptions.ValidateException;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.jwt.signers.AlgorithmUtil;
|
||||
import cn.hutool.json.jwt.signers.JWTSigner;
|
||||
@@ -266,6 +266,19 @@ public class JWT implements RegisteredPayload<JWT> {
|
||||
return getPayload().getClaim(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取payload并获取类型
|
||||
*
|
||||
* @param propertyName 需要提取的属性名称
|
||||
* @param propertyType 需要提取的属性类型
|
||||
* @return 载荷信息
|
||||
* @throws ValidateException 传入的类型不匹配payload类型
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public <T> T getPayload(final String propertyName, final Class<T> propertyType) {
|
||||
return getPayload().getClaimsJson().get(propertyName, propertyType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置JWT载荷信息
|
||||
*
|
||||
|
||||
@@ -3,18 +3,18 @@ package cn.hutool.json.serialize;
|
||||
import cn.hutool.json.JSON;
|
||||
|
||||
/**
|
||||
* JSON反序列话自定义实现类
|
||||
*
|
||||
* @author Looly
|
||||
* JSON自定义反序列化接口<br>
|
||||
* 此接口主要针对一些特殊对象的反序列化自定义转换规则,配合GlobalSerializeMapping,可以全局设定某一类对象的自定义转换,而无需修改发Bean的结构。
|
||||
*
|
||||
* @param <T> 反序列化后的类型
|
||||
* @author Looly
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface JSONDeserializer<T> {
|
||||
|
||||
|
||||
/**
|
||||
* 反序列化,通过实现此方法,自定义实现JSON转换为指定类型的逻辑
|
||||
*
|
||||
*
|
||||
* @param json {@link JSON}
|
||||
* @return 目标对象
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user