what-the-bank/sources/com/huawei/hms/utils/JsonUtil.java

354 lines
13 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package com.huawei.hms.utils;
import android.text.TextUtils;
import com.huawei.hms.core.aidl.IMessageEntity;
import com.huawei.hms.core.aidl.annotation.Packed;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.support.log.common.Base64;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/* loaded from: classes2.dex */
public class JsonUtil {
protected static final int VAL_BYTE = 2;
protected static final int VAL_ENTITY = 0;
protected static final int VAL_LIST = 1;
protected static final int VAL_MAP = 3;
protected static final int VAL_NULL = -1;
protected static final String VAL_TYPE = "_val_type_";
private static String a(IMessageEntity iMessageEntity) throws IllegalAccessException, JSONException {
JSONObject jSONObject = new JSONObject();
for (Class<?> cls = iMessageEntity.getClass(); cls != null; cls = cls.getSuperclass()) {
for (Field field : cls.getDeclaredFields()) {
if (field.isAnnotationPresent(Packed.class)) {
boolean isAccessible = field.isAccessible();
a(field, true);
String name = field.getName();
Object obj = field.get(iMessageEntity);
a(field, isAccessible);
a(name, obj, jSONObject);
}
}
}
return jSONObject.toString();
}
private static Object b(IMessageEntity iMessageEntity, Field field, JSONObject jSONObject) throws JSONException, IllegalAccessException {
Object a = a(field.getName(), jSONObject);
if (a == null) {
return null;
}
try {
if (field.getType().getName().startsWith("com.huawei") && (field.getType().newInstance() instanceof IMessageEntity)) {
return jsonToEntity((String) a, (IMessageEntity) field.getType().newInstance());
}
if (!(a instanceof JSONObject) || !((JSONObject) a).has(VAL_TYPE)) {
return a;
}
int i = ((JSONObject) a).getInt(VAL_TYPE);
if (i == 1 || i == 0) {
return a(field.getGenericType(), (JSONObject) a);
}
if (i == 2) {
return a((JSONObject) a);
}
if (i == 3) {
return b(field.getGenericType(), (JSONObject) a);
}
StringBuilder sb = new StringBuilder("cannot support type : ");
sb.append(i);
HMSLog.e("JsonUtil", sb.toString());
return null;
} catch (InstantiationException unused) {
HMSLog.e("JsonUtil", "InstantiationException ");
return null;
}
}
@Deprecated
public static String createJsonString(IMessageEntity iMessageEntity) {
if (iMessageEntity == null) {
HMSLog.e("JsonUtil", "createJsonString error, the input IMessageEntity is null");
return "";
}
try {
return a(iMessageEntity);
} catch (IllegalAccessException e) {
StringBuilder sb = new StringBuilder("catch IllegalAccessException ");
sb.append(e.getMessage());
HMSLog.e("JsonUtil", sb.toString());
return "";
} catch (JSONException e2) {
StringBuilder sb2 = new StringBuilder("catch JSONException ");
sb2.append(e2.getMessage());
HMSLog.e("JsonUtil", sb2.toString());
return "";
}
}
public static Object getInfoFromJsonobject(String str, String str2) {
if (!TextUtils.isEmpty(str) && !TextUtils.isEmpty(str2)) {
try {
JSONObject jSONObject = new JSONObject(str);
if (!jSONObject.has(str2)) {
return null;
}
Object obj = jSONObject.get(str2);
if (obj instanceof String) {
return obj;
}
} catch (JSONException unused) {
HMSLog.e("JsonUtil", "getInfoFromJsonobject:parser json error :".concat(String.valueOf(str2)));
}
}
return null;
}
public static int getIntValue(JSONObject jSONObject, String str) throws JSONException {
if (jSONObject == null || !jSONObject.has(str)) {
return -1;
}
return jSONObject.getInt(str);
}
public static String getStringValue(JSONObject jSONObject, String str) throws JSONException {
if (jSONObject == null || !jSONObject.has(str)) {
return null;
}
return jSONObject.getString(str);
}
@Deprecated
public static IMessageEntity jsonToEntity(String str, IMessageEntity iMessageEntity) {
if (iMessageEntity == null) {
return null;
}
try {
Class<?> cls = iMessageEntity.getClass();
JSONObject jSONObject = new JSONObject(str);
while (cls != null) {
Field[] declaredFields = cls.getDeclaredFields();
if (declaredFields == null) {
cls = cls.getSuperclass();
} else {
for (Field field : declaredFields) {
if (field.isAnnotationPresent(Packed.class)) {
try {
a(iMessageEntity, field, jSONObject);
} catch (IllegalAccessException unused) {
StringBuilder sb = new StringBuilder();
sb.append("jsonToEntity, set value of the field exception, field name:");
sb.append(field.getName());
HMSLog.e("JsonUtil", sb.toString());
}
}
}
cls = cls.getSuperclass();
}
}
} catch (JSONException e) {
StringBuilder sb2 = new StringBuilder("catch JSONException when parse jsonString");
sb2.append(e.getMessage());
HMSLog.e("JsonUtil", sb2.toString());
}
return iMessageEntity;
}
private static void a(Field field, boolean z) {
AccessController.doPrivileged(new PrivilegedAction(field, z) { // from class: com.huawei.hms.utils.JsonUtil.1
final Field a;
final boolean b;
{
this.a = field;
this.b = z;
}
@Override // java.security.PrivilegedAction
public Object run() {
this.a.setAccessible(this.b);
return null;
}
});
}
private static boolean a(String str, Object obj, JSONObject jSONObject) throws JSONException, IllegalAccessException {
if (obj instanceof String) {
jSONObject.put(str, (String) obj);
return true;
}
if (obj instanceof Integer) {
jSONObject.put(str, ((Integer) obj).intValue());
return true;
}
if (obj instanceof Short) {
jSONObject.put(str, (Short) obj);
return true;
}
if (obj instanceof Long) {
jSONObject.put(str, (Long) obj);
return true;
}
if (obj instanceof Float) {
jSONObject.put(str, (Float) obj);
return true;
}
if (obj instanceof Double) {
jSONObject.put(str, (Double) obj);
return true;
}
if (obj instanceof Boolean) {
jSONObject.put(str, (Boolean) obj);
return true;
}
if (obj instanceof JSONObject) {
jSONObject.put(str, (JSONObject) obj);
return true;
}
if (obj instanceof byte[]) {
a(str, (byte[]) obj, jSONObject);
return true;
}
if (obj instanceof List) {
a(str, (List<?>) obj, jSONObject);
return true;
}
if (obj instanceof Map) {
a(str, (Map) obj, jSONObject);
return true;
}
if (!(obj instanceof IMessageEntity)) {
return false;
}
try {
jSONObject.put(str, a((IMessageEntity) obj));
return true;
} catch (IllegalAccessException e) {
HMSLog.e("JsonUtil", "IllegalAccessException , ".concat(String.valueOf(e)));
return false;
}
}
private static Map b(Type type, JSONObject jSONObject) throws JSONException, IllegalAccessException, InstantiationException {
Class cls = (Class) ((ParameterizedType) type).getActualTypeArguments()[1];
JSONArray jSONArray = new JSONArray(jSONObject.getString("_map_"));
HashMap hashMap = new HashMap();
for (int i = 0; i < jSONArray.length(); i += 2) {
if (cls.newInstance() instanceof IMessageEntity) {
hashMap.put(jSONArray.get(i), jsonToEntity(jSONArray.getString(i + 1), (IMessageEntity) cls.newInstance()));
} else {
hashMap.put(jSONArray.get(i), jSONArray.get(i + 1));
}
}
return hashMap;
}
private static void a(String str, Map map, JSONObject jSONObject) throws JSONException, IllegalAccessException {
JSONArray jSONArray = new JSONArray();
for (Map.Entry entry : map.entrySet()) {
Object key = entry.getKey();
Object value = entry.getValue();
if (key instanceof IMessageEntity) {
jSONArray.put(a((IMessageEntity) key));
} else {
jSONArray.put(key);
}
if (value instanceof IMessageEntity) {
jSONArray.put(a((IMessageEntity) value));
} else {
jSONArray.put(value);
}
}
JSONObject jSONObject2 = new JSONObject();
jSONObject2.put(VAL_TYPE, 3);
jSONObject2.put("_map_", jSONArray.toString());
jSONObject.put(str, jSONObject2);
}
private static void a(String str, byte[] bArr, JSONObject jSONObject) throws JSONException {
JSONObject jSONObject2 = new JSONObject();
jSONObject2.put(VAL_TYPE, 2);
try {
jSONObject2.put("_byte_", Base64.encode(bArr));
} catch (IllegalArgumentException e) {
StringBuilder sb = new StringBuilder("writeByte failed : ");
sb.append(e.getMessage());
HMSLog.e("JsonUtil", sb.toString());
}
jSONObject.put(str, jSONObject2);
}
private static void a(String str, List<?> list, JSONObject jSONObject) throws JSONException, IllegalAccessException {
JSONObject jSONObject2 = new JSONObject();
jSONObject2.put(VAL_TYPE, 1);
jSONObject2.put("_list_size_", list.size());
for (int i = 0; i < list.size(); i++) {
a("_list_item_".concat(String.valueOf(i)), list.get(i), jSONObject2);
if (list.get(i) instanceof IMessageEntity) {
jSONObject2.put(VAL_TYPE, 0);
}
}
jSONObject.put(str, jSONObject2);
}
private static void a(IMessageEntity iMessageEntity, Field field, JSONObject jSONObject) throws JSONException, IllegalAccessException {
Object b = b(iMessageEntity, field, jSONObject);
if (b != null) {
boolean isAccessible = field.isAccessible();
a(field, true);
field.set(iMessageEntity, b);
a(field, isAccessible);
}
}
private static Object a(String str, JSONObject jSONObject) throws JSONException {
if (jSONObject.has(str)) {
return jSONObject.get(str);
}
if (jSONObject.has("header") && jSONObject.getJSONObject("header").has(str)) {
return jSONObject.getJSONObject("header").get(str);
}
if (jSONObject.has("body") && jSONObject.getJSONObject("body").has(str)) {
return jSONObject.getJSONObject("body").get(str);
}
return null;
}
private static List<Object> a(Type type, JSONObject jSONObject) throws JSONException, IllegalAccessException, InstantiationException {
int i = jSONObject.getInt("_list_size_");
int i2 = jSONObject.getInt(VAL_TYPE);
ArrayList arrayList = new ArrayList(i);
for (int i3 = 0; i3 < i; i3++) {
Object obj = jSONObject.get("_list_item_".concat(String.valueOf(i3)));
if (i2 == 0) {
arrayList.add(jsonToEntity((String) obj, (IMessageEntity) ((Class) ((ParameterizedType) type).getActualTypeArguments()[0]).newInstance()));
} else if (i2 == 1) {
arrayList.add(obj);
}
}
return arrayList;
}
private static byte[] a(JSONObject jSONObject) throws JSONException {
try {
return Base64.decode(jSONObject.getString("_byte_"));
} catch (IllegalArgumentException e) {
StringBuilder sb = new StringBuilder("readByte failed : ");
sb.append(e.getMessage());
HMSLog.e("JsonUtil", sb.toString());
return null;
}
}
}