!1431 fix(ObjectMapper):解决掉过滤器对Bean复制无效的问题

Merge pull request !1431 from 绯雾sama/v5-dev
This commit is contained in:
Looly
2026-01-03 10:11:42 +00:00
committed by Gitee
2 changed files with 37 additions and 3 deletions

View File

@@ -191,6 +191,30 @@ public final class InternalJSONUtil {
.setTransientSupport(config.isTransientSupport());
}
/**
* 将 {@link JSONConfig} 的参数和过滤器 {@link Filter}转换为 Bean 拷贝所需的 {@link CopyOptions}
*
* @param config {@link JSONConfig}
* @param filter {@link Filter}
* @return {@link CopyOptions}
*/
static CopyOptions toCopyOptions(JSONConfig config, Filter<MutablePair<String, Object>> filter) {
CopyOptions copyOptions = CopyOptions.create()
.setIgnoreCase(config.isIgnoreCase())
.setIgnoreError(config.isIgnoreError())
.setIgnoreNullValue(config.isIgnoreNullValue())
.setTransientSupport(config.isTransientSupport());
if (filter != null) {
copyOptions.setPropertiesFilter((field, value) -> {
MutablePair<String, Object> pair = new MutablePair<>(field.getName(), value);
return filter.accept(pair);
});
}
return copyOptions;
}
/**
* 根据配置创建对应的原始Map
*

View File

@@ -110,11 +110,10 @@ public class ObjectMapper {
// JSONTokener
mapFromResourceBundle((ResourceBundle) source, jsonObject, filter);
} else if (RecordUtil.isRecord(source.getClass())) {
mapFromBean(source, jsonObject);
mapFromBean(source, jsonObject,filter);
} else if (BeanUtil.isReadableBean(source.getClass())) {
// 普通Bean
// TODO 过滤器对Bean无效需补充。
mapFromBean(source, jsonObject);
mapFromBean(source, jsonObject,filter);
}
// 跳过空对象
@@ -263,7 +262,18 @@ public class ObjectMapper {
* @param bean Bean对象
* @param jsonObject {@link JSONObject}
*/
@Deprecated
private static void mapFromBean(Object bean, JSONObject jsonObject) {
BeanUtil.beanToMap(bean, jsonObject, InternalJSONUtil.toCopyOptions(jsonObject.getConfig()));
}
/**
* 从Bean转换可添加过滤器
* @param bean Bean对象
* @param jsonObject {@link JSONObject}
* @param filter {@link Filter}
*/
private static void mapFromBean(Object bean, JSONObject jsonObject,Filter<MutablePair<String,Object>> filter) {
BeanUtil.beanToMap(bean, jsonObject, InternalJSONUtil.toCopyOptions(jsonObject.getConfig(), filter));
}
}