PageRenderTime 38ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/alibaba/fastjson/parser/deserializer/TimestampDeserializer.java

https://bitbucket.org/xiejuntao/xdesktop
Java | 56 lines | 42 code | 13 blank | 1 comment | 7 complexity | 5256fba456b3e63b559bbb9163f26429 MD5 | raw file
  1. package com.alibaba.fastjson.parser.deserializer;
  2. import java.lang.reflect.Type;
  3. import java.sql.Timestamp;
  4. import java.text.DateFormat;
  5. import java.text.ParseException;
  6. import java.util.Date;
  7. import com.alibaba.fastjson.JSONException;
  8. import com.alibaba.fastjson.parser.DefaultJSONParser;
  9. import com.alibaba.fastjson.parser.JSONToken;
  10. public class TimestampDeserializer extends AbstractDateDeserializer implements ObjectDeserializer {
  11. public final static TimestampDeserializer instance = new TimestampDeserializer();
  12. @SuppressWarnings("unchecked")
  13. protected <T> T cast(DefaultJSONParser parser, Type clazz, Object fieldName, Object val) {
  14. if (val == null) {
  15. return null;
  16. }
  17. if (val instanceof java.util.Date) {
  18. return (T) new java.sql.Timestamp(((Date) val).getTime());
  19. }
  20. if (val instanceof Number) {
  21. return (T) new java.sql.Timestamp(((Number) val).longValue());
  22. }
  23. if (val instanceof String) {
  24. String strVal = (String) val;
  25. if (strVal.length() == 0) {
  26. return null;
  27. }
  28. DateFormat dateFormat = parser.getDateFormat();
  29. try {
  30. Date date = (Date) dateFormat.parse(strVal);
  31. return (T) new Timestamp(date.getTime());
  32. } catch (ParseException e) {
  33. // skip
  34. }
  35. long longVal = Long.parseLong(strVal);
  36. return (T) new java.sql.Timestamp(longVal);
  37. }
  38. throw new JSONException("parse error");
  39. }
  40. public int getFastMatchToken() {
  41. return JSONToken.LITERAL_INT;
  42. }
  43. }