add config

This commit is contained in:
Looly
2020-07-31 09:43:05 +08:00
parent dd7f04af44
commit f6f97668cf
5 changed files with 174 additions and 71 deletions

View File

@@ -4,26 +4,41 @@ import java.io.Serializable;
/**
* JSON配置项
*
*
* @author looly
* @since 4.1.19
*/
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() {
@@ -32,7 +47,7 @@ public class JSONConfig implements Serializable {
/**
* 是否有序,顺序按照加入顺序排序
*
*
* @return 是否有序
*/
public boolean isOrder() {
@@ -41,7 +56,7 @@ public class JSONConfig implements Serializable {
/**
* 设置是否有序,顺序按照加入顺序排序
*
*
* @param order 是否有序
* @return this
*/
@@ -52,7 +67,7 @@ public class JSONConfig implements Serializable {
/**
* 是否忽略转换过程中的异常
*
*
* @return 是否忽略转换过程中的异常
*/
public boolean isIgnoreError() {
@@ -61,7 +76,7 @@ public class JSONConfig implements Serializable {
/**
* 设置是否忽略转换过程中的异常
*
*
* @param ignoreError 是否忽略转换过程中的异常
* @return this
*/
@@ -72,7 +87,7 @@ public class JSONConfig implements Serializable {
/**
* 是否忽略键的大小写
*
*
* @return 是否忽略键的大小写
*/
public boolean isIgnoreCase() {
@@ -81,7 +96,7 @@ public class JSONConfig implements Serializable {
/**
* 设置是否忽略键的大小写
*
*
* @param ignoreCase 是否忽略键的大小写
* @return this
*/
@@ -92,7 +107,7 @@ public class JSONConfig implements Serializable {
/**
* 日期格式null表示默认的时间戳
*
*
* @return 日期格式null表示默认的时间戳
*/
public String getDateFormat() {
@@ -101,7 +116,7 @@ public class JSONConfig implements Serializable {
/**
* 设置日期格式null表示默认的时间戳
*
*
* @param dateFormat 日期格式null表示默认的时间戳
* @return this
*/
@@ -109,10 +124,10 @@ public class JSONConfig implements Serializable {
this.dateFormat = dateFormat;
return this;
}
/**
* 是否忽略null值
*
*
* @return 是否忽略null值
*/
public boolean isIgnoreNullValue() {
@@ -121,7 +136,7 @@ public class JSONConfig implements Serializable {
/**
* 设置是否忽略null值
*
*
* @param ignoreNullValue 是否忽略null值
* @return this
*/
@@ -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;
}
}

View File

@@ -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) {

View 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());
}
}