4
4
import java .lang .reflect .Array ;
5
5
import java .lang .reflect .Field ;
6
6
import java .util .ArrayList ;
7
+ import java .util .Collection ;
7
8
import java .util .Date ;
8
9
import java .util .HashMap ;
9
10
import java .util .LinkedList ;
@@ -76,14 +77,14 @@ public Object serialize(KsonContext ksonContext, Date value) {
76
77
}
77
78
78
79
@ Override
79
- public Date deserialize (KsonContext ksonContext , Object value ) {
80
+ public Date deserialize (KsonContext ksonContext , Class <?> fieldType , Object value ) {
80
81
return new Date ((Long ) value );
81
82
}
82
83
});
83
84
84
- this .registeredTransformers .put (List .class , new Transformer <ArrayList <?>>() {
85
+ this .registeredTransformers .put (Collection .class , new Transformer <Collection <?>>() {
85
86
@ Override
86
- public Object serialize (KsonContext ksonContext , ArrayList <?> value ) {
87
+ public Object serialize (KsonContext ksonContext , Collection <?> value ) {
87
88
KsonArray ksonArray = new KsonArray ();
88
89
89
90
for (Object object : value ) {
@@ -97,25 +98,32 @@ public Object serialize(KsonContext ksonContext, ArrayList<?> value) {
97
98
return ksonArray ;
98
99
}
99
100
101
+ @ SuppressWarnings ("deprecation" )
100
102
@ Override
101
- public ArrayList <?> deserialize (KsonContext ksonContext , Object value ) {
102
- ArrayList <Object > list = new ArrayList <Object >();
103
+ public Collection <?> deserialize (KsonContext ksonContext , Class <?> fieldType , Object value ) {
104
+ Collection <Object > collections = null ;
105
+
106
+ try {
107
+ collections = (Collection <Object >) fieldType .newInstance ();
108
+ } catch (Exception e ) {
109
+ e .printStackTrace ();
110
+ }
103
111
104
112
for (Object object : (KsonArray ) value ) {
105
113
try {
106
- list .add (ksonContext .addToObjectStack (object .getClass (), object ));
114
+ collections .add (ksonContext .addToObjectStack (object .getClass (), object ));
107
115
} catch (Exception e ) {
108
116
e .printStackTrace ();
109
117
}
110
118
}
111
119
112
- return list ;
120
+ return collections ;
113
121
}
114
122
});
115
123
116
- this .registeredTransformers .put (Map .class , new Transformer <HashMap <?, ?>>() {
124
+ this .registeredTransformers .put (Map .class , new Transformer <Map <?, ?>>() {
117
125
@ Override
118
- public Object serialize (KsonContext ksonContext , HashMap <?, ?> value ) {
126
+ public Object serialize (KsonContext ksonContext , Map <?, ?> value ) {
119
127
KsonObject ksonObject = new KsonObject ();
120
128
121
129
for (Object keyObject : value .keySet ()) {
@@ -133,22 +141,29 @@ public Object serialize(KsonContext ksonContext, HashMap<?, ?> value) {
133
141
return ksonObject ;
134
142
}
135
143
144
+ @ SuppressWarnings ("deprecation" )
136
145
@ Override
137
- public HashMap <?, ?> deserialize (KsonContext ksonContext , Object value ) {
138
- HashMap <Object , Object > hashMap = new HashMap <Object , Object >();
146
+ public Map <?, ?> deserialize (KsonContext ksonContext , Class <?> fieldType , Object value ) {
147
+ Map <Object , Object > map = null ;
148
+
149
+ try {
150
+ map = (Map <Object , Object >) fieldType .newInstance ();
151
+ } catch (Exception e ) {
152
+ e .printStackTrace ();
153
+ }
139
154
140
155
KsonObject ksonObject = (KsonObject ) value ;
141
156
for (Object keyObject : ksonObject .keySet ()) {
142
157
Object valueObject = ksonObject .get (keyObject );
143
158
144
159
try {
145
- hashMap .put (ksonContext .addToObjectStack (keyObject .getClass (), keyObject ), ksonContext .addToObjectStack (valueObject .getClass (), valueObject ));
160
+ map .put (ksonContext .addToObjectStack (keyObject .getClass (), keyObject ), ksonContext .addToObjectStack (valueObject .getClass (), valueObject ));
146
161
} catch (Exception e ) {
147
162
e .printStackTrace ();
148
163
}
149
164
}
150
165
151
- return hashMap ;
166
+ return map ;
152
167
}
153
168
});
154
169
@@ -270,6 +285,7 @@ public Object addToObjectStack(Class<?> clazz, Object object) throws Deserialize
270
285
try {
271
286
field .set (targetObject , createAtToObject (false , field .getType (), ksonValue .get (field .getName ())));
272
287
} catch (IllegalArgumentException | IllegalAccessException e ) {
288
+ e .printStackTrace ();
273
289
throw new DeserializeException ("Deserialize failed because can't access the field." );
274
290
}
275
291
}
@@ -324,8 +340,10 @@ private Object createAtToObject(boolean first, Class<?> type, Object originalVal
324
340
Transformer <Object > transformer = (Transformer <Object >) this .getTransformer (type );
325
341
326
342
if (transformer != null ) {
343
+ Class <?> realType = type ;
344
+
327
345
type = originalValue .getClass ();
328
- originalValue = transformer .deserialize (this , originalValue );
346
+ originalValue = transformer .deserialize (this , realType , originalValue );
329
347
}
330
348
}
331
349
0 commit comments