package com.binance.connector.client.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/* loaded from: binance-connector-java-3.0.0rc3.jar:com/binance/connector/client/utils/JSONParser.class */
public final class JSONParser {
    private JSONParser() {
    }

    public static String getJSONStringValue(String json, String key) {
        try {
            return new JSONObject(json).getString(key);
        } catch (JSONException e) {
            throw new JSONException(String.format("[JSONParser] Failed to get \"%s\"  from JSON object", key));
        }
    }

    public static int getJSONIntValue(String json, String key) {
        try {
            return new JSONObject(json).getInt(key);
        } catch (JSONException e) {
            throw new JSONException(String.format("[JSONParser] Failed to get \"%s\" from JSON object", key));
        }
    }

    public static String getJSONArray(ArrayList<?> symbols, String key) {
        try {
            return new JSONArray((Collection) symbols).toString();
        } catch (JSONException e) {
            throw new JSONException(String.format("[JSONParser] Failed to convert \"%s\" to JSON array", key));
        }
    }

    public static String buildJSONString(Object id, String method, JSONObject parameters) {
        try {
            JSONObject json = new JSONObject();
            json.put("id", id);
            json.put("method", method);
            json.put("params", parameters);
            return json.toString();
        } catch (JSONException e) {
            throw new JSONException(String.format("[JSONParser] Failed to convert to JSON string", new Object[0]));
        }
    }

    public static Map<String, Object> sortJSONObject(JSONObject parameters) {
        LinkedList<String> keys = new LinkedList<>(parameters.keySet());
        Map<String, Object> sortedParams = new LinkedHashMap<>();
        keys.stream().sorted().forEach(key -> {
            sortedParams.put(key, parameters.get(key));
        });
        return sortedParams;
    }

    public static JSONObject addKeyValue(JSONObject parameters, String key, Object value) {
        if (parameters == null) {
            parameters = new JSONObject();
        }
        return parameters.put(key, value);
    }

    public static Object pullValue(JSONObject parameters, String key) {
        if (parameters == null) {
            return null;
        }
        Object value = parameters.opt(key);
        parameters.remove(key);
        return value;
    }
}
