jimu-decompiled/sources/com/google/gson/internal/bind/JsonTreeReader.java
2025-05-13 19:24:51 +02:00

350 lines
11 KiB
Java

package com.google.gson.internal.bind;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import java.io.IOException;
import java.io.Reader;
import java.util.Iterator;
import java.util.Map;
/* loaded from: classes.dex */
public final class JsonTreeReader extends JsonReader {
private int[] pathIndices;
private String[] pathNames;
private Object[] stack;
private int stackSize;
private static final Reader UNREADABLE_READER = new Reader() { // from class: com.google.gson.internal.bind.JsonTreeReader.1
@Override // java.io.Reader, java.io.Closeable, java.lang.AutoCloseable
public void close() throws IOException {
throw new AssertionError();
}
@Override // java.io.Reader
public int read(char[] cArr, int i, int i2) throws IOException {
throw new AssertionError();
}
};
private static final Object SENTINEL_CLOSED = new Object();
public JsonTreeReader(JsonElement jsonElement) {
super(UNREADABLE_READER);
this.stack = new Object[32];
this.stackSize = 0;
this.pathNames = new String[32];
this.pathIndices = new int[32];
push(jsonElement);
}
private void expect(JsonToken jsonToken) throws IOException {
if (peek() == jsonToken) {
return;
}
throw new IllegalStateException("Expected " + jsonToken + " but was " + peek() + locationString());
}
private String locationString() {
return " at path " + getPath();
}
private Object peekStack() {
return this.stack[this.stackSize - 1];
}
private Object popStack() {
Object[] objArr = this.stack;
int i = this.stackSize - 1;
this.stackSize = i;
Object obj = objArr[i];
objArr[this.stackSize] = null;
return obj;
}
private void push(Object obj) {
int i = this.stackSize;
Object[] objArr = this.stack;
if (i == objArr.length) {
Object[] objArr2 = new Object[i * 2];
int[] iArr = new int[i * 2];
String[] strArr = new String[i * 2];
System.arraycopy(objArr, 0, objArr2, 0, i);
System.arraycopy(this.pathIndices, 0, iArr, 0, this.stackSize);
System.arraycopy(this.pathNames, 0, strArr, 0, this.stackSize);
this.stack = objArr2;
this.pathIndices = iArr;
this.pathNames = strArr;
}
Object[] objArr3 = this.stack;
int i2 = this.stackSize;
this.stackSize = i2 + 1;
objArr3[i2] = obj;
}
@Override // com.google.gson.stream.JsonReader
public void beginArray() throws IOException {
expect(JsonToken.BEGIN_ARRAY);
push(((JsonArray) peekStack()).iterator());
this.pathIndices[this.stackSize - 1] = 0;
}
@Override // com.google.gson.stream.JsonReader
public void beginObject() throws IOException {
expect(JsonToken.BEGIN_OBJECT);
push(((JsonObject) peekStack()).entrySet().iterator());
}
@Override // com.google.gson.stream.JsonReader, java.io.Closeable, java.lang.AutoCloseable
public void close() throws IOException {
this.stack = new Object[]{SENTINEL_CLOSED};
this.stackSize = 1;
}
@Override // com.google.gson.stream.JsonReader
public void endArray() throws IOException {
expect(JsonToken.END_ARRAY);
popStack();
popStack();
int i = this.stackSize;
if (i > 0) {
int[] iArr = this.pathIndices;
int i2 = i - 1;
iArr[i2] = iArr[i2] + 1;
}
}
@Override // com.google.gson.stream.JsonReader
public void endObject() throws IOException {
expect(JsonToken.END_OBJECT);
popStack();
popStack();
int i = this.stackSize;
if (i > 0) {
int[] iArr = this.pathIndices;
int i2 = i - 1;
iArr[i2] = iArr[i2] + 1;
}
}
@Override // com.google.gson.stream.JsonReader
public String getPath() {
StringBuilder sb = new StringBuilder();
sb.append('$');
int i = 0;
while (i < this.stackSize) {
Object[] objArr = this.stack;
if (objArr[i] instanceof JsonArray) {
i++;
if (objArr[i] instanceof Iterator) {
sb.append('[');
sb.append(this.pathIndices[i]);
sb.append(']');
}
} else if (objArr[i] instanceof JsonObject) {
i++;
if (objArr[i] instanceof Iterator) {
sb.append('.');
String[] strArr = this.pathNames;
if (strArr[i] != null) {
sb.append(strArr[i]);
}
}
}
i++;
}
return sb.toString();
}
@Override // com.google.gson.stream.JsonReader
public boolean hasNext() throws IOException {
JsonToken peek = peek();
return (peek == JsonToken.END_OBJECT || peek == JsonToken.END_ARRAY) ? false : true;
}
@Override // com.google.gson.stream.JsonReader
public boolean nextBoolean() throws IOException {
expect(JsonToken.BOOLEAN);
boolean asBoolean = ((JsonPrimitive) popStack()).getAsBoolean();
int i = this.stackSize;
if (i > 0) {
int[] iArr = this.pathIndices;
int i2 = i - 1;
iArr[i2] = iArr[i2] + 1;
}
return asBoolean;
}
@Override // com.google.gson.stream.JsonReader
public double nextDouble() throws IOException {
JsonToken peek = peek();
if (peek != JsonToken.NUMBER && peek != JsonToken.STRING) {
throw new IllegalStateException("Expected " + JsonToken.NUMBER + " but was " + peek + locationString());
}
double asDouble = ((JsonPrimitive) peekStack()).getAsDouble();
if (!isLenient() && (Double.isNaN(asDouble) || Double.isInfinite(asDouble))) {
throw new NumberFormatException("JSON forbids NaN and infinities: " + asDouble);
}
popStack();
int i = this.stackSize;
if (i > 0) {
int[] iArr = this.pathIndices;
int i2 = i - 1;
iArr[i2] = iArr[i2] + 1;
}
return asDouble;
}
@Override // com.google.gson.stream.JsonReader
public int nextInt() throws IOException {
JsonToken peek = peek();
if (peek != JsonToken.NUMBER && peek != JsonToken.STRING) {
throw new IllegalStateException("Expected " + JsonToken.NUMBER + " but was " + peek + locationString());
}
int asInt = ((JsonPrimitive) peekStack()).getAsInt();
popStack();
int i = this.stackSize;
if (i > 0) {
int[] iArr = this.pathIndices;
int i2 = i - 1;
iArr[i2] = iArr[i2] + 1;
}
return asInt;
}
@Override // com.google.gson.stream.JsonReader
public long nextLong() throws IOException {
JsonToken peek = peek();
if (peek != JsonToken.NUMBER && peek != JsonToken.STRING) {
throw new IllegalStateException("Expected " + JsonToken.NUMBER + " but was " + peek + locationString());
}
long asLong = ((JsonPrimitive) peekStack()).getAsLong();
popStack();
int i = this.stackSize;
if (i > 0) {
int[] iArr = this.pathIndices;
int i2 = i - 1;
iArr[i2] = iArr[i2] + 1;
}
return asLong;
}
@Override // com.google.gson.stream.JsonReader
public String nextName() throws IOException {
expect(JsonToken.NAME);
Map.Entry entry = (Map.Entry) ((Iterator) peekStack()).next();
String str = (String) entry.getKey();
this.pathNames[this.stackSize - 1] = str;
push(entry.getValue());
return str;
}
@Override // com.google.gson.stream.JsonReader
public void nextNull() throws IOException {
expect(JsonToken.NULL);
popStack();
int i = this.stackSize;
if (i > 0) {
int[] iArr = this.pathIndices;
int i2 = i - 1;
iArr[i2] = iArr[i2] + 1;
}
}
@Override // com.google.gson.stream.JsonReader
public String nextString() throws IOException {
JsonToken peek = peek();
if (peek == JsonToken.STRING || peek == JsonToken.NUMBER) {
String asString = ((JsonPrimitive) popStack()).getAsString();
int i = this.stackSize;
if (i > 0) {
int[] iArr = this.pathIndices;
int i2 = i - 1;
iArr[i2] = iArr[i2] + 1;
}
return asString;
}
throw new IllegalStateException("Expected " + JsonToken.STRING + " but was " + peek + locationString());
}
@Override // com.google.gson.stream.JsonReader
public JsonToken peek() throws IOException {
if (this.stackSize == 0) {
return JsonToken.END_DOCUMENT;
}
Object peekStack = peekStack();
if (peekStack instanceof Iterator) {
boolean z = this.stack[this.stackSize - 2] instanceof JsonObject;
Iterator it = (Iterator) peekStack;
if (!it.hasNext()) {
return z ? JsonToken.END_OBJECT : JsonToken.END_ARRAY;
}
if (z) {
return JsonToken.NAME;
}
push(it.next());
return peek();
}
if (peekStack instanceof JsonObject) {
return JsonToken.BEGIN_OBJECT;
}
if (peekStack instanceof JsonArray) {
return JsonToken.BEGIN_ARRAY;
}
if (!(peekStack instanceof JsonPrimitive)) {
if (peekStack instanceof JsonNull) {
return JsonToken.NULL;
}
if (peekStack == SENTINEL_CLOSED) {
throw new IllegalStateException("JsonReader is closed");
}
throw new AssertionError();
}
JsonPrimitive jsonPrimitive = (JsonPrimitive) peekStack;
if (jsonPrimitive.isString()) {
return JsonToken.STRING;
}
if (jsonPrimitive.isBoolean()) {
return JsonToken.BOOLEAN;
}
if (jsonPrimitive.isNumber()) {
return JsonToken.NUMBER;
}
throw new AssertionError();
}
public void promoteNameToValue() throws IOException {
expect(JsonToken.NAME);
Map.Entry entry = (Map.Entry) ((Iterator) peekStack()).next();
push(entry.getValue());
push(new JsonPrimitive((String) entry.getKey()));
}
@Override // com.google.gson.stream.JsonReader
public void skipValue() throws IOException {
if (peek() == JsonToken.NAME) {
nextName();
this.pathNames[this.stackSize - 2] = "null";
} else {
popStack();
int i = this.stackSize;
if (i > 0) {
this.pathNames[i - 1] = "null";
}
}
int i2 = this.stackSize;
if (i2 > 0) {
int[] iArr = this.pathIndices;
int i3 = i2 - 1;
iArr[i3] = iArr[i3] + 1;
}
}
@Override // com.google.gson.stream.JsonReader
public String toString() {
return JsonTreeReader.class.getSimpleName();
}
}