This commit is contained in:
Looly 2024-08-02 13:24:06 +08:00
parent c36669bd0b
commit 570f12e293
4 changed files with 22 additions and 26 deletions

View File

@ -26,7 +26,6 @@ import org.dromara.hutool.core.reflect.TypeUtil;
import org.dromara.hutool.core.text.CharUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.text.split.SplitUtil;
import org.dromara.hutool.json.mapper.JSONValueMapper;
import org.dromara.hutool.json.serialize.GlobalSerializeMapping;
import org.dromara.hutool.json.serialize.JSONDeserializer;
import org.dromara.hutool.json.serialize.JSONStringer;
@ -50,25 +49,6 @@ public final class InternalJSONUtil {
private InternalJSONUtil() {
}
/**
* 在需要的时候包装对象<br>
* 包装包括
* <ul>
* <li>array or collection = JSONArray</li>
* <li>map = JSONObject</li>
* <li>standard property (Double, String, et al) = 原对象</li>
* <li>来自于java包 = 字符串</li>
* <li>其它 = 尝试包装为JSONObject否则返回{@code null}</li>
* </ul>
*
* @param object 被包装的对象
* @param jsonConfig JSON选项
* @return 包装后的值null表示此值需被忽略
*/
static Object wrap(final Object object, final JSONConfig jsonConfig) {
return JSONValueMapper.of(jsonConfig).map(object);
}
/**
* 值转为String用于JSON中规则为
* <ul>

View File

@ -22,6 +22,7 @@ import org.dromara.hutool.core.lang.mutable.MutableObj;
import org.dromara.hutool.core.text.StrJoiner;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.json.mapper.JSONArrayMapper;
import org.dromara.hutool.json.mapper.JSONValueMapper;
import org.dromara.hutool.json.writer.JSONWriter;
import java.io.StringWriter;
@ -55,7 +56,11 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
/**
* 配置项
*/
private final JSONConfig config;
private JSONConfig config;
/**
* 对象转换和包装用于将Java对象和值转换为JSON值
*/
private JSONValueMapper valueMapper;
// region Constructors
@ -100,6 +105,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
public JSONArray(final int initialCapacity, final JSONConfig config) {
this.rawList = new ArrayList<>(initialCapacity);
this.config = ObjUtil.defaultIfNull(config, JSONConfig::of);
this.valueMapper = JSONValueMapper.of(this.config);
}
/**
@ -337,7 +343,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
* @return 是否加入成功
*/
public boolean add(final Object e, final Predicate<Mutable<Object>> predicate) {
return addRaw(InternalJSONUtil.wrap(e, this.config), predicate);
return addRaw(valueMapper.map(e), predicate);
}
@Override
@ -378,7 +384,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
continue;
}
this.add(index);
list.add(InternalJSONUtil.wrap(object, this.config));
list.add(valueMapper.map(object));
}
return rawList.addAll(index, list);
}
@ -438,7 +444,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
if (null == element && config.isIgnoreNullValue()) {
return null;
}
return this.rawList.set(index, InternalJSONUtil.wrap(element, this.config));
return this.rawList.set(index, valueMapper.map(element));
}
@Override
@ -450,7 +456,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
if (index < 0) {
index = 0;
}
this.rawList.add(index, InternalJSONUtil.wrap(element, this.config));
this.rawList.add(index, valueMapper.map(element));
} else {
// issue#3286, 如果用户指定的index太大容易造成Java heap space错误
if (!config.isIgnoreNullValue()) {
@ -552,6 +558,8 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
@Override
public Object clone() throws CloneNotSupportedException {
final JSONArray clone = (JSONArray) super.clone();
clone.config = this.config;
clone.valueMapper = this.valueMapper;
clone.rawList = ObjUtil.clone(this.rawList);
return clone;
}

View File

@ -22,6 +22,7 @@ import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.map.MapWrapper;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.json.mapper.JSONObjectMapper;
import org.dromara.hutool.json.mapper.JSONValueMapper;
import org.dromara.hutool.json.writer.JSONWriter;
import java.io.StringWriter;
@ -55,6 +56,10 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
* 配置项
*/
private JSONConfig config;
/**
* 对象转换和包装用于将Java对象和值转换为JSON值
*/
private JSONValueMapper valueMapper;
// -------------------------------------------------------------------------------------------------------------------- Constructor start
@ -85,6 +90,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
public JSONObject(final int capacity, final JSONConfig config) {
super(InternalJSONUtil.createRawMap(capacity, ObjUtil.defaultIfNull(config, JSONConfig.of())));
this.config = ObjUtil.defaultIfNull(config, JSONConfig.of());
this.valueMapper = JSONValueMapper.of(this.config);
}
/**
@ -440,6 +446,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
public JSONObject clone() throws CloneNotSupportedException {
final JSONObject clone = (JSONObject) super.clone();
clone.config = this.config;
clone.valueMapper = this.valueMapper;
return clone;
}
@ -479,6 +486,6 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
} else if (checkDuplicate && containsKey(key)) {
throw new JSONException("Duplicate key \"{}\"", key);
}
return super.put(key, InternalJSONUtil.wrap(value, this.config));
return super.put(key, this.valueMapper.map(value));
}
}

View File

@ -128,6 +128,7 @@ public class JSONValueMapper {
public Object map(final Object object) {
// nullJSON字符串和自定义对象原样存储
if (null == object
// 当用户自定义了对象的字符串表示形式则保留这个对象
|| null != InternalJSONUtil.getValueWriter(object)
|| object instanceof JSON //
|| object instanceof JSONStringer //