This commit is contained in:
redkale
2024-10-08 20:28:55 +08:00
parent 59ad1d8617
commit 76623e9c6b

View File

@@ -476,8 +476,33 @@ public class JsonReader extends Reader {
int end = this.limit; int end = this.limit;
char[] text0 = this.text; char[] text0 = this.text;
char ch; char ch;
boolean hex = false;
boolean dot = false; boolean dot = false;
if (value > 0) { // 十进制
for (; curr <= end; curr++) {
ch = text0[curr];
if (ch >= '0' && ch <= '9') {
if (dot) {
continue;
}
value = (value << 3) + (value << 1) + digits[ch];
} else if (ch == ',' || ch == '}' || ch == ']' || ch <= ' ' || ch == ':') {
curr--;
break;
} else if (ch == '"' || ch == '\'') {
if (quote) {
break;
}
throw new ConvertException("illegal escape(" + ch + ") (position = " + position + ")");
} else if (quote && ch <= ' ') {
// do nothing
} else if (ch == '.') {
dot = true;
} else {
throw new ConvertException("illegal escape(" + ch + ") (position = " + position + ")");
}
}
} else {
boolean hex = false;
for (; curr <= end; curr++) { for (; curr <= end; curr++) {
ch = text0[curr]; ch = text0[curr];
if (ch >= '0' && ch <= '9') { if (ch >= '0' && ch <= '9') {
@@ -522,6 +547,7 @@ public class JsonReader extends Reader {
throw new ConvertException("illegal escape(" + ch + ") (position = " + position + ")"); throw new ConvertException("illegal escape(" + ch + ") (position = " + position + ")");
} }
} }
}
this.position = curr; this.position = curr;
return negative ? -value : value; return negative ? -value : value;
} }
@@ -613,8 +639,33 @@ public class JsonReader extends Reader {
int end = this.limit; int end = this.limit;
char[] text0 = this.text; char[] text0 = this.text;
char ch; char ch;
boolean hex = false;
boolean dot = false; boolean dot = false;
if (value > 0) { // 十进制
for (; curr <= end; curr++) {
ch = text0[curr];
if (ch >= '0' && ch <= '9') {
if (dot) {
continue;
}
value = (value << 3) + (value << 1) + digits[ch];
} else if (ch == ',' || ch == '}' || ch == ']' || ch <= ' ' || ch == ':') {
curr--;
break;
} else if (ch == '"' || ch == '\'') {
if (quote) {
break;
}
throw new ConvertException("illegal escape(" + ch + ") (position = " + position + ")");
} else if (quote && ch <= ' ') {
// do nothing
} else if (ch == '.') {
dot = true;
} else {
throw new ConvertException("illegal escape(" + ch + ") (position = " + position + ")");
}
}
} else {
boolean hex = false;
for (; curr <= end; curr++) { for (; curr <= end; curr++) {
ch = text0[curr]; ch = text0[curr];
if (ch >= '0' && ch <= '9') { if (ch >= '0' && ch <= '9') {
@@ -659,6 +710,7 @@ public class JsonReader extends Reader {
throw new ConvertException("illegal escape(" + ch + ") (position = " + position + ")"); throw new ConvertException("illegal escape(" + ch + ") (position = " + position + ")");
} }
} }
}
this.position = curr; this.position = curr;
return negative ? -value : value; return negative ? -value : value;
} }
@@ -679,17 +731,17 @@ public class JsonReader extends Reader {
int curr = this.position; int curr = this.position;
if (ch == '"' || ch == '\'') { if (ch == '"' || ch == '\'') {
final char quote = ch; final char quote = ch;
for (; ; ) { while ((ch = text0[++curr]) != quote) {
ch = text0[++curr]; if (node != null) {
if (ch == quote) { node = node.getNode(ch);
break;
} }
node = node == null ? null : node.getNode(ch);
} }
this.position = curr; this.position = curr;
return node == null ? null : node.getValue(); return node == null ? null : node.getValue();
} else { } else {
node = node == null ? null : node.getNode(ch); if (node != null) {
node = node.getNode(ch);
}
for (; ; ) { for (; ; ) {
if (curr == eof) { if (curr == eof) {
break; break;
@@ -699,7 +751,9 @@ public class JsonReader extends Reader {
curr--; curr--;
break; break;
} }
node = node == null ? null : node.getNode(ch); if (node != null) {
node = node.getNode(ch);
}
} }
this.position = curr; this.position = curr;
return node == null ? null : node.getValue(); return node == null ? null : node.getValue();
@@ -973,16 +1027,16 @@ public class JsonReader extends Reader {
} }
private String readEscapeValue(final char expected, int start) { private String readEscapeValue(final char expected, int start) {
CharArray array = this.array(); CharArray tmp = this.array();
final char[] text0 = this.text; final char[] text0 = this.text;
int curr = this.position; int curr = this.position;
array.append(text0, start, curr + 1 - start); tmp.append(text0, start, curr + 1 - start);
char c; char c;
for (; ; ) { for (; ; ) {
c = text0[++curr]; c = text0[++curr];
if (c == expected) { if (c == expected) {
this.position = curr; this.position = curr;
return array.toStringThenClear(); return tmp.toStringThenClear();
} else if (c == '\\') { } else if (c == '\\') {
c = text0[++curr]; c = text0[++curr];
switch (c) { switch (c) {
@@ -990,27 +1044,29 @@ public class JsonReader extends Reader {
case '\'': case '\'':
case '\\': case '\\':
case '/': case '/':
array.append(c); tmp.append(c);
break; break;
case 'n': case 'n':
array.append('\n'); tmp.append('\n');
break; break;
case 'r': case 'r':
array.append('\r'); tmp.append('\r');
break; break;
case 'u': case 'u':
array.append((char) Integer.parseInt( int hex = (Character.digit(text0[++curr], 16) << 12)
new String(new char[] {text0[++curr], text0[++curr], text0[++curr], text0[++curr]}), + (Character.digit(text0[++curr], 16) << 8)
16)); + (Character.digit(text0[++curr], 16) << 4)
+ Character.digit(text0[++curr], 16);
tmp.append((char) hex);
break; break;
case 't': case 't':
array.append('\t'); tmp.append('\t');
break; break;
case 'b': case 'b':
array.append('\b'); tmp.append('\b');
break; break;
case 'f': case 'f':
array.append('\f'); tmp.append('\f');
break; break;
default: default:
this.position = curr; this.position = curr;
@@ -1018,7 +1074,7 @@ public class JsonReader extends Reader {
+ new String(this.text)); + new String(this.text));
} }
} else { } else {
array.append(c); tmp.append(c);
} }
} }
} }
@@ -1039,7 +1095,7 @@ public class JsonReader extends Reader {
digits[i] = i - 'A' + 10; digits[i] = i - 'A' + 10;
} }
digits['"'] = digits['\''] = -2; // -2 跳过 digits['"'] = digits['\''] = -2; // -2 跳过
digits[' '] = digits['\t'] = digits['\r'] = digits['\n'] = -3; // -3可能跳过 digits[' '] = digits['\t'] = digits['\b'] = digits['\f'] = digits['\r'] = digits['\n'] = -3; // -3可能跳过
digits[','] = digits['}'] = digits[']'] = digits[':'] = -4; // -4退出 digits[','] = digits['}'] = digits[']'] = digits[':'] = -4; // -4退出
} }