This commit is contained in:
redkale
2024-09-29 09:34:33 +08:00
parent 73eb306eb2
commit 090bfb5629
2 changed files with 90 additions and 82 deletions

View File

@@ -69,7 +69,7 @@ public class JsonByteBufferReader extends JsonReader {
return nextChar(null); return nextChar(null);
} }
protected final char nextChar(StringBuilder sb) { protected final char nextChar(CharArray sb) {
if (currentChar != 0) { if (currentChar != 0) {
char ch = currentChar; char ch = currentChar;
this.currentChar = 0; this.currentChar = 0;
@@ -496,38 +496,38 @@ public class JsonByteBufferReader extends JsonReader {
if (ch == 0) { if (ch == 0) {
return null; return null;
} }
final StringBuilder sb = new StringBuilder(); final CharArray tmp = array();
if (ch == '"' || ch == '\'') { if (ch == '"' || ch == '\'') {
final char quote = ch; final char quote = ch;
for (; ; ) { for (; ; ) {
ch = nextChar(sb); ch = nextChar(tmp);
if (ch == '\\') { if (ch == '\\') {
char c = nextChar(sb); char c = nextChar(tmp);
switch (c) { switch (c) {
case '"': case '"':
case '\'': case '\'':
case '\\': case '\\':
case '/': case '/':
sb.append(c); tmp.append(c);
break; break;
case 'n': case 'n':
sb.append('\n'); tmp.append('\n');
break; break;
case 'r': case 'r':
sb.append('\r'); tmp.append('\r');
break; break;
case 'u': case 'u':
sb.append((char) Integer.parseInt( tmp.append((char) Integer.parseInt(
new String(new char[] {nextChar(), nextChar(), nextChar(), nextChar()}), 16)); new String(new char[] {nextChar(), nextChar(), nextChar(), nextChar()}), 16));
break; break;
case 't': case 't':
sb.append('\t'); tmp.append('\t');
break; break;
case 'b': case 'b':
sb.append('\b'); tmp.append('\b');
break; break;
case 'f': case 'f':
sb.append('\f'); tmp.append('\f');
break; break;
default: default:
throw new ConvertException("illegal escape(" + c + ") (position = " + this.position + ")"); throw new ConvertException("illegal escape(" + c + ") (position = " + this.position + ")");
@@ -535,41 +535,44 @@ public class JsonByteBufferReader extends JsonReader {
} else if (ch == quote || ch == 0) { } else if (ch == quote || ch == 0) {
break; break;
} else { } else {
sb.append(ch); tmp.append(ch);
} }
} }
return sb.toString(); return tmp.toStringThenClear();
} else { } else {
sb.append(ch); tmp.append(ch);
for (; ; ) { for (; ; ) {
ch = nextChar(sb); ch = nextChar(tmp);
if (ch == '\\') { if (ch == '\\') {
char c = nextChar(sb); char c = nextChar(tmp);
switch (c) { switch (c) {
case '"': case '"':
case '\'': case '\'':
case '\\': case '\\':
case '/': case '/':
sb.append(c); tmp.append(c);
break; break;
case 'n': case 'n':
sb.append('\n'); tmp.append('\n');
break; break;
case 'r': case 'r':
sb.append('\r'); tmp.append('\r');
break; break;
case 'u': case 'u':
sb.append((char) Integer.parseInt( int hex = (Character.digit(nextChar(), 16) << 12)
new String(new char[] {nextChar(), nextChar(), nextChar(), nextChar()}), 16)); + (Character.digit(nextChar(), 16) << 8)
+ (Character.digit(nextChar(), 16) << 4)
+ Character.digit(nextChar(), 16);
tmp.append((char) hex);
break; break;
case 't': case 't':
sb.append('\t'); tmp.append('\t');
break; break;
case 'b': case 'b':
sb.append('\b'); tmp.append('\b');
break; break;
case 'f': case 'f':
sb.append('\f'); tmp.append('\f');
break; break;
default: default:
throw new ConvertException("illegal escape(" + c + ") (position = " + this.position + ")"); throw new ConvertException("illegal escape(" + c + ") (position = " + this.position + ")");
@@ -578,10 +581,10 @@ public class JsonByteBufferReader extends JsonReader {
backChar(ch); backChar(ch);
break; break;
} else { } else {
sb.append(ch); tmp.append(ch);
} }
} }
String rs = sb.toString(); String rs = tmp.toStringThenClear();
return "null".equalsIgnoreCase(rs) ? null : rs; return "null".equalsIgnoreCase(rs) ? null : rs;
} }
} }
@@ -593,22 +596,23 @@ public class JsonByteBufferReader extends JsonReader {
return null; return null;
} }
DeMemberNode node = memberInfo.getMemberNode(); DeMemberNode node = memberInfo.getMemberNode();
StringBuilder sb = new StringBuilder(); CharArray tmp = array();
if (ch == '"' || ch == '\'') { if (ch == '"' || ch == '\'') {
final char quote = ch; final char quote = ch;
for (; ; ) { for (; ; ) {
ch = nextChar(sb); ch = nextChar(tmp);
if (ch == quote || ch == 0) { if (ch == quote || ch == 0) {
break; break;
} else { } else {
node = node == null ? null : node.getNode(ch); node = node == null ? null : node.getNode(ch);
} }
} }
tmp.clear();
return node == null ? null : node.getValue(); return node == null ? null : node.getValue();
} else { } else {
node = node == null ? null : node.getNode(ch); node = node == null ? null : node.getNode(ch);
for (; ; ) { for (; ; ) {
ch = nextChar(sb); ch = nextChar(tmp);
if (ch == ',' || ch == ']' || ch == '}' || ch <= ' ' || ch == ':') { // ch <= ' ' 包含 0 if (ch == ',' || ch == ']' || ch == '}' || ch <= ' ' || ch == ':') { // ch <= ' ' 包含 0
backChar(ch); backChar(ch);
break; break;
@@ -616,6 +620,7 @@ public class JsonByteBufferReader extends JsonReader {
node = node == null ? null : node.getNode(ch); node = node == null ? null : node.getNode(ch);
} }
} }
tmp.clear();
return node == null ? null : node.getValue(); return node == null ? null : node.getValue();
} }
} }

View File

@@ -865,13 +865,67 @@ public class JsonReader extends Reader {
final int end = limit; final int end = limit;
char quote = nextGoodChar(true); char quote = nextGoodChar(true);
int curr = this.position; int curr = this.position;
if (quote != '"' && quote != '\'') { if (quote == '"' || quote == '\'') {
final int start = ++curr;
CharArray tmp = null;
char c;
for (; curr <= end; curr++) {
char ch = text0[curr];
if (ch == quote) {
break;
} else if (ch == '\\') {
if (tmp == null) {
tmp = array();
tmp.append(text0, start, curr - start);
}
c = text0[++curr];
switch (c) {
case '"':
case '\'':
case '\\':
case '/':
tmp.append(c);
break;
case 'n':
tmp.append('\n');
break;
case 'r':
tmp.append('\r');
break;
case 'u':
int hex = (Character.digit(text0[++curr], 16) << 12)
+ (Character.digit(text0[++curr], 16) << 8)
+ (Character.digit(text0[++curr], 16) << 4)
+ Character.digit(text0[++curr], 16);
tmp.append((char) hex);
break;
case 't':
tmp.append('\t');
break;
case 'b':
tmp.append('\b');
break;
case 'f':
tmp.append('\f');
break;
default:
this.position = curr;
throw new ConvertException("illegal escape(" + c + ") (position = " + this.position
+ ") in " + new String(this.text));
}
} else if (tmp != null) {
tmp.append(ch);
}
}
this.position = curr;
return tmp != null ? tmp.toStringThenClear() : new String(text0, start, curr - start);
} else { // 不带双引号
final int start = curr; final int start = curr;
if (quote == 'n' if (quote == 'n'
&& end >= curr + 3 && end >= curr + 3
&& text0[1 + curr] == 'u' && text0[1 + curr] == 'u'
&& text0[2 + curr] == 'l' && text0[2 + curr] == 'l'
&& text0[3 + curr] == 'l') { && text0[3 + curr] == 'l') { // 为null或者null开头的字符串
curr += 3; curr += 3;
this.position = curr; this.position = curr;
if (curr < end) { if (curr < end) {
@@ -915,57 +969,6 @@ public class JsonReader extends Reader {
return new String(text0, start, curr - start); return new String(text0, start, curr - start);
} }
} }
final int start = ++curr;
CharArray array = null;
char c;
for (; curr <= end; curr++) {
char ch = text0[curr];
if (ch == quote) {
break;
} else if (ch == '\\') {
if (array == null) {
array = array();
array.append(text0, start, curr - start);
}
c = text0[++curr];
switch (c) {
case '"':
case '\'':
case '\\':
case '/':
array.append(c);
break;
case 'n':
array.append('\n');
break;
case 'r':
array.append('\r');
break;
case 'u':
array.append((char) Integer.parseInt(
new String(new char[] {text0[++curr], text0[++curr], text0[++curr], text0[++curr]}),
16));
break;
case 't':
array.append('\t');
break;
case 'b':
array.append('\b');
break;
case 'f':
array.append('\f');
break;
default:
this.position = curr;
throw new ConvertException("illegal escape(" + c + ") (position = " + this.position + ") in "
+ new String(this.text));
}
} else if (array != null) {
array.append(ch);
}
}
this.position = curr;
return array != null ? array.toStringThenClear() : new String(text0, start, curr - start);
} }
private String readEscapeValue(final char expected, int start) { private String readEscapeValue(final char expected, int start) {