forked from java-json-tools/json-patch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#61 JsonPathParser wrongly parse unescaped reference-token if its con…
…tains a number
- Loading branch information
1 parent
6e29691
commit 4088b3a
Showing
2 changed files
with
46 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,36 @@ | ||
package com.gravity9.jsonpatch; | ||
|
||
import java.util.Arrays; | ||
|
||
public class JsonPathParser { | ||
|
||
private JsonPathParser() {} | ||
private JsonPathParser() { | ||
} | ||
|
||
private static final String ARRAY_ELEMENT_REGEX = "(?<=\\.)(\\d+)"; | ||
private static final String ARRAY_ELEMENT_REGEX = "\\A((\\d+)[^a-zA-Z]*)\\z"; | ||
|
||
/** | ||
* Method parses JsonPointer or JsonPath path to JsonPath syntax | ||
* @param path String containing JsonPath or JsonPointer expression | ||
* @return String containing JsonPath expression | ||
* @throws JsonPatchException throws when invalid JsonPointer expression provided | ||
*/ | ||
public static String parsePathToJsonPath(String path) throws JsonPatchException { | ||
if (path.startsWith("$")) { | ||
return path; | ||
} else if (path.contains("?")) { | ||
throw new JsonPatchException("Invalid path, `?` are not allowed in JsonPointer expressions."); | ||
} else if (path.contains("//")) { | ||
throw new JsonPatchException("Invalid path, `//` is not allowed in JsonPointer expressions."); | ||
} | ||
/** | ||
* Method parses JsonPointer or JsonPath path to JsonPath syntax | ||
* | ||
* @param path String containing JsonPath or JsonPointer expression | ||
* @return String containing JsonPath expression | ||
* @throws JsonPatchException throws when invalid JsonPointer expression provided | ||
*/ | ||
public static String parsePathToJsonPath(String path) throws JsonPatchException { | ||
if (path.startsWith("$")) { | ||
return path; | ||
} else if (path.contains("?")) { | ||
throw new JsonPatchException("Invalid path, `?` are not allowed in JsonPointer expressions."); | ||
} else if (path.contains("//")) { | ||
throw new JsonPatchException("Invalid path, `//` is not allowed in JsonPointer expressions."); | ||
} | ||
|
||
return "$" + path.replace('/', '.') | ||
.replace("~1", "/") // / must be escaped in JsonPointer using ~1 | ||
.replace("~0", "~") // ~ must be escaped in JsonPointer using ~0 | ||
.replaceAll(ARRAY_ELEMENT_REGEX, "[$1]"); | ||
} | ||
return "$" + Arrays.stream(path.replace('/', '.') | ||
.replace("~1", "/") // / must be escaped in JsonPointer using ~1 | ||
.replace("~0", "~") // ~ must be escaped in JsonPointer using ~0 | ||
.split("\\.")) // split string to analyze every path field separately | ||
.filter(s -> !s.isEmpty()) // skip empty string on the beginning of the array | ||
.map(s -> s.replaceAll(ARRAY_ELEMENT_REGEX, "[$1]")) | ||
.reduce("", (s, s2) -> s + "." + s2); //connect the path | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters