Skip to content

Commit b79c420

Browse files
Merge pull request #64 from gravity9-tech/61-fix_wrongly_parsed_tokens_containing_number_in_name
#61 JsonPathParser wrongly parse unescaped numbers - fixes #61
2 parents 6e29691 + 3ea0090 commit b79c420

File tree

2 files changed

+55
-21
lines changed

2 files changed

+55
-21
lines changed
Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
11
package com.gravity9.jsonpatch;
22

3+
import java.util.Arrays;
4+
35
public class JsonPathParser {
46

5-
private JsonPathParser() {}
7+
private JsonPathParser() {
8+
}
69

7-
private static final String ARRAY_ELEMENT_REGEX = "(?<=\\.)(\\d+)";
10+
private static final String ARRAY_ELEMENT_REGEX = "\\A((\\d+)[^a-zA-Z]*)\\z";
811

9-
/**
10-
* Method parses JsonPointer or JsonPath path to JsonPath syntax
11-
* @param path String containing JsonPath or JsonPointer expression
12-
* @return String containing JsonPath expression
13-
* @throws JsonPatchException throws when invalid JsonPointer expression provided
14-
*/
15-
public static String parsePathToJsonPath(String path) throws JsonPatchException {
16-
if (path.startsWith("$")) {
17-
return path;
18-
} else if (path.contains("?")) {
19-
throw new JsonPatchException("Invalid path, `?` are not allowed in JsonPointer expressions.");
20-
} else if (path.contains("//")) {
21-
throw new JsonPatchException("Invalid path, `//` is not allowed in JsonPointer expressions.");
22-
}
12+
/**
13+
* Method parses JsonPointer or JsonPath path to JsonPath syntax
14+
*
15+
* @param path String containing JsonPath or JsonPointer expression
16+
* @return String containing JsonPath expression
17+
* @throws JsonPatchException throws when invalid JsonPointer expression provided
18+
*/
19+
public static String parsePathToJsonPath(String path) throws JsonPatchException {
20+
if (path.startsWith("$")) {
21+
return path;
22+
} else if (path.contains("?")) {
23+
throw new JsonPatchException("Invalid path, `?` are not allowed in JsonPointer expressions.");
24+
} else if (path.contains("//")) {
25+
throw new JsonPatchException("Invalid path, `//` is not allowed in JsonPointer expressions.");
26+
}
2327

24-
return "$" + path.replace('/', '.')
25-
.replace("~1", "/") // / must be escaped in JsonPointer using ~1
26-
.replace("~0", "~") // ~ must be escaped in JsonPointer using ~0
27-
.replaceAll(ARRAY_ELEMENT_REGEX, "[$1]");
28-
}
28+
return "$" + Arrays.stream(path.replace('/', '.')
29+
.replace("~1", "/") // / must be escaped in JsonPointer using ~1
30+
.replace("~0", "~") // ~ must be escaped in JsonPointer using ~0
31+
.split("\\.")) // split string to analyze every path field separately
32+
.filter(s -> !s.isEmpty()) // skip empty string on the beginning of the array
33+
.map(s -> s.replaceAll(ARRAY_ELEMENT_REGEX, "[$1]"))
34+
.reduce("", (s, s2) -> s + "." + s2); //connect the path
35+
}
2936
}

src/test/resources/jsonpatch/move.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,33 @@
10901090
},
10911091
"expensive": 10
10921092
}
1093+
},
1094+
{
1095+
"op": { "op": "move", "from": "/5gStringToMove", "path": "/5gStringMoved" },
1096+
"node": {
1097+
"5gStringToMove":"exists"
1098+
},
1099+
"expected": {
1100+
"5gStringMoved":"exists"
1101+
}
1102+
},
1103+
{
1104+
"op": { "op": "move", "from": "/String123ToMove", "path": "/StringMoved5g" },
1105+
"node": {
1106+
"String123ToMove":"exists"
1107+
},
1108+
"expected": {
1109+
"StringMoved5g":"exists"
1110+
}
1111+
},
1112+
{
1113+
"op": { "op": "move", "from": "/data/0/1", "path": "/data/0/2" },
1114+
"node": {
1115+
"data":[["abc", "move", "def"]]
1116+
},
1117+
"expected": {
1118+
"data":[["abc", "def", "move"]]
1119+
}
10931120
}
10941121
]
10951122
}

0 commit comments

Comments
 (0)