Rabbit-R1/switch port/java/sources/com/alibaba/fastjson/JSONReader.java

298 lines
7.6 KiB
Java
Raw Normal View History

2024-05-21 16:08:36 -05:00
package com.alibaba.fastjson;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.JSONLexer;
import com.alibaba.fastjson.util.TypeUtils;
import java.io.Closeable;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Type;
import java.util.Map;
/* loaded from: classes2.dex */
public class JSONReader implements Closeable {
private JSONStreamContext context;
private final DefaultJSONParser parser;
private Reader reader;
public JSONReader(Reader reader) {
this(new JSONLexer(readAll(reader)));
this.reader = reader;
}
static String readAll(Reader reader) {
StringBuilder sb = new StringBuilder();
try {
char[] cArr = new char[2048];
while (true) {
int read = reader.read(cArr, 0, 2048);
if (read >= 0) {
sb.append(cArr, 0, read);
} else {
return sb.toString();
}
}
} catch (Exception e) {
throw new JSONException("read string from reader error", e);
}
}
public JSONReader(JSONLexer jSONLexer) {
this(new DefaultJSONParser(jSONLexer));
}
public JSONReader(DefaultJSONParser defaultJSONParser) {
this.parser = defaultJSONParser;
}
public void config(Feature feature, boolean z) {
this.parser.config(feature, z);
}
public void startObject() {
if (this.context == null) {
this.context = new JSONStreamContext(null, 1001);
} else {
startStructure();
this.context = new JSONStreamContext(this.context, 1001);
}
this.parser.accept(12);
}
public void endObject() {
this.parser.accept(13);
endStructure();
}
public void startArray() {
if (this.context == null) {
this.context = new JSONStreamContext(null, 1004);
} else {
startStructure();
this.context = new JSONStreamContext(this.context, 1004);
}
this.parser.accept(14);
}
public void endArray() {
this.parser.accept(15);
endStructure();
}
private void startStructure() {
switch (this.context.state) {
case 1001:
case 1004:
return;
case 1002:
this.parser.accept(17);
return;
case 1003:
case 1005:
this.parser.accept(16);
return;
default:
throw new JSONException("illegal state : " + this.context.state);
}
}
private void endStructure() {
int i;
JSONStreamContext jSONStreamContext = this.context.parent;
this.context = jSONStreamContext;
if (jSONStreamContext == null) {
return;
}
switch (jSONStreamContext.state) {
case 1001:
case 1003:
i = 1002;
break;
case 1002:
i = 1003;
break;
case 1004:
i = 1005;
break;
default:
i = -1;
break;
}
if (i != -1) {
this.context.state = i;
}
}
public boolean hasNext() {
if (this.context == null) {
throw new JSONException("context is null");
}
int i = this.parser.lexer.token();
int i2 = this.context.state;
switch (i2) {
case 1001:
case 1003:
return i != 13;
case 1002:
default:
throw new JSONException("illegal state : " + i2);
case 1004:
case 1005:
return i != 15;
}
}
public int peek() {
return this.parser.lexer.token();
}
@Override // java.io.Closeable, java.lang.AutoCloseable
public void close() {
this.parser.lexer.close();
Reader reader = this.reader;
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
throw new JSONException("closed reader error", e);
}
}
}
public Integer readInteger() {
Object obj;
if (this.context == null) {
obj = this.parser.parse();
} else {
readBefore();
Object parse = this.parser.parse();
readAfter();
obj = parse;
}
return TypeUtils.castToInt(obj);
}
public Long readLong() {
Object obj;
if (this.context == null) {
obj = this.parser.parse();
} else {
readBefore();
Object parse = this.parser.parse();
readAfter();
obj = parse;
}
return TypeUtils.castToLong(obj);
}
public String readString() {
Object obj;
if (this.context == null) {
obj = this.parser.parse();
} else {
readBefore();
Object parse = this.parser.parse();
readAfter();
obj = parse;
}
return TypeUtils.castToString(obj);
}
public <T> T readObject(TypeReference<T> typeReference) {
return (T) readObject(typeReference.type);
}
public <T> T readObject(Type type) {
if (this.context == null) {
return (T) this.parser.parseObject(type);
}
readBefore();
T t = (T) this.parser.parseObject(type);
readAfter();
return t;
}
public <T> T readObject(Class<T> cls) {
if (this.context == null) {
return (T) this.parser.parseObject((Class) cls);
}
readBefore();
T t = (T) this.parser.parseObject((Class) cls);
readAfter();
return t;
}
public void readObject(Object obj) {
if (this.context == null) {
this.parser.parseObject(obj);
return;
}
readBefore();
this.parser.parseObject(obj);
readAfter();
}
public Object readObject() {
if (this.context == null) {
return this.parser.parse();
}
readBefore();
Object parse = this.parser.parse();
readAfter();
return parse;
}
public Object readObject(Map map) {
if (this.context == null) {
return this.parser.parseObject(map);
}
readBefore();
Object parseObject = this.parser.parseObject(map);
readAfter();
return parseObject;
}
private void readBefore() {
int i = this.context.state;
switch (i) {
case 1001:
case 1004:
return;
case 1002:
this.parser.accept(17);
return;
case 1003:
case 1005:
this.parser.accept(16);
return;
default:
throw new JSONException("illegal state : " + i);
}
}
private void readAfter() {
int i = this.context.state;
int i2 = 1002;
switch (i) {
case 1001:
case 1003:
break;
case 1002:
i2 = 1003;
break;
case 1004:
i2 = 1005;
break;
case 1005:
i2 = -1;
break;
default:
throw new JSONException("illegal state : " + i);
}
if (i2 != -1) {
this.context.state = i2;
}
}
}