Skip to content

Commit eed1cb2

Browse files
authored
fix issue_590 and add testcases (#720)
fix issue_590 and add testcases
1 parent 12ab661 commit eed1cb2

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

json-path/src/main/java/com/jayway/jsonpath/internal/CharacterIndex.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public class CharacterIndex {
1515
private static final char PERIOD = '.';
1616
private static final char REGEX = '/';
1717

18+
//workaround for issue: https://github.com/json-path/JsonPath/issues/590
19+
private static final char SCI_E = 'E';
20+
private static final char SCI_e = 'e';
21+
1822
private final CharSequence charSequence;
1923
private int position;
2024
private int endPosition;
@@ -293,7 +297,8 @@ public String toString() {
293297

294298
public boolean isNumberCharacter(int readPosition) {
295299
char c = charAt(readPosition);
296-
return Character.isDigit(c) || c == MINUS || c == PERIOD;
300+
//workaround for issue: https://github.com/json-path/JsonPath/issues/590
301+
return Character.isDigit(c) || c == MINUS || c == PERIOD || c == SCI_E || c == SCI_e;
297302
}
298303

299304
public CharacterIndex skipBlanks() {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.jayway.jsonpath;
2+
3+
import com.google.gson.JsonArray;
4+
import org.junit.Test;
5+
6+
import java.util.List;
7+
8+
import static com.jayway.jsonpath.JsonPath.using;
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
//test for issue: https://github.com/json-path/JsonPath/issues/590
12+
public class ScientificNotationTest extends BaseTest {
13+
14+
final String sci_rep_array = "{\"num_array\": [" +
15+
"{\"num\":1}," +
16+
"{\"num\":-1e-10}," +
17+
"{\"num\":0.1e10},"+
18+
"{\"num\":2E-20}," +
19+
"{\"num\":-0.2E20}" +
20+
" ]}";
21+
22+
@Test
23+
public void testScientificNotation() {
24+
List<JsonArray> result = using(Configuration.defaultConfiguration())
25+
.parse(sci_rep_array)
26+
.read("$.num_array[?(@.num == 1 || @.num == -1e-10 || @.num == 0.1e10 || @.num == 2E-20 || @.num == -0.2E20)]");
27+
28+
assertThat(result.toString()).isEqualTo("[{\"num\":1},{\"num\":-1.0E-10},{\"num\":1.0E9},{\"num\":2.0E-20},{\"num\":-2.0E19}]");
29+
}
30+
@Test
31+
public void testScientificNotation_lt_gt() {
32+
List<JsonArray> result;
33+
result = using(Configuration.defaultConfiguration())
34+
.parse(sci_rep_array)
35+
.read("$.num_array[?(@.num > -0.0E0)]");
36+
37+
assertThat(result.toString()).isEqualTo("[{\"num\":1},{\"num\":1.0E9},{\"num\":2.0E-20}]");
38+
39+
result = using(Configuration.defaultConfiguration())
40+
.parse(sci_rep_array)
41+
.read("$.num_array[?(@.num < -0.0E0)]");
42+
43+
assertThat(result.toString()).isEqualTo("[{\"num\":-1.0E-10},{\"num\":-2.0E19}]");
44+
45+
}
46+
47+
}

0 commit comments

Comments
 (0)