Browse Source

Some methods (i.e. DocumentContext's set) fail when JSON name has ' in it #171

pull/182/head
Kalle Stenflo 9 years ago
parent
commit
d3231e7f04
  1. 13
      json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java
  2. 19
      json-path/src/test/java/com/jayway/jsonpath/old/IssuesTest.java

13
json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java

@ -4,6 +4,7 @@ import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.Predicate;
import com.jayway.jsonpath.internal.CharacterIndex;
import com.jayway.jsonpath.internal.Path;
import com.jayway.jsonpath.internal.Utils;
import com.jayway.jsonpath.internal.filter.FilterCompiler;
import java.util.ArrayList;
@ -361,16 +362,22 @@ public class PathCompiler {
int readPosition = startPosition;
int endPosition = 0;
boolean inProperty = false;
boolean inEscape = false;
while (path.inBounds(readPosition)) {
char c = path.charAt(readPosition);
if (c == CLOSE_SQUARE_BRACKET && !inProperty) {
if(inEscape){
inEscape = false;
} else if('\\' == c){
inEscape = true;
} else if (c == CLOSE_SQUARE_BRACKET && !inProperty) {
break;
} else if (c == potentialStringDelimiter) {
if (inProperty) {
if (inProperty && !inEscape) {
endPosition = readPosition;
properties.add(path.subSequence(startPosition, endPosition).toString());
String prop = path.subSequence(startPosition, endPosition).toString();
properties.add(Utils.unescape(prop));
inProperty = false;
} else {
startPosition = readPosition + 1;

19
json-path/src/test/java/com/jayway/jsonpath/old/IssuesTest.java

@ -1,5 +1,6 @@
package com.jayway.jsonpath.old;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.gson.JsonObject;
import com.jayway.jsonpath.BaseTest;
import com.jayway.jsonpath.Configuration;
@ -980,4 +981,22 @@ public class IssuesTest extends BaseTest {
assertThat(list).containsExactly(null, 1, null);
}
@Test
public void issue_171() {
String json = "{\n" +
" \"can delete\": \"this\",\n" +
" \"can't delete\": \"this\"\n" +
"}";
DocumentContext context = using(JACKSON_JSON_NODE_CONFIGURATION).parse(json);
context.set("$.['can delete']", null);
context.set("$.['can\\'t delete']", null);
ObjectNode objectNode = context.read("$");
assertThat(objectNode.get("can delete").isNull());
assertThat(objectNode.get("can't delete").isNull());
}
}

Loading…
Cancel
Save