From 5b320eeefa21163b02b0e80657f46578dcd5f969 Mon Sep 17 00:00:00 2001 From: "Allen.li" Date: Mon, 11 May 2020 14:16:03 +0800 Subject: [PATCH] add a filter to support Date --- .../internal/filter/EvaluatorFactory.java | 12 +++ .../jsonpath/internal/filter/ValueNode.java | 18 +++++ .../jsonpath/internal/filter/ValueNodes.java | 74 +++++++++++++++---- 3 files changed, 90 insertions(+), 14 deletions(-) diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/EvaluatorFactory.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/EvaluatorFactory.java index cfbfdff7..d0bb1cc8 100755 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/EvaluatorFactory.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/EvaluatorFactory.java @@ -101,6 +101,9 @@ public class EvaluatorFactory { } 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; } } @@ -113,6 +116,9 @@ public class EvaluatorFactory { } 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; } } @@ -125,6 +131,9 @@ public class EvaluatorFactory { } 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; } } @@ -137,6 +146,9 @@ public class EvaluatorFactory { } 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; } } diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java index d87994ee..c0571368 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java @@ -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; @@ -78,6 +80,14 @@ public abstract class ValueNode { 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; } @@ -161,6 +171,8 @@ public abstract class ValueNode { 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"); } @@ -180,6 +192,12 @@ public abstract class ValueNode { 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; } diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNodes.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNodes.java index c4030bc7..75e1611d 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNodes.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNodes.java @@ -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; @@ -24,6 +11,11 @@ import net.minidev.json.parser.ParseException; 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: @@ -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 { 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 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");