129 lines
4.2 KiB
Java
129 lines
4.2 KiB
Java
package com.ubt.jimu.utils;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.GsonBuilder;
|
|
import com.google.gson.TypeAdapter;
|
|
import com.google.gson.TypeAdapterFactory;
|
|
import com.google.gson.reflect.TypeToken;
|
|
import com.google.gson.stream.JsonReader;
|
|
import com.google.gson.stream.JsonToken;
|
|
import com.google.gson.stream.JsonWriter;
|
|
import java.io.IOException;
|
|
import java.lang.reflect.Type;
|
|
import java.util.Collection;
|
|
import java.util.Enumeration;
|
|
import java.util.Iterator;
|
|
|
|
/* loaded from: classes2.dex */
|
|
public class GsonUtil {
|
|
|
|
public static class NullStringToEmptyAdapterFactory<T> implements TypeAdapterFactory {
|
|
@Override // com.google.gson.TypeAdapterFactory
|
|
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
|
|
if (typeToken.getRawType() != String.class) {
|
|
return null;
|
|
}
|
|
return new StringNullAdapter();
|
|
}
|
|
}
|
|
|
|
public static class StringNullAdapter extends TypeAdapter<String> {
|
|
@Override // com.google.gson.TypeAdapter
|
|
public String read(JsonReader jsonReader) throws IOException {
|
|
if (jsonReader.peek() != JsonToken.NULL) {
|
|
return jsonReader.nextString();
|
|
}
|
|
jsonReader.nextNull();
|
|
return "";
|
|
}
|
|
|
|
@Override // com.google.gson.TypeAdapter
|
|
public void write(JsonWriter jsonWriter, String str) throws IOException {
|
|
if (str == null) {
|
|
jsonWriter.nullValue();
|
|
} else {
|
|
jsonWriter.value(str);
|
|
}
|
|
}
|
|
}
|
|
|
|
static {
|
|
Double.valueOf(1.0d);
|
|
Double.valueOf(1.1d);
|
|
Double.valueOf(1.2d);
|
|
}
|
|
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
public static String a(Object obj, Type type, boolean z, Double d, String str, boolean z2) {
|
|
String str2 = "{}";
|
|
if (obj == null) {
|
|
return "{}";
|
|
}
|
|
GsonBuilder registerTypeAdapterFactory = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory());
|
|
if (z) {
|
|
registerTypeAdapterFactory.serializeNulls();
|
|
}
|
|
if (d != null) {
|
|
registerTypeAdapterFactory.setVersion(d.doubleValue());
|
|
}
|
|
if (a(str)) {
|
|
str = "yyyy-MM-dd HH:mm:ss";
|
|
}
|
|
registerTypeAdapterFactory.setDateFormat(str);
|
|
if (z2) {
|
|
registerTypeAdapterFactory.excludeFieldsWithoutExposeAnnotation();
|
|
}
|
|
Gson create = registerTypeAdapterFactory.create();
|
|
try {
|
|
obj = type != null ? create.toJson(obj, type) : create.toJson(obj);
|
|
str2 = obj;
|
|
return str2;
|
|
} catch (Exception unused) {
|
|
return ((obj instanceof Collection) || (obj instanceof Iterator) || (obj instanceof Enumeration) || obj.getClass().isArray()) ? "[]" : str2;
|
|
}
|
|
}
|
|
|
|
public static String a(Object obj) {
|
|
return a(obj, null, false, null, null, true);
|
|
}
|
|
|
|
public static <T> T a(String str, TypeToken<T> typeToken, String str2) {
|
|
if (a(str)) {
|
|
return null;
|
|
}
|
|
GsonBuilder registerTypeAdapterFactory = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory());
|
|
a(str2);
|
|
try {
|
|
return (T) registerTypeAdapterFactory.create().fromJson(str, typeToken.getType());
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static <T> T a(String str, TypeToken<T> typeToken) {
|
|
return (T) a(str, typeToken, (String) null);
|
|
}
|
|
|
|
public static <T> T a(String str, Class<T> cls, String str2) {
|
|
if (a(str)) {
|
|
return null;
|
|
}
|
|
GsonBuilder registerTypeAdapterFactory = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory());
|
|
a(str2);
|
|
try {
|
|
return (T) registerTypeAdapterFactory.create().fromJson(str, (Class) cls);
|
|
} catch (Exception unused) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static <T> T a(String str, Class<T> cls) {
|
|
return (T) a(str, cls, (String) null);
|
|
}
|
|
|
|
public static boolean a(String str) {
|
|
return str == null || "".equals(str);
|
|
}
|
|
}
|