You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
private static final Gson GSON = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
String jsonString = gson.toJson(obj);
3.Gson对象反序列化
//1.parse object
Person person = gson.fromJson(jsonString, Person.class);
//2.parse array
List<Person> list = gson.fromJson(jsonString, new TypeToken<List<Person>>(){}.getType());
4.Gson JsonSerializer 与 JsonDeserializer
例如 针对Date 序列化与反序列化,如下:
import com.google.gson.*;
import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.Type;
import java.util.Date;
/**
* @author Ricky Fung
*/
public class JsonUtils {
private static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(Date.class, new JsonDateSerializer())
.create();
public static String toJson(Object obj) {
return GSON.toJson(obj);
}
public static <T> T parseObject(String json, Class<T> classOfT) {
return GSON.fromJson(json, classOfT);
}
public static <T> T parseObject(String json, Type typeOfT) {
return GSON.fromJson(json, typeOfT);
}
}
/**
* gson Date序列化/反序列化
* @author Ricky Fung
*/
class JsonDateSerializer implements
JsonSerializer<Date>,JsonDeserializer<Date> {
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json==null || json.isJsonNull()) {
return null;
}
String dateStr = json.getAsString();
if (StringUtils.isEmpty(dateStr)) {
return null;
}
return DateUtils.parseDate(dateStr);
}
@Override
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
if (src==null) {
return null;
}
return new JsonPrimitive(DateUtils.format(src));
}
}
json序列化
1.google gson
2.Gson对象序列化
3.Gson对象反序列化
4.Gson JsonSerializer 与 JsonDeserializer
例如 针对Date 序列化与反序列化,如下:
Cache
1.google guava
Guava: Google Core Libraries for Java.
1.guava
2.quick started
2. Caffeine
Caffeine is a high performance, near optimal caching library based on Java 8.
1.caffeine
2.quick started
获取默认类加载器
摘自Spring框架中的
org.springframework.util.ClassUtils
中更合理的实现方式:The text was updated successfully, but these errors were encountered: