// 
// Decompiled by Procyon v0.6.0
// 

package io.sentry;

import java.util.TimeZone;
import java.util.Date;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import io.sentry.vendor.gson.stream.JsonToken;
import java.io.Reader;
import org.jetbrains.annotations.NotNull;
import io.sentry.vendor.gson.stream.JsonReader;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public final class JsonObjectReader implements ObjectReader
{
    @NotNull
    private final JsonReader jsonReader;
    
    public JsonObjectReader(final Reader in) {
        this.jsonReader = new JsonReader(in);
    }
    
    @Nullable
    @Override
    public String nextStringOrNull() throws IOException {
        if (this.jsonReader.peek() == JsonToken.NULL) {
            this.jsonReader.nextNull();
            return null;
        }
        return this.jsonReader.nextString();
    }
    
    @Nullable
    @Override
    public Double nextDoubleOrNull() throws IOException {
        if (this.jsonReader.peek() == JsonToken.NULL) {
            this.jsonReader.nextNull();
            return null;
        }
        return this.jsonReader.nextDouble();
    }
    
    @Nullable
    @Override
    public Float nextFloatOrNull() throws IOException {
        if (this.jsonReader.peek() == JsonToken.NULL) {
            this.jsonReader.nextNull();
            return null;
        }
        return this.nextFloat();
    }
    
    @Override
    public float nextFloat() throws IOException {
        return (float)this.jsonReader.nextDouble();
    }
    
    @Nullable
    @Override
    public Long nextLongOrNull() throws IOException {
        if (this.jsonReader.peek() == JsonToken.NULL) {
            this.jsonReader.nextNull();
            return null;
        }
        return this.jsonReader.nextLong();
    }
    
    @Nullable
    @Override
    public Integer nextIntegerOrNull() throws IOException {
        if (this.jsonReader.peek() == JsonToken.NULL) {
            this.jsonReader.nextNull();
            return null;
        }
        return this.jsonReader.nextInt();
    }
    
    @Nullable
    @Override
    public Boolean nextBooleanOrNull() throws IOException {
        if (this.jsonReader.peek() == JsonToken.NULL) {
            this.jsonReader.nextNull();
            return null;
        }
        return this.jsonReader.nextBoolean();
    }
    
    @Override
    public void nextUnknown(final ILogger logger, final Map<String, Object> unknown, final String name) {
        try {
            unknown.put(name, this.nextObjectOrNull());
        }
        catch (final Exception exception) {
            logger.log(SentryLevel.ERROR, exception, "Error deserializing unknown key: %s", name);
        }
    }
    
    @Nullable
    @Override
    public <T> List<T> nextListOrNull(@NotNull final ILogger logger, @NotNull final JsonDeserializer<T> deserializer) throws IOException {
        if (this.jsonReader.peek() == JsonToken.NULL) {
            this.jsonReader.nextNull();
            return null;
        }
        this.jsonReader.beginArray();
        final List<T> list = new ArrayList<T>();
        if (this.jsonReader.hasNext()) {
            do {
                try {
                    list.add(deserializer.deserialize(this, logger));
                }
                catch (final Exception e) {
                    logger.log(SentryLevel.WARNING, "Failed to deserialize object in list.", e);
                }
            } while (this.jsonReader.peek() == JsonToken.BEGIN_OBJECT);
        }
        this.jsonReader.endArray();
        return list;
    }
    
    @Nullable
    @Override
    public <T> Map<String, T> nextMapOrNull(@NotNull final ILogger logger, @NotNull final JsonDeserializer<T> deserializer) throws IOException {
        if (this.jsonReader.peek() == JsonToken.NULL) {
            this.jsonReader.nextNull();
            return null;
        }
        this.jsonReader.beginObject();
        final Map<String, T> map = new HashMap<String, T>();
        if (this.jsonReader.hasNext()) {
            do {
                try {
                    final String key = this.jsonReader.nextName();
                    map.put(key, deserializer.deserialize(this, logger));
                }
                catch (final Exception e) {
                    logger.log(SentryLevel.WARNING, "Failed to deserialize object in map.", e);
                }
            } while (this.jsonReader.peek() == JsonToken.BEGIN_OBJECT || this.jsonReader.peek() == JsonToken.NAME);
        }
        this.jsonReader.endObject();
        return map;
    }
    
    @Nullable
    @Override
    public <T> Map<String, List<T>> nextMapOfListOrNull(@NotNull final ILogger logger, @NotNull final JsonDeserializer<T> deserializer) throws IOException {
        if (this.peek() == JsonToken.NULL) {
            this.nextNull();
            return null;
        }
        final Map<String, List<T>> result = new HashMap<String, List<T>>();
        this.beginObject();
        if (this.hasNext()) {
            do {
                final String key = this.nextName();
                final List<T> list = this.nextListOrNull(logger, deserializer);
                if (list != null) {
                    result.put(key, list);
                }
            } while (this.peek() == JsonToken.BEGIN_OBJECT || this.peek() == JsonToken.NAME);
        }
        this.endObject();
        return result;
    }
    
    @Nullable
    @Override
    public <T> T nextOrNull(@NotNull final ILogger logger, @NotNull final JsonDeserializer<T> deserializer) throws Exception {
        if (this.jsonReader.peek() == JsonToken.NULL) {
            this.jsonReader.nextNull();
            return null;
        }
        return deserializer.deserialize(this, logger);
    }
    
    @Nullable
    @Override
    public Date nextDateOrNull(final ILogger logger) throws IOException {
        if (this.jsonReader.peek() == JsonToken.NULL) {
            this.jsonReader.nextNull();
            return null;
        }
        return ObjectReader.dateOrNull(this.jsonReader.nextString(), logger);
    }
    
    @Nullable
    @Override
    public TimeZone nextTimeZoneOrNull(final ILogger logger) throws IOException {
        if (this.jsonReader.peek() == JsonToken.NULL) {
            this.jsonReader.nextNull();
            return null;
        }
        try {
            return TimeZone.getTimeZone(this.jsonReader.nextString());
        }
        catch (final Exception e) {
            logger.log(SentryLevel.ERROR, "Error when deserializing TimeZone", e);
            return null;
        }
    }
    
    @Nullable
    @Override
    public Object nextObjectOrNull() throws IOException {
        return new JsonObjectDeserializer().deserialize(this);
    }
    
    @NotNull
    @Override
    public JsonToken peek() throws IOException {
        return this.jsonReader.peek();
    }
    
    @NotNull
    @Override
    public String nextName() throws IOException {
        return this.jsonReader.nextName();
    }
    
    @Override
    public void beginObject() throws IOException {
        this.jsonReader.beginObject();
    }
    
    @Override
    public void endObject() throws IOException {
        this.jsonReader.endObject();
    }
    
    @Override
    public void beginArray() throws IOException {
        this.jsonReader.beginArray();
    }
    
    @Override
    public void endArray() throws IOException {
        this.jsonReader.endArray();
    }
    
    @Override
    public boolean hasNext() throws IOException {
        return this.jsonReader.hasNext();
    }
    
    @Override
    public int nextInt() throws IOException {
        return this.jsonReader.nextInt();
    }
    
    @Override
    public long nextLong() throws IOException {
        return this.jsonReader.nextLong();
    }
    
    @Override
    public String nextString() throws IOException {
        return this.jsonReader.nextString();
    }
    
    @Override
    public boolean nextBoolean() throws IOException {
        return this.jsonReader.nextBoolean();
    }
    
    @Override
    public double nextDouble() throws IOException {
        return this.jsonReader.nextDouble();
    }
    
    @Override
    public void nextNull() throws IOException {
        this.jsonReader.nextNull();
    }
    
    @Override
    public void setLenient(final boolean lenient) {
        this.jsonReader.setLenient(lenient);
    }
    
    @Override
    public void skipValue() throws IOException {
        this.jsonReader.skipValue();
    }
    
    @Override
    public void close() throws IOException {
        this.jsonReader.close();
    }
}
