mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
c36669bd0b
commit
570f12e293
@ -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>
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -128,6 +128,7 @@ public class JSONValueMapper {
|
||||
public Object map(final Object object) {
|
||||
// null、JSON、字符串和自定义对象原样存储
|
||||
if (null == object
|
||||
// 当用户自定义了对象的字符串表示形式,则保留这个对象
|
||||
|| null != InternalJSONUtil.getValueWriter(object)
|
||||
|| object instanceof JSON //
|
||||
|| object instanceof JSONStringer //
|
||||
|
Loading…
x
Reference in New Issue
Block a user