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

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 57 lines | 46 code | 10 blank | 1 comment | 16 complexity | 668b1421a05be669a5d00e82472f9e3e MD5 | raw file
  1. package com.alibaba.fastjson.parser.deserializer;
  2. import java.lang.reflect.Type;
  3. import java.util.Map;
  4. import com.alibaba.fastjson.parser.DefaultJSONParser;
  5. import com.alibaba.fastjson.parser.JSONLexer;
  6. import com.alibaba.fastjson.parser.JSONToken;
  7. import com.alibaba.fastjson.parser.ParserConfig;
  8. import com.alibaba.fastjson.util.FieldInfo;
  9. import com.alibaba.fastjson.util.TypeUtils;
  10. public class IntegerFieldDeserializer extends FieldDeserializer {
  11. public IntegerFieldDeserializer(ParserConfig mapping, Class<?> clazz, FieldInfo fieldInfo){
  12. super(clazz, fieldInfo);
  13. }
  14. @Override
  15. public void parseField(DefaultJSONParser parser, Object object, Type objectType, Map<String, Object> fieldValues) {
  16. Integer value;
  17. final JSONLexer lexer = parser.getLexer();
  18. if (lexer.token() == JSONToken.LITERAL_INT) {
  19. int val = lexer.intValue();
  20. lexer.nextToken(JSONToken.COMMA);
  21. if (object == null) {
  22. fieldValues.put(fieldInfo.getName(), val);
  23. } else {
  24. setValue(object, val);
  25. }
  26. return;
  27. } else if (lexer.token() == JSONToken.NULL) {
  28. value = null;
  29. lexer.nextToken(JSONToken.COMMA);
  30. } else {
  31. Object obj = parser.parse();
  32. value = TypeUtils.castToInt(obj);
  33. }
  34. if (value == null && getFieldClass() == int.class) {
  35. // skip
  36. return;
  37. }
  38. if (object == null) {
  39. fieldValues.put(fieldInfo.getName(), value);
  40. } else {
  41. setValue(object, value);
  42. }
  43. }
  44. public int getFastMatchToken() {
  45. return JSONToken.LITERAL_INT;
  46. }
  47. }