add checkDumplicate

This commit is contained in:
Looly
2022-07-22 17:36:52 +08:00
parent fa5763e03a
commit a36a970341
4 changed files with 34 additions and 8 deletions

View File

@@ -218,11 +218,11 @@ public final class InternalJSONUtil {
JSONObject nextTarget = target.getJSONObject(segment); JSONObject nextTarget = target.getJSONObject(segment);
if (nextTarget == null) { if (nextTarget == null) {
nextTarget = new JSONObject(target.getConfig()); nextTarget = new JSONObject(target.getConfig());
target.setOnce(segment, nextTarget, predicate); target.set(segment, nextTarget, predicate);
} }
target = nextTarget; target = nextTarget;
} }
target.setOnce(path[last], value, predicate); target.set(path[last], value, predicate);
return jsonObject; return jsonObject;
} }
@@ -318,7 +318,7 @@ public final class InternalJSONUtil {
public static Writer quote(final String str, final Writer writer, final boolean isWrap) throws IORuntimeException { public static Writer quote(final String str, final Writer writer, final boolean isWrap) throws IORuntimeException {
try { try {
return doQuote(str, writer, isWrap); return doQuote(str, writer, isWrap);
} catch (IOException e) { } catch (final IOException e) {
throw new IORuntimeException(e); throw new IORuntimeException(e);
} }
} }

View File

@@ -43,6 +43,10 @@ public class JSONConfig implements Serializable {
* 是否去除末尾多余0例如如果为true,5.0返回5 * 是否去除末尾多余0例如如果为true,5.0返回5
*/ */
private boolean stripTrailingZeros = true; private boolean stripTrailingZeros = true;
/**
* 是否检查重复key
*/
private boolean checkDuplicate;
/** /**
* 创建默认的配置项 * 创建默认的配置项
@@ -211,4 +215,26 @@ public class JSONConfig implements Serializable {
this.stripTrailingZeros = stripTrailingZeros; this.stripTrailingZeros = stripTrailingZeros;
return this; return this;
} }
/**
* 是否检查多个相同的key
*
* @return 是否检查多个相同的key
* @since 5.8.5
*/
public boolean isCheckDuplicate() {
return checkDuplicate;
}
/**
* 是否检查多个相同的key
*
* @param checkDuplicate 是否检查多个相同的key
* @return this
* @since 5.8.5
*/
public JSONConfig setCheckDuplicate(boolean checkDuplicate) {
this.checkDuplicate = checkDuplicate;
return this;
}
} }

View File

@@ -198,7 +198,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
*/ */
@Override @Override
public Object put(final String key, final Object value) throws JSONException { public Object put(final String key, final Object value) throws JSONException {
return put(key, value, null, false); return put(key, value, null, getConfig().isCheckDuplicate());
} }
/** /**
@@ -210,7 +210,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
* @throws JSONException 值是无穷数字抛出此异常 * @throws JSONException 值是无穷数字抛出此异常
*/ */
public JSONObject set(final String key, final Object value) throws JSONException { public JSONObject set(final String key, final Object value) throws JSONException {
put(key, value, null, false); set(key, value, null);
return this; return this;
} }
@@ -224,8 +224,8 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
* @throws JSONException 值是无穷数字、键重复抛出异常 * @throws JSONException 值是无穷数字、键重复抛出异常
* @since 5.8.0 * @since 5.8.0
*/ */
public JSONObject setOnce(final String key, final Object value, final Predicate<MutableEntry<String, Object>> predicate) throws JSONException { public JSONObject set(final String key, final Object value, final Predicate<MutableEntry<String, Object>> predicate) throws JSONException {
put(key, value, predicate, true); put(key, value, predicate, getConfig().isCheckDuplicate());
return this; return this;
} }

View File

@@ -74,7 +74,7 @@ public class JSONParser {
throw tokener.syntaxError("Expected a ':' after a key"); throw tokener.syntaxError("Expected a ':' after a key");
} }
jsonObject.setOnce(key, tokener.nextValue(), predicate); jsonObject.set(key, tokener.nextValue(), predicate);
// Pairs are separated by ','. // Pairs are separated by ','.