Skip to content

Commit c43e21a

Browse files
authored
Merge pull request #588 from fossterer/563-jsonpointer-do-not-encode-quotes
JSONPointer should not process reverse solidus or double-quote chars in tokens
2 parents 7844eb7 + d6ccc64 commit c43e21a

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/main/java/org/json/JSONPointer.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,11 @@ public JSONPointer(List<String> refTokens) {
187187
this.refTokens = new ArrayList<String>(refTokens);
188188
}
189189

190+
/**
191+
* @see https://tools.ietf.org/html/rfc6901#section-3
192+
*/
190193
private static String unescape(String token) {
191-
return token.replace("~1", "/").replace("~0", "~")
192-
.replace("\\\"", "\"")
193-
.replace("\\\\", "\\");
194+
return token.replace("~1", "/").replace("~0", "~");
194195
}
195196

196197
/**
@@ -263,16 +264,15 @@ public String toString() {
263264
/**
264265
* Escapes path segment values to an unambiguous form.
265266
* The escape char to be inserted is '~'. The chars to be escaped
266-
* are ~, which maps to ~0, and /, which maps to ~1. Backslashes
267-
* and double quote chars are also escaped.
267+
* are ~, which maps to ~0, and /, which maps to ~1.
268268
* @param token the JSONPointer segment value to be escaped
269269
* @return the escaped value for the token
270+
*
271+
* @see https://tools.ietf.org/html/rfc6901#section-3
270272
*/
271273
private static String escape(String token) {
272274
return token.replace("~", "~0")
273-
.replace("/", "~1")
274-
.replace("\\", "\\\\")
275-
.replace("\"", "\\\"");
275+
.replace("/", "~1");
276276
}
277277

278278
/**

src/test/java/org/json/junit/JSONPointerTest.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,24 @@ public void tildeEscaping() {
117117
assertSame(document.get("m~n"), query("/m~0n"));
118118
}
119119

120+
/**
121+
* We pass backslashes as-is
122+
*
123+
* @see https://tools.ietf.org/html/rfc6901#section-3
124+
*/
120125
@Test
121-
public void backslashEscaping() {
122-
assertSame(document.get("i\\j"), query("/i\\\\j"));
126+
public void backslashHandling() {
127+
assertSame(document.get("i\\j"), query("/i\\j"));
123128
}
124129

130+
/**
131+
* We pass quotations as-is
132+
*
133+
* @see https://tools.ietf.org/html/rfc6901#section-3
134+
*/
125135
@Test
126-
public void quotationEscaping() {
127-
assertSame(document.get("k\"l"), query("/k\\\\\\\"l"));
136+
public void quotationHandling() {
137+
assertSame(document.get("k\"l"), query("/k\"l"));
128138
}
129139

130140
@Test
@@ -189,7 +199,7 @@ public void toStringEscaping() {
189199
.append("\"")
190200
.append(0)
191201
.build();
192-
assertEquals("/obj/other~0key/another~1key/\\\"/0", pointer.toString());
202+
assertEquals("/obj/other~0key/another~1key/\"/0", pointer.toString());
193203
}
194204

195205
@Test

0 commit comments

Comments
 (0)