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 @@ -101,6 +101,9 @@ public boolean evaluate(ValueNode left, ValueNode right, Predicate.PredicateCont
} if(left.isStringNode() && right.isStringNode()){
return left.asStringNode().getString().compareTo(right.asStringNode().getString()) < 0;
}
if(left.isDateNode() && right.isDateNode()){
return left.asDateNode().getDate().compareTo(right.asDateNode().getDate()) < 0;
}
return false;
}
}
Expand All @@ -113,6 +116,9 @@ public boolean evaluate(ValueNode left, ValueNode right, Predicate.PredicateCont
} if(left.isStringNode() && right.isStringNode()){
return left.asStringNode().getString().compareTo(right.asStringNode().getString()) <= 0;
}
if(left.isDateNode() && right.isDateNode()){
return left.asDateNode().getDate().compareTo(right.asDateNode().getDate()) <= 0;
}
return false;
}
}
Expand All @@ -125,6 +131,9 @@ public boolean evaluate(ValueNode left, ValueNode right, Predicate.PredicateCont
} else if(left.isStringNode() && right.isStringNode()){
return left.asStringNode().getString().compareTo(right.asStringNode().getString()) > 0;
}
if(left.isDateNode() && right.isDateNode()){
return left.asDateNode().getDate().compareTo(right.asDateNode().getDate()) > 0;
}
return false;
}
}
Expand All @@ -137,6 +146,9 @@ public boolean evaluate(ValueNode left, ValueNode right, Predicate.PredicateCont
} else if(left.isStringNode() && right.isStringNode()){
return left.asStringNode().getString().compareTo(right.asStringNode().getString()) >= 0;
}
if(left.isDateNode() && right.isDateNode()){
return left.asDateNode().getDate().compareTo(right.asDateNode().getDate()) >= 0;
}
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.jayway.jsonpath.internal.filter;

import java.time.OffsetDateTime;
import java.util.Date;
import java.util.regex.Pattern;

import com.jayway.jsonpath.InvalidPathException;
Expand Down Expand Up @@ -78,6 +80,14 @@ public ValueListNode asValueListNode() {
throw new InvalidPathException("Expected value list node");
}

public boolean isDateNode() {
return false;
}

public DateNode asDateNode() {
throw new InvalidPathException("Expected value list node");
}

public boolean isNullNode() {
return false;
}
Expand Down Expand Up @@ -161,6 +171,8 @@ public static ValueNode toValueNode(Object o){
else if(o instanceof Number) return createNumberNode(o.toString());
else if(o instanceof Boolean) return createBooleanNode(o.toString());
else if(o instanceof Pattern) return createPatternNode((Pattern)o);
else if(o instanceof OffsetDateTime) return createDateNode((OffsetDateTime)o);
else if(o instanceof Date) return createDateNode((Date)o);
else throw new JsonPathException("Could not determine value type");
}

Expand All @@ -180,6 +192,12 @@ public static BooleanNode createBooleanNode(CharSequence charSequence){
return Boolean.parseBoolean(charSequence.toString()) ? TRUE : FALSE;
}

