Skip to content

Commit b40fd4f

Browse files
committed
support big decimal and big integer
1 parent 4defb13 commit b40fd4f

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

src/main/java/com/jsoniter/output/CodegenImplNative.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.io.IOException;
99
import java.lang.reflect.ParameterizedType;
1010
import java.lang.reflect.Type;
11+
import java.math.BigDecimal;
12+
import java.math.BigInteger;
1113
import java.util.HashMap;
1214
import java.util.IdentityHashMap;
1315
import java.util.Map;
@@ -236,6 +238,31 @@ public Any wrap(Object obj) {
236238
return JsonStream.wrap(obj);
237239
}
238240
});
241+
242+
put(BigDecimal.class, new Encoder() {
243+
@Override
244+
public void encode(Object obj, JsonStream stream) throws IOException {
245+
BigDecimal val = (BigDecimal) obj;
246+
stream.writeVal(val.toString());
247+
}
248+
249+
@Override
250+
public Any wrap(Object obj) {
251+
return Any.wrap(obj.toString());
252+
}
253+
});
254+
put(BigInteger.class, new Encoder() {
255+
@Override
256+
public void encode(Object obj, JsonStream stream) throws IOException {
257+
BigInteger val = (BigInteger) obj;
258+
stream.writeVal(val.toString());
259+
}
260+
261+
@Override
262+
public Any wrap(Object obj) {
263+
return Any.wrap(obj.toString());
264+
}
265+
});
239266
}};
240267

241268
public static String genWriteOp(String code, Type valueType) {

src/test/java/com/jsoniter/output/TestNative.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

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

810
public class TestNative extends TestCase {
911

@@ -110,4 +112,16 @@ public void test_boolean() throws IOException {
110112
stream.close();
111113
assertEquals("truefalse".replace('\'', '"'), baos.toString());
112114
}
115+
116+
public void test_big_decimal() throws IOException {
117+
stream.writeVal(new BigDecimal("12.34"));
118+
stream.close();
119+
assertEquals("'12.34'".replace('\'', '"'), baos.toString());
120+
}
121+
122+
public void test_big_integer() throws IOException {
123+
stream.writeVal(new BigInteger("1234"));
124+
stream.close();
125+
assertEquals("'1234'".replace('\'', '"'), baos.toString());
126+
}
113127
}

0 commit comments

Comments
 (0)