mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix parse bug
This commit is contained in:
parent
878c0169ea
commit
395942298e
@ -3,7 +3,7 @@
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# 5.7.0 (2021-06-13)
|
||||
# 5.7.0 (2021-06-15)
|
||||
|
||||
### 🐣新特性
|
||||
* 【jwt 】 添加JWT模块,实现了JWT的创建、解析和验证
|
||||
@ -17,6 +17,7 @@
|
||||
### 🐞Bug修复
|
||||
* 【db 】 修复count方法丢失参数问题(issue#I3VBSL@Gitee)
|
||||
* 【db 】 修复SpringUtil工具在`@PostConstruct` 注解标注的方法下失效问题(pr#341@Gitee)
|
||||
* 【db 】 修复JSONUtil.parse方法未判断有序问题(issue#I3VHVY@Gitee)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
@ -10,7 +10,7 @@ import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* optional对象判空,参考:https://mp.weixin.qq.com/s/0c8iC0OTtx5LqPkhvkK0tw<br>
|
||||
* from:https://github.com/looly/hutool/pull/1182
|
||||
* from:https://github.com/dromara/hutool/pull/1182
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @author totalo
|
||||
@ -149,4 +149,4 @@ public final class OptionalBean<T> {
|
||||
public int hashCode() {
|
||||
return Objects.hashCode(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ public class OptionalBeanTest {
|
||||
String value1 = OptionalBean.ofNullable(user)
|
||||
.getBean(User::getSchool)
|
||||
.getBean(User.School::getAddress).get();
|
||||
Assert.assertNull(value1);
|
||||
Assert.assertNull(value1);
|
||||
|
||||
boolean present = OptionalBean.ofNullable(user)
|
||||
.getBean(User::getSchool)
|
||||
|
@ -16,7 +16,9 @@ import java.util.Calendar;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.SortedMap;
|
||||
|
||||
/**
|
||||
* 内部JSON工具类,仅用于JSON内部使用
|
||||
@ -198,7 +200,7 @@ final class InternalJSONUtil {
|
||||
* @return JSONObject
|
||||
*/
|
||||
protected static JSONObject propertyPut(JSONObject jsonObject, Object key, Object value) {
|
||||
final String[] path = StrUtil.split(Convert.toStr(key), StrUtil.DOT);
|
||||
final String[] path = StrUtil.splitToArray(Convert.toStr(key), CharUtil.DOT);
|
||||
int last = path.length - 1;
|
||||
JSONObject target = jsonObject;
|
||||
for (int i = 0; i < last; i += 1) {
|
||||
@ -233,6 +235,31 @@ 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
|
||||
*/
|
||||
protected 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按照给定格式格式化日期,格式为空时返回时间戳字符串
|
||||
*
|
||||
|
@ -24,11 +24,9 @@ import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Collection;
|
||||
import java.util.Enumeration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.Set;
|
||||
import java.util.SortedMap;
|
||||
|
||||
/**
|
||||
* JSON对象<br>
|
||||
@ -158,7 +156,7 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
|
||||
* @since 3.0.9
|
||||
*/
|
||||
public JSONObject(Object source, boolean ignoreNullValue) {
|
||||
this(source, ignoreNullValue, (source instanceof LinkedHashMap) || (source instanceof SortedMap));
|
||||
this(source, ignoreNullValue, InternalJSONUtil.isOrder(source));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -653,7 +651,7 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
|
||||
return;
|
||||
}
|
||||
|
||||
if(ArrayUtil.isArray(source) || source instanceof JSONArray){
|
||||
if (ArrayUtil.isArray(source) || source instanceof JSONArray) {
|
||||
// 不支持集合类型转换为JSONObject
|
||||
throw new JSONException("Unsupported type [{}] to JSONObject!", source.getClass());
|
||||
}
|
||||
@ -663,7 +661,7 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
|
||||
for (final Entry<?, ?> e : ((Map<?, ?>) source).entrySet()) {
|
||||
this.set(Convert.toStr(e.getKey()), e.getValue());
|
||||
}
|
||||
}else if (source instanceof Map.Entry) {
|
||||
} else if (source instanceof Map.Entry) {
|
||||
final Map.Entry entry = (Map.Entry) source;
|
||||
this.set(Convert.toStr(entry.getKey()), entry.getValue());
|
||||
} else if (source instanceof CharSequence) {
|
||||
|
@ -194,7 +194,15 @@ public class JSONUtil {
|
||||
* @return JSON
|
||||
*/
|
||||
public static JSON parse(Object obj) {
|
||||
return parse(obj, JSONConfig.create());
|
||||
if(obj instanceof JSON){
|
||||
return (JSON) obj;
|
||||
}
|
||||
|
||||
final JSONConfig config = JSONConfig.create();
|
||||
if(InternalJSONUtil.isOrder(obj)){
|
||||
config.setOrder(true);
|
||||
}
|
||||
return parse(obj, config);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -15,6 +15,8 @@ import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class JSONUtilTest {
|
||||
|
||||
@ -102,6 +104,20 @@ public class JSONUtilTest {
|
||||
Assert.assertEquals("{\"name\":\"123123\",\"value\":\"\\\\\",\"value2\":\"</\"}", json2.get("user"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toJsonStrFromSortedTest() {
|
||||
SortedMap<Object, Object> sortedMap = new TreeMap<Object, Object>() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
{
|
||||
put("attributes", "a");
|
||||
put("b", "b");
|
||||
put("c", "c");
|
||||
}};
|
||||
|
||||
Assert.assertEquals("{\"attributes\":\"a\",\"b\":\"b\",\"c\":\"c\"}", JSONUtil.toJsonStr(sortedMap));
|
||||
}
|
||||
|
||||
/**
|
||||
* 泛型多层嵌套测试
|
||||
*/
|
||||
@ -196,6 +212,8 @@ public class JSONUtilTest {
|
||||
final JSONObject jsonObject = JSONUtil.parseObj("{\n" +
|
||||
" \"test\": \"\\\\地库地库\",\n" +
|
||||
"}");
|
||||
|
||||
Assert.assertEquals("\\地库地库", jsonObject.getObj("test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
x
Reference in New Issue
Block a user