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

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 131 lines | 103 code | 28 blank | 0 comment | 28 complexity | 7b877ef389cffa420115fbeabed45dad MD5 | raw file
  1. package com.alibaba.fastjson.parser.deserializer;
  2. import java.lang.reflect.ParameterizedType;
  3. import java.lang.reflect.Type;
  4. import java.lang.reflect.TypeVariable;
  5. import java.util.ArrayList;
  6. import java.util.Collection;
  7. import java.util.Map;
  8. import com.alibaba.fastjson.JSONException;
  9. import com.alibaba.fastjson.parser.DefaultJSONParser;
  10. import com.alibaba.fastjson.parser.Feature;
  11. import com.alibaba.fastjson.parser.JSONLexer;
  12. import com.alibaba.fastjson.parser.JSONToken;
  13. import com.alibaba.fastjson.parser.ParseContext;
  14. import com.alibaba.fastjson.parser.ParserConfig;
  15. import com.alibaba.fastjson.util.FieldInfo;
  16. public class ArrayListTypeFieldDeserializer extends FieldDeserializer {
  17. private final Type itemType;
  18. private int itemFastMatchToken;
  19. private ObjectDeserializer deserializer;
  20. public ArrayListTypeFieldDeserializer(ParserConfig mapping, Class<?> clazz, FieldInfo fieldInfo){
  21. super(clazz, fieldInfo);
  22. Type fieldType = getFieldType();
  23. if (fieldType instanceof ParameterizedType) {
  24. this.itemType = ((ParameterizedType) getFieldType()).getActualTypeArguments()[0];
  25. } else {
  26. this.itemType = Object.class;
  27. }
  28. }
  29. public int getFastMatchToken() {
  30. return JSONToken.LBRACKET;
  31. }
  32. @SuppressWarnings("rawtypes")
  33. @Override
  34. public void parseField(DefaultJSONParser parser, Object object, Type objectType, Map<String, Object> fieldValues) {
  35. if (parser.getLexer().token() == JSONToken.NULL) {
  36. setValue(object, null);
  37. return;
  38. }
  39. ArrayList list = new ArrayList();
  40. ParseContext context = parser.getContext();
  41. parser.setContext(context, object, fieldInfo.getName());
  42. parseArray(parser, objectType, list);
  43. parser.setContext(context);
  44. if (object == null) {
  45. fieldValues.put(fieldInfo.getName(), list);
  46. } else {
  47. setValue(object, list);
  48. }
  49. }
  50. @SuppressWarnings({ "unchecked", "rawtypes" })
  51. public final void parseArray(DefaultJSONParser parser, Type objectType, Collection array) {
  52. Type itemType = this.itemType;;
  53. if (itemType instanceof TypeVariable && objectType instanceof ParameterizedType) {
  54. TypeVariable typeVar = (TypeVariable) itemType;
  55. ParameterizedType paramType = (ParameterizedType) objectType;
  56. Class<?> objectClass = null;
  57. if (paramType.getRawType() instanceof Class) {
  58. objectClass = (Class<?>) paramType.getRawType();
  59. }
  60. int paramIndex = -1;
  61. if (objectClass != null) {
  62. for (int i = 0, size = objectClass.getTypeParameters().length; i < size; ++i) {
  63. TypeVariable item = objectClass.getTypeParameters()[i];
  64. if (item.getName().equals(typeVar.getName())) {
  65. paramIndex = i;
  66. break;
  67. }
  68. }
  69. }
  70. if (paramIndex != -1) {
  71. itemType = paramType.getActualTypeArguments()[paramIndex];
  72. }
  73. }
  74. final JSONLexer lexer = parser.getLexer();
  75. if (lexer.token() != JSONToken.LBRACKET) {
  76. throw new JSONException("exepct '[', but " + JSONToken.name(lexer.token()));
  77. }
  78. if (deserializer == null) {
  79. deserializer = parser.getConfig().getDeserializer(itemType);
  80. itemFastMatchToken = deserializer.getFastMatchToken();
  81. }
  82. lexer.nextToken(itemFastMatchToken);
  83. for (int i = 0;; ++i) {
  84. if (lexer.isEnabled(Feature.AllowArbitraryCommas)) {
  85. while (lexer.token() == JSONToken.COMMA) {
  86. lexer.nextToken();
  87. continue;
  88. }
  89. }
  90. if (lexer.token() == JSONToken.RBRACKET) {
  91. break;
  92. }
  93. Object val = deserializer.deserialze(parser, itemType, i);
  94. array.add(val);
  95. parser.checkListResolve(array);
  96. if (lexer.token() == JSONToken.COMMA) {
  97. lexer.nextToken(itemFastMatchToken);
  98. continue;
  99. }
  100. }
  101. lexer.nextToken(JSONToken.COMMA);
  102. }
  103. }