package com.bytedance.ugc.glue.json;

import X.C30038lu;
import X.C30048lv;
import com.bytedance.covode.number.Covode;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.util.Iterator;
import org.json.JSONObject;

public class UGCJson {
    public static UGCJson instance = new UGCJson();

    public <T> T fromJsonImpl(Object obj, Type type) {
        return null;
    }

    public String toJsonImpl(Object obj) {
        return "";
    }

    public static UGCJson getInstance() {
        return instance;
    }

    public final void register() {
        instance = this;
    }

    static {
        Covode.recordClassIndex(39806);
    }

    public static Type buildGenericArrayType(Type type) {
        return new C30048lv(type, (byte) 0);
    }

    public static JSONObject jsonObject(String str) {
        try {
            return new JSONObject(str);
        } catch (Throwable unused) {
            return new JSONObject();
        }
    }

    public static String toJson(Object obj) {
        return getInstance().toJsonImpl(obj);
    }

    public static JSONObject mergeJSONObject(JSONObject... jSONObjectArr) {
        JSONObject jSONObject = new JSONObject();
        if (jSONObjectArr != null) {
            for (JSONObject jSONObject2 : jSONObjectArr) {
                if (jSONObject2 != null) {
                    try {
                        Iterator<String> keys = jSONObject2.keys();
                        while (keys.hasNext()) {
                            String next = keys.next();
                            jSONObject.putOpt(next, jSONObject2.get(next));
                        }
                    } catch (Throwable unused) {
                    }
                }
            }
        }
        return jSONObject;
    }

    public static JSONObject jsonObject(Throwable th) {
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("class", th.getClass().getName());
            jSONObject.put("message", th.getMessage());
            StackTraceElement[] stackTrace = th.getStackTrace();
            int i = 0;
            while (i < 10 && i < stackTrace.length) {
                StackTraceElement stackTraceElement = stackTrace[i];
                String concat = "trace".concat(String.valueOf(i));
                jSONObject.put(concat, stackTraceElement.getClassName() + "#" + stackTraceElement.getMethodName() + "@" + stackTraceElement.getLineNumber());
                i++;
            }
        } catch (Throwable unused) {
        }
        return jSONObject;
    }

    public static Type buildParameterizedType(Type type, Type... typeArr) {
        return new C30038lu(type, typeArr, (byte) 0);
    }

    public static <T> T convert(Object obj, Type type) {
        return getInstance().convertImpl(obj, type);
    }

    public static <T> T fromJson(InputStream inputStream, Type type) {
        return getInstance().fromJsonImpl(new InputStreamReader(inputStream), type);
    }

    public <T> T convertImpl(Object obj, Type type) {
        return fromJsonImpl(toJsonImpl(obj), type);
    }

    public static <T> T fromJson(Reader reader, Type type) {
        return getInstance().fromJsonImpl(reader, type);
    }

    public static <T> T fromJson(String str, Type type) {
        return getInstance().fromJsonImpl(str, type);
    }

    public static JSONObject put(JSONObject jSONObject, String str, Object obj) {
        if (jSONObject == null) {
            jSONObject = new JSONObject();
        }
        if (!(str == null || obj == null)) {
            try {
                jSONObject.put(str, obj);
            } catch (Throwable unused) {
            }
        }
        return jSONObject;
    }
}
