Skip to content

Commit 9b6eea4

Browse files
committed
add toBigInteger and toBigDecimal
1 parent 68990b2 commit 9b6eea4

22 files changed

+240
-2
lines changed

src/main/java/com/jsoniter/IterImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ final static boolean skipNumber(JsonIterator iter) throws IOException {
119119
boolean dotFound = false;
120120
for (int i = iter.head; i < iter.tail; i++) {
121121
byte c = iter.buf[i];
122-
if (c == '.') {
122+
if (c == '.' || c == 'e' || c == 'E') {
123123
dotFound = true;
124124
continue;
125125
}

src/main/java/com/jsoniter/IterImplForStreaming.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ final static boolean skipNumber(JsonIterator iter) throws IOException {
179179
for (; ; ) {
180180
for (int i = iter.head; i < iter.tail; i++) {
181181
byte c = iter.buf[i];
182-
if (c == '.') {
182+
if (c == '.' || c == 'e' || c == 'E') {
183183
dotFound = true;
184184
continue;
185185
}

src/main/java/com/jsoniter/any/Any.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import com.jsoniter.spi.TypeLiteral;
99

1010
import java.io.IOException;
11+
import java.math.BigDecimal;
12+
import java.math.BigInteger;
1113
import java.util.*;
1214

1315
public abstract class Any implements Iterable<Any> {
@@ -174,6 +176,14 @@ public final double toDouble(Object... keys) {
174176

175177
public abstract double toDouble();
176178

179+
public final BigInteger toBigInteger(Object ...keys) { return get(keys).toBigInteger(); }
180+
181+
public abstract BigInteger toBigInteger();
182+
183+
public final BigDecimal toBigDecimal(Object ...keys) { return get(keys).toBigDecimal(); }
184+
185+
public abstract BigDecimal toBigDecimal();
186+
177187
public final String toString(Object... keys) {
178188
return get(keys).toString();
179189
}

src/main/java/com/jsoniter/any/ArrayAny.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.jsoniter.output.JsonStream;
55

66
import java.io.IOException;
7+
import java.math.BigDecimal;
8+
import java.math.BigInteger;
79
import java.util.ArrayList;
810
import java.util.Iterator;
911
import java.util.List;
@@ -115,4 +117,14 @@ public float toFloat() {
115117
public double toDouble() {
116118
return val.size();
117119
}
120+
121+
@Override
122+
public BigInteger toBigInteger() {
123+
return BigInteger.valueOf(val.size());
124+
}
125+
126+
@Override
127+
public BigDecimal toBigDecimal() {
128+
return BigDecimal.valueOf(val.size());
129+
}
118130
}

src/main/java/com/jsoniter/any/ArrayLazyAny.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.jsoniter.spi.TypeLiteral;
77

88
import java.io.IOException;
9+
import java.math.BigDecimal;
10+
import java.math.BigInteger;
911
import java.util.ArrayList;
1012
import java.util.Iterator;
1113
import java.util.List;
@@ -65,6 +67,16 @@ public double toDouble() {
6567
return size();
6668
}
6769

70+
@Override
71+
public BigInteger toBigInteger() {
72+
return BigInteger.valueOf(size());
73+
}
74+
75+
@Override
76+
public BigDecimal toBigDecimal() {
77+
return BigDecimal.valueOf(size());
78+
}
79+
6880
@Override
6981
public int size() {
7082
fillCache();

src/main/java/com/jsoniter/any/ArrayWrapperAny.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import java.io.IOException;
77
import java.lang.reflect.Array;
8+
import java.math.BigDecimal;
9+
import java.math.BigInteger;
810
import java.util.ArrayList;
911
import java.util.Iterator;
1012
import java.util.List;
@@ -54,6 +56,16 @@ public double toDouble() {
5456
return size();
5557
}
5658

59+
@Override
60+
public BigInteger toBigInteger() {
61+
return BigInteger.valueOf(size());
62+
}
63+
64+
@Override
65+
public BigDecimal toBigDecimal() {
66+
return BigDecimal.valueOf(size());
67+
}
68+
5769
@Override
5870
public String toString() {
5971
if (cache == null) {

src/main/java/com/jsoniter/any/DoubleAny.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.jsoniter.output.JsonStream;
55

66
import java.io.IOException;
7+
import java.math.BigDecimal;
8+
import java.math.BigInteger;
79

810
class DoubleAny extends Any {
911

@@ -48,6 +50,16 @@ public double toDouble() {
4850
return val;
4951
}
5052

53+
@Override
54+
public BigInteger toBigInteger() {
55+
return BigInteger.valueOf((long) val);
56+
}
57+
58+
@Override
59+
public BigDecimal toBigDecimal() {
60+
return BigDecimal.valueOf(val);
61+
}
62+
5163
@Override
5264
public String toString() {
5365
return String.valueOf(val);

src/main/java/com/jsoniter/any/DoubleLazyAny.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.jsoniter.ValueType;
77

88
import java.io.IOException;
9+
import java.math.BigDecimal;
10+
import java.math.BigInteger;
911

1012
class DoubleLazyAny extends LazyAny {
1113

@@ -57,6 +59,16 @@ public double toDouble() {
5759
return cache;
5860
}
5961

62+
@Override
63+
public BigInteger toBigInteger() {
64+
return new BigInteger(toString());
65+
}
66+
67+
@Override
68+
public BigDecimal toBigDecimal() {
69+
return new BigDecimal(toString());
70+
}
71+
6072
private void fillCache() {
6173
if (!isCached) {
6274
JsonIterator iter = parse();

src/main/java/com/jsoniter/any/FalseAny.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.jsoniter.output.JsonStream;
55

66
import java.io.IOException;
7+
import java.math.BigDecimal;
8+
import java.math.BigInteger;
79

810
class FalseAny extends Any {
911

@@ -44,6 +46,16 @@ public double toDouble() {
4446
return 0;
4547
}
4648

49+
@Override
50+
public BigInteger toBigInteger() {
51+
return BigInteger.ZERO;
52+
}
53+
54+
@Override
55+
public BigDecimal toBigDecimal() {
56+
return BigDecimal.ZERO;
57+
}
58+
4759
@Override
4860
public String toString() {
4961
return "false";

src/main/java/com/jsoniter/any/FloatAny.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.jsoniter.spi.TypeLiteral;
66

77
import java.io.IOException;
8+
import java.math.BigDecimal;
9+
import java.math.BigInteger;
810

911
class FloatAny extends Any {
1012

@@ -49,6 +51,16 @@ public double toDouble() {
4951
return val;
5052
}
5153

54+
@Override
55+
public BigInteger toBigInteger() {
56+
return BigInteger.valueOf((long) val);
57+
}
58+
59+
@Override
60+
public BigDecimal toBigDecimal() {
61+
return BigDecimal.valueOf(val);
62+
}
63+
5264
@Override
5365
public String toString() {
5466
return String.valueOf(val);

0 commit comments

Comments
 (0)