mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
fix code
This commit is contained in:
@@ -24,7 +24,6 @@ import org.dromara.hutool.core.map.CaseInsensitiveTreeMap;
|
||||
import org.dromara.hutool.core.text.CharUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.json.reader.JSONTokener;
|
||||
import org.dromara.hutool.json.serializer.JSONMapper;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -104,25 +103,6 @@ public final class InternalJSONUtil {
|
||||
return string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认情况下是否忽略null值的策略选择,以下对象不忽略null值,其它对象忽略:
|
||||
*
|
||||
* <pre>
|
||||
* 1. CharSequence
|
||||
* 2. JSONTokener
|
||||
* 3. Map
|
||||
* </pre>
|
||||
*
|
||||
* @param obj 需要检查的对象
|
||||
* @return 是否忽略null值
|
||||
* @since 4.3.1
|
||||
*/
|
||||
static boolean defaultIgnoreNullValue(final Object obj) {
|
||||
return (!(obj instanceof CharSequence))//
|
||||
&& (!(obj instanceof JSONTokener))//
|
||||
&& (!(obj instanceof Map));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将{@link JSONConfig}参数转换为Bean拷贝所用的{@link CopyOptions}
|
||||
*
|
||||
|
@@ -118,9 +118,8 @@ public class JSONArray extends ListWrapper<JSON> implements JSON, JSONGetter<Int
|
||||
*
|
||||
* @param value 值,可以是: Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the JSONNull.NULL。
|
||||
* @return this.
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public JSONArray set(final Object value) {
|
||||
public JSONArray addObj(final Object value) {
|
||||
this.add(this.factory.getMapper().map(value));
|
||||
return this;
|
||||
}
|
||||
@@ -138,7 +137,7 @@ public class JSONArray extends ListWrapper<JSON> implements JSON, JSONGetter<Int
|
||||
}
|
||||
final JSONObject jo = this.factory.ofObj();
|
||||
for (int i = 0; i < names.size(); i += 1) {
|
||||
jo.set(names.getStr(i), this.getObj(i));
|
||||
jo.putObj(names.getStr(i), this.getObj(i));
|
||||
}
|
||||
return jo;
|
||||
}
|
||||
@@ -169,10 +168,11 @@ public class JSONArray extends ListWrapper<JSON> implements JSON, JSONGetter<Int
|
||||
*
|
||||
* @param index 位置
|
||||
* @param element 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
|
||||
* @return 替换的值,即之前的值
|
||||
* @return this
|
||||
*/
|
||||
public JSON setValue(final int index, final Object element) {
|
||||
return set(index, this.factory.getMapper().map(element));
|
||||
public JSONArray setObj(final int index, final Object element) {
|
||||
set(index, this.factory.getMapper().map(element));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -184,17 +184,32 @@ public class JSONArray extends ListWrapper<JSON> implements JSON, JSONGetter<Int
|
||||
*/
|
||||
@Override
|
||||
public JSON set(final int index, final JSON element) {
|
||||
// 越界则追加到指定位置
|
||||
if (index >= size()) {
|
||||
add(index, element);
|
||||
return null;
|
||||
}
|
||||
if (null == element && config().isIgnoreNullValue()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 越界则追加到指定位置
|
||||
final int size = size();
|
||||
if(index == size){
|
||||
add(element);
|
||||
return null;
|
||||
}
|
||||
if (index > size) {
|
||||
add(index, element);
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.raw.set(index, element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(final JSON element) {
|
||||
if (null == element && config().isIgnoreNullValue()) {
|
||||
return false;
|
||||
}
|
||||
return super.add(element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(int index, final JSON element) {
|
||||
final boolean ignoreNullValue = config().isIgnoreNullValue();
|
||||
|
@@ -27,6 +27,7 @@ import org.dromara.hutool.json.writer.JSONWriter;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
@@ -66,7 +67,9 @@ public class JSONFactory {
|
||||
|
||||
private final JSONConfig config;
|
||||
/**
|
||||
* 过滤器,用于过滤或修改键值对,返回null表示忽略此键值对,返回非null表示修改后返回<br>
|
||||
* 过滤器,用于过滤或修改键值对<br>
|
||||
* {@link Predicate#test(Object)} 返回{@code true}表示接受,{@code false}表示忽略<br>
|
||||
* 同时{@link MutableEntry}为可变键值对,在判断逻辑中可同时修改键和值,修改后返回{@code true}<br>
|
||||
* entry中,key在JSONObject中为name,在JSONArray中为index
|
||||
*/
|
||||
private final Predicate<MutableEntry<Object, Object>> predicate;
|
||||
@@ -84,7 +87,7 @@ public class JSONFactory {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置项
|
||||
* 获取配置项,始终非空
|
||||
*
|
||||
* @return 配置项
|
||||
*/
|
||||
@@ -93,7 +96,10 @@ public class JSONFactory {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取键值对过滤器
|
||||
* 获取键值对过滤器<br>
|
||||
* {@link Predicate#test(Object)} 返回{@code true}表示接受,{@code false}表示忽略<br>
|
||||
* 同时{@link MutableEntry}为可变键值对,在判断逻辑中可同时修改键和值,修改后返回{@code true}<br>
|
||||
* entry中,key在JSONObject中为name,在JSONArray中为index
|
||||
*
|
||||
* @return 键值对过滤器
|
||||
*/
|
||||
@@ -101,6 +107,26 @@ public class JSONFactory {
|
||||
return this.predicate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行键值对过滤,如果提供的键值对执行{@link Predicate#test(Object)}返回{@code false},则忽略此键值对;<br>
|
||||
* 如果处理后返回{@code true}表示接受,调用{@link Consumer#accept(Object)}执行逻辑。<br>
|
||||
* 如果用户未定义{@link #predicate},则接受所有键值对。
|
||||
*
|
||||
* @param entry 键值对
|
||||
* @param consumer 键值对处理逻辑,如果处理后返回{@code true}表示接受,{@code false}表示忽略
|
||||
*/
|
||||
public void doPredicate(final MutableEntry<Object, Object> entry,
|
||||
final Consumer<MutableEntry<Object, Object>> consumer) {
|
||||
final Predicate<MutableEntry<Object, Object>> predicate = this.predicate;
|
||||
if (null != predicate && !predicate.test(entry)) {
|
||||
// 过滤键值对
|
||||
return;
|
||||
}
|
||||
|
||||
// 键值对处理
|
||||
consumer.accept(entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取{@link JSONMapper},用于实现Bean和JSON的转换<br>
|
||||
* 此方法使用双重检查锁实现懒加载模式,只有mapper被使用时才初始化
|
||||
@@ -154,7 +180,7 @@ public class JSONFactory {
|
||||
* @param tokener {@link JSONTokener}
|
||||
* @return {@link JSONParser}
|
||||
*/
|
||||
public JSONParser ofParser(final JSONTokener tokener){
|
||||
public JSONParser ofParser(final JSONTokener tokener) {
|
||||
return JSONParser.of(tokener, this);
|
||||
}
|
||||
|
||||
|
@@ -143,7 +143,7 @@ public class JSONObject extends MapWrapper<String, JSON> implements JSON, JSONGe
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region ----- set
|
||||
// region ----- put
|
||||
/**
|
||||
* 对值加一,如果值不存在,赋值1,如果为数字类型,做加一操作
|
||||
*
|
||||
@@ -154,7 +154,7 @@ public class JSONObject extends MapWrapper<String, JSON> implements JSON, JSONGe
|
||||
public JSONObject increment(final String key) throws JSONException {
|
||||
final JSON json = this.get(key);
|
||||
if(null == json){
|
||||
return set(key, 1);
|
||||
return putObj(key, 1);
|
||||
}
|
||||
|
||||
if(json instanceof JSONPrimitive){
|
||||
@@ -185,11 +185,11 @@ public class JSONObject extends MapWrapper<String, JSON> implements JSON, JSONGe
|
||||
public JSONObject append(final String key, final Object value) throws JSONException {
|
||||
final Object object = this.getObj(key);
|
||||
if (object == null) {
|
||||
this.set(key, value);
|
||||
this.putObj(key, value);
|
||||
} else if (object instanceof JSONArray) {
|
||||
((JSONArray) object).set(value);
|
||||
((JSONArray) object).addObj(value);
|
||||
} else {
|
||||
this.set(key, factory.ofArray().set(object).set(value));
|
||||
this.putObj(key, factory.ofArray().addObj(object).addObj(value));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
@@ -205,8 +205,8 @@ public class JSONObject extends MapWrapper<String, JSON> implements JSON, JSONGe
|
||||
* @param fields lambda,不能为空
|
||||
* @return this
|
||||
*/
|
||||
public JSONObject setFields(final SerSupplier<?>... fields) {
|
||||
Arrays.stream(fields).forEach(f -> set(LambdaUtil.getFieldName(f), f.get()));
|
||||
public JSONObject putFields(final SerSupplier<?>... fields) {
|
||||
Arrays.stream(fields).forEach(f -> putObj(LambdaUtil.getFieldName(f), f.get()));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -217,10 +217,10 @@ public class JSONObject extends MapWrapper<String, JSON> implements JSON, JSONGe
|
||||
* @return this.
|
||||
* @throws JSONException 值是无穷数字抛出此异常
|
||||
*/
|
||||
public JSONObject setAll(final Map<?, ?> map) {
|
||||
public JSONObject putAllObj(final Map<?, ?> map) {
|
||||
if(MapUtil.isNotEmpty(map)){
|
||||
for (final Entry<?, ?> entry : map.entrySet()) {
|
||||
this.set(StrUtil.toStringOrNull(entry.getKey()), entry.getValue());
|
||||
this.putObj(StrUtil.toStringOrNull(entry.getKey()), entry.getValue());
|
||||
}
|
||||
}
|
||||
return this;
|
||||
@@ -234,7 +234,7 @@ public class JSONObject extends MapWrapper<String, JSON> implements JSON, JSONGe
|
||||
* @return this.
|
||||
* @throws JSONException 值是无穷数字抛出此异常
|
||||
*/
|
||||
public JSONObject set(final String key, final Object value) throws JSONException {
|
||||
public JSONObject putObj(final String key, final Object value) throws JSONException {
|
||||
this.put(key, factory.getMapper().map(value));
|
||||
return this;
|
||||
}
|
||||
|
@@ -55,7 +55,7 @@ public class Claims implements Serializable {
|
||||
claimJSON.remove(name);
|
||||
return;
|
||||
}
|
||||
claimJSON.set(name, value);
|
||||
claimJSON.putObj(name, value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -165,20 +165,8 @@ public class JSONParser {
|
||||
// The key is followed by ':'.
|
||||
tokener.nextColon();
|
||||
|
||||
// 过滤并设置键值对
|
||||
final JSON value = nextJSON(tokener.nextClean());
|
||||
// 添加前置过滤,通过MutablePair实现过滤、修改键值对等
|
||||
final Predicate<MutableEntry<Object, Object>> predicate = factory.getPredicate();
|
||||
if (null != predicate) {
|
||||
final MutableEntry<Object, Object> entry = new MutableEntry<>(key, value);
|
||||
if (predicate.test(entry)) {
|
||||
// 使用修改后的键值对
|
||||
key = (String) entry.getKey();
|
||||
jsonObject.set(key, entry.getValue());
|
||||
}
|
||||
} else {
|
||||
jsonObject.set(key, value);
|
||||
}
|
||||
// 过滤并设置键值对,通过MutablePair实现过滤、修改键值对等
|
||||
set(jsonObject, key, nextJSON(tokener.nextClean()));
|
||||
|
||||
// Pairs are separated by ',' or ';'
|
||||
switch (tokener.nextClean()) {
|
||||
@@ -199,6 +187,27 @@ public class JSONParser {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置键值对,通过前置过滤器过滤、修改键值对等
|
||||
*
|
||||
* @param jsonObject JSON对象
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
*/
|
||||
private void set(final JSONObject jsonObject, final String key, final JSON value){
|
||||
// 添加前置过滤,通过MutablePair实现过滤、修改键值对等
|
||||
final Predicate<MutableEntry<Object, Object>> predicate = factory.getPredicate();
|
||||
if (null != predicate) {
|
||||
final MutableEntry<Object, Object> entry = new MutableEntry<>(key, value);
|
||||
if (predicate.test(entry)) {
|
||||
// 使用修改后的键值对
|
||||
jsonObject.putObj((String) entry.getKey(), entry.getValue());
|
||||
}
|
||||
} else {
|
||||
jsonObject.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析下一个值为JSONArray,第一个字符必须读取完后再调用此方法
|
||||
*
|
||||
@@ -214,23 +223,31 @@ public class JSONParser {
|
||||
return;
|
||||
} else {
|
||||
// ,value or value
|
||||
JSON value = nextJSON(CharUtil.COMMA == c ? tokener.nextClean() : c);
|
||||
final Predicate<MutableEntry<Object, Object>> predicate = factory.getPredicate();
|
||||
if (null != predicate) {
|
||||
// 使用过滤器
|
||||
final MutableEntry<Object, Object> entry = MutableEntry.of(jsonArray.size(), value);
|
||||
if (predicate.test(entry)) {
|
||||
// 使用修改后的键值对
|
||||
value = (JSON) entry.getValue();
|
||||
jsonArray.add(value);
|
||||
}
|
||||
} else {
|
||||
jsonArray.add(value);
|
||||
}
|
||||
set(jsonArray, nextJSON(CharUtil.COMMA == c ? tokener.nextClean() : c));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置数组元素,通过前置过滤器过滤、修改键值对等
|
||||
*
|
||||
* @param jsonArray JSON数组
|
||||
* @param value 值
|
||||
*/
|
||||
private void set(final JSONArray jsonArray, final JSON value) {
|
||||
final Predicate<MutableEntry<Object, Object>> predicate = factory.getPredicate();
|
||||
if (null != predicate) {
|
||||
// 使用过滤器
|
||||
final MutableEntry<Object, Object> entry = MutableEntry.of(jsonArray.size(), value);
|
||||
if (predicate.test(entry)) {
|
||||
// 使用修改后的键值对,用户修改后可能不是JSON,此处使用set,调用mapper转换
|
||||
jsonArray.setObj((Integer) entry.getKey(), entry.getValue());
|
||||
}
|
||||
} else {
|
||||
jsonArray.add(value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析为JSONPrimitive或{@code null},解析值包括:
|
||||
* <pre>
|
||||
|
@@ -147,7 +147,7 @@ public class JSONMapper implements Serializable {
|
||||
public JSONArray mapFromJSONObject(final JSONObject jsonObject) {
|
||||
final JSONArray array = factory.ofArray();
|
||||
for (final Map.Entry<String, JSON> entry : jsonObject) {
|
||||
array.set(entry);
|
||||
array.addObj(entry);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
@@ -109,7 +109,7 @@ public class ArrayTypeAdapter implements MatcherJSONSerializer<Object>, MatcherJ
|
||||
// 非标准的二进制流,则按照普通数组对待
|
||||
final JSONArray result = context.getOrCreateArray();
|
||||
for (final byte b : bytes) {
|
||||
result.set(b);
|
||||
result.addObj(b);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@@ -18,7 +18,6 @@ package org.dromara.hutool.json.serializer.impl;
|
||||
|
||||
import org.dromara.hutool.core.bean.BeanDesc;
|
||||
import org.dromara.hutool.core.bean.BeanUtil;
|
||||
import org.dromara.hutool.core.bean.copier.BeanToMapCopier;
|
||||
import org.dromara.hutool.core.bean.copier.ValueProviderToBeanCopier;
|
||||
import org.dromara.hutool.core.lang.copier.Copier;
|
||||
import org.dromara.hutool.core.reflect.ConstructorUtil;
|
||||
@@ -31,6 +30,7 @@ import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.serializer.JSONContext;
|
||||
import org.dromara.hutool.json.serializer.MatcherJSONDeserializer;
|
||||
import org.dromara.hutool.json.serializer.MatcherJSONSerializer;
|
||||
import org.dromara.hutool.json.support.BeanToJSONCopier;
|
||||
import org.dromara.hutool.json.support.JSONObjectValueProvider;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
@@ -72,14 +72,9 @@ public class BeanTypeAdapter implements MatcherJSONSerializer<Object>, MatcherJS
|
||||
|
||||
@Override
|
||||
public JSON serialize(final Object bean, final JSONContext context) {
|
||||
final JSONObject contextJson = context.getOrCreateObj();
|
||||
|
||||
final BeanToMapCopier copier = new BeanToMapCopier(
|
||||
bean,
|
||||
contextJson,
|
||||
JSONObject.class, InternalJSONUtil.toCopyOptions(context.config())
|
||||
);
|
||||
return (JSON) copier.copy();
|
||||
final BeanToJSONCopier copier = new BeanToJSONCopier(
|
||||
bean, context.getOrCreateObj(), context.getFactory());
|
||||
return copier.copy();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -59,7 +59,7 @@ public class EntryTypeAdapter implements MatcherJSONSerializer<Map.Entry<?, ?>>,
|
||||
@Override
|
||||
public JSON serialize(final Map.Entry<?, ?> bean, final JSONContext context) {
|
||||
return context.getOrCreateObj()
|
||||
.set(ConvertUtil.toStr(bean.getKey()), bean.getValue());
|
||||
.putObj(ConvertUtil.toStr(bean.getKey()), bean.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -113,7 +113,7 @@ public class IterTypeAdapter implements MatcherJSONSerializer<Object>, MatcherJS
|
||||
next = iter.next();
|
||||
// 检查循环引用
|
||||
if (next != source) {
|
||||
jsonArray.set(next);
|
||||
jsonArray.addObj(next);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -63,7 +63,7 @@ public class MapTypeAdapter implements MatcherJSONSerializer<Map<?, ?>>, Matcher
|
||||
final JSONObject result = context.getOrCreateObj();
|
||||
// 注入键值对
|
||||
for (final Map.Entry<?, ?> e : bean.entrySet()) {
|
||||
result.set(ConvertUtil.toStr(e.getKey()), e.getValue());
|
||||
result.putObj(ConvertUtil.toStr(e.getKey()), e.getValue());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@@ -87,10 +87,10 @@ public class ResourceBundleSerializer implements MatcherJSONSerializer<ResourceB
|
||||
JSONObject nextTarget = target.getJSONObject(segment);
|
||||
if (nextTarget == null) {
|
||||
nextTarget = JSONUtil.ofObj(target.config());
|
||||
target.set(segment, nextTarget);
|
||||
target.putObj(segment, nextTarget);
|
||||
}
|
||||
target = nextTarget;
|
||||
}
|
||||
target.set(path[last], value);
|
||||
target.putObj(path[last], value);
|
||||
}
|
||||
}
|
||||
|
@@ -136,24 +136,24 @@ public class TemporalTypeAdapter implements MatcherJSONSerializer<TemporalAccess
|
||||
private static void toJSONObject(final TemporalAccessor bean, final JSONObject json) {
|
||||
if (bean instanceof LocalDate) {
|
||||
final LocalDate localDate = (LocalDate) bean;
|
||||
json.set(YEAR_KEY, localDate.getYear());
|
||||
json.set(MONTH_KEY, localDate.getMonthValue());
|
||||
json.set(DAY_KEY, localDate.getDayOfMonth());
|
||||
json.putObj(YEAR_KEY, localDate.getYear());
|
||||
json.putObj(MONTH_KEY, localDate.getMonthValue());
|
||||
json.putObj(DAY_KEY, localDate.getDayOfMonth());
|
||||
} else if (bean instanceof LocalDateTime) {
|
||||
final LocalDateTime localDateTime = (LocalDateTime) bean;
|
||||
json.set(YEAR_KEY, localDateTime.getYear());
|
||||
json.set(MONTH_KEY, localDateTime.getMonthValue());
|
||||
json.set(DAY_KEY, localDateTime.getDayOfMonth());
|
||||
json.set(HOUR_KEY, localDateTime.getHour());
|
||||
json.set(MINUTE_KEY, localDateTime.getMinute());
|
||||
json.set(SECOND_KEY, localDateTime.getSecond());
|
||||
json.set(NANO_KEY, localDateTime.getNano());
|
||||
json.putObj(YEAR_KEY, localDateTime.getYear());
|
||||
json.putObj(MONTH_KEY, localDateTime.getMonthValue());
|
||||
json.putObj(DAY_KEY, localDateTime.getDayOfMonth());
|
||||
json.putObj(HOUR_KEY, localDateTime.getHour());
|
||||
json.putObj(MINUTE_KEY, localDateTime.getMinute());
|
||||
json.putObj(SECOND_KEY, localDateTime.getSecond());
|
||||
json.putObj(NANO_KEY, localDateTime.getNano());
|
||||
} else if (bean instanceof LocalTime) {
|
||||
final LocalTime localTime = (LocalTime) bean;
|
||||
json.set(HOUR_KEY, localTime.getHour());
|
||||
json.set(MINUTE_KEY, localTime.getMinute());
|
||||
json.set(SECOND_KEY, localTime.getSecond());
|
||||
json.set(NANO_KEY, localTime.getNano());
|
||||
json.putObj(HOUR_KEY, localTime.getHour());
|
||||
json.putObj(MINUTE_KEY, localTime.getMinute());
|
||||
json.putObj(SECOND_KEY, localTime.getSecond());
|
||||
json.putObj(NANO_KEY, localTime.getNano());
|
||||
} else{
|
||||
throw new JSONException("Unsupported type: {}", bean.getClass().getName());
|
||||
}
|
||||
|
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Hutool Team and hutool.cn
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.json.support;
|
||||
|
||||
import org.dromara.hutool.core.bean.BeanUtil;
|
||||
import org.dromara.hutool.core.bean.PropDesc;
|
||||
import org.dromara.hutool.core.lang.copier.Copier;
|
||||
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONFactory;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* Bean转JSON对象复制器
|
||||
*
|
||||
* @author looly
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public class BeanToJSONCopier implements Copier<JSONObject> {
|
||||
|
||||
private final Object source;
|
||||
private final JSONObject target;
|
||||
private final JSONFactory factory;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param source 源对象
|
||||
* @param target 目标JSON
|
||||
* @param factory JSON工厂
|
||||
*/
|
||||
public BeanToJSONCopier(final Object source, final JSONObject target, final JSONFactory factory) {
|
||||
this.source = source;
|
||||
this.target = target;
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject copy() {
|
||||
final JSONConfig config = factory.getConfig();
|
||||
final Map<String, PropDesc> sourcePropDescMap = BeanUtil.getBeanDesc(source.getClass())
|
||||
.getPropMap(config.isIgnoreCase());
|
||||
|
||||
sourcePropDescMap.forEach((sFieldName, sDesc) -> {
|
||||
if (null == sFieldName || !sDesc.isReadable(config.isTransientSupport())) {
|
||||
// 字段空或不可读,跳过
|
||||
return;
|
||||
}
|
||||
|
||||
final Object sValue = sDesc.getValue(this.source, config.isIgnoreError());
|
||||
putValue(sFieldName, sValue, config.isIgnoreNullValue());
|
||||
});
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
/**
|
||||
* 赋值,过滤则跳过
|
||||
*
|
||||
* @param fieldName 字段名
|
||||
* @param sValue 源值
|
||||
* @param ignoreNullValue 是否忽略null值
|
||||
*/
|
||||
private void putValue(final String fieldName, final Object sValue, final boolean ignoreNullValue) {
|
||||
final Predicate<MutableEntry<Object, Object>> predicate = factory.getPredicate();
|
||||
if (null != predicate) {
|
||||
final MutableEntry<Object, Object> entry = new MutableEntry<>(fieldName, sValue);
|
||||
if (predicate.test(entry)) {
|
||||
// 使用修改后的键值对
|
||||
target.putObj((String) entry.getKey(), entry.getValue());
|
||||
}
|
||||
} else if (null != sValue || !ignoreNullValue) {
|
||||
target.putObj(fieldName, sValue);
|
||||
}
|
||||
}
|
||||
}
|
@@ -82,9 +82,9 @@ public class JSONNodeBeanFactory implements NodeBeanFactory<JSON> {
|
||||
return bean;
|
||||
} else if (node instanceof NameNode) {
|
||||
if(bean instanceof JSONObject){
|
||||
((JSONObject) bean).set(((NameNode) node).getName(), value);
|
||||
((JSONObject) bean).putObj(((NameNode) node).getName(), value);
|
||||
} else if(bean instanceof JSONArray){
|
||||
((JSONArray) bean).setValue(Integer.parseInt(((NameNode) node).getName()), value);
|
||||
((JSONArray) bean).setObj(Integer.parseInt(((NameNode) node).getName()), value);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
Reference in New Issue
Block a user