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.CharUtil;
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
import org.dromara.hutool.core.text.split.SplitUtil;
|
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.GlobalSerializeMapping;
|
||||||
import org.dromara.hutool.json.serialize.JSONDeserializer;
|
import org.dromara.hutool.json.serialize.JSONDeserializer;
|
||||||
import org.dromara.hutool.json.serialize.JSONStringer;
|
import org.dromara.hutool.json.serialize.JSONStringer;
|
||||||
@ -50,25 +49,6 @@ public final class InternalJSONUtil {
|
|||||||
private 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中。规则为:
|
* 值转为String,用于JSON中。规则为:
|
||||||
* <ul>
|
* <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.text.StrJoiner;
|
||||||
import org.dromara.hutool.core.util.ObjUtil;
|
import org.dromara.hutool.core.util.ObjUtil;
|
||||||
import org.dromara.hutool.json.mapper.JSONArrayMapper;
|
import org.dromara.hutool.json.mapper.JSONArrayMapper;
|
||||||
|
import org.dromara.hutool.json.mapper.JSONValueMapper;
|
||||||
import org.dromara.hutool.json.writer.JSONWriter;
|
import org.dromara.hutool.json.writer.JSONWriter;
|
||||||
|
|
||||||
import java.io.StringWriter;
|
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
|
// region Constructors
|
||||||
|
|
||||||
@ -100,6 +105,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
|||||||
public JSONArray(final int initialCapacity, final JSONConfig config) {
|
public JSONArray(final int initialCapacity, final JSONConfig config) {
|
||||||
this.rawList = new ArrayList<>(initialCapacity);
|
this.rawList = new ArrayList<>(initialCapacity);
|
||||||
this.config = ObjUtil.defaultIfNull(config, JSONConfig::of);
|
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 是否加入成功
|
* @return 是否加入成功
|
||||||
*/
|
*/
|
||||||
public boolean add(final Object e, final Predicate<Mutable<Object>> predicate) {
|
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
|
@Override
|
||||||
@ -378,7 +384,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
this.add(index);
|
this.add(index);
|
||||||
list.add(InternalJSONUtil.wrap(object, this.config));
|
list.add(valueMapper.map(object));
|
||||||
}
|
}
|
||||||
return rawList.addAll(index, list);
|
return rawList.addAll(index, list);
|
||||||
}
|
}
|
||||||
@ -438,7 +444,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
|||||||
if (null == element && config.isIgnoreNullValue()) {
|
if (null == element && config.isIgnoreNullValue()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return this.rawList.set(index, InternalJSONUtil.wrap(element, this.config));
|
return this.rawList.set(index, valueMapper.map(element));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -450,7 +456,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
|||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
index = 0;
|
index = 0;
|
||||||
}
|
}
|
||||||
this.rawList.add(index, InternalJSONUtil.wrap(element, this.config));
|
this.rawList.add(index, valueMapper.map(element));
|
||||||
} else {
|
} else {
|
||||||
// issue#3286, 如果用户指定的index太大,容易造成Java heap space错误。
|
// issue#3286, 如果用户指定的index太大,容易造成Java heap space错误。
|
||||||
if (!config.isIgnoreNullValue()) {
|
if (!config.isIgnoreNullValue()) {
|
||||||
@ -552,6 +558,8 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
|||||||
@Override
|
@Override
|
||||||
public Object clone() throws CloneNotSupportedException {
|
public Object clone() throws CloneNotSupportedException {
|
||||||
final JSONArray clone = (JSONArray) super.clone();
|
final JSONArray clone = (JSONArray) super.clone();
|
||||||
|
clone.config = this.config;
|
||||||
|
clone.valueMapper = this.valueMapper;
|
||||||
clone.rawList = ObjUtil.clone(this.rawList);
|
clone.rawList = ObjUtil.clone(this.rawList);
|
||||||
return clone;
|
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.map.MapWrapper;
|
||||||
import org.dromara.hutool.core.util.ObjUtil;
|
import org.dromara.hutool.core.util.ObjUtil;
|
||||||
import org.dromara.hutool.json.mapper.JSONObjectMapper;
|
import org.dromara.hutool.json.mapper.JSONObjectMapper;
|
||||||
|
import org.dromara.hutool.json.mapper.JSONValueMapper;
|
||||||
import org.dromara.hutool.json.writer.JSONWriter;
|
import org.dromara.hutool.json.writer.JSONWriter;
|
||||||
|
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
@ -55,6 +56,10 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
|||||||
* 配置项
|
* 配置项
|
||||||
*/
|
*/
|
||||||
private JSONConfig config;
|
private JSONConfig config;
|
||||||
|
/**
|
||||||
|
* 对象转换和包装,用于将Java对象和值转换为JSON值
|
||||||
|
*/
|
||||||
|
private JSONValueMapper valueMapper;
|
||||||
|
|
||||||
// -------------------------------------------------------------------------------------------------------------------- Constructor start
|
// -------------------------------------------------------------------------------------------------------------------- Constructor start
|
||||||
|
|
||||||
@ -85,6 +90,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
|||||||
public JSONObject(final int capacity, final JSONConfig config) {
|
public JSONObject(final int capacity, final JSONConfig config) {
|
||||||
super(InternalJSONUtil.createRawMap(capacity, ObjUtil.defaultIfNull(config, JSONConfig.of())));
|
super(InternalJSONUtil.createRawMap(capacity, ObjUtil.defaultIfNull(config, JSONConfig.of())));
|
||||||
this.config = 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 {
|
public JSONObject clone() throws CloneNotSupportedException {
|
||||||
final JSONObject clone = (JSONObject) super.clone();
|
final JSONObject clone = (JSONObject) super.clone();
|
||||||
clone.config = this.config;
|
clone.config = this.config;
|
||||||
|
clone.valueMapper = this.valueMapper;
|
||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -479,6 +486,6 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
|||||||
} else if (checkDuplicate && containsKey(key)) {
|
} else if (checkDuplicate && containsKey(key)) {
|
||||||
throw new JSONException("Duplicate key \"{}\"", 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) {
|
public Object map(final Object object) {
|
||||||
// null、JSON、字符串和自定义对象原样存储
|
// null、JSON、字符串和自定义对象原样存储
|
||||||
if (null == object
|
if (null == object
|
||||||
|
// 当用户自定义了对象的字符串表示形式,则保留这个对象
|
||||||
|| null != InternalJSONUtil.getValueWriter(object)
|
|| null != InternalJSONUtil.getValueWriter(object)
|
||||||
|| object instanceof JSON //
|
|| object instanceof JSON //
|
||||||
|| object instanceof JSONStringer //
|
|| object instanceof JSONStringer //
|
||||||
|
Loading…
x
Reference in New Issue
Block a user