This commit is contained in:
redkale
2024-09-17 16:33:03 +08:00
parent 5015834d69
commit 016ee632c1
5 changed files with 59 additions and 73 deletions

View File

@@ -20,9 +20,6 @@ public abstract class Reader {
MAP;
}
// 当前对象字段名的游标
protected int fieldIndex;
public static final short SIGN_NULL = -1;
public static final short SIGN_NOLENGTH = -2;
@@ -93,7 +90,6 @@ public abstract class Reader {
* @return 返回字段数
*/
public String readObjectB(final Class clazz) {
this.fieldIndex = 0;
return null;
}

View File

@@ -138,7 +138,6 @@ public class BsonReader extends Reader {
@Override
public final String readObjectB(final Class clazz) {
this.fieldIndex = 0; // 必须要重置为0
final String newcls = readClassName();
if (newcls != null && !newcls.isEmpty()) {
return newcls;

View File

@@ -164,7 +164,6 @@ public class JsonByteBufferReader extends JsonReader {
*/
@Override
public final String readObjectB(final Class clazz) {
this.fieldIndex = 0; // 必须要重置为0
char ch = nextGoodChar(true);
if (ch == '{') {
return "";
@@ -175,17 +174,7 @@ public class JsonByteBufferReader extends JsonReader {
if (ch == 'N' && nextChar() == 'U' && nextChar() == 'L' && nextChar() == 'L') {
return null;
}
int pos = this.position;
StringBuilder sb = new StringBuilder();
sb.append(ch);
char one;
try {
while ((one = nextChar()) != 0) sb.append(one);
} catch (Exception e) {
// do nothing
}
throw new ConvertException(
"a json object text must begin with '{' (position = " + pos + ") but '" + ch + "' in (" + sb + ")");
throw new ConvertException("a json object must begin with '{' (position = " + position + ") but '" + ch + "'");
}
/**

View File

@@ -175,41 +175,45 @@ public class JsonReader extends Reader {
* @return 有效字符
*/
protected char nextGoodChar(boolean allowComment) {
char c;
for (; ; ) {
c = nextChar();
if (c == 0) {
return c; // 0 表示buffer结尾了
}
char c = 0;
char[] text0 = this.text;
int end = this.limit;
int curr = ++this.position;
for (; curr <= end; curr++) {
c = text0[curr];
if (c > ' ') {
if (allowComment && c == '/') { // 支持单行和多行注释
char n = nextChar();
if (n == '/') {
for (; ; ) {
if (nextChar() == '\n') {
if (c == '/' && allowComment) { // 支持单行和多行注释
char n = text0[++curr];
if (n == '/') { // 单行注释
for (++curr; curr <= end; curr++) {
if (text0[curr] == '\n') {
break;
}
}
this.position = curr;
return nextGoodChar(allowComment);
} else if (n == '*') {
} else if (n == '*') { // 多行注释
char nc;
char lc = 0;
for (; ; ) {
nc = nextChar();
for (++curr; curr <= end; curr++) {
nc = text0[curr];
if (nc == '/' && lc == '*') {
break;
}
lc = nc;
}
this.position = curr;
return nextGoodChar(allowComment);
} else {
throw new ConvertException("illegal escape(" + n + ") (position = " + this.position + ") in '"
+ new String(text) + "'");
throw new ConvertException(
"illegal escape(" + n + ") (position = " + curr + ") in " + new String(text));
}
}
return c;
break;
}
}
this.position = curr;
return c;
}
/**
@@ -266,7 +270,6 @@ public class JsonReader extends Reader {
*/
@Override
public String readObjectB(final Class clazz) {
this.fieldIndex = 0; // 必须要重置为0
if (this.text.length == 0) {
return null;
}
@@ -280,7 +283,7 @@ public class JsonReader extends Reader {
if (ch == 'N' && text[++position] == 'U' && text[++position] == 'L' && text[++position] == 'L') {
return null;
}
throw new ConvertException("a json object text must begin with '{' (position = " + position + ") but '" + ch
throw new ConvertException("a json object must begin with '{' (position = " + position + ") but '" + ch
+ "' in " + new String(this.text));
}
@@ -350,8 +353,8 @@ public class JsonReader extends Reader {
if (ch == ':') {
return;
}
throw new ConvertException("'" + new String(text) + "'expected a ':' but '" + ch + "'(position = " + position
+ ") in (" + new String(this.text) + ")");
throw new ConvertException(
"expected a ':' but '" + ch + "'(position = " + position + ") in " + new String(this.text));
}
@Override
@@ -477,13 +480,14 @@ public class JsonReader extends Reader {
}
return negative ? Integer.MIN_VALUE : Integer.MAX_VALUE;
}
int curr = ++this.position;
int end = this.limit;
char[] text0 = this.text;
char ch;
boolean hex = false;
boolean dot = false;
for (; ; ) {
char ch = nextChar();
if (ch == 0) {
break;
}
for (; curr <= end; curr++) {
ch = text0[curr];
if (ch >= '0' && ch <= '9') {
if (dot) {
continue;
@@ -520,12 +524,13 @@ public class JsonReader extends Reader {
} else if (ch == '.') {
dot = true;
} else if (ch == ',' || ch == '}' || ch == ']' || ch <= ' ' || ch == ':') {
backChar(ch);
curr--;
break;
} else {
throw new ConvertException("illegal escape(" + ch + ") (position = " + position + ")");
}
}
this.position = curr;
return negative ? -value : value;
}
@@ -612,13 +617,14 @@ public class JsonReader extends Reader {
}
return negative ? Long.MIN_VALUE : Long.MAX_VALUE;
}
int curr = ++this.position;
int end = this.limit;
char[] text0 = this.text;
char ch;
boolean hex = false;
boolean dot = false;
for (; ; ) {
char ch = nextChar();
if (ch == 0) {
break;
}
for (; curr <= end; curr++) {
ch = text0[curr];
if (ch >= '0' && ch <= '9') {
if (dot) {
continue;
@@ -655,12 +661,13 @@ public class JsonReader extends Reader {
} else if (ch == '.') {
dot = true;
} else if (ch == ',' || ch == '}' || ch == ']' || ch <= ' ' || ch == ':') {
backChar(ch);
curr--;
break;
} else {
throw new ConvertException("illegal escape(" + ch + ") (position = " + position + ")");
}
}
this.position = curr;
return negative ? -value : value;
}
@@ -677,37 +684,32 @@ public class JsonReader extends Reader {
DeMemberNode node = memberInfo.getMemberNode();
char ch = nextGoodChar(true); // 需要跳过注释
final char[] text0 = this.text;
int currpos = this.position;
int curr = this.position;
if (ch == '"' || ch == '\'') {
final char quote = ch;
for (; ; ) {
ch = text0[++currpos];
ch = text0[++curr];
if (ch == quote) {
break;
}
node = node == null ? null : node.getNode(ch);
}
this.position = currpos;
this.position = curr;
return node == null ? null : node.getValue();
} else {
node = node == null ? null : node.getNode(ch);
int start = currpos;
for (; ; ) {
if (currpos == eof) {
if (curr == eof) {
break;
}
ch = text0[++currpos];
ch = text0[++curr];
if (ch == ',' || ch == ']' || ch == '}' || ch <= ' ' || ch == ':') {
curr--;
break;
}
node = node == null ? null : node.getNode(ch);
}
int len = currpos - start;
if (len < 1) {
this.position = currpos;
} else {
this.position = currpos - 1;
}
this.position = curr;
return node == null ? null : node.getValue();
}
}

View File

@@ -22,15 +22,6 @@ public class Json5Test {
@Test
public void run1() throws Exception {
JsonFactory factory = JsonFactory.root().withFeatures(Convert.FEATURE_TINY | Convert.FEATURE_NULLABLE);
final JsonConvert convert = factory.getConvert();
Json5Bean bean = new Json5Bean();
bean.id = 60;
System.out.println(convert.convertTo(bean));
}
@Test
public void run2() throws Exception {
JsonConvert convert = JsonConvert.root();
Json5Bean bean = new Json5Bean();
bean.id = 500;
@@ -55,11 +46,20 @@ public class Json5Test {
System.out.println(Arrays.toString(ints1));
}
@Test
public void run2() throws Exception {
JsonFactory factory = JsonFactory.root().withFeatures(Convert.FEATURE_TINY | Convert.FEATURE_NULLABLE);
final JsonConvert convert = factory.getConvert();
Json5Bean bean = new Json5Bean();
bean.id = 60;
System.out.println(convert.convertTo(bean));
}
public static class Json5Bean {
public int id;
public int idx;
public long idx;
public float decmails;
@@ -74,7 +74,7 @@ public class Json5Test {
public int hashCode() {
int hash = 7;
hash = 47 * hash + this.id;
hash = 47 * hash + this.idx;
hash = 47 * hash + (int) this.idx;
hash = 47 * hash + Float.floatToIntBits(this.decmails);
hash = 47 * hash + (int) (this.value ^ (this.value >>> 32));
hash = 47 * hash + Objects.hashCode(this.name);