PageRenderTime 142ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 47 lines | 35 code | 12 blank | 0 comment | 6 complexity | b24f86e66b119128123ff2073a1ff387 MD5 | raw file
  1. package com.alibaba.fastjson.parser.deserializer;
  2. import java.lang.reflect.Type;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.parser.DefaultJSONParser;
  5. import com.alibaba.fastjson.parser.JSONLexer;
  6. import com.alibaba.fastjson.parser.JSONToken;
  7. public class CharArrayDeserializer implements ObjectDeserializer {
  8. public final static CharArrayDeserializer instance = new CharArrayDeserializer();
  9. @SuppressWarnings("unchecked")
  10. public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
  11. return (T) deserialze(parser);
  12. }
  13. @SuppressWarnings("unchecked")
  14. public static <T> T deserialze(DefaultJSONParser parser) {
  15. final JSONLexer lexer = parser.getLexer();
  16. if (lexer.token() == JSONToken.LITERAL_STRING) {
  17. String val = lexer.stringVal();
  18. lexer.nextToken(JSONToken.COMMA);
  19. return (T) val.toCharArray();
  20. }
  21. if (lexer.token() == JSONToken.LITERAL_INT) {
  22. Number val = lexer.integerValue();
  23. lexer.nextToken(JSONToken.COMMA);
  24. return (T) val.toString().toCharArray();
  25. }
  26. Object value = parser.parse();
  27. if (value == null) {
  28. return null;
  29. }
  30. return (T) JSON.toJSONString(value).toCharArray();
  31. }
  32. public int getFastMatchToken() {
  33. return JSONToken.LITERAL_STRING;
  34. }
  35. }