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

@ -2,7 +2,7 @@
# 🚀Changelog # 🚀Changelog
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.8.0 (2022-03-27) # 5.8.0 (2022-03-28)
### ❌不兼容特性 ### ❌不兼容特性
* 【db 】 【不向下兼容 】增加MongoDB4.x支持返回MongoClient变更pr#568@Gitee * 【db 】 【不向下兼容 】增加MongoDB4.x支持返回MongoClient变更pr#568@Gitee
@ -17,6 +17,7 @@
* 【poi 】 【可能兼容问题】ExcelUtil.getBigWriter返回值改为BigExcelWriter * 【poi 】 【可能兼容问题】ExcelUtil.getBigWriter返回值改为BigExcelWriter
* 【core 】 【可能兼容问题】Opt.ofEmptyAble参数由List改为Collection子类pr#580@Gitee * 【core 】 【可能兼容问题】Opt.ofEmptyAble参数由List改为Collection子类pr#580@Gitee
* 【json 】 【可能兼容问题】JSON转Bean时使用JSON本身的相关设置而非默认issue#2212@Github * 【json 】 【可能兼容问题】JSON转Bean时使用JSON本身的相关设置而非默认issue#2212@Github
* 【json 】 【可能兼容问题】JSONConfig中isOrder废弃默认全部有序
### 🐣新特性 ### 🐣新特性
* 【http 】 HttpRequest.form采用TableMap方式issue#I4W427@Gitee * 【http 】 HttpRequest.form采用TableMap方式issue#I4W427@Gitee

View File

