Browse Source

Fixed merge issue.

pull/29/head
Kalle Stenflo 11 years ago
parent
commit
f7cc1139ce
  1. 8
      README
  2. 279
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/eval/ExpressionEvaluator.java
  3. 21
      json-path/src/test/java/com/jayway/jsonpath/CastTest.java
  4. 43
      json-path/src/test/java/com/jayway/jsonpath/ExpressionEvalTest.java
  5. 7
      pom.xml

8
README

@ -1,5 +1,13 @@
Java DSL for reading and testing JSON documents. Java DSL for reading and testing JSON documents.
------------------------------------------
0.8.2 2013-08-19
------------------------------------------
- OSGI ready
- Bug fixes
- Performance improvements
------------------------------------------ ------------------------------------------
0.8.1 2012-04-16 0.8.1 2012-04-16
------------------------------------------ ------------------------------------------

279
json-path/src/main/java/com/jayway/jsonpath/internal/filter/eval/ExpressionEvaluator.java

@ -14,6 +14,8 @@
*/ */
package com.jayway.jsonpath.internal.filter.eval; package com.jayway.jsonpath.internal.filter.eval;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -22,151 +24,198 @@ import java.util.Map;
* @author Kalle Stenflo * @author Kalle Stenflo
*/ */
public class ExpressionEvaluator { public class ExpressionEvaluator {
public enum Operator { public enum Operator {
equal("=="), not_equal("!="), less_or_greater_than("<>"),greater_than(">"), greater_than_or_equal(">="), less_than("<"), less_than_or_equal("<="); equal("=="), not_equal("!="), less_or_greater_than("<>"), greater_than(">"), greater_than_or_equal(">="), less_than("<"), less_than_or_equal("<=");
private final String representation; private final String representation;
private Operator(String representation) { private Operator(String representation) {
this.representation = representation; this.representation = representation;
} }
public String getRepresentation() { public String getRepresentation() {
return representation; return representation;
} }
} }
private static Map<String, Operator> operatorsByRepresentation; private static Map<String, Operator> operatorsByRepresentation;
static { static {
Map<String, Operator> map = new HashMap<String, Operator>(); Map<String, Operator> map = new HashMap<String, Operator>();
for (Operator op : Operator.values()){ for (Operator op : Operator.values()) {
map.put(op.getRepresentation(), op); map.put(op.getRepresentation(), op);
} }
ExpressionEvaluator.operatorsByRepresentation = Collections.unmodifiableMap(map); ExpressionEvaluator.operatorsByRepresentation = Collections.unmodifiableMap(map);
} }
public static <T> boolean eval(T actual, String comparator, String expected) { public static <T> boolean eval(T actual, String comparator, String expected) {
Operator operator = operatorsByRepresentation.get(comparator); Operator operator = operatorsByRepresentation.get(comparator);
if (operator == null){ if (operator == null) {
throw new IllegalArgumentException("Unsupported operator " + comparator); throw new IllegalArgumentException("Unsupported operator " + comparator);
} }
if (actual instanceof Long) { if (actual instanceof Long) {
Long a = (Long) actual; Long a = (Long) actual;
Long e = Long.parseLong(expected.trim()); Long e = Long.parseLong(expected.trim());
switch (operator){ switch (operator) {
case equal: case equal:
return a.longValue() == e.longValue(); return a.longValue() == e.longValue();
case not_equal: case not_equal:
case less_or_greater_than: case less_or_greater_than:
return a.longValue() != e.longValue(); return a.longValue() != e.longValue();
case greater_than: case greater_than:
return a > e; return a > e;
case greater_than_or_equal: case greater_than_or_equal:
return a >= e; return a >= e;
case less_than: case less_than:
return a < e; return a < e;
case less_than_or_equal: case less_than_or_equal:
return a <= e; return a <= e;
default: default:
throw new UnsupportedOperationException("Cannot handle operator " + operator); throw new UnsupportedOperationException("Cannot handle operator " + operator);
} }
} else if (actual instanceof Integer) { } else if (actual instanceof Integer) {
Integer a = (Integer) actual; Integer a = (Integer) actual;
Integer e = Integer.parseInt(expected.trim()); Integer e = Integer.parseInt(expected.trim());
switch (operator){ switch (operator) {
case equal: case equal:
return a.intValue() == e.intValue(); return a.intValue() == e.intValue();
case not_equal: case not_equal:
case less_or_greater_than: case less_or_greater_than:
return a.intValue() != e.intValue(); return a.intValue() != e.intValue();
case greater_than: case greater_than:
return a > e; return a > e;
case greater_than_or_equal: case greater_than_or_equal:
return a >= e; return a >= e;
case less_than: case less_than:
return a < e; return a < e;
case less_than_or_equal: case less_than_or_equal:
return a <= e; return a <= e;
default: default:
throw new UnsupportedOperationException("Cannot handle operator " + operator); throw new UnsupportedOperationException("Cannot handle operator " + operator);
} }
} else if (actual instanceof Double) { } else if (actual instanceof Double) {
Double a = (Double) actual; Double a = (Double) actual;
Double e = Double.parseDouble(expected.trim()); Double e = Double.parseDouble(expected.trim());
switch (operator){ switch (operator) {
case equal: case equal:
return a.doubleValue() == e.doubleValue(); return a.doubleValue() == e.doubleValue();
case not_equal: case not_equal:
case less_or_greater_than: case less_or_greater_than:
return a.doubleValue() != e.doubleValue(); return a.doubleValue() != e.doubleValue();
case greater_than: case greater_than:
return a > e; return a > e;
case greater_than_or_equal: case greater_than_or_equal:
return a >= e; return a >= e;
case less_than: case less_than:
return a < e; return a < e;
case less_than_or_equal: case less_than_or_equal:
return a <= e; return a <= e;
default: default:
throw new UnsupportedOperationException("Cannot handle operator " + operator); throw new UnsupportedOperationException("Cannot handle operator " + operator);
} }
} else if (actual instanceof String) { } else if (actual instanceof String) {
switch (operator){ switch (operator) {
case greater_than: case greater_than:
case greater_than_or_equal: case greater_than_or_equal:
case less_than: case less_than:
case less_than_or_equal: case less_than_or_equal:
// we might want to throw an exception here // we might want to throw an exception here
return false; return false;
case equal: case equal:
case not_equal: case not_equal:
case less_or_greater_than: case less_or_greater_than:
String a = (String)actual; String a = (String) actual;
String expectedTrimmed = expected.trim(); String expectedTrimmed = expected.trim();
if(expectedTrimmed.startsWith("'")) { if (expectedTrimmed.startsWith("'")) {
expectedTrimmed = expectedTrimmed.substring(1); expectedTrimmed = expectedTrimmed.substring(1);
} }
if(expectedTrimmed.endsWith("'")){ if (expectedTrimmed.endsWith("'")) {
expectedTrimmed = expectedTrimmed.substring(0, expected.length()-1); expectedTrimmed = expectedTrimmed.substring(0, expected.length() - 1);
} }
if (operator == Operator.equal) { if (operator == Operator.equal) {
return a.equals(expectedTrimmed); return a.equals(expectedTrimmed);
} else if (operator == Operator.not_equal || operator == Operator.less_or_greater_than) { } else if (operator == Operator.not_equal || operator == Operator.less_or_greater_than) {
return !a.equals(expectedTrimmed); return !a.equals(expectedTrimmed);
} }
default: default:
throw new UnsupportedOperationException("Cannot handle operator " + operator); throw new UnsupportedOperationException("Cannot handle operator " + operator);
} }
} else if (actual instanceof Boolean) { } else if (actual instanceof Boolean) {
switch (operator){ switch (operator) {
case equal: case equal:
case not_equal: case not_equal:
case less_or_greater_than: case less_or_greater_than:
Boolean a = (Boolean) actual; Boolean a = (Boolean) actual;
Boolean e = Boolean.valueOf(expected); Boolean e = Boolean.valueOf(expected);
if (operator == Operator.equal) { if (operator == Operator.equal) {
return a.equals(e); return a.equals(e);
} else if (operator == Operator.not_equal || operator == Operator.less_or_greater_than) { } else if (operator == Operator.not_equal || operator == Operator.less_or_greater_than) {
return !a.equals(e); return !a.equals(e);
}
default:
throw new UnsupportedOperationException("Cannot handle operator " + operator);
}
} else if (actual instanceof BigInteger) {
BigInteger a = (BigInteger) actual;
BigInteger e = new BigInteger(expected.trim());
switch (operator){
case equal:
return a.compareTo(e) == 0;
case not_equal:
return a.compareTo(e) != 0;
case less_or_greater_than:
return a.compareTo(e) != 0;
case greater_than:
return a.compareTo(e) > 0;
case greater_than_or_equal:
return a.compareTo(e) >= 0;
case less_than:
return a.compareTo(e) < 0;
case less_than_or_equal:
return a.compareTo(e) <= 0;
default:
throw new UnsupportedOperationException("Cannot handle operator " + operator);
} }
default: } else if (actual instanceof BigDecimal) {
throw new UnsupportedOperationException("Cannot handle operator " + operator);
} BigDecimal a = (BigDecimal) actual;
BigDecimal e = new BigDecimal(expected);
switch (operator){
case equal:
return a.compareTo(e) == 0;
case not_equal:
return a.compareTo(e) != 0;
case less_or_greater_than:
return a.compareTo(e) != 0;
case greater_than:
return a.compareTo(e) > 0;
case greater_than_or_equal:
return a.compareTo(e) >= 0;
case less_than:
return a.compareTo(e) < 0;
case less_than_or_equal:
return a.compareTo(e) <= 0;
default:
throw new UnsupportedOperationException("Cannot handle operator " + operator);
}
} }
return false; return false;
} }
} }

