json
This commit is contained in:
@@ -104,6 +104,49 @@ public class JsonByteBufferReader extends JsonReader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 跳过空白字符、单行或多行注释, 返回一个非空白字符
|
||||||
|
*
|
||||||
|
* @param allowComment 是否容许含注释
|
||||||
|
* @return 有效字符
|
||||||
|
*/
|
||||||
|
protected char nextGoodChar(boolean allowComment) {
|
||||||
|
char c;
|
||||||
|
for (; ; ) {
|
||||||
|
c = nextChar();
|
||||||
|
if (c == 0) {
|
||||||
|
return c; // 0 表示buffer结尾了
|
||||||
|
}
|
||||||
|
if (c > ' ') {
|
||||||
|
if (allowComment && c == '/') { // 支持单行和多行注释
|
||||||
|
char n = nextChar();
|
||||||
|
if (n == '/') {
|
||||||
|
for (; ; ) {
|
||||||
|
if (nextChar() == '\n') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nextGoodChar(allowComment);
|
||||||
|
} else if (n == '*') {
|
||||||
|
char nc;
|
||||||
|
char lc = 0;
|
||||||
|
for (; ; ) {
|
||||||
|
nc = nextChar();
|
||||||
|
if (nc == '/' && lc == '*') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
lc = nc;
|
||||||
|
}
|
||||||
|
return nextGoodChar(allowComment);
|
||||||
|
} else {
|
||||||
|
throw new ConvertException("illegal escape(" + n + ") (position = " + this.position + ")");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 回退最后读取的字符
|
* 回退最后读取的字符
|
||||||
*
|
*
|
||||||
@@ -166,16 +209,7 @@ public class JsonByteBufferReader extends JsonReader {
|
|||||||
return SIGN_NULL;
|
return SIGN_NULL;
|
||||||
}
|
}
|
||||||
int pos = this.position;
|
int pos = this.position;
|
||||||
StringBuilder sb = new StringBuilder();
|
throw new ConvertException("a json array must begin with '[' (position = " + pos + ") but '" + ch + "'");
|
||||||
sb.append(ch);
|
|
||||||
char one;
|
|
||||||
try {
|
|
||||||
while ((one = nextChar()) != 0) sb.append(one);
|
|
||||||
} catch (Exception e) {
|
|
||||||
// do nothing
|
|
||||||
}
|
|
||||||
throw new ConvertException(
|
|
||||||
"a json array text must begin with '[' (position = " + pos + ") but '" + ch + "' in (" + sb + ")");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 判断下一个非空白字符是否: */
|
/** 判断下一个非空白字符是否: */
|
||||||
@@ -185,16 +219,7 @@ public class JsonByteBufferReader extends JsonReader {
|
|||||||
if (ch == ':') {
|
if (ch == ':') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int pos = this.position;
|
throw new ConvertException("expected a ':' but '" + ch + "'(position = " + 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("expected a ':' but '" + ch + "'(position = " + pos + ") in (" + sb + ")");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -477,16 +502,6 @@ public class JsonByteBufferReader extends JsonReader {
|
|||||||
return readString(true);
|
return readString(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取字符串, 必须是"或者'包围的字符串值
|
|
||||||
*
|
|
||||||
* @return String值
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public final String readString() {
|
|
||||||
return readString(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String readString(boolean flag) {
|
protected String readString(boolean flag) {
|
||||||
char ch = nextGoodChar(true);
|
char ch = nextGoodChar(true);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ package org.redkale.convert.json;
|
|||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Objects;
|
||||||
import org.redkale.convert.*;
|
import org.redkale.convert.*;
|
||||||
import static org.redkale.convert.Reader.*;
|
import static org.redkale.convert.Reader.*;
|
||||||
import org.redkale.convert.Reader.ValueType;
|
import org.redkale.convert.Reader.ValueType;
|
||||||
@@ -32,15 +33,19 @@ public class JsonReader extends Reader {
|
|||||||
public JsonReader() {}
|
public JsonReader() {}
|
||||||
|
|
||||||
public JsonReader(String json) {
|
public JsonReader(String json) {
|
||||||
setText(Utility.charArray(json));
|
char[] chs = Utility.charArray(json);
|
||||||
|
this.text = chs;
|
||||||
|
this.limit = chs.length - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsonReader(char[] text) {
|
public JsonReader(char[] text) {
|
||||||
setText(text, 0, text.length);
|
this(text, 0, text.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsonReader(char[] text, int start, int len) {
|
public JsonReader(char[] text, int start, int len) {
|
||||||
setText(text, start, len);
|
this.text = Objects.requireNonNull(text);
|
||||||
|
this.position = start - 1;
|
||||||
|
this.limit = start + len - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final JsonReader setText(String text) {
|
public final JsonReader setText(String text) {
|
||||||
@@ -79,7 +84,7 @@ public class JsonReader extends Reader {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected CharArray array() {
|
protected final CharArray array() {
|
||||||
if (array == null) {
|
if (array == null) {
|
||||||
array = new CharArray();
|
array = new CharArray();
|
||||||
}
|
}
|
||||||
@@ -221,7 +226,7 @@ public class JsonReader extends Reader {
|
|||||||
*
|
*
|
||||||
* @return 是否对象字符
|
* @return 是否对象字符
|
||||||
*/
|
*/
|
||||||
public boolean isNextObject() {
|
public final boolean isNextObject() {
|
||||||
char ch = nextGoodChar(true);
|
char ch = nextGoodChar(true);
|
||||||
backChar(ch);
|
backChar(ch);
|
||||||
return ch == '{';
|
return ch == '{';
|
||||||
@@ -232,7 +237,7 @@ public class JsonReader extends Reader {
|
|||||||
*
|
*
|
||||||
* @return 是否数组字符
|
* @return 是否数组字符
|
||||||
*/
|
*/
|
||||||
public boolean isNextArray() {
|
public final boolean isNextArray() {
|
||||||
char ch = nextGoodChar(true);
|
char ch = nextGoodChar(true);
|
||||||
backChar(ch);
|
backChar(ch);
|
||||||
return ch == '[';
|
return ch == '[';
|
||||||
@@ -280,7 +285,9 @@ public class JsonReader extends Reader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void readObjectE(final Class clazz) {}
|
public final void readObjectE(final Class clazz) {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断下一个非空白字符是否为{
|
* 判断下一个非空白字符是否为{
|
||||||
@@ -297,7 +304,9 @@ public class JsonReader extends Reader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void readMapE() {}
|
public final void readMapE() {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断下一个非空白字符是否为[
|
* 判断下一个非空白字符是否为[
|
||||||
@@ -330,7 +339,9 @@ public class JsonReader extends Reader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void readArrayE() {}
|
public final void readArrayE() {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
|
||||||
/** 判断下一个非空白字符是否: */
|
/** 判断下一个非空白字符是否: */
|
||||||
@Override
|
@Override
|
||||||
@@ -361,7 +372,7 @@ public class JsonReader extends Reader {
|
|||||||
* @return 是否存在
|
* @return 是否存在
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean hasNext(int startPosition, int contentLength) {
|
public final boolean hasNext(int startPosition, int contentLength) {
|
||||||
char ch = nextGoodChar(true);
|
char ch = nextGoodChar(true);
|
||||||
if (ch == ',') {
|
if (ch == ',') {
|
||||||
char nt = nextGoodChar(true);
|
char nt = nextGoodChar(true);
|
||||||
@@ -854,7 +865,7 @@ public class JsonReader extends Reader {
|
|||||||
* @return String值
|
* @return String值
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String readString() {
|
public final String readString() {
|
||||||
return readString(true);
|
return readString(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user