Skip to content

Commit a17aa11

Browse files
committed
add test for number
1 parent d7ae705 commit a17aa11

6 files changed

Lines changed: 374 additions & 205 deletions

File tree

src/main/java/com/jsoniter/Codegen.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class Codegen {
2525
put(Float.class.getName(), "Float.valueOf(iter.readFloat())");
2626
put(Double.class.getName(), "Double.valueOf(iter.readDouble())");
2727
put(Boolean.class.getName(), "Boolean.valueOf(iter.readBoolean())");
28-
put(Byte.class.getName(), "Byte.valueOf(iter.readShort())");
28+
put(Byte.class.getName(), "Byte.valueOf((byte)iter.readShort())");
29+
put(Character.class.getName(), "Character.valueOf((char)iter.readShort())");
2930
put(Short.class.getName(), "Short.valueOf(iter.readShort())");
3031
put(Integer.class.getName(), "Integer.valueOf(iter.readInt())");
3132
put(Long.class.getName(), "Long.valueOf(iter.readLong())");
@@ -166,9 +167,30 @@ private static String genMap(Class clazz, Type valueType) {
166167
}
167168

168169
private static String genNative(String nativeReadKey) {
170+
if ("boolean".equals(nativeReadKey)) {
171+
nativeReadKey = Boolean.class.getName();
172+
} else if ("byte".equals(nativeReadKey)) {
173+
nativeReadKey = Byte.class.getName();
174+
} else if ("char".equals(nativeReadKey)) {
175+
nativeReadKey = Character.class.getName();
176+
} else if ("short".equals(nativeReadKey)) {
177+
nativeReadKey = Short.class.getName();
178+
} else if ("int".equals(nativeReadKey)) {
179+
nativeReadKey = Integer.class.getName();
180+
} else if ("long".equals(nativeReadKey)) {
181+
nativeReadKey = Long.class.getName();
182+
} else if ("float".equals(nativeReadKey)) {
183+
nativeReadKey = Float.class.getName();
184+
} else if ("double".equals(nativeReadKey)) {
185+
nativeReadKey = Double.class.getName();
186+
}
187+
String op = NATIVE_READS.get(nativeReadKey);
188+
if (op == null) {
189+
throw new RuntimeException("do not know how to read: " + nativeReadKey);
190+
}
169191
StringBuilder lines = new StringBuilder();
170192
append(lines, "public static Object decode_(com.jsoniter.Jsoniter iter) {");
171-
append(lines, "return " + NATIVE_READS.get(nativeReadKey) + ";");
193+
append(lines, "return " + op + ";");
172194
append(lines, "}");
173195
return lines.toString();
174196
}

src/main/java/com/jsoniter/Jsoniter.java

Lines changed: 15 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class Jsoniter implements Closeable {
4040
int tail;
4141
boolean eof;
4242
private final Slice reusableSlice = new Slice(null, 0, 0);
43-
private char[] reusableChars = new char[32];
43+
char[] reusableChars = new char[32];
4444

4545
static {
4646
for (int i = 0; i < digits.length; i++) {
@@ -84,40 +84,44 @@ public class Jsoniter implements Closeable {
8484
breaks[']'] = true;
8585
}
8686

87-
public Jsoniter(InputStream in, byte[] buf) throws IOException {
87+
public Jsoniter(InputStream in, byte[] buf) {
8888
this.in = in;
8989
this.buf = buf;
9090
if (this.in == null) {
9191
tail = buf.length;
9292
}
9393
}
9494

95-
public static Jsoniter parse(InputStream in, int bufSize) throws IOException {
95+
public static Jsoniter parse(InputStream in, int bufSize) {
9696
return new Jsoniter(in, new byte[bufSize]);
9797
}
9898

99-
public static Jsoniter parse(byte[] buf) throws IOException {
99+
public static Jsoniter parse(byte[] buf) {
100100
return new Jsoniter(null, buf);
101101
}
102102

103-
public static Jsoniter parse(String str) throws IOException {
103+
public static Jsoniter parse(String str) {
104104
return parse(str.getBytes());
105105
}
106106

107-
public final void reset(byte[] buf) throws IOException {
107+
public final void reset(byte[] buf) {
108108
this.buf = buf;
109109
this.head = 0;
110110
this.tail = buf.length;
111111
this.eof = false;
112112
}
113113

114-
public final void reset(InputStream in) throws IOException {
114+
public final void reset(InputStream in) {
115115
this.in = in;
116116
this.head = 0;
117117
this.tail = 0;
118118
this.eof = false;
119119
}
120120

121+
public void reset() {
122+
reset(this.buf);
123+
}
124+
121125
public final void close() throws IOException {
122126
if (in != null) {
123127
in.close();
@@ -586,143 +590,20 @@ public final String readObject() throws IOException {
586590
}
587591
}
588592

589-
final String readNumber() throws IOException {
590-
int j = 0;
591-
for (byte c = nextToken(); !eof; c = readByte()) {
592-
if (j == reusableChars.length) {
593-
char[] newBuf = new char[reusableChars.length * 2];
594-
System.arraycopy(reusableChars, 0, newBuf, 0, reusableChars.length);
595-
reusableChars = newBuf;
596-
}
597-
switch (c) {
598-
case '-':
599-
case '+':
600-
case '.':
601-
case 'e':
602-
case 'E':
603-
case '0':
604-
case '1':
605-
case '2':
606-
case '3':
607-
case '4':
608-
case '5':
609-
case '6':
610-
case '7':
611-
case '8':
612-
case '9':
613-
reusableChars[j++] = (char) c;
614-
break;
615-
default:
616-
unreadByte();
617-
return new String(reusableChars, 0, j);
618-
}
619-
}
620-
return new String(reusableChars, 0, j);
621-
}
622-
623593
public final float readFloat() throws IOException {
624-
return Float.valueOf(readNumber());
625-
}
626-
627-
public final double readDoubleSlowPath() throws IOException {
628-
return Double.valueOf(readNumber());
594+
return NumberReader.readFloat(this);
629595
}
630596

631597
public final double readDouble() throws IOException {
632-
if (head == tail) {
633-
if (!loadMore()) {
634-
throw reportError("readDouble", "no more to read");
635-
}
636-
}
637-
final byte ch = buf[head];
638-
if (ch == '-') {
639-
return parseNegativeDouble(head + 1);
640-
} else if (ch == '+') {
641-
return parsePositiveDouble(head + 1);
642-
}
643-
return parsePositiveDouble(head);
644-
}
645-
646-
private final double parsePositiveDouble(int start) throws IOException {
647-
long value = 0;
648-
byte c = ' ';
649-
int i = start;
650-
for (; i < tail; i++) {
651-
c = buf[i];
652-
if (c == ',' || c == '}' || c == ']' || c == ' ') {
653-
head = i;
654-
return value;
655-
}
656-
if (c == '.') break;
657-
final int ind = digits[c];
658-
value = (value << 3) + (value << 1) + ind;
659-
if (ind < 0 || ind > 9) {
660-
return readDoubleSlowPath();
661-
}
662-
}
663-
if (c == '.') {
664-
i++;
665-
long div = 1;
666-
for (; i < tail; i++) {
667-
c = buf[i];
668-
if (c == ',' || c == '}' || c == ']' || c == ' ') {
669-
head = i;
670-
return value / (double) div;
671-
}
672-
final int ind = digits[c];
673-
div = (div << 3) + (div << 1);
674-
value = (value << 3) + (value << 1) + ind;
675-
if (ind < 0 || ind > 9) {
676-
return readDoubleSlowPath();
677-
}
678-
}
679-
}
680-
return readDoubleSlowPath();
681-
}
682-
683-
private final double parseNegativeDouble(int start) throws IOException {
684-
long value = 0;
685-
byte c = ' ';
686-
int i = start;
687-
for (; i < tail; i++) {
688-
c = buf[i];
689-
if (c == ',' || c == '}' || c == ']' || c == ' ') {
690-
head = i;
691-
return value;
692-
}
693-
if (c == '.') break;
694-
final int ind = digits[c];
695-
value = (value << 3) + (value << 1) - ind;
696-
if (ind < 0 || ind > 9) {
697-
return readDoubleSlowPath();
698-
}
699-
}
700-
if (c == '.') {
701-
i++;
702-
long div = 1;
703-
for (; i < tail; i++) {
704-
c = buf[i];
705-
if (c == ',' || c == '}' || c == ']' || c == ' ') {
706-
head = i;
707-
return value / (double) div;
708-
}
709-
final int ind = digits[c];
710-
div = (div << 3) + (div << 1);
711-
value = (value << 3) + (value << 1) - ind;
712-
if (ind < 0 || ind > 9) {
713-
return readDoubleSlowPath();
714-
}
715-
}
716-
}
717-
return readDoubleSlowPath();
598+
return NumberReader.readDouble(this);
718599
}
719600

720601
public final BigDecimal readBigDecimal() throws IOException {
721-
return new BigDecimal(readNumber());
602+
return new BigDecimal(NumberReader.readNumber(this));
722603
}
723604

724605
public final BigInteger readBigInteger() throws IOException {
725-
return new BigInteger(readNumber());
606+
return new BigInteger(NumberReader.readNumber(this));
726607
}
727608

728609
public final Any readAny() throws IOException {

0 commit comments

Comments
 (0)