This commit is contained in:
Looly
2020-09-09 14:23:21 +08:00
parent 5aa10c06ee
commit 0c3a1ea298
11 changed files with 513 additions and 465 deletions

View File

@@ -1,8 +1,9 @@
package cn.hutool.json;
import cn.hutool.core.bean.BeanDesc.PropDesc;
import cn.hutool.core.bean.BeanPath;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.BeanCopier;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.CaseInsensitiveLinkedMap;
@@ -19,7 +20,6 @@ import cn.hutool.json.serialize.JSONSerializer;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
@@ -620,41 +620,12 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
* @param bean Bean对象
*/
private void populateMap(Object bean) {
final Collection<PropDesc> props = BeanUtil.getBeanDesc(bean.getClass()).getProps();
Method getter;
Object value;
for (PropDesc prop : props) {
if(this.config.isTransientSupport() && prop.isTransient()){
// 忽略Transient字段和方法
continue;
}
// 得到property对应的getter方法
getter = prop.getGetter();
if (null == getter) {
// 无Getter跳过
continue;
}
// 只读取有getter方法的属性
try {
value = getter.invoke(bean);
} catch (Exception ignore) {
// 忽略读取失败的属性
continue;
}
if (ObjectUtil.isNull(value) && this.config.isIgnoreNullValue()) {
// 值为null且用户定义跳过则跳过
continue;
}
if (value != bean) {
// 防止循环引用
this.put(prop.getFieldName(), value);
}
}
BeanCopier.create(bean, this,
CopyOptions.create()
.setIgnoreCase(config.isIgnoreCase())
.setIgnoreError(true)
.setIgnoreNullValue(config.isIgnoreNullValue())
).copy();
}
/**