334 lines
12 KiB
Java
334 lines
12 KiB
Java
package com.google.firebase.encoders.json;
|
|
|
|
import android.util.Base64;
|
|
import android.util.JsonWriter;
|
|
import com.google.firebase.encoders.EncodingException;
|
|
import com.google.firebase.encoders.FieldDescriptor;
|
|
import com.google.firebase.encoders.ObjectEncoder;
|
|
import com.google.firebase.encoders.ObjectEncoderContext;
|
|
import com.google.firebase.encoders.ValueEncoder;
|
|
import com.google.firebase.encoders.ValueEncoderContext;
|
|
import java.io.IOException;
|
|
import java.io.Writer;
|
|
import java.util.Collection;
|
|
import java.util.Date;
|
|
import java.util.Iterator;
|
|
import java.util.Map;
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
/* loaded from: classes3.dex */
|
|
public final class JsonValueObjectEncoderContext implements ObjectEncoderContext, ValueEncoderContext {
|
|
private final ObjectEncoder<Object> fallbackEncoder;
|
|
private final boolean ignoreNullValues;
|
|
private final JsonWriter jsonWriter;
|
|
private final Map<Class<?>, ObjectEncoder<?>> objectEncoders;
|
|
private final Map<Class<?>, ValueEncoder<?>> valueEncoders;
|
|
private JsonValueObjectEncoderContext childContext = null;
|
|
private boolean active = true;
|
|
|
|
public JsonValueObjectEncoderContext(Writer writer, Map<Class<?>, ObjectEncoder<?>> map, Map<Class<?>, ValueEncoder<?>> map2, ObjectEncoder<Object> objectEncoder, boolean z3) {
|
|
this.jsonWriter = new JsonWriter(writer);
|
|
this.objectEncoders = map;
|
|
this.valueEncoders = map2;
|
|
this.fallbackEncoder = objectEncoder;
|
|
this.ignoreNullValues = z3;
|
|
}
|
|
|
|
private boolean cannotBeInline(Object obj) {
|
|
return obj == null || obj.getClass().isArray() || (obj instanceof Collection) || (obj instanceof Date) || (obj instanceof Enum) || (obj instanceof Number);
|
|
}
|
|
|
|
private JsonValueObjectEncoderContext internalAdd(String str, Object obj) throws IOException, EncodingException {
|
|
maybeUnNest();
|
|
this.jsonWriter.name(str);
|
|
if (obj != null) {
|
|
return add(obj, false);
|
|
}
|
|
this.jsonWriter.nullValue();
|
|
return this;
|
|
}
|
|
|
|
private JsonValueObjectEncoderContext internalAddIgnoreNullValues(String str, Object obj) throws IOException, EncodingException {
|
|
if (obj == null) {
|
|
return this;
|
|
}
|
|
maybeUnNest();
|
|
this.jsonWriter.name(str);
|
|
return add(obj, false);
|
|
}
|
|
|
|
private void maybeUnNest() throws IOException {
|
|
if (!this.active) {
|
|
throw new IllegalStateException("Parent context used since this context was created. Cannot use this context anymore.");
|
|
}
|
|
JsonValueObjectEncoderContext jsonValueObjectEncoderContext = this.childContext;
|
|
if (jsonValueObjectEncoderContext != null) {
|
|
jsonValueObjectEncoderContext.maybeUnNest();
|
|
this.childContext.active = false;
|
|
this.childContext = null;
|
|
this.jsonWriter.endObject();
|
|
}
|
|
}
|
|
|
|
public void close() throws IOException {
|
|
maybeUnNest();
|
|
this.jsonWriter.flush();
|
|
}
|
|
|
|
public JsonValueObjectEncoderContext doEncode(ObjectEncoder<Object> objectEncoder, Object obj, boolean z3) throws IOException {
|
|
if (!z3) {
|
|
this.jsonWriter.beginObject();
|
|
}
|
|
objectEncoder.encode(obj, this);
|
|
if (!z3) {
|
|
this.jsonWriter.endObject();
|
|
}
|
|
return this;
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ObjectEncoderContext
|
|
public ObjectEncoderContext inline(Object obj) throws IOException {
|
|
return add(obj, true);
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ObjectEncoderContext
|
|
public ObjectEncoderContext nested(String str) throws IOException {
|
|
maybeUnNest();
|
|
this.childContext = new JsonValueObjectEncoderContext(this);
|
|
this.jsonWriter.name(str);
|
|
this.jsonWriter.beginObject();
|
|
return this.childContext;
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ObjectEncoderContext
|
|
public ObjectEncoderContext nested(FieldDescriptor fieldDescriptor) throws IOException {
|
|
return nested(fieldDescriptor.getName());
|
|
}
|
|
|
|
private JsonValueObjectEncoderContext(JsonValueObjectEncoderContext jsonValueObjectEncoderContext) {
|
|
this.jsonWriter = jsonValueObjectEncoderContext.jsonWriter;
|
|
this.objectEncoders = jsonValueObjectEncoderContext.objectEncoders;
|
|
this.valueEncoders = jsonValueObjectEncoderContext.valueEncoders;
|
|
this.fallbackEncoder = jsonValueObjectEncoderContext.fallbackEncoder;
|
|
this.ignoreNullValues = jsonValueObjectEncoderContext.ignoreNullValues;
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ObjectEncoderContext
|
|
public JsonValueObjectEncoderContext add(String str, Object obj) throws IOException {
|
|
if (this.ignoreNullValues) {
|
|
return internalAddIgnoreNullValues(str, obj);
|
|
}
|
|
return internalAdd(str, obj);
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ObjectEncoderContext
|
|
public JsonValueObjectEncoderContext add(String str, double d4) throws IOException {
|
|
maybeUnNest();
|
|
this.jsonWriter.name(str);
|
|
return add(d4);
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ObjectEncoderContext
|
|
public JsonValueObjectEncoderContext add(String str, int i) throws IOException {
|
|
maybeUnNest();
|
|
this.jsonWriter.name(str);
|
|
return add(i);
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ObjectEncoderContext
|
|
public JsonValueObjectEncoderContext add(String str, long j4) throws IOException {
|
|
maybeUnNest();
|
|
this.jsonWriter.name(str);
|
|
return add(j4);
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ObjectEncoderContext
|
|
public JsonValueObjectEncoderContext add(String str, boolean z3) throws IOException {
|
|
maybeUnNest();
|
|
this.jsonWriter.name(str);
|
|
return add(z3);
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ObjectEncoderContext
|
|
public ObjectEncoderContext add(FieldDescriptor fieldDescriptor, Object obj) throws IOException {
|
|
return add(fieldDescriptor.getName(), obj);
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ObjectEncoderContext
|
|
public ObjectEncoderContext add(FieldDescriptor fieldDescriptor, float f2) throws IOException {
|
|
return add(fieldDescriptor.getName(), f2);
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ObjectEncoderContext
|
|
public ObjectEncoderContext add(FieldDescriptor fieldDescriptor, double d4) throws IOException {
|
|
return add(fieldDescriptor.getName(), d4);
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ObjectEncoderContext
|
|
public ObjectEncoderContext add(FieldDescriptor fieldDescriptor, int i) throws IOException {
|
|
return add(fieldDescriptor.getName(), i);
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ObjectEncoderContext
|
|
public ObjectEncoderContext add(FieldDescriptor fieldDescriptor, long j4) throws IOException {
|
|
return add(fieldDescriptor.getName(), j4);
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ObjectEncoderContext
|
|
public ObjectEncoderContext add(FieldDescriptor fieldDescriptor, boolean z3) throws IOException {
|
|
return add(fieldDescriptor.getName(), z3);
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ValueEncoderContext
|
|
public JsonValueObjectEncoderContext add(String str) throws IOException {
|
|
maybeUnNest();
|
|
this.jsonWriter.value(str);
|
|
return this;
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ValueEncoderContext
|
|
public JsonValueObjectEncoderContext add(float f2) throws IOException {
|
|
maybeUnNest();
|
|
this.jsonWriter.value(f2);
|
|
return this;
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ValueEncoderContext
|
|
public JsonValueObjectEncoderContext add(double d4) throws IOException {
|
|
maybeUnNest();
|
|
this.jsonWriter.value(d4);
|
|
return this;
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ValueEncoderContext
|
|
public JsonValueObjectEncoderContext add(int i) throws IOException {
|
|
maybeUnNest();
|
|
this.jsonWriter.value(i);
|
|
return this;
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ValueEncoderContext
|
|
public JsonValueObjectEncoderContext add(long j4) throws IOException {
|
|
maybeUnNest();
|
|
this.jsonWriter.value(j4);
|
|
return this;
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ValueEncoderContext
|
|
public JsonValueObjectEncoderContext add(boolean z3) throws IOException {
|
|
maybeUnNest();
|
|
this.jsonWriter.value(z3);
|
|
return this;
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ValueEncoderContext
|
|
public JsonValueObjectEncoderContext add(byte[] bArr) throws IOException {
|
|
maybeUnNest();
|
|
if (bArr == null) {
|
|
this.jsonWriter.nullValue();
|
|
return this;
|
|
}
|
|
this.jsonWriter.value(Base64.encodeToString(bArr, 2));
|
|
return this;
|
|
}
|
|
|
|
public JsonValueObjectEncoderContext add(Object obj, boolean z3) throws IOException {
|
|
if (z3 && cannotBeInline(obj)) {
|
|
throw new EncodingException((obj == null ? null : obj.getClass()) + " cannot be encoded inline");
|
|
}
|
|
if (obj == null) {
|
|
this.jsonWriter.nullValue();
|
|
return this;
|
|
}
|
|
if (obj instanceof Number) {
|
|
this.jsonWriter.value((Number) obj);
|
|
return this;
|
|
}
|
|
int i = 0;
|
|
if (obj.getClass().isArray()) {
|
|
if (obj instanceof byte[]) {
|
|
return add((byte[]) obj);
|
|
}
|
|
this.jsonWriter.beginArray();
|
|
if (obj instanceof int[]) {
|
|
int length = ((int[]) obj).length;
|
|
while (i < length) {
|
|
this.jsonWriter.value(r6[i]);
|
|
i++;
|
|
}
|
|
} else if (obj instanceof long[]) {
|
|
long[] jArr = (long[]) obj;
|
|
int length2 = jArr.length;
|
|
while (i < length2) {
|
|
add(jArr[i]);
|
|
i++;
|
|
}
|
|
} else if (obj instanceof double[]) {
|
|
double[] dArr = (double[]) obj;
|
|
int length3 = dArr.length;
|
|
while (i < length3) {
|
|
this.jsonWriter.value(dArr[i]);
|
|
i++;
|
|
}
|
|
} else if (obj instanceof boolean[]) {
|
|
boolean[] zArr = (boolean[]) obj;
|
|
int length4 = zArr.length;
|
|
while (i < length4) {
|
|
this.jsonWriter.value(zArr[i]);
|
|
i++;
|
|
}
|
|
} else if (obj instanceof Number[]) {
|
|
for (Number number : (Number[]) obj) {
|
|
add((Object) number, false);
|
|
}
|
|
} else {
|
|
for (Object obj2 : (Object[]) obj) {
|
|
add(obj2, false);
|
|
}
|
|
}
|
|
this.jsonWriter.endArray();
|
|
return this;
|
|
}
|
|
if (obj instanceof Collection) {
|
|
this.jsonWriter.beginArray();
|
|
Iterator it = ((Collection) obj).iterator();
|
|
while (it.hasNext()) {
|
|
add(it.next(), false);
|
|
}
|
|
this.jsonWriter.endArray();
|
|
return this;
|
|
}
|
|
if (obj instanceof Map) {
|
|
this.jsonWriter.beginObject();
|
|
for (Map.Entry entry : ((Map) obj).entrySet()) {
|
|
Object key = entry.getKey();
|
|
try {
|
|
add((String) key, entry.getValue());
|
|
} catch (ClassCastException e4) {
|
|
throw new EncodingException(String.format("Only String keys are currently supported in maps, got %s of type %s instead.", key, key.getClass()), e4);
|
|
}
|
|
}
|
|
this.jsonWriter.endObject();
|
|
return this;
|
|
}
|
|
ObjectEncoder<?> objectEncoder = this.objectEncoders.get(obj.getClass());
|
|
if (objectEncoder != null) {
|
|
return doEncode(objectEncoder, obj, z3);
|
|
}
|
|
ValueEncoder<?> valueEncoder = this.valueEncoders.get(obj.getClass());
|
|
if (valueEncoder != null) {
|
|
valueEncoder.encode(obj, this);
|
|
return this;
|
|
}
|
|
if (obj instanceof Enum) {
|
|
if (obj instanceof NumberedEnum) {
|
|
add(((NumberedEnum) obj).getNumber());
|
|
return this;
|
|
}
|
|
add(((Enum) obj).name());
|
|
return this;
|
|
}
|
|
return doEncode(this.fallbackEncoder, obj, z3);
|
|
}
|
|
}
|