Skip to content

Commit 69383f7

Browse files
authored
Merge pull request stleary#1090 from JimmyLiuJia/fix/json-pointer-fragment-plus
Preserve plus signs in JSONPointer URI fragments
2 parents e5e97f8 + 4713079 commit 69383f7

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ public JSONPointer(final String pointer) {
135135
if (pointer.startsWith("#/")) {
136136
refs = pointer.substring(2);
137137
try {
138-
refs = URLDecoder.decode(refs, ENCODING);
138+
// URI fragments preserve plus signs rather than treating them as form-encoded spaces.
139+
refs = URLDecoder.decode(refs.replace("+", "%2B"), ENCODING);
139140
} catch (UnsupportedEncodingException e) {
140141
throw new RuntimeException(e);
141142
}
@@ -276,7 +277,7 @@ public String toURIFragment() {
276277
try {
277278
StringBuilder rval = new StringBuilder("#");
278279
for (String token : this.refTokens) {
279-
rval.append('/').append(URLEncoder.encode(escape(token), ENCODING));
280+
rval.append('/').append(URLEncoder.encode(escape(token), ENCODING).replace("+", "%20"));
280281
}
281282
return rval.toString();
282283
} catch (UnsupportedEncodingException e) {

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,41 @@ public void uriFragmentPercentHandling() {
185185
assertEquals(8, query("#/m~0n"));
186186
}
187187

188+
@Test
189+
public void uriFragmentPreservesLiteralPlus() {
190+
JSONObject object = new JSONObject().put("a+b", "plus").put("a b", "space")
191+
.put("a%2Bb", "percent");
192+
assertEquals("plus", object.query("#/a+b"));
193+
assertEquals("plus", object.query("#/a%2Bb"));
194+
assertEquals("space", object.query("#/a%20b"));
195+
assertEquals("percent", object.query("#/a%252Bb"));
196+
assertEquals("plus", object.query("/a+b"));
197+
}
198+
199+
@Test
200+
public void uriFragmentEncodesSpaces() {
201+
JSONObject object = new JSONObject().put("a+b", "plus").put("a b", "space");
202+
JSONPointer plus = JSONPointer.builder().append("a+b").build();
203+
JSONPointer space = JSONPointer.builder().append("a b").build();
204+
assertEquals("#/a%2Bb", plus.toURIFragment());
205+
assertEquals("#/a%20b", space.toURIFragment());
206+
assertEquals("#/%20", new JSONPointer("/ ").toURIFragment());
207+
assertEquals("plus", object.query(plus.toURIFragment()));
208+
assertEquals("space", object.query(space.toURIFragment()));
209+
}
210+
211+
@Test
212+
public void uriFragmentPreservesUtf8AndPointerEscapes() {
213+
String key = "\u4e2d +/~1";
214+
JSONObject object = new JSONObject().put(key, "expected")
215+
.put("\u4e2d /~1", "wrong space");
216+
assertEquals("expected", object.query("#/%E4%B8%AD%20+~1~01"));
217+
assertEquals("expected", object.query("#/%E4%B8%AD%20%2B%7E1%7E01"));
218+
JSONPointer pointer = JSONPointer.builder().append(key).build();
219+
assertEquals("#/%E4%B8%AD%20%2B%7E1%7E01", pointer.toURIFragment());
220+
assertEquals("expected", object.query(pointer.toURIFragment()));
221+
}
222+
188223
@SuppressWarnings("unused")
189224
@Test(expected = IllegalArgumentException.class)
190225
public void syntaxError() {

0 commit comments

Comments
 (0)