Skip to content

Commit

Permalink
Merge pull request #588 from fossterer/563-jsonpointer-do-not-encode-…
Browse files Browse the repository at this point in the history
…quotes

JSONPointer should not process reverse solidus or double-quote chars in tokens
  • Loading branch information
stleary authored Mar 6, 2021
2 parents 7844eb7 + d6ccc64 commit c43e21a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
16 changes: 8 additions & 8 deletions src/main/java/org/json/JSONPointer.java
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit c43e21a

Please sign in to comment.