1 package net.minidev.json.writer;
2
3
18 import java.lang.reflect.ParameterizedType;
19 import java.lang.reflect.Type;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.concurrent.ConcurrentHashMap;
24
25 import net.minidev.json.JSONArray;
26 import net.minidev.json.JSONAware;
27 import net.minidev.json.JSONAwareEx;
28 import net.minidev.json.JSONObject;
29
30 public class JsonReader {
31 private final ConcurrentHashMap<Type, JsonReaderI<?>> cache;
32
33 public JsonReaderI<JSONAwareEx> DEFAULT;
34 public JsonReaderI<JSONAwareEx> DEFAULT_ORDERED;
35
36 public JsonReader() {
37 cache = new ConcurrentHashMap<Type, JsonReaderI<?>>(100);
38
39 cache.put(Date.class, BeansMapper.MAPPER_DATE);
40
41 cache.put(int[].class, ArraysMapper.MAPPER_PRIM_INT);
42 cache.put(Integer[].class, ArraysMapper.MAPPER_INT);
43
44 cache.put(short[].class, ArraysMapper.MAPPER_PRIM_INT);
45 cache.put(Short[].class, ArraysMapper.MAPPER_INT);
46
47 cache.put(long[].class, ArraysMapper.MAPPER_PRIM_LONG);
48 cache.put(Long[].class, ArraysMapper.MAPPER_LONG);
49
50 cache.put(byte[].class, ArraysMapper.MAPPER_PRIM_BYTE);
51 cache.put(Byte[].class, ArraysMapper.MAPPER_BYTE);
52
53 cache.put(char[].class, ArraysMapper.MAPPER_PRIM_CHAR);
54 cache.put(Character[].class, ArraysMapper.MAPPER_CHAR);
55
56 cache.put(float[].class, ArraysMapper.MAPPER_PRIM_FLOAT);
57 cache.put(Float[].class, ArraysMapper.MAPPER_FLOAT);
58
59 cache.put(double[].class, ArraysMapper.MAPPER_PRIM_DOUBLE);
60 cache.put(Double[].class, ArraysMapper.MAPPER_DOUBLE);
61
62 cache.put(boolean[].class, ArraysMapper.MAPPER_PRIM_BOOL);
63 cache.put(Boolean[].class, ArraysMapper.MAPPER_BOOL);
64
65 this.DEFAULT = new DefaultMapper<JSONAwareEx>(this);
66 this.DEFAULT_ORDERED = new DefaultMapperOrdered(this);
67
68 cache.put(JSONAwareEx.class, this.DEFAULT);
69 cache.put(JSONAware.class, this.DEFAULT);
70 cache.put(JSONArray.class, this.DEFAULT);
71 cache.put(JSONObject.class, this.DEFAULT);
72 }
73
74
83 public <T> void remapField(Class<T> type, String fromJson, String toJava) {
84 JsonReaderI<T> map = this.getMapper(type);
85 if (!(map instanceof MapperRemapped)) {
86 map = new MapperRemapped<T>(map);
87 registerReader(type, map);
88 }
89 ((MapperRemapped<T>) map).renameField(fromJson, toJava);
90 }
91
92 public <T> void registerReader(Class<T> type, JsonReaderI<T> mapper) {
93 cache.put(type, mapper);
94 }
95
96 @SuppressWarnings("unchecked")
97 public <T> JsonReaderI<T> getMapper(Type type) {
98 if (type instanceof ParameterizedType)
99 return getMapper((ParameterizedType) type);
100 return getMapper((Class<T>) type);
101 }
102
103
109 public <T> JsonReaderI<T> getMapper(Class<T> type) {
110
111 @SuppressWarnings("unchecked")
112 JsonReaderI<T> map = (JsonReaderI<T>) cache.get(type);
113 if (map != null)
114 return map;
115
118 if (type instanceof Class) {
119 if (Map.class.isAssignableFrom(type))
120 map = new DefaultMapperCollection<T>(this, type);
121 else if (List.class.isAssignableFrom(type))
122 map = new DefaultMapperCollection<T>(this, type);
123 if (map != null) {
124 cache.put(type, map);
125 return map;
126 }
127 }
128
129 if (type.isArray())
130 map = new ArraysMapper.GenericMapper<T>(this, type);
131 else if (List.class.isAssignableFrom(type))
132 map = new CollectionMapper.ListClass<T>(this, type);
133 else if (Map.class.isAssignableFrom(type))
134 map = new CollectionMapper.MapClass<T>(this, type);
135 else
136
137 map = new BeansMapper.Bean<T>(this, type);
138 cache.putIfAbsent(type, map);
139 return map;
140 }
141
142 @SuppressWarnings("unchecked")
143 public <T> JsonReaderI<T> getMapper(ParameterizedType type) {
144 JsonReaderI<T> map = (JsonReaderI<T>) cache.get(type);
145 if (map != null)
146 return map;
147 Class<T> clz = (Class<T>) type.getRawType();
148 if (List.class.isAssignableFrom(clz))
149 map = new CollectionMapper.ListType<T>(this, type);
150 else if (Map.class.isAssignableFrom(clz))
151 map = new CollectionMapper.MapType<T>(this, type);
152 cache.putIfAbsent(type, map);
153 return map;
154 }
155 }
156