127 lines
4.7 KiB
Java
127 lines
4.7 KiB
Java
package io.grpc.internal;
|
|
|
|
import com.google.common.base.Preconditions;
|
|
import com.google.gson.stream.JsonReader;
|
|
import com.google.gson.stream.JsonToken;
|
|
import java.io.IOException;
|
|
import java.io.StringReader;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
/* loaded from: classes6.dex */
|
|
public final class JsonParser {
|
|
private static final Logger logger = Logger.getLogger(JsonParser.class.getName());
|
|
|
|
private JsonParser() {
|
|
}
|
|
|
|
public static Object parse(String str) throws IOException {
|
|
JsonReader jsonReader = new JsonReader(new StringReader(str));
|
|
try {
|
|
return parseRecursive(jsonReader);
|
|
} finally {
|
|
try {
|
|
jsonReader.close();
|
|
} catch (IOException e) {
|
|
logger.log(Level.WARNING, "Failed to close", (Throwable) e);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
/* renamed from: io.grpc.internal.JsonParser$1, reason: invalid class name */
|
|
/* loaded from: classes6.dex */
|
|
public static /* synthetic */ class AnonymousClass1 {
|
|
static final int[] $SwitchMap$com$google$gson$stream$JsonToken;
|
|
|
|
static {
|
|
int[] iArr = new int[JsonToken.values().length];
|
|
$SwitchMap$com$google$gson$stream$JsonToken = iArr;
|
|
try {
|
|
iArr[JsonToken.BEGIN_ARRAY.ordinal()] = 1;
|
|
} catch (NoSuchFieldError unused) {
|
|
}
|
|
try {
|
|
$SwitchMap$com$google$gson$stream$JsonToken[JsonToken.BEGIN_OBJECT.ordinal()] = 2;
|
|
} catch (NoSuchFieldError unused2) {
|
|
}
|
|
try {
|
|
$SwitchMap$com$google$gson$stream$JsonToken[JsonToken.STRING.ordinal()] = 3;
|
|
} catch (NoSuchFieldError unused3) {
|
|
}
|
|
try {
|
|
$SwitchMap$com$google$gson$stream$JsonToken[JsonToken.NUMBER.ordinal()] = 4;
|
|
} catch (NoSuchFieldError unused4) {
|
|
}
|
|
try {
|
|
$SwitchMap$com$google$gson$stream$JsonToken[JsonToken.BOOLEAN.ordinal()] = 5;
|
|
} catch (NoSuchFieldError unused5) {
|
|
}
|
|
try {
|
|
$SwitchMap$com$google$gson$stream$JsonToken[JsonToken.NULL.ordinal()] = 6;
|
|
} catch (NoSuchFieldError unused6) {
|
|
}
|
|
}
|
|
}
|
|
|
|
private static Object parseRecursive(JsonReader jsonReader) throws IOException {
|
|
Preconditions.checkState(jsonReader.hasNext(), "unexpected end of JSON");
|
|
switch (AnonymousClass1.$SwitchMap$com$google$gson$stream$JsonToken[jsonReader.peek().ordinal()]) {
|
|
case 1:
|
|
return parseJsonArray(jsonReader);
|
|
case 2:
|
|
return parseJsonObject(jsonReader);
|
|
case 3:
|
|
return jsonReader.nextString();
|
|
case 4:
|
|
return Double.valueOf(jsonReader.nextDouble());
|
|
case 5:
|
|
return Boolean.valueOf(jsonReader.nextBoolean());
|
|
case 6:
|
|
return parseJsonNull(jsonReader);
|
|
default:
|
|
StringBuilder sb = new StringBuilder("Bad token: ");
|
|
sb.append(jsonReader.getPath());
|
|
throw new IllegalStateException(sb.toString());
|
|
}
|
|
}
|
|
|
|
private static Map<String, ?> parseJsonObject(JsonReader jsonReader) throws IOException {
|
|
jsonReader.beginObject();
|
|
LinkedHashMap linkedHashMap = new LinkedHashMap();
|
|
while (jsonReader.hasNext()) {
|
|
linkedHashMap.put(jsonReader.nextName(), parseRecursive(jsonReader));
|
|
}
|
|
boolean z = jsonReader.peek() == JsonToken.END_OBJECT;
|
|
StringBuilder sb = new StringBuilder("Bad token: ");
|
|
sb.append(jsonReader.getPath());
|
|
Preconditions.checkState(z, sb.toString());
|
|
jsonReader.endObject();
|
|
return Collections.unmodifiableMap(linkedHashMap);
|
|
}
|
|
|
|
private static List<?> parseJsonArray(JsonReader jsonReader) throws IOException {
|
|
jsonReader.beginArray();
|
|
ArrayList arrayList = new ArrayList();
|
|
while (jsonReader.hasNext()) {
|
|
arrayList.add(parseRecursive(jsonReader));
|
|
}
|
|
boolean z = jsonReader.peek() == JsonToken.END_ARRAY;
|
|
StringBuilder sb = new StringBuilder("Bad token: ");
|
|
sb.append(jsonReader.getPath());
|
|
Preconditions.checkState(z, sb.toString());
|
|
jsonReader.endArray();
|
|
return Collections.unmodifiableList(arrayList);
|
|
}
|
|
|
|
private static Void parseJsonNull(JsonReader jsonReader) throws IOException {
|
|
jsonReader.nextNull();
|
|
return null;
|
|
}
|
|
}
|