Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONPointer should not process reverse solidus or double-quote chars in tokens #588

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/main/java/org/json/JSONPointer.java
Expand Up @@ -187,10 +187,11 @@ public JSONPointer(List<String> refTokens) {
this.refTokens = new ArrayList<String>(refTokens);
}

/**
* @see https://tools.ietf.org/html/rfc6901#section-3
*/
private static String unescape(String token) {
return token.replace("~1", "/").replace("~0", "~")
.replace("\\\"", "\"")
.replace("\\\\", "\\");
return token.replace("~1", "/").replace("~0", "~");
}

/**
Expand Down Expand Up @@ -263,16 +264,15 @@ public String toString() {
/**
* Escapes path segment values to an unambiguous form.
* The escape char to be inserted is '~'. The chars to be escaped
* are ~, which maps to ~0, and /, which maps to ~1. Backslashes
* and double quote chars are also escaped.
* are ~, which maps to ~0, and /, which maps to ~1.
* @param token the JSONPointer segment value to be escaped
* @return the escaped value for the token
*
* @see https://tools.ietf.org/html/rfc6901#section-3
*/
private static String escape(String token) {
return token.replace("~", "~0")
.replace("/", "~1")
.replace("\\", "\\\\")
.replace("\"", "\\\"");
.replace("/", "~1");
}

/**
Expand Down
20 changes: 15 additions & 5 deletions src/test/java/org/json/junit/JSONPointerTest.java
Expand Up @@ -117,14 +117,24 @@ public void tildeEscaping() {
assertSame(document.get("m~n"), query("/m~0n"));
}

/**
* We pass backslashes as-is
*
* @see https://tools.ietf.org/html/rfc6901#section-3
*/
@Test
public void backslashEscaping() {
assertSame(document.get("i\\j"), query("/i\\\\j"));
public void backslashHandling() {
assertSame(document.get("i\\j"), query("/i\\j"));
}

/**
* We pass quotations as-is
*
* @see https://tools.ietf.org/html/rfc6901#section-3
*/
@Test
public void quotationEscaping() {
assertSame(document.get("k\"l"), query("/k\\\\\\\"l"));
public void quotationHandling() {
assertSame(document.get("k\"l"), query("/k\"l"));
}

@Test
Expand Down Expand Up @@ -189,7 +199,7 @@ public void toStringEscaping() {
.append("\"")
.append(0)
.build();
assertEquals("/obj/other~0key/another~1key/\\\"/0", pointer.toString());
assertEquals("/obj/other~0key/another~1key/\"/0", pointer.toString());
}

@Test
Expand Down