@ -10,9 +10,7 @@ import cn.hutool.core.util.StrUtil;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.SortedMap;
/** /**
* 内部JSON工具类仅用于JSON内部使用 * 内部JSON工具类仅用于JSON内部使用
@ -168,31 +166,6 @@ public final class InternalJSONUtil {
&& (false == (obj instanceof Map)); && (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} * {@link JSONConfig}参数转换为Bean拷贝所用的{@link CopyOptions}
* *

View File

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

View File

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

View File

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

View File

@ -0,0 +1,38 @@
package cn.hutool.json;
import lombok.Data;
import org.junit.Assert;
import org.junit.Test;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class Issue2223Test {
@Test
public void toStrOrderTest() {
Map<String, Long> m1 = new LinkedHashMap<>();
for (long i = 0; i < 5; i++) {
m1.put("2022/" + i, i);
}
Assert.assertEquals("{\"2022/0\":0,\"2022/1\":1,\"2022/2\":2,\"2022/3\":3,\"2022/4\":4}", JSONUtil.toJsonStr(m1));
Map<String, Map<String, Long>> map1 = new HashMap<>();
map1.put("m1", m1);
Assert.assertEquals("{\"m1\":{\"2022/0\":0,\"2022/1\":1,\"2022/2\":2,\"2022/3\":3,\"2022/4\":4}}",
JSONUtil.toJsonStr(map1, JSONConfig.create()));
final BeanDemo beanDemo = new BeanDemo();
beanDemo.setMap1(map1);
Assert.assertEquals("{\"map1\":{\"m1\":{\"2022/0\":0,\"2022/1\":1,\"2022/2\":2,\"2022/3\":3,\"2022/4\":4}}}",
JSONUtil.toJsonStr(beanDemo, JSONConfig.create()));
}
@Data
static class BeanDemo {
Map<String, Map<String, Long>> map1;
}
}

View File

@ -31,6 +31,6 @@ public class Issues1881Test {
holderContactVOList.add(new ThingsHolderContactVO().setId(1L).setName("1")); holderContactVOList.add(new ThingsHolderContactVO().setId(1L).setName("1"));
holderContactVOList.add(new ThingsHolderContactVO().setId(2L).setName("2")); holderContactVOList.add(new ThingsHolderContactVO().setId(2L).setName("2"));
Assert.assertEquals("[{\"name\":\"1\",\"id\":1},{\"name\":\"2\",\"id\":2}]", JSONUtil.parseArray(holderContactVOList).toString()); Assert.assertEquals("[{\"id\":1,\"name\":\"1\"},{\"id\":2,\"name\":\"2\"}]", JSONUtil.parseArray(holderContactVOList).toString());
} }
} }

View File

@ -26,7 +26,7 @@ public class JSONNullTest {
" \"device_model\": null,\n" + " \"device_model\": null,\n" +
" \"device_status_date\": null,\n" + " \"device_status_date\": null,\n" +
" \"imsi\": null,\n" + " \"imsi\": null,\n" +
" \"act_date\": \"2021-07-23T06:23:26.000+00:00\"}", true, true); " \"act_date\": \"2021-07-23T06:23:26.000+00:00\"}", true);
Assert.assertFalse(bodyjson.containsKey("device_model")); Assert.assertFalse(bodyjson.containsKey("device_model"));
Assert.assertFalse(bodyjson.containsKey("device_status_date")); Assert.assertFalse(bodyjson.containsKey("device_status_date"));
Assert.assertFalse(bodyjson.containsKey("imsi")); Assert.assertFalse(bodyjson.containsKey("imsi"));

View File

@ -289,7 +289,7 @@ public class JSONObjectTest {
userA.setDate(new Date()); userA.setDate(new Date());
userA.setSqs(CollectionUtil.newArrayList(new Seq(null), new Seq("seq2"))); userA.setSqs(CollectionUtil.newArrayList(new Seq(null), new Seq("seq2")));
JSONObject json = JSONUtil.parseObj(userA, false, true); JSONObject json = JSONUtil.parseObj(userA, false);
Assert.assertTrue(json.containsKey("a")); Assert.assertTrue(json.containsKey("a"));
Assert.assertTrue(json.getJSONArray("sqs").getJSONObject(0).containsKey("seq")); Assert.assertTrue(json.getJSONArray("sqs").getJSONObject(0).containsKey("seq"));
@ -422,7 +422,6 @@ public class JSONObjectTest {
public void setDateFormatTest() { public void setDateFormatTest() {
JSONConfig jsonConfig = JSONConfig.create(); JSONConfig jsonConfig = JSONConfig.create();
jsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss"); jsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
jsonConfig.setOrder(true);
JSONObject json = new JSONObject(jsonConfig); JSONObject json = new JSONObject(jsonConfig);
json.append("date", DateUtil.parse("2020-06-05 11:16:11")); json.append("date", DateUtil.parse("2020-06-05 11:16:11"));
@ -435,7 +434,6 @@ public class JSONObjectTest {
public void setDateFormatTest2() { public void setDateFormatTest2() {
JSONConfig jsonConfig = JSONConfig.create(); JSONConfig jsonConfig = JSONConfig.create();
jsonConfig.setDateFormat("yyyy#MM#dd"); jsonConfig.setDateFormat("yyyy#MM#dd");
jsonConfig.setOrder(true);
Date date = DateUtil.parse("2020-06-05 11:16:11"); Date date = DateUtil.parse("2020-06-05 11:16:11");
JSONObject json = new JSONObject(jsonConfig); JSONObject json = new JSONObject(jsonConfig);
@ -456,7 +454,6 @@ public class JSONObjectTest {
public void setCustomDateFormatTest() { public void setCustomDateFormatTest() {
JSONConfig jsonConfig = JSONConfig.create(); JSONConfig jsonConfig = JSONConfig.create();
jsonConfig.setDateFormat("#sss"); jsonConfig.setDateFormat("#sss");
jsonConfig.setOrder(true);
Date date = DateUtil.parse("2020-06-05 11:16:11"); Date date = DateUtil.parse("2020-06-05 11:16:11");
JSONObject json = new JSONObject(jsonConfig); JSONObject json = new JSONObject(jsonConfig);
@ -616,7 +613,7 @@ public class JSONObjectTest {
@Test @Test
public void filterIncludeTest() { public void filterIncludeTest() {
JSONObject json1 = JSONUtil.createObj(JSONConfig.create().setOrder(true)) JSONObject json1 = JSONUtil.createObj(JSONConfig.create())
.set("a", "value1") .set("a", "value1")
.set("b", "value2") .set("b", "value2")
.set("c", "value3") .set("c", "value3")
@ -628,7 +625,7 @@ public class JSONObjectTest {
@Test @Test
public void filterExcludeTest() { public void filterExcludeTest() {
JSONObject json1 = JSONUtil.createObj(JSONConfig.create().setOrder(true)) JSONObject json1 = JSONUtil.createObj(JSONConfig.create())
.set("a", "value1") .set("a", "value1")
.set("b", "value2") .set("b", "value2")
.set("c", "value3") .set("c", "value3")
@ -640,7 +637,7 @@ public class JSONObjectTest {
@Test @Test
public void editTest() { public void editTest() {
JSONObject json1 = JSONUtil.createObj(JSONConfig.create().setOrder(true)) JSONObject json1 = JSONUtil.createObj(JSONConfig.create())
.set("a", "value1") .set("a", "value1")
.set("b", "value2") .set("b", "value2")
.set("c", "value3") .set("c", "value3")
@ -660,7 +657,7 @@ public class JSONObjectTest {
@Test @Test
public void toUnderLineCaseTest() { public void toUnderLineCaseTest() {
JSONObject json1 = JSONUtil.createObj(JSONConfig.create().setOrder(true)) JSONObject json1 = JSONUtil.createObj(JSONConfig.create())
.set("aKey", "value1") .set("aKey", "value1")
.set("bJob", "value2") .set("bJob", "value2")
.set("cGood", "value3") .set("cGood", "value3")
@ -675,7 +672,7 @@ public class JSONObjectTest {
@Test @Test
public void nullToEmptyTest() { public void nullToEmptyTest() {
JSONObject json1 = JSONUtil.createObj(JSONConfig.create().setOrder(true).setIgnoreNullValue(false)) JSONObject json1 = JSONUtil.createObj(JSONConfig.create().setIgnoreNullValue(false))
.set("a", null) .set("a", null)
.set("b", "value2"); .set("b", "value2");

View File

@ -21,7 +21,7 @@ public class TransientTest {
//noinspection MismatchedQueryAndUpdateOfCollection //noinspection MismatchedQueryAndUpdateOfCollection
final JSONObject jsonObject = new JSONObject(detailBill, final JSONObject jsonObject = new JSONObject(detailBill,
JSONConfig.create().setTransientSupport(false)); JSONConfig.create().setTransientSupport(false));
Assert.assertEquals("{\"bizNo\":\"bizNo\",\"id\":\"3243\"}", jsonObject.toString()); Assert.assertEquals("{\"id\":\"3243\",\"bizNo\":\"bizNo\"}", jsonObject.toString());
} }
@Test @Test

View File

@ -21,7 +21,7 @@ public class Claims implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
// 时间使用秒级时间戳表示 // 时间使用秒级时间戳表示
private final JSONConfig CONFIG = JSONConfig.create().setDateFormat("#sss").setOrder(true); private final JSONConfig CONFIG = JSONConfig.create().setDateFormat("#sss");
private JSONObject claimJSON; private JSONObject claimJSON;