Skip to content

Commit 5622f5e

Browse files
committed
feature: json的getByPath方法新增更为通用的指定出参类型重载
1 parent fe567de commit 5622f5e

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

hutool-json/src/main/java/cn/hutool/json/JSON.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,32 @@ public interface JSON extends Cloneable, Serializable, IJSONTypeConverter {
9797
*/
9898
<T> T getByPath(String expression, Class<T> resultType);
9999

100+
/**
101+
* 通过表达式获取JSON中嵌套的对象<br>
102+
* <ol>
103+
* <li>.表达式,可以获取Bean对象中的属性(字段)值或者Map中key对应的值</li>
104+
* <li>[]表达式,可以获取集合等对象中对应index的值</li>
105+
* </ol>
106+
* <p>
107+
* 表达式栗子:
108+
*
109+
* <pre>
110+
* persion
111+
* persion.name
112+
* persons[3]
113+
* person.friends[5].name
114+
* </pre>
115+
* <p>
116+
* 获取表达式对应值后转换为对应类型的值
117+
*
118+
* @param expression 表达式
119+
* @param targetType 返回值类型
120+
* @return 对象
121+
* @see BeanPath#get(Object)
122+
* @since 5.8.34
123+
*/
124+
<T> T getByPath(String expression, TypeReference<T> targetType);
125+
100126
/**
101127
* 格式化打印JSON,缩进为4个空格
102128
*

hutool-json/src/main/java/cn/hutool/json/JSONArray.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import cn.hutool.core.bean.BeanPath;
44
import cn.hutool.core.collection.CollUtil;
55
import cn.hutool.core.lang.Filter;
6+
import cn.hutool.core.lang.TypeReference;
67
import cn.hutool.core.lang.Validator;
78
import cn.hutool.core.lang.mutable.Mutable;
89
import cn.hutool.core.lang.mutable.MutableObj;
@@ -13,6 +14,7 @@
1314

1415
import java.io.StringWriter;
1516
import java.io.Writer;
17+
import java.lang.reflect.Type;
1618
import java.util.ArrayList;
1719
import java.util.Collection;
1820
import java.util.Iterator;
@@ -195,7 +197,7 @@ public JSONArray setDateFormat(String format) {
195197
*/
196198
public String join(String separator) throws JSONException {
197199
return StrJoiner.of(separator)
198-
.append(this, InternalJSONUtil::valueToString).toString();
200+
.append(this, InternalJSONUtil::valueToString).toString();
199201
}
200202

201203
@Override
@@ -218,6 +220,11 @@ public <T> T getByPath(String expression, Class<T> resultType) {
218220
return JSONConverter.jsonConvert(resultType, getByPath(expression), getConfig());
219221
}
220222

223+
@Override
224+
public <T> T getByPath(String expression, TypeReference<T> targetType) {
225+
return JSONConverter.jsonConvert(targetType, getByPath(expression), getConfig());
226+
}
227+
221228
@Override
222229
public void putByPath(String expression, Object value) {
223230
BeanPath.create(expression).set(this, value);

hutool-json/src/main/java/cn/hutool/json/JSONObject.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import cn.hutool.core.bean.BeanPath;
44
import cn.hutool.core.collection.CollectionUtil;
55
import cn.hutool.core.lang.Filter;
6+
import cn.hutool.core.lang.TypeReference;
67
import cn.hutool.core.lang.mutable.MutablePair;
78
import cn.hutool.core.map.CaseInsensitiveMap;
89
import cn.hutool.core.map.MapUtil;
@@ -14,6 +15,7 @@
1415

1516
import java.io.StringWriter;
1617
import java.io.Writer;
18+
import java.lang.reflect.Type;
1719
import java.math.BigDecimal;
1820
import java.math.BigInteger;
1921
import java.util.Collection;
@@ -161,8 +163,8 @@ public JSONObject(Object source, boolean ignoreNullValue) {
161163
@Deprecated
162164
public JSONObject(Object source, boolean ignoreNullValue, boolean isOrder) {
163165
this(source, JSONConfig.create()//
164-
.setIgnoreCase((source instanceof CaseInsensitiveMap))//
165-
.setIgnoreNullValue(ignoreNullValue)
166+
.setIgnoreCase((source instanceof CaseInsensitiveMap))//
167+
.setIgnoreNullValue(ignoreNullValue)
166168
);
167169
}
168170

@@ -320,6 +322,11 @@ public <T> T getByPath(String expression, Class<T> resultType) {
320322
return JSONConverter.jsonConvert(resultType, getByPath(expression), getConfig());
321323
}
322324

325+
@Override
326+
public <T> T getByPath(String expression, TypeReference<T> targetType) {
327+
return JSONConverter.jsonConvert(targetType, getByPath(expression), getConfig());
328+
}
329+
323330
@Override
324331
public void putByPath(String expression, Object value) {
325332
BeanPath.create(expression).set(this, value);
@@ -561,7 +568,7 @@ public Writer write(Writer writer, int indentFactor, int indent) throws JSONExce
561568
*/
562569
public Writer write(Writer writer, int indentFactor, int indent, Filter<MutablePair<Object, Object>> filter) throws JSONException {
563570
final JSONWriter jsonWriter = JSONWriter.of(writer, indentFactor, indent, config)
564-
.beginObj();
571+
.beginObj();
565572
this.forEach((key, value) -> jsonWriter.writeField(new MutablePair<>(key, value), filter));
566573
jsonWriter.end();
567574
// 此处不关闭Writer,考虑writer后续还需要填内容

hutool-json/src/test/java/cn/hutool/json/JSONPathTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package cn.hutool.json;
22

33
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import cn.hutool.core.lang.TypeReference;
46
import org.junit.jupiter.api.Test;
57

8+
import java.util.List;
9+
610
/**
711
* JSON路径单元测试
812
*
@@ -27,4 +31,23 @@ public void getByPathTest2(){
2731
Long accountId = JSONUtil.getByPath(json, "$.accountId", 0L);
2832
assertEquals(111L, accountId.longValue());
2933
}
34+
35+
@Test
36+
public void getByPathTest3(){
37+
String str = "[{'accountId':1},{'accountId':2},{'accountId':3}]";
38+
JSON json = JSONUtil.parse(str);
39+
// 返回指定泛型的对象 List<Long>
40+
List<Long> accountIds = json.getByPath("$.accountId", new TypeReference<List<Long>>() {
41+
});
42+
assertNotNull(accountIds);
43+
assertArrayEquals(new Long[]{1L, 2L, 3L}, accountIds.toArray());
44+
45+
str = "{\"accountInfos\": [{\"accountId\":1},{\"accountId\":2},{\"accountId\":3}]}";
46+
json = JSONUtil.parse(str);
47+
// 返回指定泛型的对象 List<Long>
48+
accountIds = json.getByPath("$.accountInfos.accountId", new TypeReference<List<Long>>() {
49+
});
50+
assertNotNull(accountIds);
51+
assertArrayEquals(new Long[]{1L, 2L, 3L}, accountIds.toArray());
52+
}
3053
}

0 commit comments

Comments
 (0)