1 package net.minidev.json.writer;
2
3
18
19 import java.lang.reflect.Type;
20 import java.util.Date;
21 import java.util.HashMap;
22
23 import net.minidev.asm.Accessor;
24 import net.minidev.asm.BeansAccess;
25 import net.minidev.asm.ConvertDate;
26 import net.minidev.json.JSONUtil;
27
28 @SuppressWarnings("unchecked")
29 public abstract class BeansMapper<T> extends JsonReaderI<T> {
30
31 public BeansMapper(JsonReader base) {
32 super(base);
33 }
34
35 public abstract Object getValue(Object current, String key);
36
37 public static class Bean<T> extends JsonReaderI<T> {
38 final Class<T> clz;
39 final BeansAccess<T> ba;
40 final HashMap<String, Accessor> index;
41
42 public Bean(JsonReader base, Class<T> clz) {
43 super(base);
44 this.clz = clz;
45 this.ba = BeansAccess.get(clz, JSONUtil.JSON_SMART_FIELD_FILTER);
46 this.index = ba.getMap();
47 }
48
49 @Override
50 public void setValue(Object current, String key, Object value) {
51 ba.set((T) current, key, value);
52
53
54
55
56
57
58 }
59
60 public Object getValue(Object current, String key) {
61 return ba.get((T) current, key);
62
63
64
65
66
67 }
68
69 @Override
70 public Type getType(String key) {
71 Accessor nfo = index.get(key);
72 return nfo.getGenericType();
73 }
74
75 @Override
76 public JsonReaderI<?> startArray(String key) {
77 Accessor nfo = index.get(key);
78 if (nfo == null)
79 throw new RuntimeException("Can not find Array '" + key + "' field in " + clz);
80 return base.getMapper(nfo.getGenericType());
81 }
82
83 @Override
84 public JsonReaderI<?> startObject(String key) {
85 Accessor f = index.get(key);
86 if (f == null)
87 throw new RuntimeException("Can not find Object '" + key + "' field in " + clz);
88 return base.getMapper(f.getGenericType());
89 }
90
91 @Override
92 public Object createObject() {
93 return ba.newInstance();
94 }
95 }
96
97 public static class BeanNoConv<T> extends JsonReaderI<T> {
98 final Class<T> clz;
99 final BeansAccess<T> ba;
100 final HashMap<String, Accessor> index;
101
102 public BeanNoConv(JsonReader base, Class<T> clz) {
103 super(base);
104 this.clz = clz;
105 this.ba = BeansAccess.get(clz, JSONUtil.JSON_SMART_FIELD_FILTER);
106 this.index = ba.getMap();
107 }
108
109 @Override
110 public void setValue(Object current, String key, Object value) {
111 ba.set((T) current, key, value);
112 }
113
114 public Object getValue(Object current, String key) {
115 return ba.get((T) current, key);
116 }
117
118 @Override
119 public Type getType(String key) {
120 Accessor nfo = index.get(key);
121 return nfo.getGenericType();
122 }
123
124 @Override
125 public JsonReaderI<?> startArray(String key) {
126 Accessor nfo = index.get(key);
127 if (nfo == null)
128 throw new RuntimeException("Can not set " + key + " field in " + clz);
129 return base.getMapper(nfo.getGenericType());
130 }
131
132 @Override
133 public JsonReaderI<?> startObject(String key) {
134 Accessor f = index.get(key);
135 if (f == null)
136 throw new RuntimeException("Can not set " + key + " field in " + clz);
137 return base.getMapper(f.getGenericType());
138 }
139
140 @Override
141 public Object createObject() {
142 return ba.newInstance();
143 }
144 }
145
146 public static JsonReaderI<Date> MAPPER_DATE = new ArraysMapper<Date>(null) {
147 @Override
148 public Date convert(Object current) {
149 return ConvertDate.convertToDate(current);
150 }
151 };
152
153 }
154