mirror of
https://gitee.com/dromara/hutool.git
synced 2025-07-16 16:50:45 +08:00
add config
This commit is contained in:
parent
dd7f04af44
commit
f6f97668cf
@ -9,6 +9,7 @@
|
|||||||
* 【captcha】 AbstractCaptcha增加getImageBase64Data方法(pr#985@Github)
|
* 【captcha】 AbstractCaptcha增加getImageBase64Data方法(pr#985@Github)
|
||||||
* 【core 】 增加PhoneUtil(pr#990@Github)
|
* 【core 】 增加PhoneUtil(pr#990@Github)
|
||||||
* 【core 】 改进Img,目标图片类型未定义使用源图片类型(issue#I1PB0B@Gitee)
|
* 【core 】 改进Img,目标图片类型未定义使用源图片类型(issue#I1PB0B@Gitee)
|
||||||
|
* 【json 】 JSONConfig增加Transient选项(issue#I1PLHN@Gitee)
|
||||||
|
|
||||||
### Bug修复
|
### Bug修复
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package cn.hutool.core.bean;
|
package cn.hutool.core.bean;
|
||||||
|
|
||||||
|
import cn.hutool.core.annotation.AnnotationUtil;
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.map.CaseInsensitiveMap;
|
import cn.hutool.core.map.CaseInsensitiveMap;
|
||||||
import cn.hutool.core.util.BooleanUtil;
|
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.StrUtil;
|
||||||
import cn.hutool.core.util.TypeUtil;
|
import cn.hutool.core.util.TypeUtil;
|
||||||
|
|
||||||
|
import java.beans.Transient;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
@ -34,9 +36,13 @@ import java.util.Map;
|
|||||||
public class BeanDesc implements Serializable {
|
public class BeanDesc implements Serializable {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
/** Bean类 */
|
/**
|
||||||
|
* Bean类
|
||||||
|
*/
|
||||||
private final Class<?> beanClass;
|
private final Class<?> beanClass;
|
||||||
/** 属性Map */
|
/**
|
||||||
|
* 属性Map
|
||||||
|
*/
|
||||||
private final Map<String, PropDesc> propMap = new LinkedHashMap<>();
|
private final Map<String, PropDesc> propMap = new LinkedHashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -131,6 +137,7 @@ public class BeanDesc implements Serializable{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------------ Private method start
|
// ------------------------------------------------------------------------------------------------------ Private method start
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化<br>
|
* 初始化<br>
|
||||||
* 只有与属性关联的相关Getter和Setter方法才会被读取,无关的getXXX和setXXX都被忽略
|
* 只有与属性关联的相关Getter和Setter方法才会被读取,无关的getXXX和setXXX都被忽略
|
||||||
@ -295,15 +302,20 @@ public class BeanDesc implements Serializable{
|
|||||||
* 属性描述
|
* 属性描述
|
||||||
*
|
*
|
||||||
* @author looly
|
* @author looly
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public static class PropDesc {
|
public static class PropDesc {
|
||||||
|
|
||||||
/** 字段 */
|
/**
|
||||||
|
* 字段
|
||||||
|
*/
|
||||||
private final Field field;
|
private final Field field;
|
||||||
/** Getter方法 */
|
/**
|
||||||
|
* Getter方法
|
||||||
|
*/
|
||||||
private final Method getter;
|
private final Method getter;
|
||||||
/** Setter方法 */
|
/**
|
||||||
|
* Setter方法
|
||||||
|
*/
|
||||||
private final Method setter;
|
private final Method setter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -427,7 +439,30 @@ public class BeanDesc implements Serializable{
|
|||||||
return this;
|
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
|
//------------------------------------------------------------------------------------ Private method start
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过Getter和Setter方法中找到属性类型
|
* 通过Getter和Setter方法中找到属性类型
|
||||||
*
|
*
|
||||||
|
@ -11,19 +11,34 @@ import java.io.Serializable;
|
|||||||
public class JSONConfig implements Serializable {
|
public class JSONConfig implements Serializable {
|
||||||
private static final long serialVersionUID = 119730355204738278L;
|
private static final long serialVersionUID = 119730355204738278L;
|
||||||
|
|
||||||
/** 是否有序,顺序按照加入顺序排序 */
|
/**
|
||||||
|
* 是否有序,顺序按照加入顺序排序
|
||||||
|
*/
|
||||||
private boolean order;
|
private boolean order;
|
||||||
/** 是否忽略转换过程中的异常 */
|
/**
|
||||||
|
* 是否忽略转换过程中的异常
|
||||||
|
*/
|
||||||
private boolean ignoreError;
|
private boolean ignoreError;
|
||||||
/** 是否忽略键的大小写 */
|
/**
|
||||||
|
* 是否忽略键的大小写
|
||||||
|
*/
|
||||||
private boolean ignoreCase;
|
private boolean ignoreCase;
|
||||||
/** 日期格式,null表示默认的时间戳 */
|
/**
|
||||||
|
* 日期格式,null表示默认的时间戳
|
||||||
|
*/
|
||||||
private String dateFormat;
|
private String dateFormat;
|
||||||
/** 是否忽略null值 */
|
/**
|
||||||
|
* 是否忽略null值
|
||||||
|
*/
|
||||||
private boolean ignoreNullValue = true;
|
private boolean ignoreNullValue = true;
|
||||||
|
/**
|
||||||
|
* 是否忽略transient关键字修饰的字段
|
||||||
|
*/
|
||||||
|
private boolean ignoreTransient = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建默认的配置项
|
* 创建默认的配置项
|
||||||
|
*
|
||||||
* @return JSONConfig
|
* @return JSONConfig
|
||||||
*/
|
*/
|
||||||
public static JSONConfig create() {
|
public static JSONConfig create() {
|
||||||
@ -129,4 +144,26 @@ public class JSONConfig implements Serializable {
|
|||||||
this.ignoreNullValue = ignoreNullValue;
|
this.ignoreNullValue = ignoreNullValue;
|
||||||
return this;
|
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;
|
Method getter;
|
||||||
Object value;
|
Object value;
|
||||||
for (PropDesc prop : props) {
|
for (PropDesc prop : props) {
|
||||||
|
if(this.config.isIgnoreTransient() && prop.isTransient()){
|
||||||
|
// 忽略Transient字段和方法
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// 得到property对应的getter方法
|
// 得到property对应的getter方法
|
||||||
getter = prop.getGetter();
|
getter = prop.getGetter();
|
||||||
if (null == getter) {
|
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