io.github.simplefixture.utils.JsonUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simplefixture Show documentation
Show all versions of simplefixture Show documentation
auto generator fixture for test
package io.github.simplefixture.utils;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonElement;
import org.apache.commons.lang.StringUtils;
import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class JsonUtils {
public static Object create(String json, Class clazz){
return new GsonBuilder()
.registerTypeAdapter(Date.class, new DateDeserializer())
.create().fromJson(json, clazz);
}
}
class DateDeserializer implements com.google.gson.JsonDeserializer {
@Override
public Date deserialize(JsonElement element, Type type, JsonDeserializationContext context) {
ThreadLocal df = ThreadLocal.withInitial(()->new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
String el = element.getAsString();
Date convertedDate;
try {
if(StringUtils.isNumeric(el)){
convertedDate = new Date(new Timestamp(Long.parseLong(el)).getTime());
}else{
convertedDate = df.get().parse(el);
}
} catch (Exception e) {
SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy hh:mm:ss a");
try {
convertedDate = sdf.parse(el);
} catch (ParseException ex) {
throw new RuntimeException("DateType value is Not valid", e);
}
}
return convertedDate;
}
}