public static DateNode createDateNode(OffsetDateTime offsetDateTime){
return new DateNode(offsetDateTime);
}
public static DateNode createDateNode(Date dateTime){
return new DateNode(dateTime);
}
public static NullNode createNullNode(){
return NULL_NODE;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
package com.jayway.jsonpath.internal.filter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPathException;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.Predicate;
import com.jayway.jsonpath.*;
import com.jayway.jsonpath.internal.Path;
import com.jayway.jsonpath.internal.Utils;
import com.jayway.jsonpath.internal.path.PathCompiler;
Expand All @@ -24,6 +11,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.time.OffsetDateTime;
import java.util.*;
import java.util.regex.Pattern;

/**
* Moved these nodes out of the ValueNode abstract class.
* This is to avoid this possible issue:
Expand Down Expand Up @@ -332,6 +324,58 @@ public boolean equals(Object o) {
}
}
}
class DateNode extends ValueNode {


private final Date date;

DateNode(OffsetDateTime offsetDateTime) {
this.date = new Date(offsetDateTime.toInstant().getEpochSecond() * 1000 + offsetDateTime.getNano()/1000000);
}
DateNode(Date date) {
this.date = date;
}

@Override
public StringNode asStringNode() {
return new StringNode(date.toString(), false);
}



public Date getDate() {
return date;
}

@Override
public Class<?> type(Predicate.PredicateContext ctx) {
return Date.class;
}

public boolean isDateNode() {
return true;
}

public DateNode asDateNode() {
return this;
}


@Override
public String toString() {
return date.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof DateNode) && !(o instanceof StringNode)) return false;

DateNode that = ((DateNode)o).asDateNode();

return date.compareTo(that.date) == 0;
}
}

class BooleanNode extends ValueNode {
private final Boolean value;
Expand Down Expand Up @@ -647,6 +691,8 @@ public ValueNode evaluate(Predicate.PredicateContext ctx) {
else if (res instanceof String) return ValueNode.createStringNode(res.toString(), false);
else if (res instanceof Boolean) return ValueNode.createBooleanNode(res.toString());
else if (res == null) return NULL_NODE;
else if (res instanceof OffsetDateTime) return ValueListNode.createDateNode((OffsetDateTime)res);
else if (res instanceof Date) return ValueListNode.createDateNode((Date)res);
else if (ctx.configuration().jsonProvider().isArray(res)) return ValueNode.createJsonNode(ctx.configuration().mappingProvider().map(res, List.class, ctx.configuration()));
else if (ctx.configuration().jsonProvider().isMap(res)) return ValueNode.createJsonNode(ctx.configuration().mappingProvider().map(res, Map.class, ctx.configuration()));
else throw new JsonPathException("Could not convert " + res.toString() + " to a ValueNode");
Expand Down
51 changes: 51 additions & 0 deletions json-path/src/test/java/com/jayway/jsonpath/FilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.assertj.core.util.Lists;
import org.junit.Test;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -31,7 +32,15 @@ public class FilterTest extends BaseTest {
" \"string-arr\" : [\"a\",\"b\",\"c\",\"d\",\"e\"] " +
"}"
);
Date now = new Date();
Date positiveDeltatNow = new Date(now.getTime() + 1000 * 60);
Date negativeDeltatNow = new Date(now.getTime() - 1000 * 60);

private Object addDateToObj(Object json) {
Map map = (Map) json;
map.put("date-key", now);
return map;
}
//----------------------------------------------------------------------------
//
// EQ
Expand All @@ -43,6 +52,7 @@ public void int_eq_evals() {
assertThat(filter(where("int-key").eq(666)).apply(createPredicateContext(json))).isEqualTo(false);
}


@Test
public void int_eq_string_evals() {
assertThat(filter(where("int-key").eq("1")).apply(createPredicateContext(json))).isEqualTo(true);
Expand Down Expand Up @@ -97,6 +107,13 @@ public void arr_eq_evals() {
assertThat(filter(where("int-arr").eq("[0,1,2,3]")).apply(createPredicateContext(json))).isEqualTo(false);
assertThat(filter(where("int-arr").eq("[0,1,2,3,4,5]")).apply(createPredicateContext(json))).isEqualTo(false);
}

@Test
public void date_eq_evals() {
assertThat(filter(where("date-key").eq(now)).apply(createPredicateContext(addDateToObj(json)))).isEqualTo(true);
assertThat(filter(where("date-key").eq(positiveDeltatNow)).apply(createPredicateContext(addDateToObj(json)))).isEqualTo(false);
}

//----------------------------------------------------------------------------
//
// NE
Expand Down Expand Up @@ -140,6 +157,12 @@ public void null_ne_evals() {
assertThat(filter(where("string-key").ne(null)).apply(createPredicateContext(json))).isEqualTo(true);
}

@Test
public void date_ne_evals() {
assertThat(filter(where("date-key").ne(now)).apply(createPredicateContext(addDateToObj(json)))).isEqualTo(false);
assertThat(filter(where("date-key").ne(positiveDeltatNow)).apply(createPredicateContext(addDateToObj(json)))).isEqualTo(true);
}

//----------------------------------------------------------------------------
//
// LT
Expand Down Expand Up @@ -169,6 +192,13 @@ public void string_lt_evals() {
assertThat(filter(where("char-key").lt("a")).apply(createPredicateContext(json))).isEqualTo(false);
}

@Test
public void date_lt_evals() {
assertThat(filter(where("date-key").lt(positiveDeltatNow)).apply(createPredicateContext(addDateToObj(json)))).isEqualTo(true);
assertThat(filter(where("date-key").lt(now)).apply(createPredicateContext(addDateToObj(json)))).isEqualTo(false);
assertThat(filter(where("date-key").lt(negativeDeltatNow)).apply(createPredicateContext(addDateToObj(json)))).isEqualTo(false);
}

//----------------------------------------------------------------------------
//
// LTE
Expand All @@ -195,6 +225,13 @@ public void double_lte_evals() {
assertThat(filter(where("double-key").lte(1.1D)).apply(createPredicateContext(json))).isEqualTo(false);
}

@Test
public void date_lte_evals() {
assertThat(filter(where("date-key").lte(positiveDeltatNow)).apply(createPredicateContext(addDateToObj(json)))).isEqualTo(true);
assertThat(filter(where("date-key").lte(now)).apply(createPredicateContext(addDateToObj(json)))).isEqualTo(true);
assertThat(filter(where("date-key").lte(negativeDeltatNow)).apply(createPredicateContext(addDateToObj(json)))).isEqualTo(false);
}

//----------------------------------------------------------------------------
//
// GT
Expand Down Expand Up @@ -224,6 +261,13 @@ public void string_gt_evals() {
assertThat(filter(where("char-key").gt("a")).apply(createPredicateContext(json))).isEqualTo(true);
}

@Test
public void date_gt_evals() {
assertThat(filter(where("date-key").gt(positiveDeltatNow)).apply(createPredicateContext(addDateToObj(json)))).isEqualTo(false);
assertThat(filter(where("date-key").gt(now)).apply(createPredicateContext(addDateToObj(json)))).isEqualTo(false);
assertThat(filter(where("date-key").gt(negativeDeltatNow)).apply(createPredicateContext(addDateToObj(json)))).isEqualTo(true);
}

//----------------------------------------------------------------------------
//
// GTE
Expand All @@ -250,6 +294,13 @@ public void double_gte_evals() {
assertThat(filter(where("double-key").gte(1.1D)).apply(createPredicateContext(json))).isEqualTo(true);
}

@Test
public void date_gte_evals() {
assertThat(filter(where("date-key").gte(positiveDeltatNow)).apply(createPredicateContext(addDateToObj(json)))).isEqualTo(false);
assertThat(filter(where("date-key").gte(now)).apply(createPredicateContext(addDateToObj(json)))).isEqualTo(true);
assertThat(filter(where("date-key").gte(negativeDeltatNow)).apply(createPredicateContext(addDateToObj(json)))).isEqualTo(true);
}

//----------------------------------------------------------------------------
//
// Regex
Expand Down