修复NumberWithFormat没有实现Comparable接口导致的JSON排序报错问题(issue#ID61QR@Gitee)

This commit is contained in:
Looly
2025-11-12 22:22:29 +08:00
parent 9b3414b397
commit 41307a6e3d
4 changed files with 32 additions and 3 deletions

View File

@@ -1,7 +1,7 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.42(2025-11-04)
# 5.8.42(2025-11-12)
### 🐣新特性
* 【core 】 `ListUtil`增加`zip`方法pr#4052@Github
@@ -17,6 +17,7 @@
* 【http 】 修复`HttpConnection.reflectSetMethod`反射在JDK9+权限问题issue#4109@Github
* 【http 】 修复`JsonUtil.toJsonStr`对Boolean和Number返回错误问题issue#4109@Github
* 【core 】 修复`FileUtil.listFileNames`相对路径index混乱问题issue#4121@Github
* 【core 】 修复`NumberWithFormat`没有实现Comparable接口导致的JSON排序报错问题issue#ID61QR@Gitee
-------------------------------------------------------------------------------------------------------------
# 5.8.41(2025-10-12)

View File

@@ -13,7 +13,7 @@ import java.util.Date;
* @author looly
* @since 5.8.13
*/
public class NumberWithFormat extends Number implements TypeConverter {
public class NumberWithFormat extends Number implements TypeConverter, Comparable<NumberWithFormat> {
private static final long serialVersionUID = 1L;
private final Number number;
@@ -86,4 +86,13 @@ public class NumberWithFormat extends Number implements TypeConverter {
public String toString() {
return this.number.toString();
}
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public int compareTo(NumberWithFormat o) {
if(this.number instanceof Comparable && o.getNumber() instanceof Comparable) {
return ((Comparable) this.number).compareTo(o.getNumber());
}
return Double.compare(this.doubleValue(), o.doubleValue());
}
}

View File

@@ -168,7 +168,7 @@ public class MutableLong extends Number implements Comparable<MutableLong>, Muta
* </ol>
*
* @param obj 比对的对象
* @return 相同返回<code>true</code>,否则 {@code false}
* @return 相同返回{@code true},否则 {@code false}
*/
@Override
public boolean equals(final Object obj) {

View File

@@ -0,0 +1,19 @@
package cn.hutool.json;
import cn.hutool.core.map.MapUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Map;
public class IssueID61QRTest {
@Test
public void testName() {
JSONObject map1 = JSONUtil.createObj(new JSONConfig().setDateFormat("yyyy"));
// JSONObject map1 = JSONUtil.createObj();
map1.set("a", 3);
map1.set("b", 5);
map1.set("c", 5432);
Assertions.assertEquals("{c=5432, b=5, a=3}", MapUtil.sortByValue(JSONUtil.toBean(map1, Map.class), true).toString());
}
}