Skip to content

Commit

Permalink
json-path#1016 Fixed that issue by adding support for Maps and JSONAr…
Browse files Browse the repository at this point in the history
…ray to toValueNode. Added tests.
  • Loading branch information
DK committed Jul 30, 2024
1 parent 45333e0 commit a2c1d9b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
import com.jayway.jsonpath.Predicate;
import com.jayway.jsonpath.internal.Path;
import com.jayway.jsonpath.internal.path.PathCompiler;
import net.minidev.json.JSONArray;
import net.minidev.json.parser.JSONParser;

import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import static com.jayway.jsonpath.internal.filter.ValueNodes.*;
Expand Down Expand Up @@ -175,6 +179,8 @@ public static ValueNode toValueNode(Object o){
else if(o instanceof Boolean) return createBooleanNode(o.toString());
else if(o instanceof Pattern) return createPatternNode((Pattern)o);
else if (o instanceof OffsetDateTime) return createOffsetDateTimeNode(o.toString()); //workaround for issue: https://github.com/json-path/JsonPath/issues/613
else if (o instanceof Map) return new JsonNode(o);
else if (o instanceof JSONArray) return new ValueListNode(Collections.unmodifiableList((List) o));
else throw new JsonPathException("Could not determine value type");

}
Expand Down Expand Up @@ -234,5 +240,4 @@ public static ValueNode createPathNode(Path path) {
}


}

}
52 changes: 52 additions & 0 deletions json-path/src/test/java/com/jayway/jsonpath/Issue_1016.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.jayway.jsonpath;

import org.junit.jupiter.api.Test;


public class Issue_1016
{
public static final Configuration jsonConf = Configuration.defaultConfiguration();

@Test
public void test_read_with_empty_array()
{
// Before fix:
// assertEvaluationThrows("[{\"id\":1,\"array\":[\"a\",{\"b\":\"c\"}]}]", "$.*[?(\"a\" in @.array)]", JsonPathException.class);

DocumentContext emptyArray = JsonPath.using(jsonConf).parse("[{\"id\":1,\"array\":[\"a\",[]]}]");
Object read = emptyArray.read("$.*[?(\"a\" in @.array)]");
assert(read.toString().equals("[{\"id\":1,\"array\":[\"a\",[]]}]"));
}

@Test
public void test_read_with_filled_array()
{
DocumentContext filledArray = JsonPath.using(jsonConf).parse("[{\"id\":1,\"array\":[\"a\",[\"b\", \"c\"]]}]");
Object read = filledArray.read("$.*[?(\"a\" in @.array)]");
assert(read.toString().equals("[{\"id\":1,\"array\":[\"a\",[\"b\",\"c\"]]}]"));
}

@Test
public void test_read_with_empty_object()
{
DocumentContext emptyObj = JsonPath.using(jsonConf).parse("[{\"id\":1,\"array\":[\"a\",{}]}]");
Object read = emptyObj.read("$.*[?(\"a\" in @.array)]");
assert(read.toString().equals("[{\"id\":1,\"array\":[\"a\",{}]}]"));
}

@Test
public void test_read_with_filled_object()
{
DocumentContext filledObj = JsonPath.using(jsonConf).parse("[{\"id\":1,\"array\":[\"a\",{\"b\":\"c\"}]}]");
Object read = filledObj.read("$.*[?(\"a\" in @.array)]");
assert(read.toString().equals("[{\"id\":1,\"array\":[\"a\",{\"b\":\"c\"}]}]"));
}

@Test
public void test_read_with_combined_elements()
{
DocumentContext combined = JsonPath.using(jsonConf).parse("[{\"id\":1,\"array\":[\"a\",[\"b\", {\"c\" : \"d\"}]]}]");
Object read = combined.read("$.*[?(\"a\" in @.array)]");
assert(read.toString().equals("[{\"id\":1,\"array\":[\"a\",[\"b\",{\"c\":\"d\"}]]}]"));
}
}

0 comments on commit a2c1d9b

Please sign in to comment.