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

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 73 lines | 56 code | 17 blank | 0 comment | 19 complexity | 6c32e3d8fad18da7f3e4f3bc48207a2b MD5 | raw file
  1. package com.alibaba.fastjson.parser.deserializer;
  2. import java.lang.reflect.Type;
  3. import com.alibaba.fastjson.JSONException;
  4. import com.alibaba.fastjson.parser.DefaultJSONParser;
  5. import com.alibaba.fastjson.parser.JSONScanner;
  6. import com.alibaba.fastjson.parser.JSONToken;
  7. public class TimeDeserializer implements ObjectDeserializer {
  8. public final static TimeDeserializer instance = new TimeDeserializer();
  9. @SuppressWarnings("unchecked")
  10. public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
  11. JSONScanner lexer = (JSONScanner) parser.getLexer();
  12. if (lexer.token() == JSONToken.COMMA) {
  13. lexer.nextToken(JSONToken.LITERAL_STRING);
  14. if (lexer.token() != JSONToken.LITERAL_STRING) {
  15. throw new JSONException("syntax error");
  16. }
  17. lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
  18. if (lexer.token() != JSONToken.LITERAL_INT) {
  19. throw new JSONException("syntax error");
  20. }
  21. long time = lexer.longValue();
  22. lexer.nextToken(JSONToken.RBRACE);
  23. if (lexer.token() != JSONToken.RBRACE) {
  24. throw new JSONException("syntax error");
  25. }
  26. lexer.nextToken(JSONToken.COMMA);
  27. return (T) new java.sql.Time(time);
  28. }
  29. Object val = parser.parse();
  30. if (val == null) {
  31. return null;
  32. }
  33. if (val instanceof java.sql.Time) {
  34. return (T) val;
  35. } else if (val instanceof Number) {
  36. return (T) new java.sql.Time(((Number) val).longValue());
  37. } else if (val instanceof String) {
  38. String strVal = (String) val;
  39. if (strVal.length() == 0) {
  40. return null;
  41. }
  42. long longVal;
  43. JSONScanner dateLexer = new JSONScanner(strVal);
  44. if (dateLexer.scanISO8601DateIfMatch()) {
  45. longVal = dateLexer.getCalendar().getTimeInMillis();
  46. } else {
  47. longVal = Long.parseLong(strVal);
  48. }
  49. return (T) new java.sql.Time(longVal);
  50. }
  51. throw new JSONException("parse error");
  52. }
  53. public int getFastMatchToken() {
  54. return JSONToken.LITERAL_INT;
  55. }
  56. }