Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,17 @@ public int length(Predicate.PredicateContext ctx) {
}

public boolean isEmpty(Predicate.PredicateContext ctx) {
if (isArray(ctx) || isMap(ctx)) return ((Collection<?>) parse(ctx)).size() == 0;
if (isArray(ctx) || isMap(ctx)) {
Object parsedObj = parse(ctx);
if (parsedObj instanceof Collection) {
return ((Collection<?>) parsedObj).size() == 0;
} else if (parsedObj instanceof Map) {
return ((Map<?, ?>) parsedObj).isEmpty();
} else {
throw new IllegalArgumentException("Expected a Collection or Map object, but got " +
parsedObj.getClass().getSimpleName());
}
}
else if((parse(ctx) instanceof String)) return ((String)parse(ctx)).length() == 0;
return true;
}
Expand Down
48 changes: 48 additions & 0 deletions json-path/src/test/java/com/jayway/jsonpath/EmptyFilterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.jayway.jsonpath;

import org.junit.Assert;
import org.junit.Test;

/**
* test for issue 900
*/
public class EmptyFilterTest {

@Test
public void test() {
String json = "{\n" +
" \"data1\": {\n" +
" \"data\": [\n" +
" {\n" +
" \"attribute1\": \"string1\",\n" +
" \"attribute2\": \"string2\"\n" +
" },\n" +
" {\n" +
" \"attribute1\": \"string3\",\n" +
" \"attribute2\": \"string4\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"data2\": {\n" +
" \"data\": [\n" +
" {}\n" +
" ]\n" +
" },\n" +
" \"data3\": {\n" +
" \"data\": [\n" +
" {\n" +
" \"attribute1\": \"string5\",\n" +
" \"attribute2\": \"string6\"\n" +
" },\n" +
" {\n" +
" \"attribute1\": \"string7\",\n" +
" \"attribute2\": \"string8\"\n" +
" }\n" +
" ]\n" +
" }\n" +
"}";
JsonPath jsonPath = JsonPath.compile("$..data.*[?(@ empty false)]");
Object jsonPathValue = jsonPath.read(json);
Assert.assertNotNull(jsonPathValue);
}
}