mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
8bd07803e4
commit
4702174334
@ -15,20 +15,33 @@ import java.io.StringReader;
|
|||||||
public class JSONTokener {
|
public class JSONTokener {
|
||||||
|
|
||||||
private long character;
|
private long character;
|
||||||
/** 是否结尾 End of stream */
|
/**
|
||||||
|
* 是否结尾 End of stream
|
||||||
|
*/
|
||||||
private boolean eof;
|
private boolean eof;
|
||||||
/** 在Reader的位置(解析到第几个字符) */
|
/**
|
||||||
|
* 在Reader的位置(解析到第几个字符)
|
||||||
|
*/
|
||||||
private long index;
|
private long index;
|
||||||
/** 当前所在行 */
|
/**
|
||||||
|
* 当前所在行
|
||||||
|
*/
|
||||||
private long line;
|
private long line;
|
||||||
/** 前一个字符 */
|
/**
|
||||||
|
* 前一个字符
|
||||||
|
*/
|
||||||
private char previous;
|
private char previous;
|
||||||
/** 是否使用前一个字符 */
|
/**
|
||||||
|
* 是否使用前一个字符
|
||||||
|
*/
|
||||||
private boolean usePrevious;
|
private boolean usePrevious;
|
||||||
/** 源 */
|
/**
|
||||||
|
* 源
|
||||||
|
*/
|
||||||
private Reader reader;
|
private Reader reader;
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------ Constructor start
|
// ------------------------------------------------------------------------------------ Constructor start
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从Reader中构建
|
* 从Reader中构建
|
||||||
*
|
*
|
||||||
@ -176,8 +189,8 @@ public class JSONTokener {
|
|||||||
/**
|
/**
|
||||||
* 获得下一个字符,跳过空白符
|
* 获得下一个字符,跳过空白符
|
||||||
*
|
*
|
||||||
* @throws JSONException 获得下一个字符时抛出的异常
|
|
||||||
* @return 获得的字符,0表示没有更多的字符
|
* @return 获得的字符,0表示没有更多的字符
|
||||||
|
* @throws JSONException 获得下一个字符时抛出的异常
|
||||||
*/
|
*/
|
||||||
public char nextClean() throws JSONException {
|
public char nextClean() throws JSONException {
|
||||||
char c;
|
char c;
|
||||||
@ -200,49 +213,49 @@ public class JSONTokener {
|
|||||||
public String nextString(char quote) throws JSONException {
|
public String nextString(char quote) throws JSONException {
|
||||||
char c;
|
char c;
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (;;) {
|
while (true) {
|
||||||
c = this.next();
|
c = this.next();
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 0:
|
case 0:
|
||||||
case '\n':
|
case '\n':
|
||||||
case '\r':
|
case '\r':
|
||||||
throw this.syntaxError("Unterminated string");
|
throw this.syntaxError("Unterminated string");
|
||||||
case '\\':// 转义符
|
case '\\':// 转义符
|
||||||
c = this.next();
|
c = this.next();
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'b':
|
case 'b':
|
||||||
sb.append('\b');
|
sb.append('\b');
|
||||||
break;
|
break;
|
||||||
case 't':
|
case 't':
|
||||||
sb.append('\t');
|
sb.append('\t');
|
||||||
break;
|
break;
|
||||||
case 'n':
|
case 'n':
|
||||||
sb.append('\n');
|
sb.append('\n');
|
||||||
break;
|
break;
|
||||||
case 'f':
|
case 'f':
|
||||||
sb.append('\f');
|
sb.append('\f');
|
||||||
break;
|
break;
|
||||||
case 'r':
|
case 'r':
|
||||||
sb.append('\r');
|
sb.append('\r');
|
||||||
break;
|
break;
|
||||||
case 'u':// Unicode符
|
case 'u':// Unicode符
|
||||||
sb.append((char) Integer.parseInt(this.next(4), 16));
|
sb.append((char) Integer.parseInt(this.next(4), 16));
|
||||||
break;
|
break;
|
||||||
case '"':
|
case '"':
|
||||||
case '\'':
|
case '\'':
|
||||||
case '\\':
|
case '\\':
|
||||||
case '/':
|
case '/':
|
||||||
sb.append(c);
|
sb.append(c);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw this.syntaxError("Illegal escape.");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw this.syntaxError("Illegal escape.");
|
if (c == quote) {
|
||||||
}
|
return sb.toString();
|
||||||
break;
|
}
|
||||||
default:
|
sb.append(c);
|
||||||
if (c == quote) {
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
sb.append(c);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -256,7 +269,7 @@ public class JSONTokener {
|
|||||||
*/
|
*/
|
||||||
public String nextTo(char delimiter) throws JSONException {
|
public String nextTo(char delimiter) throws JSONException {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (;;) {
|
for (; ; ) {
|
||||||
char c = this.next();
|
char c = this.next();
|
||||||
if (c == delimiter || c == 0 || c == '\n' || c == '\r') {
|
if (c == delimiter || c == 0 || c == '\n' || c == '\r') {
|
||||||
if (c != 0) {
|
if (c != 0) {
|
||||||
@ -277,7 +290,7 @@ public class JSONTokener {
|
|||||||
public String nextTo(String delimiters) throws JSONException {
|
public String nextTo(String delimiters) throws JSONException {
|
||||||
char c;
|
char c;
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (;;) {
|
for (; ; ) {
|
||||||
c = this.next();
|
c = this.next();
|
||||||
if (delimiters.indexOf(c) >= 0 || c == 0 || c == '\n' || c == '\r') {
|
if (delimiters.indexOf(c) >= 0 || c == 0 || c == '\n' || c == '\r') {
|
||||||
if (c != 0) {
|
if (c != 0) {
|
||||||
@ -292,24 +305,23 @@ public class JSONTokener {
|
|||||||
/**
|
/**
|
||||||
* 获得下一个值,值类型可以是Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the JSONObject.NULL
|
* 获得下一个值,值类型可以是Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the JSONObject.NULL
|
||||||
*
|
*
|
||||||
* @throws JSONException 语法错误
|
|
||||||
*
|
|
||||||
* @return Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the JSONObject.NULL
|
* @return Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the JSONObject.NULL
|
||||||
|
* @throws JSONException 语法错误
|
||||||
*/
|
*/
|
||||||
public Object nextValue() throws JSONException {
|
public Object nextValue() throws JSONException {
|
||||||
char c = this.nextClean();
|
char c = this.nextClean();
|
||||||
String string;
|
String string;
|
||||||
|
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '"':
|
case '"':
|
||||||
case '\'':
|
case '\'':
|
||||||
return this.nextString(c);
|
return this.nextString(c);
|
||||||
case '{':
|
case '{':
|
||||||
this.back();
|
this.back();
|
||||||
return new JSONObject(this);
|
return new JSONObject(this);
|
||||||
case '[':
|
case '[':
|
||||||
this.back();
|
this.back();
|
||||||
return new JSONArray(this);
|
return new JSONArray(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -394,16 +406,16 @@ public class JSONTokener {
|
|||||||
jsonArray.add(this.nextValue());
|
jsonArray.add(this.nextValue());
|
||||||
}
|
}
|
||||||
switch (this.nextClean()) {
|
switch (this.nextClean()) {
|
||||||
case ',':
|
case ',':
|
||||||
if (this.nextClean() == ']') {
|
if (this.nextClean() == ']') {
|
||||||
|
return jsonArray;
|
||||||
|
}
|
||||||
|
this.back();
|
||||||
|
break;
|
||||||
|
case ']':
|
||||||
return jsonArray;
|
return jsonArray;
|
||||||
}
|
default:
|
||||||
this.back();
|
throw this.syntaxError("Expected a ',' or ']'");
|
||||||
break;
|
|
||||||
case ']':
|
|
||||||
return jsonArray;
|
|
||||||
default:
|
|
||||||
throw this.syntaxError("Expected a ',' or ']'");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Console;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -16,6 +17,15 @@ import cn.hutool.json.test.bean.UserC;
|
|||||||
|
|
||||||
public class JSONUtilTest {
|
public class JSONUtilTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 出现语法错误时报错,检查解析\x字符时是否会导致死循环异常
|
||||||
|
*/
|
||||||
|
@Test(expected = JSONException.class)
|
||||||
|
public void parseTest(){
|
||||||
|
JSONArray jsonArray = JSONUtil.parseArray("[{\"a\":\"a\\x]");
|
||||||
|
Console.log(jsonArray);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toJsonStrTest() {
|
public void toJsonStrTest() {
|
||||||
UserA a1 = new UserA();
|
UserA a1 = new UserA();
|
||||||
@ -38,11 +48,11 @@ public class JSONUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toJsonStrTest2() {
|
public void toJsonStrTest2() {
|
||||||
Map<String, Object> model = new HashMap<String, Object>();
|
Map<String, Object> model = new HashMap<>();
|
||||||
model.put("mobile", "17610836523");
|
model.put("mobile", "17610836523");
|
||||||
model.put("type", 1);
|
model.put("type", 1);
|
||||||
|
|
||||||
Map<String, Object> data = new HashMap<String, Object>();
|
Map<String, Object> data = new HashMap<>();
|
||||||
data.put("model", model);
|
data.put("model", model);
|
||||||
data.put("model2", model);
|
data.put("model2", model);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user