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

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 109 lines | 86 code | 23 blank | 0 comment | 34 complexity | f01354a45883cc8b6a301ae822b899fe 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.util.ArrayList;
  5. import java.util.Collection;
  6. import java.util.HashSet;
  7. import java.util.Set;
  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. public class ArrayListStringDeserializer implements ObjectDeserializer {
  14. public final static ArrayListStringDeserializer instance = new ArrayListStringDeserializer();
  15. @SuppressWarnings({ "unchecked", "rawtypes" })
  16. public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
  17. final JSONLexer lexer = parser.getLexer();
  18. if (lexer.token() == JSONToken.NULL) {
  19. lexer.nextToken(JSONToken.COMMA);
  20. return null;
  21. }
  22. Collection array = null;
  23. if (type == Set.class || type == HashSet.class) {
  24. array = new HashSet();
  25. } else {
  26. if (type instanceof ParameterizedType) {
  27. Type rawType = ((ParameterizedType) type).getRawType();
  28. if (rawType == Set.class || rawType == HashSet.class) {
  29. array = new HashSet();
  30. }
  31. }
  32. }
  33. if (array == null) {
  34. array = new ArrayList();
  35. }
  36. parseArray(parser, array);
  37. return (T) array;
  38. }
  39. @SuppressWarnings({ "rawtypes", "unchecked" })
  40. public static void parseArray(DefaultJSONParser parser, Collection array) {
  41. JSONLexer lexer = parser.getLexer();
  42. if (lexer.token() == JSONToken.NULL) {
  43. lexer.nextToken(JSONToken.COMMA);
  44. return;
  45. }
  46. if (lexer.token() == JSONToken.SET) {
  47. lexer.nextToken();
  48. }
  49. if (lexer.token() != JSONToken.LBRACKET) {
  50. throw new JSONException("exepct '[', but " + lexer.token());
  51. }
  52. lexer.nextToken(JSONToken.LITERAL_STRING);
  53. for (;;) {
  54. if (lexer.isEnabled(Feature.AllowArbitraryCommas)) {
  55. while (lexer.token() == JSONToken.COMMA) {
  56. lexer.nextToken();
  57. continue;
  58. }
  59. }
  60. if (lexer.token() == JSONToken.RBRACKET) {
  61. break;
  62. }
  63. String value;
  64. if (lexer.token() == JSONToken.LITERAL_STRING) {
  65. value = lexer.stringVal();
  66. lexer.nextToken(JSONToken.COMMA);
  67. } else {
  68. Object obj = parser.parse();
  69. if (obj == null) {
  70. value = null;
  71. } else {
  72. value = obj.toString();
  73. }
  74. }
  75. array.add(value);
  76. if (lexer.token() == JSONToken.COMMA) {
  77. lexer.nextToken(JSONToken.LITERAL_STRING);
  78. continue;
  79. }
  80. }
  81. lexer.nextToken(JSONToken.COMMA);
  82. }
  83. public int getFastMatchToken() {
  84. return JSONToken.LBRACKET;
  85. }
  86. }