307 lines
10 KiB
Java
307 lines
10 KiB
Java
package com.mitac.cell.utils;
|
|
|
|
import com.fatboyindustrial.gsonjodatime.Converters;
|
|
import com.google.common.base.Charsets;
|
|
import com.google.gson.ExclusionStrategy;
|
|
import com.google.gson.FieldAttributes;
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.GsonBuilder;
|
|
import com.google.gson.JsonElement;
|
|
import com.google.gson.JsonObject;
|
|
import com.google.gson.JsonParser;
|
|
import com.google.gson.JsonPrimitive;
|
|
import com.google.gson.JsonSyntaxException;
|
|
import com.google.gson.annotations.Expose;
|
|
import com.mitac.cell.gson.typeAdapter.CipherTypeAdapter;
|
|
import com.mitac.cell.gson.typeAdapter.CompatibleTimeZoneTypeAdapter;
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.io.Reader;
|
|
import java.lang.reflect.Type;
|
|
import java.net.MalformedURLException;
|
|
import java.net.URL;
|
|
import java.util.Date;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.TimeZone;
|
|
import java.util.UUID;
|
|
import javax.crypto.Cipher;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
public class McJsonUtils {
|
|
public static String FULL_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
|
|
private static final Gson gson;
|
|
private static final Gson gsonCloner;
|
|
|
|
public static String getString(JsonObject jInfo, String property) {
|
|
JsonElement element = getProperty(jInfo, property);
|
|
if (element == null) {
|
|
return null;
|
|
} else {
|
|
return element.isJsonPrimitive() ? element.getAsString() : element.toString();
|
|
}
|
|
}
|
|
|
|
public static int getInt(JsonObject jInfo, String property, int defaultValue) {
|
|
JsonElement element = getProperty(jInfo, property);
|
|
return element == null ? defaultValue : element.getAsInt();
|
|
}
|
|
|
|
public static long getLong(JsonObject jInfo, String property, long defaultValue) {
|
|
JsonElement element = getProperty(jInfo, property);
|
|
return element == null ? defaultValue : element.getAsLong();
|
|
}
|
|
|
|
public static float getFloat(JsonObject jInfo, String property, float defaultValue) {
|
|
JsonElement element = getProperty(jInfo, property);
|
|
return element == null ? defaultValue : element.getAsFloat();
|
|
}
|
|
|
|
public static boolean getBoolean(JsonObject jInfo, String property, boolean defaultValue) {
|
|
JsonElement element = getProperty(jInfo, property);
|
|
return element == null ? defaultValue : element.getAsBoolean();
|
|
}
|
|
|
|
public static Date getDate(JsonObject jInfo, String property, Date defaultValue) {
|
|
JsonElement element = getProperty(jInfo, property);
|
|
return element == null ? defaultValue : new Date(element.getAsLong());
|
|
}
|
|
|
|
public static URL getUrl(JsonObject jInfo, String property) {
|
|
JsonElement element = getProperty(jInfo, property);
|
|
|
|
try {
|
|
return element == null ? null : new URL(element.getAsString());
|
|
} catch (MalformedURLException e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static UUID getUuid(JsonObject jInfo, String property) {
|
|
JsonElement element = getProperty(jInfo, property);
|
|
return element == null ? null : UUID.fromString(element.getAsString());
|
|
}
|
|
|
|
public static JsonElement getProperty(JsonObject jInfo, String property) {
|
|
if (jInfo == null) {
|
|
jInfo = new JsonObject();
|
|
}
|
|
|
|
return jInfo == null ? null : jInfo.get(property);
|
|
}
|
|
|
|
public static boolean isValid(JsonObject jObject) {
|
|
return !isNullOrEmpty(jObject);
|
|
}
|
|
|
|
public static boolean isNullOrEmpty(JsonObject jObject) {
|
|
return jObject == null || jObject.entrySet().isEmpty();
|
|
}
|
|
|
|
public static JsonElement toJson(byte[] content) {
|
|
return content == null ? null : JsonParser.parseReader(new InputStreamReader(new ByteArrayInputStream(content)));
|
|
}
|
|
|
|
public static JsonElement toJsonElement(Object src) {
|
|
return gsonCloner.toJsonTree(src);
|
|
}
|
|
|
|
public static void setProperty(JsonObject jInfo, String property, Object value) {
|
|
if (jInfo == null) {
|
|
jInfo = new JsonObject();
|
|
}
|
|
|
|
jInfo.add(property, toJsonElement(value));
|
|
}
|
|
|
|
public static JsonElement toJsonElement(String jsonSource) {
|
|
try {
|
|
return McStringUtils.isNullOrEmpty(jsonSource) ? null : JsonParser.parseString(jsonSource);
|
|
} catch (JsonSyntaxException var2) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static JsonElement toJsonElement(HttpServletRequest request) {
|
|
try {
|
|
return toJsonElement((InputStream)request.getInputStream());
|
|
} catch (Exception var2) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static JsonElement toJsonElement(InputStream is) {
|
|
return is == null ? null : toJsonElement((Reader)(new InputStreamReader(is, Charsets.UTF_8)));
|
|
}
|
|
|
|
public static JsonElement toJsonElement(File file) {
|
|
try {
|
|
return file != null && file.exists() && !file.isDirectory() ? toJsonElement((InputStream)(new FileInputStream(file))) : null;
|
|
} catch (FileNotFoundException e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static JsonElement toJsonElement(Reader jsonReader) {
|
|
try {
|
|
return jsonReader == null ? null : JsonParser.parseReader(jsonReader);
|
|
} catch (Exception var2) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static Map toBooleanMap(String jsonSource) {
|
|
if (McStringUtils.isNullOrEmpty(jsonSource)) {
|
|
return null;
|
|
} else {
|
|
JsonElement jElement = toJsonElement(jsonSource);
|
|
return jElement.isJsonObject() ? toBooleanMap(jElement.getAsJsonObject()) : null;
|
|
}
|
|
}
|
|
|
|
public static Map toBooleanMap(JsonObject jObject) {
|
|
if (jObject != null && !jObject.entrySet().isEmpty()) {
|
|
Map<String, Boolean> map = new HashMap();
|
|
|
|
for(Map.Entry jEntry : jObject.entrySet()) {
|
|
boolean value = false;
|
|
if (((JsonElement)jEntry.getValue()).isJsonPrimitive()) {
|
|
JsonPrimitive jPrimitive = ((JsonElement)jEntry.getValue()).getAsJsonPrimitive();
|
|
if (jPrimitive.isBoolean()) {
|
|
value = jPrimitive.getAsBoolean();
|
|
} else if (jPrimitive.isNumber()) {
|
|
value = jPrimitive.getAsDouble() != (double)0.0F;
|
|
} else if (jPrimitive.isString()) {
|
|
value = McBooleanUtils.isTrue(jPrimitive.getAsString());
|
|
}
|
|
}
|
|
|
|
map.put((String)jEntry.getKey(), value);
|
|
}
|
|
|
|
return map;
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static Object toObject(HttpServletRequest request, Type dataType) {
|
|
try {
|
|
return toObject(toJsonElement((InputStream)request.getInputStream()), dataType);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static Object toObject(InputStream is, Type dataType) {
|
|
return toObject(toJsonElement(is), dataType);
|
|
}
|
|
|
|
public static Object toObject(byte[] bytes, Type dataType) {
|
|
return toObject((InputStream)(new ByteArrayInputStream(bytes)), dataType);
|
|
}
|
|
|
|
public static Object toObject(String jElement, Type dataType) {
|
|
return toObject(toJsonElement(jElement), dataType);
|
|
}
|
|
|
|
public static Object toObject(JsonElement jElement, Type dataType) {
|
|
return jElement != null && !jElement.isJsonPrimitive() ? gson.fromJson(jElement, dataType) : null;
|
|
}
|
|
|
|
public static Object clone(Object object) {
|
|
return gsonCloner.fromJson(toJsonElement(object), object.getClass());
|
|
}
|
|
|
|
public static String toJson(Object object) {
|
|
return gson.toJson(object);
|
|
}
|
|
|
|
public static String toJson(Object object, boolean pretty) {
|
|
GsonBuilder builder = new GsonBuilder();
|
|
if (pretty) {
|
|
builder.setPrettyPrinting();
|
|
}
|
|
|
|
return builder.create().toJson(object);
|
|
}
|
|
|
|
public static JsonObject merge(JsonObject... jObjects) {
|
|
if (jObjects != null && jObjects.length != 0) {
|
|
JsonObject jResult = null;
|
|
|
|
for(JsonObject jObject : jObjects) {
|
|
if (jResult == null) {
|
|
jResult = jObject;
|
|
} else {
|
|
for(Map.Entry entry : jObject.entrySet()) {
|
|
jResult.add((String)entry.getKey(), (JsonElement)entry.getValue());
|
|
}
|
|
}
|
|
}
|
|
|
|
return jResult;
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static boolean equals(JsonElement src, JsonElement target) {
|
|
if (src != null && target != null) {
|
|
if (src.isJsonObject()) {
|
|
if (target.isJsonObject()) {
|
|
}
|
|
|
|
return false;
|
|
} else if (src.isJsonArray()) {
|
|
if (target.isJsonObject()) {
|
|
}
|
|
|
|
return false;
|
|
} else if (src.isJsonPrimitive()) {
|
|
if (!target.isJsonPrimitive()) {
|
|
return false;
|
|
} else {
|
|
JsonPrimitive srcPrim = src.getAsJsonPrimitive();
|
|
JsonPrimitive targetPrim = target.getAsJsonPrimitive();
|
|
if (srcPrim.isString()) {
|
|
return targetPrim.isString() && src.getAsString().equals(targetPrim.getAsString());
|
|
} else if (srcPrim.isNumber()) {
|
|
return targetPrim.isNumber() && src.getAsDouble() == targetPrim.getAsDouble();
|
|
} else {
|
|
return targetPrim.isBoolean() && src.getAsBoolean() == targetPrim.getAsBoolean();
|
|
}
|
|
}
|
|
} else {
|
|
return src.isJsonNull() && target.isJsonNull();
|
|
}
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static {
|
|
gson = Converters.registerDateTime((new GsonBuilder()).registerTypeAdapter(TimeZone.class, new CompatibleTimeZoneTypeAdapter()).registerTypeAdapter(Cipher.class, new CipherTypeAdapter()).addSerializationExclusionStrategy(new SerializeExclusionStragtegy()).setDateFormat(FULL_FORMAT)).create();
|
|
gsonCloner = Converters.registerDateTime((new GsonBuilder()).registerTypeAdapter(TimeZone.class, new CompatibleTimeZoneTypeAdapter()).registerTypeAdapter(Cipher.class, new CipherTypeAdapter()).setDateFormat(FULL_FORMAT)).create();
|
|
}
|
|
|
|
private static class SerializeExclusionStragtegy implements ExclusionStrategy {
|
|
public boolean shouldSkipField(FieldAttributes f) {
|
|
Expose expose = (Expose)f.getAnnotation(Expose.class);
|
|
return expose != null && !expose.serialize();
|
|
}
|
|
|
|
public boolean shouldSkipClass(Class clazz) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|