Skip to content

Commit

Permalink
fix : jsonpath number key, for issue #956
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Nov 30, 2022
1 parent 5f9a6ea commit 808340a
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 72 deletions.
130 changes: 59 additions & 71 deletions core/src/main/java/com/alibaba/fastjson2/JSONPathParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,80 +317,68 @@ private JSONPathSegment parseProperty() {
}
}
}
JSONPathSegmentIndex indexSegment = null;
if (isNum) {
try {
int index = Integer.parseInt(name);
indexSegment = JSONPathSegmentIndex.of(index);
} catch (NumberFormatException ignored) {
}
}

if (indexSegment != null) {
segment = indexSegment;
} else {
if (jsonReader.ch == '(') {
switch (name) {
case "length":
case "size":
segment = JSONPathSegment.LengthSegment.INSTANCE;
break;
case "keys":
segment = JSONPathSegment.KeysSegment.INSTANCE;
break;
case "values":
segment = JSONPathSegment.ValuesSegment.INSTANCE;
break;
case "entrySet":
segment = JSONPathSegment.EntrySetSegment.INSTANCE;
break;
case "min":
segment = JSONPathSegment.MinSegment.INSTANCE;
break;
case "max":
segment = JSONPathSegment.MaxSegment.INSTANCE;
break;
case "sum":
segment = JSONPathSegment.SumSegment.INSTANCE;
break;
case "type":
segment = JSONPathFunction.FUNC_TYPE;
break;
case "floor":
segment = JSONPathFunction.FUNC_FLOOR;
break;
case "ceil":
case "ceiling":
segment = JSONPathFunction.FUNC_CEIL;
break;
case "double":
segment = JSONPathFunction.FUNC_DOUBLE;
break;
case "abs":
segment = JSONPathFunction.FUNC_ABS;
break;
case "lower":
segment = JSONPathFunction.FUNC_LOWER;
break;
case "upper":
segment = JSONPathFunction.FUNC_UPPER;
break;
case "trim":
segment = JSONPathFunction.FUNC_TRIM;
break;
case "negative":
segment = JSONPathFunction.FUNC_NEGATIVE;
break;
default:
throw new JSONException("not support syntax, path : " + path);
}
jsonReader.next();
if (!jsonReader.nextIfMatch(')')) {
if (jsonReader.ch == '(') {
switch (name) {
case "length":
case "size":
segment = JSONPathSegment.LengthSegment.INSTANCE;
break;
case "keys":
segment = JSONPathSegment.KeysSegment.INSTANCE;
break;
case "values":
segment = JSONPathSegment.ValuesSegment.INSTANCE;
break;
case "entrySet":
segment = JSONPathSegment.EntrySetSegment.INSTANCE;
break;
case "min":
segment = JSONPathSegment.MinSegment.INSTANCE;
break;
case "max":
segment = JSONPathSegment.MaxSegment.INSTANCE;
break;
case "sum":
segment = JSONPathSegment.SumSegment.INSTANCE;
break;
case "type":
segment = JSONPathFunction.FUNC_TYPE;
break;
case "floor":
segment = JSONPathFunction.FUNC_FLOOR;
break;
case "ceil":
case "ceiling":
segment = JSONPathFunction.FUNC_CEIL;
break;
case "double":
segment = JSONPathFunction.FUNC_DOUBLE;
break;
case "abs":
segment = JSONPathFunction.FUNC_ABS;
break;
case "lower":
segment = JSONPathFunction.FUNC_LOWER;
break;
case "upper":
segment = JSONPathFunction.FUNC_UPPER;
break;
case "trim":
segment = JSONPathFunction.FUNC_TRIM;
break;
case "negative":
segment = JSONPathFunction.FUNC_NEGATIVE;
break;
default:
throw new JSONException("not support syntax, path : " + path);
}
} else {
segment = new JSONPathSegmentName(name, hashCode);
}
jsonReader.next();
if (!jsonReader.nextIfMatch(')')) {
throw new JSONException("not support syntax, path : " + path);
}
} else {
segment = new JSONPathSegmentName(name, hashCode);
}
}
return segment;
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/com/alibaba/fastjson2/JSONBTest4.java
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ public void test_3_path_7() {

@Test
public void test_3_path_8() {
JSONPath path = JSONPath.of("$.phoneNumbers.0.type");
JSONPath path = JSONPath.of("$.phoneNumbers[0]type");
String expected = "\"iPhone\"";

assertEquals(expected,
Expand Down
23 changes: 23 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/issues/Issue842.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ public void test() {
public void test1() {
String jsonPath = "$.items[0].data.0";

Map data = new LinkedHashMap<>();
JSONPath.set(data, jsonPath, "设置成功");
assertEquals("{\"items\":[{\"data\":{\"0\":\"设置成功\"}}]}", JSON.toJSONString(data));
}

@Test
public void test1a() {
String jsonPath = "$.items[0].data[0]";

Map data = new LinkedHashMap<>();
JSONPath.set(data, jsonPath, "设置成功");
assertEquals("{\"items\":[{\"data\":[\"设置成功\"]}]}", JSON.toJSONString(data));
Expand All @@ -31,13 +40,27 @@ public void test1() {
public void test2() {
Map data = new LinkedHashMap<>();
JSONPath.set(data, "$.obj.5", "设置成功");
assertEquals("{\"obj\":{\"5\":\"设置成功\"}}", JSONObject.toJSONString(data));
}

@Test
public void test2a() {
Map data = new LinkedHashMap<>();
JSONPath.set(data, "$.obj[5]", "设置成功");
assertEquals("{\"obj\":[null,null,null,null,null,\"设置成功\"]}", JSONObject.toJSONString(data));
}

@Test
public void test3() {
Map data = new LinkedHashMap<>();
JSONPath.set(data, "$.obj.2.5", "设置成功");
assertEquals("{\"obj\":{\"2\":{\"5\":\"设置成功\"}}}", JSONObject.toJSONString(data));
}

@Test
public void test3a() {
Map data = new LinkedHashMap<>();
JSONPath.set(data, "$.obj[2][5]", "设置成功");
assertEquals("{\"obj\":[null,null,[null,null,null,null,null,\"设置成功\"]]}", JSONObject.toJSONString(data));
}
}
7 changes: 7 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/issues/Issue956.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ public void test() {
JSONPath.set(data, "$.obj['5']", "设置成功");
assertEquals("{\"obj\":{\"5\":\"设置成功\"}}", JSONObject.toJSONString(data));
}

@Test
public void test1() {
Map data = new LinkedHashMap<>();
JSONPath.set(data, "$.obj.5", "设置成功");
assertEquals("{\"obj\":{\"5\":\"设置成功\"}}", JSONObject.toJSONString(data));
}
}

0 comments on commit 808340a

Please sign in to comment.