Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/java/org/json/JSONPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ public JSONPointer(final String pointer) {
if (pointer.startsWith("#/")) {
refs = pointer.substring(2);
try {
refs = URLDecoder.decode(refs, ENCODING);
// URI fragments preserve plus signs rather than treating them as form-encoded spaces.
refs = URLDecoder.decode(refs.replace("+", "%2B"), ENCODING);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -276,7 +277,7 @@ public String toURIFragment() {
try {
StringBuilder rval = new StringBuilder("#");
for (String token : this.refTokens) {
rval.append('/').append(URLEncoder.encode(escape(token), ENCODING));
rval.append('/').append(URLEncoder.encode(escape(token), ENCODING).replace("+", "%20"));
}
return rval.toString();
} catch (UnsupportedEncodingException e) {
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/org/json/junit/JSONPointerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,41 @@ public void uriFragmentPercentHandling() {
assertEquals(8, query("#/m~0n"));
}

@Test
public void uriFragmentPreservesLiteralPlus() {
JSONObject object = new JSONObject().put("a+b", "plus").put("a b", "space")
.put("a%2Bb", "percent");
assertEquals("plus", object.query("#/a+b"));
assertEquals("plus", object.query("#/a%2Bb"));
assertEquals("space", object.query("#/a%20b"));
assertEquals("percent", object.query("#/a%252Bb"));
assertEquals("plus", object.query("/a+b"));
}

@Test
public void uriFragmentEncodesSpaces() {
JSONObject object = new JSONObject().put("a+b", "plus").put("a b", "space");
JSONPointer plus = JSONPointer.builder().append("a+b").build();
JSONPointer space = JSONPointer.builder().append("a b").build();
assertEquals("#/a%2Bb", plus.toURIFragment());
assertEquals("#/a%20b", space.toURIFragment());
assertEquals("#/%20", new JSONPointer("/ ").toURIFragment());
assertEquals("plus", object.query(plus.toURIFragment()));
assertEquals("space", object.query(space.toURIFragment()));
}

@Test
public void uriFragmentPreservesUtf8AndPointerEscapes() {
String key = "\u4e2d +/~1";
JSONObject object = new JSONObject().put(key, "expected")
.put("\u4e2d /~1", "wrong space");
assertEquals("expected", object.query("#/%E4%B8%AD%20+~1~01"));
assertEquals("expected", object.query("#/%E4%B8%AD%20%2B%7E1%7E01"));
JSONPointer pointer = JSONPointer.builder().append(key).build();
assertEquals("#/%E4%B8%AD%20%2B%7E1%7E01", pointer.toURIFragment());
assertEquals("expected", object.query(pointer.toURIFragment()));
}

@SuppressWarnings("unused")
@Test(expected = IllegalArgumentException.class)
public void syntaxError() {
Expand Down
Loading