From eed1cb281447f6feb273da292282baff8ccfbebd Mon Sep 17 00:00:00 2001 From: Youngea <64700468+Youngea@users.noreply.github.com> Date: Wed, 2 Jun 2021 19:36:03 +0800 Subject: [PATCH] fix issue_590 and add testcases (#720) fix issue_590 and add testcases --- .../jsonpath/internal/CharacterIndex.java | 7 ++- .../jsonpath/ScientificNotationTest.java | 47 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 json-path/src/test/java/com/jayway/jsonpath/ScientificNotationTest.java diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/CharacterIndex.java b/json-path/src/main/java/com/jayway/jsonpath/internal/CharacterIndex.java index bc152776e..72da4ffc7 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/CharacterIndex.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/CharacterIndex.java @@ -15,6 +15,10 @@ public class CharacterIndex { private static final char PERIOD = '.'; private static final char REGEX = '/'; + //workaround for issue: https://github.com/json-path/JsonPath/issues/590 + private static final char SCI_E = 'E'; + private static final char SCI_e = 'e'; + private final CharSequence charSequence; private int position; private int endPosition; @@ -293,7 +297,8 @@ public String toString() { public boolean isNumberCharacter(int readPosition) { char c = charAt(readPosition); - return Character.isDigit(c) || c == MINUS || c == PERIOD; + //workaround for issue: https://github.com/json-path/JsonPath/issues/590 + return Character.isDigit(c) || c == MINUS || c == PERIOD || c == SCI_E || c == SCI_e; } public CharacterIndex skipBlanks() { diff --git a/json-path/src/test/java/com/jayway/jsonpath/ScientificNotationTest.java b/json-path/src/test/java/com/jayway/jsonpath/ScientificNotationTest.java new file mode 100644 index 000000000..8a05be9c9 --- /dev/null +++ b/json-path/src/test/java/com/jayway/jsonpath/ScientificNotationTest.java @@ -0,0 +1,47 @@ +package com.jayway.jsonpath; + +import com.google.gson.JsonArray; +import org.junit.Test; + +import java.util.List; + +import static com.jayway.jsonpath.JsonPath.using; +import static org.assertj.core.api.Assertions.assertThat; + +//test for issue: https://github.com/json-path/JsonPath/issues/590 +public class ScientificNotationTest extends BaseTest { + + final String sci_rep_array = "{\"num_array\": [" + + "{\"num\":1}," + + "{\"num\":-1e-10}," + + "{\"num\":0.1e10},"+ + "{\"num\":2E-20}," + + "{\"num\":-0.2E20}" + + " ]}"; + + @Test + public void testScientificNotation() { + List result = using(Configuration.defaultConfiguration()) + .parse(sci_rep_array) + .read("$.num_array[?(@.num == 1 || @.num == -1e-10 || @.num == 0.1e10 || @.num == 2E-20 || @.num == -0.2E20)]"); + + assertThat(result.toString()).isEqualTo("[{\"num\":1},{\"num\":-1.0E-10},{\"num\":1.0E9},{\"num\":2.0E-20},{\"num\":-2.0E19}]"); + } + @Test + public void testScientificNotation_lt_gt() { + List result; + result = using(Configuration.defaultConfiguration()) + .parse(sci_rep_array) + .read("$.num_array[?(@.num > -0.0E0)]"); + + assertThat(result.toString()).isEqualTo("[{\"num\":1},{\"num\":1.0E9},{\"num\":2.0E-20}]"); + + result = using(Configuration.defaultConfiguration()) + .parse(sci_rep_array) + .read("$.num_array[?(@.num < -0.0E0)]"); + + assertThat(result.toString()).isEqualTo("[{\"num\":-1.0E-10},{\"num\":-2.0E19}]"); + + } + +}