Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests for multiple backslashes in JSONPointer #640

Merged
merged 3 commits into from Nov 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 28 additions & 3 deletions src/test/java/org/json/junit/JSONPointerTest.java
Expand Up @@ -41,10 +41,11 @@ public class JSONPointerTest {

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


static {
@SuppressWarnings("resource")
InputStream resourceAsStream = JSONPointerTest.class.getClassLoader().getResourceAsStream("jsonpointer-testdoc.json");
Expand Down Expand Up @@ -124,7 +125,7 @@ public void tildeEscaping() {
public void backslashHandling() {
assertEquals(5, query("/i\\j"));
}

/**
* We pass quotations as-is
*
Expand All @@ -134,7 +135,7 @@ public void backslashHandling() {
public void quotationHandling() {
assertEquals(6, query("/k\"l"));
}

@Test
public void whitespaceKey() {
assertEquals(7, query("/ "));
Expand Down Expand Up @@ -389,4 +390,28 @@ public void optQueryFromJSONArrayUsingPointer() {
obj = jsonArray.optQuery(new JSONPointer("/a/b/c"));
assertTrue("Expected null", obj == null);
}

/**
* When creating a jsonObject we need to parse escaped characters "\\\\"
* --> it's the string representation of "\\", so when query'ing via the JSONPointer
* we DON'T escape them
*
*/
@Test
public void queryFromJSONObjectUsingPointer0() {
String str = "{"+
"\"string\\\\\\\\Key\":\"hello world!\","+

"\"\\\\\":\"slash test\"," +
"}"+
"}";
JSONObject jsonObject = new JSONObject(str);
//Summary of issue: When a KEY in the jsonObject is "\\\\" --> it's held
// as "\\" which means when querying, we need to use "\\"
Object twoBackslahObj = jsonObject.optQuery(new JSONPointer("/\\"));
assertEquals("slash test", twoBackslahObj);

Object fourBackslashObj = jsonObject.optQuery(new JSONPointer("/string\\\\Key"));
assertEquals("hello world!", fourBackslashObj);
}
}