import com.changfu.common.json.*;
import com.google.gson.*;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.sql.Date;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class JsonUtil {
private static Gson gson = new GsonBuilder().create();
private static Gson customGson = new GsonBuilder()
.registerTypeAdapter(Date.class, new DateAdapter())
.registerTypeAdapter(Integer.class, new IntegerDeserializer())
.registerTypeAdapter(Long.class, new LongDeserializer())
.registerTypeAdapter(BigDecimal.class, new BigDecimalDeserializer())
.registerTypeAdapter(
TreeMap.class,
new JsonDeserializer<TreeMap<String, Object>>() {
@Override
public TreeMap<String, Object> deserialize(
JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
TreeMap<String, Object> treeMap = new TreeMap<>();
JsonObject jsonObject = json.getAsJsonObject();
Set<Map.Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
for (Map.Entry<String, JsonElement> entry : entrySet) {
Object ot = entry.getValue();
if (ot instanceof JsonPrimitive) {
treeMap.put(entry.getKey(), ((JsonPrimitive) ot).getAsString());
} else {
treeMap.put(entry.getKey(), ot);
}
}
return treeMap;
}
}).create();
private static Gson customSerializer = new GsonBuilder()
.registerTypeAdapter(Date.class, new DateAdapter())
.registerTypeAdapter(Integer.class, new IntegerDeserializer())
.registerTypeAdapter(Long.class, new LongDeserializer())
.registerTypeAdapter(BigDecimal.class, new BigDecimalDeserializer())
.registerTypeAdapter(BigDecimal.class, new BigDecimalSerializer())
.registerTypeAdapter(Long.class, new LongSerializer())
.registerTypeAdapter(Integer.class, new IntegerSerializer())
.create();
private static JsonParser jsonParser = new JsonParser();
public static String encode(Object obj) {
return gson.toJson(obj);
}
public static String customEncode(Object obj) {
return customSerializer.toJson(obj);
}
public static <T> T decode(String json, Class<T> classOfT) {
return gson.fromJson(json, classOfT);
}
public static <T> T decode(JsonElement jsonElement, Class<T> classOfT) {
return gson.fromJson(jsonElement, classOfT);
}
/**
* 自定义解析规则:
* 1、"" 转 Integer、Long、Bigdecimal 返回null
*
* @param json
* @param classOfT
* @param <T>
* @return
*/
public static <T> T decodeByCustomGson(String json, Class<T> classOfT) {
return customGson.fromJson(json, classOfT);
}
public static <T> T decodeByCustomGson(String json, Type type) {
return customGson.fromJson(json, type);
}
public static JsonObject parseToJsonObject(String content) {
return jsonParser.parse(content).getAsJsonObject();
}
public static JsonArray parseToJsonArray(String content) {
return jsonParser.parse(content).getAsJsonArray();
}
public static void main(String[] args) {
}
}
import com.google.gson.*;
import java.lang.reflect.Type;
import java.sql.Date;
import java.text.SimpleDateFormat;
/**
* @Author:FeiCongcong
* @Date:2017/6/21 0021 14:32
*/
public class DateAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {
@Override
public Date deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
if (json == null) {
return null;
} else {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return new Date(sdf.parse(json.getAsString()).getTime());
} catch (Exception e) {
return null;
}
}
}
@Override
public JsonElement serialize(Date src, Type typeOfSrc,
JsonSerializationContext context) {
String value = "";
if (src != null) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
value = sdf.format(src);
} else {
return null;
}
return new JsonPrimitive(value);
}
}