Merge pull request #1090 from JimmyLiuJia/fix/json-pointer-fragment-plus
Preserve plus signs in JSONPointer URI fragments
This commit is contained in:
@@ -135,7 +135,8 @@ public class JSONPointer {
|
||||
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);
|
||||
}
|
||||
@@ -276,7 +277,7 @@ public class JSONPointer {
|
||||
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) {
|
||||
|
||||
@@ -185,6 +185,41 @@ public class JSONPointerTest {
|
||||
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() {
|
||||
|
||||
Reference in New Issue
Block a user