ENDRPrint_12CRelease/.svn/pristine/ec/ec6ad253d60b0ddff8d9a59ed3a1e99aaa6d1c3b.svn-base

147 lines
6.2 KiB
Plaintext
Raw Permalink Normal View History

2024-08-14 10:58:03 +07:00
package th.co.muangthai.endrprint.util;
import com.google.gson.*;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by IntelliJ IDEA.
* User: ZIZU
* Date: 5/16/13
* Time: 9:21 AM
* To change this template use File | Settings | File Templates.
*/
public class JsonUtil {
private static Gson gson;
public static Gson getGsonInstance() {
if (gson == null) {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(BigDecimal.class, new JsonDeserializer<BigDecimal>() {
@Override
public BigDecimal deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
throws JsonParseException {
try {
String value = json.getAsString();
if (value != null) {
value = value.replace("\\r", "");
}
return new BigDecimal(value);
} catch (Exception e) {
return null;
}
}
});
gsonBuilder.registerTypeAdapter(Integer.class, new JsonDeserializer<Integer>() {
@Override
public Integer deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
throws JsonParseException {
try {
String value = json.getAsString();
if (value != null) {
value = value.replace("\\r", "");
}
return new Integer(value);
} catch (Exception e) {
return null;
}
}
});
gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
// SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy", VSMUtil.LOCALE);
// SimpleDateFormat dfTime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", VSMUtil.LOCALE);
// SimpleDateFormat df2Time = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", VSMUtil.LOCALE_EN);
// SimpleDateFormat df2 = new SimpleDateFormat("dd/MM/yyyy", VSMUtil.LOCALE_EN);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", VSMUtil.LOCALE);
SimpleDateFormat dfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", VSMUtil.LOCALE);
SimpleDateFormat df2Time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", VSMUtil.LOCALE_EN);
SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd", VSMUtil.LOCALE_EN);
@Override
public Date deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
throws JsonParseException {
// public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
try {
if (json.getAsString().length() > 10) {
String[] str1 = json.getAsString().split(" ");
// System.out.println("str1 > "+ str1.length);
// System.out.println("str1 > "+ str1[0]);
// System.out.println("str1 > "+ str1[1]);
if (str1[0].trim().length() > 10) {
String[] str = str1[0].split(TextContent.splitSpe);
// System.out.println("str > "+ str.length);
// System.out.println("str > "+ str[0]);
// System.out.println("str > "+ str[1]);
// System.out.println("str > "+ str[2]);
String bb = json.getAsString().replace(str[2], "9999");
// System.out.println("dd > "+ bb);
if (str1.length > 1) {
return df2Time.parse(bb);
} else {
return df2.parse(bb);
}
} else {
return dfTime.parse(json.getAsString());
}
}
// else if (json.getAsString().length() > 5) {
// String[] str = json.getAsString().split(TextContent.splitSpe);
// if (null != str && str.length == 3) {
// if (12 < Integer.parseInt(str[1])) {
// throw new RuntimeException("Invalid month!, " + json.getAsString());
// }
// try {
// String dataChk = str[0] + "/" + str[1] + "/" + TextContent.getLPAD ((Integer.parseInt(str[2]) - 543)+"","0",4);
// String dataChk2 = DateUtil.toFormatString(DateUtil.toDate(dataChk), "dd/MM/yyyy");
// if (!dataChk.equals(dataChk2)) {
// throw new RuntimeException("Invalid Date " + json.getAsString() + "!");
// }
// } catch (Exception e) {
// throw new RuntimeException("Invalid Format Date dd/MM/yyyy!, " + json.getAsString());
// }
//
// } else {
// throw new RuntimeException("Invalid Format Date dd/MM/yyyy!, " + json.getAsString());
// }
// }
return df2.parse(json.getAsString());
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
});
gsonBuilder.setDateFormat("yyyy/MM/dd");
gson = gsonBuilder.create();
}
return gson;
}
}