eleme.openapi.sdk.utils.json.JSONReader Maven / Gradle / Ivy
package eleme.openapi.sdk.utils.json;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 不能直接使用JSONReader,请用JSONValidatingReader,所以这里改为abstract修饰。
*/
public abstract class JSONReader {
private static final Object OBJECT_END = new Object();
private static final Object ARRAY_END = new Object();
private static final Object COLON = new Object();
private static final Object COMMA = new Object();
public static final int FIRST = 0;
public static final int CURRENT = 1;
public static final int NEXT = 2;
private static Map escapes = new HashMap();
static {
escapes.put(Character.valueOf('"'), Character.valueOf('"'));
escapes.put(Character.valueOf('\\'), Character.valueOf('\\'));
escapes.put(Character.valueOf('/'), Character.valueOf('/'));
escapes.put(Character.valueOf('b'), Character.valueOf('\b'));
escapes.put(Character.valueOf('f'), Character.valueOf('\f'));
escapes.put(Character.valueOf('n'), Character.valueOf('\n'));
escapes.put(Character.valueOf('r'), Character.valueOf('\r'));
escapes.put(Character.valueOf('t'), Character.valueOf('\t'));
}
private CharacterIterator it;
private char c;
private Object token;
private StringBuffer buf = new StringBuffer();
private char next() {
c = it.next();
return c;
}
private void skipWhiteSpace() {
while (Character.isWhitespace(c)) {
next();
}
}
public Object read(CharacterIterator ci, int start) {
it = ci;
switch (start) {
case FIRST:
c = it.first();
break;
case CURRENT:
c = it.current();
break;
case NEXT:
c = it.next();
break;
}
return read();
}
public Object read(CharacterIterator it) {
return read(it, NEXT);
}
public Object read(String string) {
return read(new StringCharacterIterator(string), FIRST);
}
private Object read() {
skipWhiteSpace();
char ch = c;
next();
switch (ch) {
case '"':
token = string();
break;
case '[':
token = array();
break;
case ']':
token = ARRAY_END;
break;
case ',':
token = COMMA;
break;
case '{':
token = object();
break;
case '}':
token = OBJECT_END;
break;
case ':':
token = COLON;
break;
case 't':
next();
next();
next(); // assumed r-u-e
token = Boolean.TRUE;
break;
case 'f':
next();
next();
next();
next(); // assumed a-l-s-e
token = Boolean.FALSE;
break;
case 'n':
next();
next();
next(); // assumed u-l-l
token = null;
break;
default:
c = it.previous();
if (Character.isDigit(c) || c == '-') {
token = number();
}
}
// System.out.println("token: " + token); // enable this line to see the token stream
return token;
}
private Object object() {
Map
© 2015 - 2024 Weber Informatics LLC | Privacy Policy