Browse Source

add a filter to support Date

pull/614/head
Allen.li 5 years ago
parent
commit
5b320eeefa
  1. 12
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/EvaluatorFactory.java
  2. 18
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java
  3. 74
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNodes.java

12
json-path/src/main/java/com/jayway/jsonpath/internal/filter/EvaluatorFactory.java

@ -101,6 +101,9 @@ public class EvaluatorFactory {
} if(left.isStringNode() && right.isStringNode()){ } if(left.isStringNode() && right.isStringNode()){
return left.asStringNode().getString().compareTo(right.asStringNode().getString()) < 0; 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; return false;
} }
} }
@ -113,6 +116,9 @@ public class EvaluatorFactory {
} if(left.isStringNode() && right.isStringNode()){ } if(left.isStringNode() && right.isStringNode()){
return left.asStringNode().getString().compareTo(right.asStringNode().getString()) <= 0; 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; return false;
} }
} }
@ -125,6 +131,9 @@ public class EvaluatorFactory {
} else if(left.isStringNode() && right.isStringNode()){ } else if(left.isStringNode() && right.isStringNode()){
return left.asStringNode().getString().compareTo(right.asStringNode().getString()) > 0; 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; return false;
} }
} }
@ -137,6 +146,9 @@ public class EvaluatorFactory {
} else if(left.isStringNode() && right.isStringNode()){ } else if(left.isStringNode() && right.isStringNode()){
return left.asStringNode().getString().compareTo(right.asStringNode().getString()) >= 0; 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; return false;
} }
} }

18
json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java

@ -1,5 +1,7 @@
package com.jayway.jsonpath.internal.filter; package com.jayway.jsonpath.internal.filter;
import java.time.OffsetDateTime;
import java.util.Date;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import com.jayway.jsonpath.InvalidPathException; import com.jayway.jsonpath.InvalidPathException;
@ -78,6 +80,14 @@ public abstract class ValueNode {
throw new InvalidPathException("Expected value list node"); 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() { public boolean isNullNode() {
return false; return false;
} }
@ -161,6 +171,8 @@ public abstract class ValueNode {
else if(o instanceof Number) return createNumberNode(o.toString()); else if(o instanceof Number) return createNumberNode(o.toString());
else if(o instanceof Boolean) return createBooleanNode(o.toString()); else if(o instanceof Boolean) return createBooleanNode(o.toString());
else if(o instanceof Pattern) return createPatternNode((Pattern)o); 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"); else throw new JsonPathException("Could not determine value type");
} }
@ -180,6 +192,12 @@ public abstract class ValueNode {
return Boolean.parseBoolean(charSequence.toString()) ? TRUE : FALSE; 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(){ public static NullNode createNullNode(){
return NULL_NODE; return NULL_NODE;
} }

74
json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNodes.java

@ -1,19 +1,6 @@
package com.jayway.jsonpath.internal.filter; package com.jayway.jsonpath.internal.filter;
import java.math.BigDecimal; import com.jayway.jsonpath.*;
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.internal.Path; import com.jayway.jsonpath.internal.Path;
import com.jayway.jsonpath.internal.Utils; import com.jayway.jsonpath.internal.Utils;
import com.jayway.jsonpath.internal.path.PathCompiler; import com.jayway.jsonpath.internal.path.PathCompiler;
@ -24,6 +11,11 @@ import net.minidev.json.parser.ParseException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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. * Moved these nodes out of the ValueNode abstract class.
* This is to avoid this possible issue: * This is to avoid this possible issue:
@ -332,6 +324,58 @@ public interface ValueNodes {
} }
} }
} }
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 { class BooleanNode extends ValueNode {
private final Boolean value; private final Boolean value;
@ -647,6 +691,8 @@ public interface ValueNodes {
else if (res instanceof String) return ValueNode.createStringNode(res.toString(), false); else if (res instanceof String) return ValueNode.createStringNode(res.toString(), false);
else if (res instanceof Boolean) return ValueNode.createBooleanNode(res.toString()); else if (res instanceof Boolean) return ValueNode.createBooleanNode(res.toString());
else if (res == null) return NULL_NODE; 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().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 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"); else throw new JsonPathException("Could not convert " + res.toString() + " to a ValueNode");

Loading…
Cancel
Save