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

package com.nimbusds.jose.util;

import com.nimbusds.jose.shaded.gson.ToNumberStrategy;
import com.nimbusds.jose.shaded.gson.ToNumberPolicy;
import com.nimbusds.jose.shaded.gson.Strictness;
import com.nimbusds.jose.shaded.gson.GsonBuilder;
import java.util.Objects;
import com.nimbusds.jwt.util.DateUtils;
import java.util.Date;
import java.util.Arrays;
import java.util.Iterator;
import java.util.HashMap;
import java.util.List;
import java.net.URISyntaxException;
import java.net.URI;
import com.nimbusds.jose.shaded.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.util.Map;
import com.nimbusds.jose.shaded.gson.Gson;

public class JSONObjectUtils
{
    private static final Gson GSON;
    
    public static Map<String, Object> parse(final String s) throws ParseException {
        return parse(s, -1);
    }
    
    public static Map<String, Object> parse(final String s, final int sizeLimit) throws ParseException {
        if (s == null) {
            throw new ParseException("The JSON object string must not be null", 0);
        }
        if (s.trim().isEmpty()) {
            throw new ParseException("Invalid JSON object", 0);
        }
        if (sizeLimit >= 0 && s.length() > sizeLimit) {
            throw new ParseException("The parsed string is longer than the max accepted size of " + sizeLimit + " characters", 0);
        }
        final Type mapType = TypeToken.getParameterized(Map.class, String.class, Object.class).getType();
        try {
            return JSONObjectUtils.GSON.fromJson(s, mapType);
        }
        catch (final Exception e) {
            throw new ParseException("Invalid JSON object", 0);
        }
        catch (final StackOverflowError e2) {
            throw new ParseException("Excessive JSON object and / or array nesting", 0);
        }
    }
    
    @Deprecated
    public static Map<String, Object> parseJSONObject(final String s) throws ParseException {
        return parse(s);
    }
    
    private static <T> T getGeneric(final Map<String, Object> o, final String name, final Class<T> clazz) throws ParseException {
        if (o.get(name) == null) {
            return null;
        }
        final Object value = o.get(name);
        if (!clazz.isAssignableFrom(value.getClass())) {
            throw new ParseException("Unexpected type of JSON object member " + name + "", 0);
        }
        final T castValue = (T)value;
        return castValue;
    }
    
    public static boolean getBoolean(final Map<String, Object> o, final String name) throws ParseException {
        final Boolean value = getGeneric(o, name, Boolean.class);
        if (value == null) {
            throw new ParseException("JSON object member " + name + " is missing or null", 0);
        }
        return value;
    }
    
    public static int getInt(final Map<String, Object> o, final String name) throws ParseException {
        final Number value = getGeneric(o, name, Number.class);
        if (value == null) {
            throw new ParseException("JSON object member " + name + " is missing or null", 0);
        }
        return value.intValue();
    }
    
    public static long getLong(final Map<String, Object> o, final String name) throws ParseException {
        final Number value = getGeneric(o, name, Number.class);
        if (value == null) {
            throw new ParseException("JSON object member " + name + " is missing or null", 0);
        }
        return value.longValue();
    }
    
    public static float getFloat(final Map<String, Object> o, final String name) throws ParseException {
        final Number value = getGeneric(o, name, Number.class);
        if (value == null) {
            throw new ParseException("JSON object member " + name + " is missing or null", 0);
        }
        return value.floatValue();
    }
    
    public static double getDouble(final Map<String, Object> o, final String name) throws ParseException {
        final Number value = getGeneric(o, name, Number.class);
        if (value == null) {
            throw new ParseException("JSON object member " + name + " is missing or null", 0);
        }
        return value.doubleValue();
    }
    
    public static String getString(final Map<String, Object> o, final String name) throws ParseException {
        return getGeneric(o, name, String.class);
    }
    
    public static URI getURI(final Map<String, Object> o, final String name) throws ParseException {
        final String value = getString(o, name);
        if (value == null) {
            return null;
        }
        try {
            return new URI(value);
        }
        catch (final URISyntaxException e) {
            throw new ParseException(e.getMessage(), 0);
        }
    }
    
    public static List<Object> getJSONArray(final Map<String, Object> o, final String name) throws ParseException {
        final List<Object> jsonArray = getGeneric(o, name, List.class);
        return jsonArray;
    }
    
    public static String[] getStringArray(final Map<String, Object> o, final String name) throws ParseException {
        final List<Object> jsonArray = getJSONArray(o, name);
        if (jsonArray == null) {
            return null;
        }
        try {
            return jsonArray.toArray(new String[0]);
        }
        catch (final ArrayStoreException e) {
            throw new ParseException("JSON object member " + name + " is not an array of strings", 0);
        }
    }
    
    public static Map<String, Object>[] getJSONObjectArray(final Map<String, Object> o, final String name) throws ParseException {
        final List<Object> jsonArray = getJSONArray(o, name);
        if (jsonArray == null) {
            return null;
        }
        if (jsonArray.isEmpty()) {
            return new HashMap[0];
        }
        for (final Object member : jsonArray) {
            if (member == null) {
                continue;
            }
            if (!(member instanceof Map)) {
                continue;
            }
            try {
                return jsonArray.toArray(new Map[0]);
            }
            catch (final ArrayStoreException e) {
                break;
            }
        }
        throw new ParseException("JSON object member " + name + " is not an array of JSON objects", 0);
    }
    
    public static List<String> getStringList(final Map<String, Object> o, final String name) throws ParseException {
        final String[] array = getStringArray(o, name);
        if (array == null) {
            return null;
        }
        return Arrays.asList(array);
    }
    
    public static Map<String, Object> getJSONObject(final Map<String, Object> o, final String name) throws ParseException {
        final Map<?, ?> jsonObject = getGeneric(o, name, Map.class);
        if (jsonObject == null) {
            return null;
        }
        for (final Object oKey : jsonObject.keySet()) {
            if (!(oKey instanceof String)) {
                throw new ParseException("JSON object member " + name + " not a JSON object", 0);
            }
        }
        final Map<String, Object> castJSONObject = (Map<String, Object>)jsonObject;
        return castJSONObject;
    }
    
    public static Base64URL getBase64URL(final Map<String, Object> o, final String name) throws ParseException {
        final String value = getString(o, name);
        if (value == null) {
            return null;
        }
        return new Base64URL(value);
    }
    
    public static Date getEpochSecondAsDate(final Map<String, Object> o, final String name) throws ParseException {
        final Number value = getGeneric(o, name, Number.class);
        if (value == null) {
            return null;
        }
        return DateUtils.fromSecondsSinceEpoch(value.longValue());
    }
    
    public static String toJSONString(final Map<String, ?> o) {
        return JSONObjectUtils.GSON.toJson(Objects.requireNonNull(o));
    }
    
    public static Map<String, Object> newJSONObject() {
        return new HashMap<String, Object>();
    }
    
    private JSONObjectUtils() {
    }
    
    static {
        GSON = new GsonBuilder().setStrictness(Strictness.STRICT).serializeNulls().setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE).disableHtmlEscaping().create();
    }
}
