This commit is contained in:
Looly
2022-06-07 15:31:55 +08:00
parent 084870261f
commit 9b9b35ffc2
22 changed files with 165 additions and 122 deletions

View File

@@ -184,7 +184,7 @@ public final class InternalJSONUtil {
* @since 5.8.0
*/
static CopyOptions toCopyOptions(final JSONConfig config) {
return CopyOptions.create()
return CopyOptions.of()
.setIgnoreCase(config.isIgnoreCase())
.setIgnoreError(config.isIgnoreError())
.setIgnoreNullValue(config.isIgnoreNullValue())

View File

@@ -1,6 +1,7 @@
package cn.hutool.json;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.iter.ArrayIter;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.IoUtil;
@@ -110,8 +111,7 @@ public class ObjectMapper {
mapFromResourceBundle((ResourceBundle) source, jsonObject, filter);
} else if (BeanUtil.isReadableBean(source.getClass())) {
// 普通Bean
// TODO 过滤器对Bean无效需补充。
mapFromBean(source, jsonObject);
mapFromBean(source, jsonObject, filter);
} else {
// 不支持对象类型转换为JSONObject
throw new JSONException("Unsupported type [{}] to JSONObject!", source.getClass());
@@ -248,7 +248,9 @@ public class ObjectMapper {
* @param bean Bean对象
* @param jsonObject {@link JSONObject}
*/
private static void mapFromBean(final Object bean, final JSONObject jsonObject) {
BeanUtil.beanToMap(bean, jsonObject, InternalJSONUtil.toCopyOptions(jsonObject.getConfig()));
private static void mapFromBean(final Object bean, final JSONObject jsonObject, final Filter<MutableEntry<String, Object>> filter) {
final CopyOptions copyOptions = InternalJSONUtil.toCopyOptions(jsonObject.getConfig());
copyOptions.setFieldEditor((entry -> filter.accept(entry) ? entry : null));
BeanUtil.beanToMap(bean, jsonObject, copyOptions);
}
}