mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-04 20:58:00 +08:00
add config
This commit is contained in:
parent
dd7f04af44
commit
f6f97668cf
@ -9,6 +9,7 @@
|
||||
* 【captcha】 AbstractCaptcha增加getImageBase64Data方法(pr#985@Github)
|
||||
* 【core 】 增加PhoneUtil(pr#990@Github)
|
||||
* 【core 】 改进Img,目标图片类型未定义使用源图片类型(issue#I1PB0B@Gitee)
|
||||
* 【json 】 JSONConfig增加Transient选项(issue#I1PLHN@Gitee)
|
||||
|
||||
### Bug修复
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
package cn.hutool.core.bean;
|
||||
|
||||
import cn.hutool.core.annotation.AnnotationUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.map.CaseInsensitiveMap;
|
||||
import cn.hutool.core.util.BooleanUtil;
|
||||
@ -9,6 +10,7 @@ import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.core.util.TypeUtil;
|
||||
|
||||
import java.beans.Transient;
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
@ -31,12 +33,16 @@ import java.util.Map;
|
||||
* @author looly
|
||||
* @since 3.1.2
|
||||
*/
|
||||
public class BeanDesc implements Serializable{
|
||||
public class BeanDesc implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** Bean类 */
|
||||
/**
|
||||
* Bean类
|
||||
*/
|
||||
private final Class<?> beanClass;
|
||||
/** 属性Map */
|
||||
/**
|
||||
* 属性Map
|
||||
*/
|
||||
private final Map<String, PropDesc> propMap = new LinkedHashMap<>();
|
||||
|
||||
/**
|
||||
@ -131,6 +137,7 @@ public class BeanDesc implements Serializable{
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------------ Private method start
|
||||
|
||||
/**
|
||||
* 初始化<br>
|
||||
* 只有与属性关联的相关Getter和Setter方法才会被读取,无关的getXXX和setXXX都被忽略
|
||||
@ -139,7 +146,7 @@ public class BeanDesc implements Serializable{
|
||||
*/
|
||||
private BeanDesc init() {
|
||||
for (Field field : ReflectUtil.getFields(this.beanClass)) {
|
||||
if(false == ModifierUtil.isStatic(field)) {
|
||||
if (false == ModifierUtil.isStatic(field)) {
|
||||
//只针对非static属性
|
||||
this.propMap.put(ReflectUtil.getFieldName(field), createProp(field));
|
||||
}
|
||||
@ -211,8 +218,8 @@ public class BeanDesc implements Serializable{
|
||||
* name -》 getName
|
||||
* </pre>
|
||||
*
|
||||
* @param methodName 方法名
|
||||
* @param fieldName 字段名
|
||||
* @param methodName 方法名
|
||||
* @param fieldName 字段名
|
||||
* @param isBooeanField 是否为Boolean类型字段
|
||||
* @return 是否匹配
|
||||
*/
|
||||
@ -225,7 +232,7 @@ public class BeanDesc implements Serializable{
|
||||
// 非标准Getter方法
|
||||
return false;
|
||||
}
|
||||
if("getclass".equals(methodName)) {
|
||||
if ("getclass".equals(methodName)) {
|
||||
//跳过getClass方法
|
||||
return false;
|
||||
}
|
||||
@ -261,8 +268,8 @@ public class BeanDesc implements Serializable{
|
||||
* name -》 setName
|
||||
* </pre>
|
||||
*
|
||||
* @param methodName 方法名
|
||||
* @param fieldName 字段名
|
||||
* @param methodName 方法名
|
||||
* @param fieldName 字段名
|
||||
* @param isBooeanField 是否为Boolean类型字段
|
||||
* @return 是否匹配
|
||||
*/
|
||||
@ -295,22 +302,27 @@ public class BeanDesc implements Serializable{
|
||||
* 属性描述
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public static class PropDesc {
|
||||
|
||||
/** 字段 */
|
||||
/**
|
||||
* 字段
|
||||
*/
|
||||
private final Field field;
|
||||
/** Getter方法 */
|
||||
/**
|
||||
* Getter方法
|
||||
*/
|
||||
private final Method getter;
|
||||
/** Setter方法 */
|
||||
/**
|
||||
* Setter方法
|
||||
*/
|
||||
private final Method setter;
|
||||
|
||||
/**
|
||||
* 构造<br>
|
||||
* Getter和Setter方法设置为默认可访问
|
||||
*
|
||||
* @param field 字段
|
||||
* @param field 字段
|
||||
* @param getter get方法
|
||||
* @param setter set方法
|
||||
*/
|
||||
@ -401,9 +413,9 @@ public class BeanDesc implements Serializable{
|
||||
* @since 4.0.5
|
||||
*/
|
||||
public Object getValue(Object bean) {
|
||||
if(null != this.getter) {
|
||||
if (null != this.getter) {
|
||||
return ReflectUtil.invoke(bean, this.getter);
|
||||
} else if(ModifierUtil.isPublic(this.field)) {
|
||||
} else if (ModifierUtil.isPublic(this.field)) {
|
||||
return ReflectUtil.getFieldValue(bean, this.field);
|
||||
}
|
||||
return null;
|
||||
@ -413,21 +425,44 @@ public class BeanDesc implements Serializable{
|
||||
* 设置Bean的字段值<br>
|
||||
* 首先调用字段对应的Setter方法,如果Setter方法不存在,则判断字段如果为public,则直接赋值字段值
|
||||
*
|
||||
* @param bean Bean对象
|
||||
* @param bean Bean对象
|
||||
* @param value 值
|
||||
* @return this
|
||||
* @since 4.0.5
|
||||
*/
|
||||
public PropDesc setValue(Object bean, Object value) {
|
||||
if(null != this.setter) {
|
||||
if (null != this.setter) {
|
||||
ReflectUtil.invoke(bean, this.setter, value);
|
||||
} else if(ModifierUtil.isPublic(this.field)) {
|
||||
} else if (ModifierUtil.isPublic(this.field)) {
|
||||
ReflectUtil.setFieldValue(bean, this.field, value);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段和Getter方法是否为Transient关键字修饰的
|
||||
*
|
||||
* @return 是否为Transient关键字修饰的
|
||||
* @since 5.3.11
|
||||
*/
|
||||
public boolean isTransient() {
|
||||
boolean isTransient = ModifierUtil.hasModifier(this.field, ModifierUtil.ModifierType.TRANSIENT);
|
||||
|
||||
// 检查Getter方法
|
||||
if(false == isTransient && null != this.getter){
|
||||
isTransient = ModifierUtil.hasModifier(this.getter, ModifierUtil.ModifierType.TRANSIENT);
|
||||
|
||||
// 检查注解
|
||||
if(false == isTransient){
|
||||
isTransient = null != AnnotationUtil.getAnnotation(this.getter, Transient.class);
|
||||
}
|
||||
}
|
||||
|
||||
return isTransient;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------ Private method start
|
||||
|
||||
/**
|
||||
* 通过Getter和Setter方法中找到属性类型
|
||||
*
|
||||
|
@ -11,19 +11,34 @@ import java.io.Serializable;
|
||||
public class JSONConfig implements Serializable {
|
||||
private static final long serialVersionUID = 119730355204738278L;
|
||||
|
||||
/** 是否有序,顺序按照加入顺序排序 */
|
||||
/**
|
||||
* 是否有序,顺序按照加入顺序排序
|
||||
*/
|
||||
private boolean order;
|
||||
/** 是否忽略转换过程中的异常 */
|
||||
/**
|
||||
* 是否忽略转换过程中的异常
|
||||
*/
|
||||
private boolean ignoreError;
|
||||
/** 是否忽略键的大小写 */
|
||||
/**
|
||||
* 是否忽略键的大小写
|
||||
*/
|
||||
private boolean ignoreCase;
|
||||
/** 日期格式,null表示默认的时间戳 */
|
||||
/**
|
||||
* 日期格式,null表示默认的时间戳
|
||||
*/
|
||||
private String dateFormat;
|
||||
/** 是否忽略null值 */
|
||||
/**
|
||||
* 是否忽略null值
|
||||
*/
|
||||
private boolean ignoreNullValue = true;
|
||||
/**
|
||||
* 是否忽略transient关键字修饰的字段
|
||||
*/
|
||||
private boolean ignoreTransient = true;
|
||||
|
||||
/**
|
||||
* 创建默认的配置项
|
||||
*
|
||||
* @return JSONConfig
|
||||
*/
|
||||
public static JSONConfig create() {
|
||||
@ -129,4 +144,26 @@ public class JSONConfig implements Serializable {
|
||||
this.ignoreNullValue = ignoreNullValue;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否忽略transient关键字修饰的字段
|
||||
*
|
||||
* @return 是否忽略transient关键字修饰的字段
|
||||
* @since 5.3.11
|
||||
*/
|
||||
public boolean isIgnoreTransient() {
|
||||
return this.ignoreTransient;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否忽略transient关键字修饰的字段
|
||||
*
|
||||
* @param ignoreTransient 是否忽略transient关键字修饰的字段
|
||||
* @return this
|
||||
* @since 5.3.11
|
||||
*/
|
||||
public JSONConfig setIgnoreTransient(boolean ignoreTransient) {
|
||||
this.ignoreTransient = ignoreTransient;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
@ -625,6 +625,11 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
|
||||
Method getter;
|
||||
Object value;
|
||||
for (PropDesc prop : props) {
|
||||
if(this.config.isIgnoreTransient() && prop.isTransient()){
|
||||
// 忽略Transient字段和方法
|
||||
continue;
|
||||
}
|
||||
|
||||
// 得到property对应的getter方法
|
||||
getter = prop.getGetter();
|
||||
if (null == getter) {
|
||||
|
25
hutool-json/src/test/java/cn/hutool/json/TransientTest.java
Normal file
25
hutool-json/src/test/java/cn/hutool/json/TransientTest.java
Normal file
@ -0,0 +1,25 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import lombok.Data;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TransientTest {
|
||||
|
||||
@Data
|
||||
static class Bill{
|
||||
private transient String id;
|
||||
private String bizNo;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanWithTransientTest(){
|
||||
Bill detailBill = new Bill();
|
||||
detailBill.setId("3243");
|
||||
detailBill.setBizNo("bizNo");
|
||||
|
||||
final JSONObject jsonObject = new JSONObject(detailBill,
|
||||
JSONConfig.create().setIgnoreTransient(true));
|
||||
Assert.assertEquals("{\"bizNo\":\"bizNo\"}", jsonObject.toString());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user