package org.forks.jsonrpc;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

final class Helper {
    static String getString(JSONObject obj, String name)
            throws JsonRpcException {
        try {
            return obj.getString(name);
        } catch (JSONException e) {
            throw new JsonRpcException(e);
        }
    }

    static int getInt(JSONObject obj, String name) throws JsonRpcException {
        try {
            return obj.getInt(name);
        } catch (JSONException e) {
            throw new JsonRpcException(e);
        }
    }

    static JSONObject parseJsonObject(String json) throws JsonRpcException {
        try {
            return (JSONObject) new JSONTokener(json).nextValue();
        } catch (JSONException e) {
            throw new JsonRpcException(e);
        }
    }

    static JSONArray getJsonArray(JSONObject obj, String name)
            throws JsonRpcException {
        try {
            return obj.getJSONArray(name);
        } catch (JSONException e) {
            throw new JsonRpcException(e);
        }
    }

    public static JSONArray getJsonArray(JSONArray array, int i)
            throws JsonRpcException {
        try {
            return array.getJSONArray(i);
        } catch (JSONException e) {
            throw new JsonRpcException(e);
        }
    }

    static JSONObject getJsonObject(JSONArray arr, int idx)
            throws JsonRpcException {
        try {
            return arr.getJSONObject(idx);
        } catch (JSONException e) {
            throw new JsonRpcException(e);
        }
    }

    static Object get(JSONObject obj, String name) throws JsonRpcException {
        try {
            return obj.get(name);
        } catch (JSONException e) {
            throw new JsonRpcException(e);
        }
    }

    public static Object get(JSONArray array, int idx) throws JsonRpcException {
        try {
            return array.get(idx);
        } catch (JSONException e) {
            throw new JsonRpcException(e);
        }
    }
}