21
json-path/src/test/java/com/jayway/jsonpath/CastTest.java

@ -0,0 +1,21 @@
package com.jayway.jsonpath;
import org.junit.Assert;
import org.junit.Test;
/**
* User: kalle
* Date: 5/15/13
* Time: 9:22 PM
*/
public class CastTest {
public static final String JSON = "{\"sessionID\":7242750700467747000}" ;
@Test
public void result_can_be_cast_to_Long(){
Long actual = JsonPath.read(JSON, "$.sessionID");
Long expected = new Long("7242750700467747000");
Assert.assertEquals(expected, actual);
}
}

43
json-path/src/test/java/com/jayway/jsonpath/ExpressionEvalTest.java

@ -1,8 +1,12 @@
package com.jayway.jsonpath; package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.filter.eval.ExpressionEvaluator; import com.jayway.jsonpath.internal.filter.eval.ExpressionEvaluator;
import org.codehaus.jackson.node.BigIntegerNode;
import org.junit.Test; import org.junit.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -33,6 +37,45 @@ public class ExpressionEvalTest {
assertFalse(ExpressionEvaluator.eval(1, "!=", "1")); assertFalse(ExpressionEvaluator.eval(1, "!=", "1"));
} }
@Test
public void bigint_eval() throws Exception {
assertTrue(ExpressionEvaluator.eval(new BigInteger("1"), "==", "1"));
assertTrue(ExpressionEvaluator.eval(new BigInteger("2"), "!=", "1"));
assertTrue(ExpressionEvaluator.eval(new BigInteger("2"), ">", "1"));
assertTrue(ExpressionEvaluator.eval(new BigInteger("2"), ">=", "1"));
assertTrue(ExpressionEvaluator.eval(new BigInteger("2"), ">=", "2"));
assertTrue(ExpressionEvaluator.eval(new BigInteger("1"), "<", "2"));
assertTrue(ExpressionEvaluator.eval(new BigInteger("2"), "<=", "2"));
assertFalse(ExpressionEvaluator.eval(new BigInteger("1"), ">", "2"));
assertFalse(ExpressionEvaluator.eval(new BigInteger("1"), ">=", "2"));
assertFalse(ExpressionEvaluator.eval(new BigInteger("2"), "<", "1"));
assertFalse(ExpressionEvaluator.eval(new BigInteger("2"), "<=", "1"));
assertFalse(ExpressionEvaluator.eval(new BigInteger("1"), "==", "2"));
assertFalse(ExpressionEvaluator.eval(new BigInteger("1"), "!=", "1"));
}
@Test
public void bigdec_eval() throws Exception {
assertTrue(ExpressionEvaluator.eval(new BigDecimal("1.1"), "==", "1.1"));
assertTrue(ExpressionEvaluator.eval(new BigDecimal("2"), "!=", "1"));
assertTrue(ExpressionEvaluator.eval(new BigDecimal("2"), ">", "1"));
assertTrue(ExpressionEvaluator.eval(new BigDecimal("2"), ">=", "1"));
assertTrue(ExpressionEvaluator.eval(new BigDecimal("2"), ">=", "2"));
assertTrue(ExpressionEvaluator.eval(new BigDecimal("1"), "<", "2"));
assertTrue(ExpressionEvaluator.eval(new BigDecimal("2"), "<=", "2"));
assertFalse(ExpressionEvaluator.eval(new BigDecimal("1"), ">", "2"));
assertFalse(ExpressionEvaluator.eval(new BigDecimal("1"), ">=", "2"));
assertFalse(ExpressionEvaluator.eval(new BigDecimal("2"), "<", "1"));
assertFalse(ExpressionEvaluator.eval(new BigDecimal("2"), "<=", "1"));
assertFalse(ExpressionEvaluator.eval(new BigDecimal("1"), "==", "2"));
assertFalse(ExpressionEvaluator.eval(new BigDecimal("1"), "!=", "1"));
}
@Test @Test
public void double_eval() throws Exception { public void double_eval() throws Exception {

7
pom.xml

@ -24,7 +24,7 @@
<groupId>com.jayway.jsonpath</groupId> <groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path-parent</artifactId> <artifactId>json-path-parent</artifactId>
<packaging>pom</packaging> <packaging>pom</packaging>
<version>0.8.1-HBO</version> <version>0.8.2-SNAPSHOT</version>
<url>https://github.com/jayway/JsonPath</url> <url>https://github.com/jayway/JsonPath</url>
<name>json-path-parent-pom</name> <name>json-path-parent-pom</name>
<description>Java JsonPath implementation</description> <description>Java JsonPath implementation</description>
@ -177,7 +177,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId> <artifactId>maven-javadoc-plugin</artifactId>
<version>2.8.1</version> <version>2.9.1</version>
<executions> <executions>
<execution> <execution>
<id>attach-javadocs</id> <id>attach-javadocs</id>
@ -211,7 +211,7 @@
<dependency> <dependency>
<groupId>net.minidev</groupId> <groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId> <artifactId>json-smart</artifactId>
<version>1.1.1</version> <version>1.2</version>
</dependency> </dependency>
<dependency> <dependency>
@ -252,6 +252,7 @@
<version>2.4</version> <version>2.4</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
</project> </project>
Loading…
Cancel
Save