Skip to content

Commit d6227c8

Browse files
authored
Merge pull request #640 from katldewitt/561-backslashes
Address #561: Add unit tests for multiple backslashes
2 parents f54b5e4 + 3ed9154 commit d6227c8

1 file changed

Lines changed: 28 additions & 3 deletions

File tree

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ public class JSONPointerTest {
4141

4242
private static final JSONObject document;
4343
private static final String EXPECTED_COMPLETE_DOCUMENT = "{\"\":0,\" \":7,\"g|h\":4,\"c%d\":2,\"k\\\"l\":6,\"a/b\":1,\"i\\\\j\":5," +
44-
"\"obj\":{\"\":{\"\":\"empty key of an object with an empty key\",\"subKey\":\"Some other value\"}," +
44+
"\"obj\":{\"\":{\"\":\"empty key of an object with an empty key\",\"subKey\":\"Some other value\"}," +
4545
"\"other~key\":{\"another/key\":[\"val\"]},\"key\":\"value\"},\"foo\":[\"bar\",\"baz\"],\"e^f\":3," +
4646
"\"m~n\":8}";
4747

48+
4849
static {
4950
@SuppressWarnings("resource")
5051
InputStream resourceAsStream = JSONPointerTest.class.getClassLoader().getResourceAsStream("jsonpointer-testdoc.json");
@@ -124,7 +125,7 @@ public void tildeEscaping() {
124125
public void backslashHandling() {
125126
assertEquals(5, query("/i\\j"));
126127
}
127-
128+
128129
/**
129130
* We pass quotations as-is
130131
*
@@ -134,7 +135,7 @@ public void backslashHandling() {
134135
public void quotationHandling() {
135136
assertEquals(6, query("/k\"l"));
136137
}
137-
138+
138139
@Test
139140
public void whitespaceKey() {
140141
assertEquals(7, query("/ "));
@@ -389,4 +390,28 @@ public void optQueryFromJSONArrayUsingPointer() {
389390
obj = jsonArray.optQuery(new JSONPointer("/a/b/c"));
390391
assertTrue("Expected null", obj == null);
391392
}
393+
394+
/**
395+
* When creating a jsonObject we need to parse escaped characters "\\\\"
396+
* --> it's the string representation of "\\", so when query'ing via the JSONPointer
397+
* we DON'T escape them
398+
*
399+
*/
400+
@Test
401+
public void queryFromJSONObjectUsingPointer0() {
402+
String str = "{"+
403+
"\"string\\\\\\\\Key\":\"hello world!\","+
404+
405+
"\"\\\\\":\"slash test\"," +
406+
"}"+
407+
"}";
408+
JSONObject jsonObject = new JSONObject(str);
409+
//Summary of issue: When a KEY in the jsonObject is "\\\\" --> it's held
410+
// as "\\" which means when querying, we need to use "\\"
411+
Object twoBackslahObj = jsonObject.optQuery(new JSONPointer("/\\"));
412+
assertEquals("slash test", twoBackslahObj);
413+
414+
Object fourBackslashObj = jsonObject.optQuery(new JSONPointer("/string\\\\Key"));
415+
assertEquals("hello world!", fourBackslashObj);
416+
}
392417
}

0 commit comments

Comments
 (0)