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; import org.apache.log4j.Logger; /** * 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 { static Logger log = Logger.getLogger(JsonUtil.class); private static Gson gson; public static Gson getGsonInstance() { log.info("[Start] JsonUtil.getGsonInstance"); if (gson == null) { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(BigDecimal.class, new JsonDeserializer() { @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) { log.error("JsonUtil Exception [line 43]"); return null; } } }); gsonBuilder.registerTypeAdapter(Integer.class, new JsonDeserializer() { @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) { log.error("JsonUtil Exception [line 66]"); return null; } } }); gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer() { 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 (VSMUtil.isEmpty(json.getAsString())) { return null; } if (json.getAsString().length() == 13) { log.error("JsonUtil format date length = 13"); Date d = new Date(Long.parseLong(json.getAsString())); String dateString = df2Time.format(d); return df2Time.parse(dateString); } else 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]); try { 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); } } catch (Exception e) { log.error("JsonUtil Exception [line 118]"); // throw new RuntimeException("java.lang.ArrayIndexOutOfBoundsException"); // System.out.println("Print >>>> java.lang.ArrayIndexOutOfBoundsException"); e.printStackTrace(); } } else { log.error("[return] dfTime.parse(json.getAsString()) [line 126]"); 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])) { log.error("JsonUtil RuntimeException [line 134]"); 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)) { log.error("JsonUtil RuntimeException [line 142]"); throw new RuntimeException("Invalid Date " + json.getAsString() + "!"); } } catch (Exception e) { log.error("JsonUtil throw RuntimeException [line 146]"); throw new RuntimeException("Invalid Format Date dd/MM/yyyy!, " + json.getAsString()); } } else { log.error("JsonUtil throw RuntimeException [line 151]"); throw new RuntimeException("Invalid Format Date dd/MM/yyyy!, " + json.getAsString()); } } log.error("JsonUtil [return] df.parse(json.getAsString()); [line 156]"); return df.parse(json.getAsString()); } catch (ParseException e) { throw new RuntimeException(e); } } }); gsonBuilder.setDateFormat("dd/MM/yyyy"); gson = gsonBuilder.create(); } log.info("[END] JsonUtil.getGsonInstance"); return gson; } }