deprecated json isOrder

This commit is contained in:
Looly
2022-03-28 13:29:31 +08:00
parent 695ea8ae3b
commit f69d49593b
11 changed files with 81 additions and 68 deletions

View File

@@ -10,9 +10,7 @@ import cn.hutool.core.util.StrUtil;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.SortedMap;
/**
* 内部JSON工具类仅用于JSON内部使用
@@ -168,31 +166,6 @@ public final class InternalJSONUtil {
&& (false == (obj instanceof Map));
}
/**
* 判断给定对象是否有序,用于辅助创建{@link JSONObject}时是否有序
*
* <ul>
* <li>对象为{@link LinkedHashMap}子类或{@link LinkedHashMap}子类</li>
* <li>对象实现</li>
* </ul>
*
* @param value 被转换的对象
* @return 是否有序
* @since 5.7.0
*/
static boolean isOrder(Object value) {
if (value instanceof LinkedHashMap || value instanceof SortedMap) {
return true;
} else if (value instanceof JSONGetter) {
final JSONConfig config = ((JSONGetter<?>) value).getConfig();
if (null != config) {
return config.isOrder();
}
}
return false;
}
/**
* 将{@link JSONConfig}参数转换为Bean拷贝所用的{@link CopyOptions}
*

View File

@@ -15,11 +15,7 @@ public class JSONConfig implements Serializable {
private static final long serialVersionUID = 119730355204738278L;
/**
* 是否有序顺序按照加入顺序排序只针对JSONObject有效
*/
private boolean order;
/**
* 键排序规则,{@code null}表示不排序,不排序情况下,如果{@link #order}为{@code true}按照加入顺序排序否则按照hash排序
* 键排序规则,{@code null}表示不排序,不排序情况下,按照加入顺序排序
*/
private Comparator<String> keyComparator;
/**
@@ -61,9 +57,11 @@ public class JSONConfig implements Serializable {
* 是否有序顺序按照加入顺序排序只针对JSONObject有效
*
* @return 是否有序
* @deprecated 始终返回 {@code true}
*/
@Deprecated
public boolean isOrder() {
return order;
return true;
}
/**
@@ -71,15 +69,17 @@ public class JSONConfig implements Serializable {
*
* @param order 是否有序
* @return this
* @deprecated 始终有序,无需设置
*/
@SuppressWarnings("unused")
@Deprecated
public JSONConfig setOrder(boolean order) {
this.order = order;
return this;
}
/**
* 获取键排序规则<br>
* 键排序规则,{@code null}表示不排序,不排序情况下,如果{@link #order}为{@code true}按照加入顺序排序否则按照hash排序
* 键排序规则,{@code null}表示不排序,不排序情况下,按照加入顺序排序
*
* @return 键排序规则
* @since 5.7.21
@@ -100,7 +100,7 @@ public class JSONConfig implements Serializable {
/**
* 设置键排序规则<br>
* 键排序规则,{@code null}表示不排序,不排序情况下,如果{@link #order}为{@code true}按照加入顺序排序否则按照hash排序
* 键排序规则,{@code null}表示不排序,不排序情况下,按照加入顺序排序
*
* @param keyComparator 键排序规则
* @return this

View File

@@ -27,6 +27,7 @@ import java.math.BigInteger;
import java.util.Collection;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.TreeMap;
@@ -91,9 +92,12 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
* @param isIgnoreCase 是否忽略KEY大小写
* @param isOrder 是否有序
* @since 3.3.1
* @deprecated isOrder无效
*/
@SuppressWarnings("unused")
@Deprecated
public JSONObject(int capacity, boolean isIgnoreCase, boolean isOrder) {
this(capacity, JSONConfig.create().setIgnoreCase(isIgnoreCase).setOrder(isOrder));
this(capacity, JSONConfig.create().setIgnoreCase(isIgnoreCase));
}
/**
@@ -148,7 +152,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
* @since 3.0.9
*/
public JSONObject(Object source, boolean ignoreNullValue) {
this(source, ignoreNullValue, InternalJSONUtil.isOrder(source));
this(source, JSONConfig.create().setIgnoreNullValue(ignoreNullValue));
}
/**
@@ -164,9 +168,12 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
* @param ignoreNullValue 是否忽略空值如果source为JSON字符串不忽略空值
* @param isOrder 是否有序
* @since 4.2.2
* @deprecated isOrder参数不再需要JSONObject默认有序
*/
@SuppressWarnings("unused")
@Deprecated
public JSONObject(Object source, boolean ignoreNullValue, boolean isOrder) {
this(source, JSONConfig.create().setOrder(isOrder)//
this(source, JSONConfig.create()//
.setIgnoreCase((source instanceof CaseInsensitiveMap))//
.setIgnoreNullValue(ignoreNullValue)
);
@@ -239,9 +246,12 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
* @param isOrder 是否有序
* @throws JSONException JSON字符串语法错误
* @since 4.2.2
* @deprecated isOrder无效
*/
@SuppressWarnings("unused")
@Deprecated
public JSONObject(CharSequence source, boolean isOrder) throws JSONException {
this(source, JSONConfig.create().setOrder(isOrder));
this(source, JSONConfig.create());
}
// -------------------------------------------------------------------------------------------------------------------- Constructor end
@@ -689,17 +699,15 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
final Comparator<String> keyComparator = config.getKeyComparator();
if (config.isIgnoreCase()) {
if (null != keyComparator) {
// 比较器存在情况下isOrder无效
rawHashMap = new CaseInsensitiveTreeMap<>(keyComparator);
} else {
rawHashMap = config.isOrder() ? new CaseInsensitiveLinkedMap<>(capacity) : new CaseInsensitiveMap<>(capacity);
rawHashMap = new CaseInsensitiveLinkedMap<>(capacity);
}
} else {
if (null != keyComparator) {
// 比较器存在情况下isOrder无效
rawHashMap = new TreeMap<>(keyComparator);
} else {
rawHashMap = MapUtil.newHashMap(capacity, config.isOrder());
rawHashMap = new LinkedHashMap<>(capacity);
}
}
return rawHashMap;

View File

@@ -110,14 +110,7 @@ public class JSONUtil {
* @since 5.3.1
*/
public static JSONObject parseObj(Object obj, JSONConfig config) {
// 默认配置,根据对象类型决定是否有序
if(null == config){
config = JSONConfig.create();
if(InternalJSONUtil.isOrder(obj)){
config.setOrder(true);
}
}
return new JSONObject(obj, config);
return new JSONObject(obj, ObjectUtil.defaultIfNull(config, JSONConfig::create));
}
/**
@@ -129,7 +122,7 @@ public class JSONUtil {
* @since 3.0.9
*/
public static JSONObject parseObj(Object obj, boolean ignoreNullValue) {
return parseObj(obj, ignoreNullValue, InternalJSONUtil.isOrder(obj));
return new JSONObject(obj, ignoreNullValue);
}
/**
@@ -140,9 +133,12 @@ public class JSONUtil {
* @param isOrder 是否有序
* @return JSONObject
* @since 4.2.2
* @deprecated isOrder参数不再有效
*/
@SuppressWarnings("unused")
@Deprecated
public static JSONObject parseObj(Object obj, boolean ignoreNullValue, boolean isOrder) {
return new JSONObject(obj, ignoreNullValue, isOrder);
return new JSONObject(obj, ignoreNullValue);
}
/**