mirror of
https://gitee.com/dromara/hutool.git
synced 2026-02-09 09:16:26 +08:00
fix code
This commit is contained in:
@@ -133,7 +133,7 @@ public final class InternalJSONUtil {
|
||||
/**
|
||||
* 将Property的键转化为JSON形式<br>
|
||||
* 用于识别类似于:com.luxiaolei.package.hutool这类用点隔开的键<br>
|
||||
* 注意:不允许重复键
|
||||
* 注意:是否允许重复键,取决于JSONObject配置
|
||||
*
|
||||
* @param jsonObject JSONObject
|
||||
* @param key 键
|
||||
@@ -142,18 +142,18 @@ public final class InternalJSONUtil {
|
||||
*/
|
||||
static JSONObject propertyPut(JSONObject jsonObject, Object key, Object value, Filter<MutablePair<String, Object>> filter) {
|
||||
final String[] path = StrUtil.splitToArray(Convert.toStr(key), CharUtil.DOT);
|
||||
int last = path.length - 1;
|
||||
final int last = path.length - 1;
|
||||
JSONObject target = jsonObject;
|
||||
for (int i = 0; i < last; i += 1) {
|
||||
String segment = path[i];
|
||||
final String segment = path[i];
|
||||
JSONObject nextTarget = target.getJSONObject(segment);
|
||||
if (nextTarget == null) {
|
||||
nextTarget = new JSONObject(target.getConfig());
|
||||
target.setOnce(segment, nextTarget, filter);
|
||||
target.set(segment, nextTarget, filter, target.getConfig().isCheckDuplicate());
|
||||
}
|
||||
target = nextTarget;
|
||||
}
|
||||
target.setOnce(path[last], value, filter);
|
||||
target.set(path[last], value, filter, target.getConfig().isCheckDuplicate());
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
|
||||
@@ -45,9 +45,9 @@ public class JSONConfig implements Serializable {
|
||||
private boolean stripTrailingZeros = true;
|
||||
|
||||
/**
|
||||
* 是否忽略多个相同的key
|
||||
* 是否检查重复key
|
||||
*/
|
||||
private boolean ignoreDuplicateKey = false;
|
||||
private boolean checkDuplicate;
|
||||
|
||||
/**
|
||||
* 创建默认的配置项
|
||||
@@ -242,20 +242,24 @@ public class JSONConfig implements Serializable {
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否忽略多个相同的key
|
||||
* @return
|
||||
* 是否检查多个相同的key
|
||||
*
|
||||
* @return 是否检查多个相同的key
|
||||
* @since 5.8.5
|
||||
*/
|
||||
public boolean isIgnoreDuplicateKey() {
|
||||
return ignoreDuplicateKey;
|
||||
public boolean isCheckDuplicate() {
|
||||
return checkDuplicate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否忽略多个相同的key
|
||||
* @param ignoreDuplicateKey
|
||||
* @return
|
||||
* 是否检查多个相同的key
|
||||
*
|
||||
* @param checkDuplicate 是否检查多个相同的key
|
||||
* @return this
|
||||
* @since 5.8.5
|
||||
*/
|
||||
public JSONConfig setIgnoreDuplicateKey(boolean ignoreDuplicateKey) {
|
||||
this.ignoreDuplicateKey = ignoreDuplicateKey;
|
||||
public JSONConfig setCheckDuplicate(boolean checkDuplicate) {
|
||||
this.checkDuplicate = checkDuplicate;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,7 +236,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
Object value;
|
||||
for (String name : names) {
|
||||
value = ((Map<?, ?>) source).get(name);
|
||||
this.putOnce(name, value);
|
||||
this.set(name, value, null, getConfig().isCheckDuplicate());
|
||||
}
|
||||
} else {
|
||||
for (String name : names) {
|
||||
@@ -392,9 +392,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
// 忽略值模式下如果值为空清除key
|
||||
this.remove(key);
|
||||
} else {
|
||||
/*如果允许多个key,就不抛出异常,使用后面的值覆盖前面的值*/
|
||||
boolean ignoreDuplicateKey = this.config.isIgnoreDuplicateKey();
|
||||
if (checkDuplicate && containsKey(key) && false == ignoreDuplicateKey) {
|
||||
if (checkDuplicate && containsKey(key)) {
|
||||
throw new JSONException("Duplicate key \"{}\"", key);
|
||||
}
|
||||
|
||||
|
||||
@@ -69,7 +69,7 @@ public class JSONParser {
|
||||
throw tokener.syntaxError("Expected a ':' after a key");
|
||||
}
|
||||
|
||||
jsonObject.setOnce(key, tokener.nextValue(), filter);
|
||||
jsonObject.set(key, tokener.nextValue(), filter, jsonObject.getConfig().isCheckDuplicate());
|
||||
|
||||
// Pairs are separated by ','.
|
||||
|
||||
|
||||
@@ -88,11 +88,11 @@ public class ObjectMapper {
|
||||
if (source instanceof Map) {
|
||||
// Map
|
||||
for (final Map.Entry<?, ?> e : ((Map<?, ?>) source).entrySet()) {
|
||||
jsonObject.set(Convert.toStr(e.getKey()), e.getValue(), filter, false);
|
||||
jsonObject.set(Convert.toStr(e.getKey()), e.getValue(), filter, jsonObject.getConfig().isCheckDuplicate());
|
||||
}
|
||||
} else if (source instanceof Map.Entry) {
|
||||
final Map.Entry entry = (Map.Entry) source;
|
||||
jsonObject.set(Convert.toStr(entry.getKey()), entry.getValue(), filter, false);
|
||||
jsonObject.set(Convert.toStr(entry.getKey()), entry.getValue(), filter, jsonObject.getConfig().isCheckDuplicate());
|
||||
} else if (source instanceof CharSequence) {
|
||||
// 可能为JSON字符串
|
||||
mapFromStr((CharSequence) source, jsonObject, filter);
|
||||
|
||||
Reference in New Issue
Block a user