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

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 47 lines | 36 code | 11 blank | 0 comment | 6 complexity | 1ead68ccab6ed3dc81120fe01f9fda39 MD5 | raw file
  1. package com.alibaba.fastjson.parser.deserializer;
  2. import java.lang.reflect.Type;
  3. import java.math.BigDecimal;
  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.util.TypeUtils;
  8. public class BigDecimalDeserializer implements ObjectDeserializer {
  9. public final static BigDecimalDeserializer instance = new BigDecimalDeserializer();
  10. @SuppressWarnings("unchecked")
  11. public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
  12. return (T) deserialze(parser);
  13. }
  14. @SuppressWarnings("unchecked")
  15. public static <T> T deserialze(DefaultJSONParser parser) {
  16. final JSONLexer lexer = parser.getLexer();
  17. if (lexer.token() == JSONToken.LITERAL_INT) {
  18. long val = lexer.longValue();
  19. lexer.nextToken(JSONToken.COMMA);
  20. return (T) new BigDecimal(val);
  21. }
  22. if (lexer.token() == JSONToken.LITERAL_FLOAT) {
  23. BigDecimal val = lexer.decimalValue();
  24. lexer.nextToken(JSONToken.COMMA);
  25. return (T) val;
  26. }
  27. Object value = parser.parse();
  28. if (value == null) {
  29. return null;
  30. }
  31. return (T) TypeUtils.castToBigDecimal(value);
  32. }
  33. public int getFastMatchToken() {
  34. return JSONToken.LITERAL_INT;
  35. }
  36. }