mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
7cf1b3758d
commit
e491349b81
@ -48,7 +48,7 @@ public class ArrayConverter extends AbstractConverter {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object convertInternal(Class<?> targetClass, final Object value) {
|
||||
protected Object convertInternal(final Class<?> targetClass, final Object value) {
|
||||
final Class<?> targetComponentType;
|
||||
if (targetClass.isArray()) {
|
||||
targetComponentType = targetClass.getComponentType();
|
||||
|
@ -59,7 +59,7 @@ public class DateConverter extends AbstractConverter {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected java.util.Date convertInternal(Class<?> targetClass, final Object value) {
|
||||
protected java.util.Date convertInternal(final Class<?> targetClass, final Object value) {
|
||||
if (value == null || (value instanceof CharSequence && StrUtil.isBlank(value.toString()))) {
|
||||
return null;
|
||||
}
|
||||
@ -89,7 +89,7 @@ public class DateConverter extends AbstractConverter {
|
||||
* @param date Date
|
||||
* @return 目标类型对象
|
||||
*/
|
||||
private java.util.Date wrap(Class<?> targetClass, final DateTime date) {
|
||||
private java.util.Date wrap(final Class<?> targetClass, final DateTime date) {
|
||||
// 返回指定类型
|
||||
if (java.util.Date.class == targetClass) {
|
||||
return date.toJdkDate();
|
||||
@ -116,7 +116,7 @@ public class DateConverter extends AbstractConverter {
|
||||
* @param mills Date
|
||||
* @return 目标类型对象
|
||||
*/
|
||||
private java.util.Date wrap(Class<?> targetClass, final long mills) {
|
||||
private java.util.Date wrap(final Class<?> targetClass, final long mills) {
|
||||
// 返回指定类型
|
||||
if (java.util.Date.class == targetClass) {
|
||||
return new java.util.Date(mills);
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -202,8 +153,7 @@ public interface JSONGetter<K> extends OptNullBasicTypeFromObjectGetter<K> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定类型的对象<br>
|
||||
* 转换失败或抛出异常
|
||||
* 获取指定类型的对象
|
||||
*
|
||||
* @param <T> 获取的对象类型
|
||||
* @param key 键
|
||||
@ -213,25 +163,11 @@ public interface JSONGetter<K> extends OptNullBasicTypeFromObjectGetter<K> {
|
||||
* @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 {
|
||||
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,46 +426,17 @@ 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());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -460,15 +445,14 @@ public class JSONUtil {
|
||||
* @param <T> Bean类型
|
||||
* @param json JSONObject
|
||||
* @param beanType 实体类对象类型
|
||||
* @param ignoreError 是否忽略转换错误
|
||||
* @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,11 +3,11 @@ 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> {
|
||||
|
@ -13,7 +13,7 @@ public class Issue2369Test {
|
||||
final String s = JSONUtil.toJsonStr(bytes);
|
||||
Assert.assertEquals("[10,11]", s);
|
||||
|
||||
final Object o = JSONUtil.toBean(s, byte[].class, false);
|
||||
final Object o = JSONUtil.toBean(s, byte[].class);
|
||||
Assert.assertArrayEquals(bytes, (byte[])o);
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ public class Issue2377Test {
|
||||
final String paramBytesStr = JSONUtil.toJsonStr(paramList.get(1));
|
||||
Assert.assertEquals("[10,11]", paramBytesStr);
|
||||
|
||||
final byte[] paramBytes = JSONUtil.toBean(paramBytesStr, byte[].class, false);
|
||||
final byte[] paramBytes = JSONUtil.toBean(paramBytesStr, byte[].class);
|
||||
Assert.assertArrayEquals((byte[]) paramArray[1], paramBytes);
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,12 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.reflect.TypeReference;
|
||||
import lombok.Data;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Issue488Test {
|
||||
|
||||
@ -15,8 +14,8 @@ public class Issue488Test {
|
||||
public void toBeanTest() {
|
||||
final String jsonStr = ResourceUtil.readUtf8Str("issue488.json");
|
||||
|
||||
final ResultSuccess<List<EmailAddress>> result = JSONUtil.toBean(jsonStr,
|
||||
new TypeReference<ResultSuccess<List<EmailAddress>>>() {}, false);
|
||||
final ResultSuccess<List<EmailAddress>> result = JSONUtil.toBean(jsonStr, JSONConfig.of(),
|
||||
new TypeReference<ResultSuccess<List<EmailAddress>>>() {});
|
||||
|
||||
Assert.assertEquals("https://graph.microsoft.com/beta/$metadata#Collection(microsoft.graph.emailAddress)", result.getContext());
|
||||
|
||||
@ -35,8 +34,8 @@ public class Issue488Test {
|
||||
public void toCollctionBeanTest() {
|
||||
final String jsonStr = ResourceUtil.readUtf8Str("issue488Array.json");
|
||||
|
||||
final List<ResultSuccess<List<EmailAddress>>> resultList = JSONUtil.toBean(jsonStr,
|
||||
new TypeReference<List<ResultSuccess<List<EmailAddress>>>>() {}, false);
|
||||
final List<ResultSuccess<List<EmailAddress>>> resultList = JSONUtil.toBean(jsonStr, JSONConfig.of(),
|
||||
new TypeReference<List<ResultSuccess<List<EmailAddress>>>>() {});
|
||||
|
||||
final ResultSuccess<List<EmailAddress>> result = resultList.get(0);
|
||||
|
||||
|
@ -218,14 +218,14 @@ public class JSONObjectTest {
|
||||
|
||||
@Test
|
||||
public void toBeanNullStrTest() {
|
||||
final JSONObject json = JSONUtil.createObj()//
|
||||
final JSONObject json = JSONUtil.createObj(JSONConfig.of().setIgnoreError(true))//
|
||||
.set("strValue", "null")//
|
||||
.set("intValue", 123)//
|
||||
// 子对象对应"null"字符串,如果忽略错误,跳过,否则抛出转换异常
|
||||
.set("beanValue", "null")//
|
||||
.set("list", JSONUtil.createArray().set("a").set("b"));
|
||||
|
||||
final TestBean bean = json.toBean(TestBean.class, true);
|
||||
final TestBean bean = json.toBean(TestBean.class);
|
||||
// 当JSON中为字符串"null"时应被当作字符串处理
|
||||
Assert.assertEquals("null", bean.getStrValue());
|
||||
// 当JSON中为字符串"null"时Bean中的字段类型不匹配应在ignoreError模式下忽略注入
|
||||
@ -483,6 +483,22 @@ public class JSONObjectTest {
|
||||
Assert.assertEquals(DateUtil.beginOfDay(date), parse.getDate("date"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setDateFormatTest3() {
|
||||
// 自定义格式为只有秒的时间戳,一版用于JWT
|
||||
final JSONConfig jsonConfig = JSONConfig.of().setDateFormat("#sss");
|
||||
|
||||
final Date date = DateUtil.parse("2020-06-05 11:16:11");
|
||||
final JSONObject json = new JSONObject(jsonConfig);
|
||||
json.set("date", date);
|
||||
|
||||
Assert.assertEquals("{\"date\":1591326971}", json.toString());
|
||||
|
||||
// 解析测试
|
||||
final JSONObject parse = JSONUtil.parseObj(json.toString(), jsonConfig);
|
||||
Assert.assertEquals(date, DateUtil.date(parse.getDate("date")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setCustomDateFormatTest() {
|
||||
final JSONConfig jsonConfig = JSONConfig.of();
|
||||
|
@ -1,13 +1,13 @@
|
||||
package cn.hutool.json.issueIVMD5;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.reflect.TypeReference;
|
||||
import cn.hutool.json.JSONConfig;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class IssueIVMD5Test {
|
||||
|
||||
@ -19,7 +19,7 @@ public class IssueIVMD5Test {
|
||||
final String jsonStr = ResourceUtil.readUtf8Str("issueIVMD5.json");
|
||||
|
||||
final TypeReference<BaseResult<StudentInfo>> typeReference = new TypeReference<BaseResult<StudentInfo>>() {};
|
||||
final BaseResult<StudentInfo> bean = JSONUtil.toBean(jsonStr, typeReference.getType(), false);
|
||||
final BaseResult<StudentInfo> bean = JSONUtil.toBean(jsonStr, JSONConfig.of(), typeReference.getType());
|
||||
|
||||
final StudentInfo data2 = bean.getData2();
|
||||
Assert.assertEquals("B4DDF491FDF34074AE7A819E1341CB6C", data2.getAccountId());
|
||||
@ -33,7 +33,7 @@ public class IssueIVMD5Test {
|
||||
final String jsonStr = ResourceUtil.readUtf8Str("issueIVMD5.json");
|
||||
|
||||
final TypeReference<BaseResult<StudentInfo>> typeReference = new TypeReference<BaseResult<StudentInfo>>() {};
|
||||
final BaseResult<StudentInfo> bean = JSONUtil.toBean(jsonStr, typeReference.getType(), false);
|
||||
final BaseResult<StudentInfo> bean = JSONUtil.toBean(jsonStr, JSONConfig.of(), typeReference.getType());
|
||||
|
||||
final List<StudentInfo> data = bean.getData();
|
||||
final StudentInfo studentInfo = data.get(0);
|
||||
|
@ -1,11 +1,19 @@
|
||||
package cn.hutool.json.jwt;
|
||||
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.json.jwt.signers.JWTSignerUtil;
|
||||
import lombok.Data;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class JWTTest {
|
||||
|
||||
@Test
|
||||
@ -88,4 +96,67 @@ public class JWTTest {
|
||||
final boolean verify = JWT.of(token).setKey(StrUtil.utf8Bytes("123456")).verify();
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class UserTest {
|
||||
private String name;
|
||||
private Integer age;
|
||||
}
|
||||
@Test
|
||||
public void payloadTest() {
|
||||
|
||||
final UserTest bean = new UserTest();
|
||||
bean.setAge(18);
|
||||
bean.setName("takaki");
|
||||
|
||||
final Date date = new Date();
|
||||
final List<Integer> list = Arrays.asList(1, 2, 3);
|
||||
final Integer num = 18;
|
||||
final String username = "takaki";
|
||||
final HashMap<String, String> map = new HashMap<>();
|
||||
map.put("test1", "1");
|
||||
map.put("test2", "2");
|
||||
final Map<String, Object> payload = new HashMap<String, Object>() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
{
|
||||
put("username", username);
|
||||
put("bean", bean);
|
||||
put("number", num);
|
||||
put("list", list);
|
||||
put("date", date);
|
||||
put("map", map);
|
||||
}
|
||||
};
|
||||
|
||||
final String token = JWTUtil.createToken(payload, "123".getBytes());
|
||||
final JWT jwt = JWT.of(token);
|
||||
final String strRes = jwt.getPayload("username", String.class);
|
||||
final UserTest beanRes = jwt.getPayload("bean", UserTest.class);
|
||||
final Date dateRes = jwt.getPayload("date", Date.class);
|
||||
final List<?> listRes = jwt.getPayload("list", List.class);
|
||||
final Integer numRes = jwt.getPayload("number", Integer.class);
|
||||
final HashMap<?, ?> mapRes = jwt.getPayload("map", HashMap.class);
|
||||
|
||||
Assert.assertEquals(bean, beanRes);
|
||||
Assert.assertEquals(numRes, num);
|
||||
Assert.assertEquals(username, strRes);
|
||||
Assert.assertEquals(list, listRes);
|
||||
|
||||
final String formattedDate = DateUtil.format(date, "yyyy-MM-dd HH:mm:ss");
|
||||
final String formattedRes = DateUtil.format(dateRes, "yyyy-MM-dd HH:mm:ss");
|
||||
Assert.assertEquals(formattedDate, formattedRes);
|
||||
Assert.assertEquals(map, mapRes);
|
||||
}
|
||||
|
||||
@Test()
|
||||
public void getDateTest(){
|
||||
final String token = JWT.create()
|
||||
.setIssuedAt(DateUtil.parse("2022-02-02"))
|
||||
.setKey("123456".getBytes())
|
||||
.sign();
|
||||
|
||||
// 签发时间早于被检查的时间
|
||||
final Date date = JWT.of(token).getPayload().getClaimsJson().getDate(JWTPayload.ISSUED_AT);
|
||||
Assert.assertEquals("2022-02-02", DateUtil.format(date, DatePattern.NORM_DATE_PATTERN));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user