test
This commit is contained in:
@@ -396,9 +396,9 @@ public class JsonReader extends Reader {
|
|||||||
@Override
|
@Override
|
||||||
public int readInt() {
|
public int readInt() {
|
||||||
char firstchar = nextGoodChar(true);
|
char firstchar = nextGoodChar(true);
|
||||||
boolean quote = false;
|
char quote = 0;
|
||||||
if (firstchar == '"' || firstchar == '\'') {
|
if (firstchar == '"' || firstchar == '\'') {
|
||||||
quote = true;
|
quote = firstchar;
|
||||||
firstchar = nextGoodChar(false);
|
firstchar = nextGoodChar(false);
|
||||||
if (firstchar == '"' || firstchar == '\'') {
|
if (firstchar == '"' || firstchar == '\'') {
|
||||||
return 0;
|
return 0;
|
||||||
@@ -406,15 +406,25 @@ public class JsonReader extends Reader {
|
|||||||
}
|
}
|
||||||
int value = 0;
|
int value = 0;
|
||||||
final boolean negative = firstchar == '-';
|
final boolean negative = firstchar == '-';
|
||||||
if (!negative) {
|
if (negative) { // 负数
|
||||||
if (firstchar == '+') {
|
firstchar = nextChar();
|
||||||
firstchar = nextChar(); // 兼容+开头的
|
if (firstchar != 'N' && firstchar != 'I') {
|
||||||
}
|
|
||||||
if (firstchar < '0' || firstchar > '9') {
|
if (firstchar < '0' || firstchar > '9') {
|
||||||
throw new ConvertException("illegal escape(" + firstchar + ") (position = " + position + ")");
|
throw new ConvertException("illegal escape(" + firstchar + ") (position = " + position + ")");
|
||||||
}
|
}
|
||||||
value = digits[firstchar];
|
value = digits[firstchar];
|
||||||
}
|
}
|
||||||
|
} else { // 正数
|
||||||
|
if (firstchar == '+') {
|
||||||
|
firstchar = nextChar(); // 兼容+开头的
|
||||||
|
}
|
||||||
|
if (firstchar != 'N' && firstchar != 'I') {
|
||||||
|
if (firstchar < '0' || firstchar > '9') {
|
||||||
|
throw new ConvertException("illegal escape(" + firstchar + ") (position = " + position + ")");
|
||||||
|
}
|
||||||
|
value = digits[firstchar];
|
||||||
|
}
|
||||||
|
}
|
||||||
if (firstchar == 'N') {
|
if (firstchar == 'N') {
|
||||||
if (negative) {
|
if (negative) {
|
||||||
throw new ConvertException("illegal escape(" + firstchar + ") (position = " + position + ")");
|
throw new ConvertException("illegal escape(" + firstchar + ") (position = " + position + ")");
|
||||||
@@ -422,11 +432,8 @@ public class JsonReader extends Reader {
|
|||||||
if (nextChar() != 'a' || nextChar() != 'N') {
|
if (nextChar() != 'a' || nextChar() != 'N') {
|
||||||
throw new ConvertException("illegal escape(" + text[position] + ") (position = " + position + ")");
|
throw new ConvertException("illegal escape(" + text[position] + ") (position = " + position + ")");
|
||||||
}
|
}
|
||||||
if (quote) {
|
if (quote > 0 && nextChar() != quote) {
|
||||||
char c = nextChar();
|
throw new ConvertException("illegal escape(" + text[position] + ") (position = " + position + ")");
|
||||||
if (c != '"' && c != '\'') {
|
|
||||||
throw new ConvertException("illegal escape(" + c + ") (position = " + position + ")");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return 0; // NaN 返回0;
|
return 0; // NaN 返回0;
|
||||||
} else if (firstchar == 'I') { // Infinity
|
} else if (firstchar == 'I') { // Infinity
|
||||||
@@ -439,11 +446,8 @@ public class JsonReader extends Reader {
|
|||||||
|| nextChar() != 'y') {
|
|| nextChar() != 'y') {
|
||||||
throw new ConvertException("illegal escape(" + text[position] + ") (position = " + position + ")");
|
throw new ConvertException("illegal escape(" + text[position] + ") (position = " + position + ")");
|
||||||
}
|
}
|
||||||
if (quote) {
|
if (quote > 0 && nextChar() != quote) {
|
||||||
char c = nextChar();
|
throw new ConvertException("illegal escape(" + text[position] + ") (position = " + position + ")");
|
||||||
if (c != '"' && c != '\'') {
|
|
||||||
throw new ConvertException("illegal escape(" + c + ") (position = " + position + ")");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return negative ? Integer.MIN_VALUE : Integer.MAX_VALUE;
|
return negative ? Integer.MIN_VALUE : Integer.MAX_VALUE;
|
||||||
}
|
}
|
||||||
@@ -463,12 +467,9 @@ public class JsonReader extends Reader {
|
|||||||
} else if (ch == ',' || ch == '}' || ch == ']' || ch <= ' ' || ch == ':') {
|
} else if (ch == ',' || ch == '}' || ch == ']' || ch <= ' ' || ch == ':') {
|
||||||
curr--;
|
curr--;
|
||||||
break;
|
break;
|
||||||
} else if (ch == '"' || ch == '\'') {
|
} else if (ch == quote) {
|
||||||
if (quote) {
|
|
||||||
break;
|
break;
|
||||||
}
|
} else if (quote > 0 && ch <= ' ') {
|
||||||
throw new ConvertException("illegal escape(" + ch + ") (position = " + position + ")");
|
|
||||||
} else if (quote && ch <= ' ') {
|
|
||||||
// do nothing
|
// do nothing
|
||||||
} else if (ch == '.') {
|
} else if (ch == '.') {
|
||||||
dot = true;
|
dot = true;
|
||||||
@@ -481,15 +482,12 @@ public class JsonReader extends Reader {
|
|||||||
for (; curr <= end; curr++) {
|
for (; curr <= end; curr++) {
|
||||||
ch = text0[curr];
|
ch = text0[curr];
|
||||||
if (ch >= '0' && ch <= '9') {
|
if (ch >= '0' && ch <= '9') {
|
||||||
if (dot) {
|
if (dot) { // 兼容 123.456
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
value = (hex ? (value << 4) : ((value << 3) + (value << 1))) + digits[ch];
|
value = (hex ? (value << 4) : ((value << 3) + (value << 1))) + digits[ch];
|
||||||
} else if (ch == '"' || ch == '\'') {
|
} else if (ch == quote) {
|
||||||
if (quote) {
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
throw new ConvertException("illegal escape(" + ch + ") (position = " + position + ")");
|
|
||||||
} else if (ch == 'x' || ch == 'X') {
|
} else if (ch == 'x' || ch == 'X') {
|
||||||
if (value != 0) {
|
if (value != 0) {
|
||||||
throw new ConvertException("illegal escape(" + ch + ") (position = " + position + ")");
|
throw new ConvertException("illegal escape(" + ch + ") (position = " + position + ")");
|
||||||
@@ -511,7 +509,7 @@ public class JsonReader extends Reader {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
value = (value << 4) + digits[ch];
|
value = (value << 4) + digits[ch];
|
||||||
} else if (quote && ch <= ' ') {
|
} else if (quote > 0 && ch <= ' ') { // 兼容 "123 "
|
||||||
// do nothing
|
// do nothing
|
||||||
} else if (ch == '.') {
|
} else if (ch == '.') {
|
||||||
dot = true;
|
dot = true;
|
||||||
@@ -535,9 +533,9 @@ public class JsonReader extends Reader {
|
|||||||
@Override
|
@Override
|
||||||
public long readLong() {
|
public long readLong() {
|
||||||
char firstchar = nextGoodChar(true);
|
char firstchar = nextGoodChar(true);
|
||||||
boolean quote = false;
|
char quote = 0;
|
||||||
if (firstchar == '"' || firstchar == '\'') {
|
if (firstchar == '"' || firstchar == '\'') {
|
||||||
quote = true;
|
quote = firstchar;
|
||||||
firstchar = nextGoodChar(false);
|
firstchar = nextGoodChar(false);
|
||||||
if (firstchar == '"' || firstchar == '\'') {
|
if (firstchar == '"' || firstchar == '\'') {
|
||||||
return 0L;
|
return 0L;
|
||||||
@@ -545,15 +543,25 @@ public class JsonReader extends Reader {
|
|||||||
}
|
}
|
||||||
long value = 0;
|
long value = 0;
|
||||||
final boolean negative = firstchar == '-';
|
final boolean negative = firstchar == '-';
|
||||||
if (!negative) {
|
if (negative) { // 负数
|
||||||
if (firstchar == '+') {
|
firstchar = nextChar();
|
||||||
firstchar = nextChar(); // 兼容+开头的
|
if (firstchar != 'N' && firstchar != 'I') {
|
||||||
}
|
|
||||||
if (firstchar < '0' || firstchar > '9') {
|
if (firstchar < '0' || firstchar > '9') {
|
||||||
throw new ConvertException("illegal escape(" + firstchar + ") (position = " + position + ")");
|
throw new ConvertException("illegal escape(" + firstchar + ") (position = " + position + ")");
|
||||||
}
|
}
|
||||||
value = digits[firstchar];
|
value = digits[firstchar];
|
||||||
}
|
}
|
||||||
|
} else { // 正数
|
||||||
|
if (firstchar == '+') {
|
||||||
|
firstchar = nextChar(); // 兼容+开头的
|
||||||
|
}
|
||||||
|
if (firstchar != 'N' && firstchar != 'I') {
|
||||||
|
if (firstchar < '0' || firstchar > '9') {
|
||||||
|
throw new ConvertException("illegal escape(" + firstchar + ") (position = " + position + ")");
|
||||||
|
}
|
||||||
|
value = digits[firstchar];
|
||||||
|
}
|
||||||
|
}
|
||||||
if (firstchar == 'N') {
|
if (firstchar == 'N') {
|
||||||
if (negative) {
|
if (negative) {
|
||||||
throw new ConvertException("illegal escape(" + firstchar + ") (position = " + position + ")");
|
throw new ConvertException("illegal escape(" + firstchar + ") (position = " + position + ")");
|
||||||
@@ -561,11 +569,8 @@ public class JsonReader extends Reader {
|
|||||||
if (nextChar() != 'a' || nextChar() != 'N') {
|
if (nextChar() != 'a' || nextChar() != 'N') {
|
||||||
throw new ConvertException("illegal escape(" + text[position] + ") (position = " + position + ")");
|
throw new ConvertException("illegal escape(" + text[position] + ") (position = " + position + ")");
|
||||||
}
|
}
|
||||||
if (quote) {
|
if (quote > 0 && nextChar() != quote) {
|
||||||
char c = nextChar();
|
throw new ConvertException("illegal escape(" + text[position] + ") (position = " + position + ")");
|
||||||
if (c != '"' && c != '\'') {
|
|
||||||
throw new ConvertException("illegal escape(" + c + ") (position = " + position + ")");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return 0L; // NaN 返回0;
|
return 0L; // NaN 返回0;
|
||||||
} else if (firstchar == 'I') { // Infinity
|
} else if (firstchar == 'I') { // Infinity
|
||||||
@@ -578,11 +583,8 @@ public class JsonReader extends Reader {
|
|||||||
|| nextChar() != 'y') {
|
|| nextChar() != 'y') {
|
||||||
throw new ConvertException("illegal escape(" + text[position] + ") (position = " + position + ")");
|
throw new ConvertException("illegal escape(" + text[position] + ") (position = " + position + ")");
|
||||||
}
|
}
|
||||||
if (quote) {
|
if (quote > 0 && nextChar() != quote) {
|
||||||
char c = nextChar();
|
throw new ConvertException("illegal escape(" + text[position] + ") (position = " + position + ")");
|
||||||
if (c != '"' && c != '\'') {
|
|
||||||
throw new ConvertException("illegal escape(" + c + ") (position = " + position + ")");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return negative ? Long.MIN_VALUE : Long.MAX_VALUE;
|
return negative ? Long.MIN_VALUE : Long.MAX_VALUE;
|
||||||
}
|
}
|
||||||
@@ -595,19 +597,16 @@ public class JsonReader extends Reader {
|
|||||||
for (; curr <= end; curr++) {
|
for (; curr <= end; curr++) {
|
||||||
ch = text0[curr];
|
ch = text0[curr];
|
||||||
if (ch >= '0' && ch <= '9') {
|
if (ch >= '0' && ch <= '9') {
|
||||||
if (dot) {
|
if (dot) { // 兼容 123.456
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
value = (value << 3) + (value << 1) + digits[ch];
|
value = (value << 3) + (value << 1) + digits[ch];
|
||||||
} else if (ch == ',' || ch == '}' || ch == ']' || ch <= ' ' || ch == ':') {
|
} else if (ch == ',' || ch == '}' || ch == ']' || ch <= ' ' || ch == ':') {
|
||||||
curr--;
|
curr--;
|
||||||
break;
|
break;
|
||||||
} else if (ch == '"' || ch == '\'') {
|
} else if (ch == quote) {
|
||||||
if (quote) {
|
|
||||||
break;
|
break;
|
||||||
}
|
} else if (quote > 0 && ch <= ' ') { // 兼容 "123 "
|
||||||
throw new ConvertException("illegal escape(" + ch + ") (position = " + position + ")");
|
|
||||||
} else if (quote && ch <= ' ') {
|
|
||||||
// do nothing
|
// do nothing
|
||||||
} else if (ch == '.') {
|
} else if (ch == '.') {
|
||||||
dot = true;
|
dot = true;
|
||||||
@@ -624,11 +623,8 @@ public class JsonReader extends Reader {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
value = (hex ? (value << 4) : ((value << 3) + (value << 1))) + digits[ch];
|
value = (hex ? (value << 4) : ((value << 3) + (value << 1))) + digits[ch];
|
||||||
} else if (ch == '"' || ch == '\'') {
|
} else if (ch == quote) {
|
||||||
if (quote) {
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
throw new ConvertException("illegal escape(" + ch + ") (position = " + position + ")");
|
|
||||||
} else if (ch == 'x' || ch == 'X') {
|
} else if (ch == 'x' || ch == 'X') {
|
||||||
if (value != 0) {
|
if (value != 0) {
|
||||||
throw new ConvertException("illegal escape(" + ch + ") (position = " + position + ")");
|
throw new ConvertException("illegal escape(" + ch + ") (position = " + position + ")");
|
||||||
@@ -650,7 +646,7 @@ public class JsonReader extends Reader {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
value = (value << 4) + digits[ch];
|
value = (value << 4) + digits[ch];
|
||||||
} else if (quote && ch <= ' ') {
|
} else if (quote > 0 && ch <= ' ') {
|
||||||
// do nothing
|
// do nothing
|
||||||
} else if (ch == '.') {
|
} else if (ch == '.') {
|
||||||
dot = true;
|
dot = true;
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ public class Json5Test {
|
|||||||
test.run1();
|
test.run1();
|
||||||
test.run2();
|
test.run2();
|
||||||
test.run3();
|
test.run3();
|
||||||
|
test.run4();
|
||||||
|
test.run5();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -79,6 +81,20 @@ public class Json5Test {
|
|||||||
Assertions.assertEquals(json, json2);
|
Assertions.assertEquals(json, json2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void run4() throws Exception {
|
||||||
|
int val = JsonConvert.root().convertFrom(int.class, "NaN");
|
||||||
|
Assertions.assertEquals(0, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void run5() throws Exception {
|
||||||
|
long val = JsonConvert.root().convertFrom(long.class, "Infinity");
|
||||||
|
Assertions.assertEquals(Long.MAX_VALUE, val);
|
||||||
|
val = JsonConvert.root().convertFrom(long.class, "-Infinity");
|
||||||
|
Assertions.assertEquals(Long.MIN_VALUE, val);
|
||||||
|
}
|
||||||
|
|
||||||
private static Type MAP_TYPE = new TypeToken<LinkedHashMap<String, String>>() {}.getType();
|
private static Type MAP_TYPE = new TypeToken<LinkedHashMap<String, String>>() {}.getType();
|
||||||
|
|
||||||
public static class Json5Bean {
|
public static class Json5Bean {
|
||||||
|
|||||||
@@ -28,6 +28,9 @@ public class JsonMainTest {
|
|||||||
test.run4();
|
test.run4();
|
||||||
test.run5();
|
test.run5();
|
||||||
test.run6();
|
test.run6();
|
||||||
|
test.run7();
|
||||||
|
test.run8();
|
||||||
|
test.run9();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -148,4 +151,22 @@ public class JsonMainTest {
|
|||||||
Assertions.assertEquals(
|
Assertions.assertEquals(
|
||||||
JsonArray.class.getName(), obj.get("images").getClass().getName());
|
JsonArray.class.getName(), obj.get("images").getClass().getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void run7() throws Throwable {
|
||||||
|
long val = JsonConvert.root().convertFrom(long.class, "12345");
|
||||||
|
Assertions.assertEquals(12345L, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void run8() throws Throwable {
|
||||||
|
long[] vals = JsonConvert.root().convertFrom(long[].class, "[-12345,'23456']");
|
||||||
|
Assertions.assertEquals(-12345L, vals[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void run9() throws Throwable {
|
||||||
|
long[] vals = JsonConvert.root().convertFrom(long[].class, "['12345','-23456']");
|
||||||
|
Assertions.assertEquals(-23456L, vals[1]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user