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

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 129 lines | 103 code | 26 blank | 0 comment | 32 complexity | 0c3bfe9073d3810bd0125d7da9f96cfc 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.HashMap;
  5. import java.util.Hashtable;
  6. import java.util.IdentityHashMap;
  7. import java.util.LinkedHashMap;
  8. import java.util.Map;
  9. import java.util.Properties;
  10. import java.util.SortedMap;
  11. import java.util.TreeMap;
  12. import java.util.concurrent.ConcurrentHashMap;
  13. import java.util.concurrent.ConcurrentMap;
  14. import com.alibaba.fastjson.JSONException;
  15. import com.alibaba.fastjson.parser.DefaultJSONParser;
  16. import com.alibaba.fastjson.parser.JSONLexer;
  17. import com.alibaba.fastjson.parser.JSONToken;
  18. import com.alibaba.fastjson.parser.ParseContext;
  19. import com.alibaba.fastjson.util.AntiCollisionHashMap;
  20. public class MapDeserializer implements ObjectDeserializer {
  21. public final static MapDeserializer instance = new MapDeserializer();
  22. @SuppressWarnings("unchecked")
  23. public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
  24. final JSONLexer lexer = parser.getLexer();
  25. if (lexer.token() == JSONToken.NULL) {
  26. lexer.nextToken(JSONToken.COMMA);
  27. return null;
  28. }
  29. Map<Object, Object> map = createMap(type);
  30. ParseContext context = parser.getContext();
  31. try {
  32. parser.setContext(context, map, fieldName);
  33. if (lexer.token() == JSONToken.RBRACE) {
  34. lexer.nextToken(JSONToken.COMMA);
  35. return (T) map;
  36. }
  37. return (T) deserialze(parser, type, fieldName, map);
  38. } finally {
  39. parser.setContext(context);
  40. }
  41. }
  42. @SuppressWarnings({ "rawtypes", "unchecked" })
  43. protected Object deserialze(DefaultJSONParser parser, Type type, Object fieldName, Map map) {
  44. if (type instanceof ParameterizedType) {
  45. ParameterizedType parameterizedType = (ParameterizedType) type;
  46. Type keyType = parameterizedType.getActualTypeArguments()[0];
  47. Type valueType = parameterizedType.getActualTypeArguments()[1];
  48. if (String.class == keyType) {
  49. return DefaultObjectDeserializer.instance.parseMap(parser, (Map<String, Object>) map, valueType, fieldName);
  50. } else {
  51. return DefaultObjectDeserializer.instance.parseMap(parser, map, keyType, valueType, fieldName);
  52. }
  53. } else {
  54. return parser.parseObject(map, fieldName);
  55. }
  56. }
  57. @SuppressWarnings({ "unchecked", "rawtypes" })
  58. protected Map<Object, Object> createMap(Type type) {
  59. if (type == Properties.class) {
  60. return new Properties();
  61. }
  62. if (type == Hashtable.class) {
  63. return new Hashtable();
  64. }
  65. if (type == IdentityHashMap.class) {
  66. return new IdentityHashMap();
  67. }
  68. if (type == SortedMap.class || type == TreeMap.class) {
  69. return new TreeMap();
  70. }
  71. if (type == ConcurrentMap.class || type == ConcurrentHashMap.class) {
  72. return new ConcurrentHashMap();
  73. }
  74. if (type == Map.class ) {
  75. return new AntiCollisionHashMap();
  76. }
  77. if(type == HashMap.class){ //
  78. return new HashMap();
  79. }
  80. if (type == LinkedHashMap.class) {
  81. return new LinkedHashMap();
  82. }
  83. if (type instanceof ParameterizedType) {
  84. ParameterizedType parameterizedType = (ParameterizedType) type;
  85. return createMap(parameterizedType.getRawType());
  86. }
  87. if (type instanceof Class<?>) {
  88. Class<?> clazz = (Class<?>) type;
  89. if (clazz.isInterface()) {
  90. throw new JSONException("unsupport type " + type);
  91. }
  92. try {
  93. return (Map<Object, Object>) clazz.newInstance();
  94. } catch (Exception e) {
  95. throw new JSONException("unsupport type " + type, e);
  96. }
  97. }
  98. throw new JSONException("unsupport type " + type);
  99. }
  100. public int getFastMatchToken() {
  101. return JSONToken.LBRACE;
  102. }
  103. }