15
15
import com .realtimetech .kson .element .KsonArray ;
16
16
import com .realtimetech .kson .element .KsonObject ;
17
17
import com .realtimetech .kson .element .KsonValue ;
18
- import com .realtimetech .kson .stack .FastStack ;
19
- import com .realtimetech .kson .string .StringMaker ;
18
+ import com .realtimetech .kson .exception .DeserializeException ;
19
+ import com .realtimetech .kson .exception .SerializeException ;
20
+ import com .realtimetech .kson .util .stack .FastStack ;
21
+ import com .realtimetech .kson .util .string .StringMaker ;
20
22
import com .realtimetech .kson .transform .Transformer ;
21
23
import com .realtimetech .reflection .access .ArrayAccessor ;
22
24
import com .realtimetech .reflection .allocate .UnsafeAllocator ;
@@ -66,7 +68,6 @@ public KsonContext(int stackSize, int stringBufferSize) {
66
68
this .primaryObjects = new HashMap <Class <?>, HashMap <Object , Object >>();
67
69
68
70
this .cachedFields = new HashMap <Class <?>, Field []>();
69
-
70
71
71
72
this .registeredTransformers .put (Date .class , new Transformer <Date >() {
72
73
@ Override
@@ -88,7 +89,7 @@ public Object serialize(KsonContext ksonContext, ArrayList<?> value) {
88
89
for (Object object : value ) {
89
90
try {
90
91
ksonArray .add (ksonContext .addFromObjectStack (object ));
91
- } catch (IllegalArgumentException | IllegalAccessException | IOException e ) {
92
+ } catch (SerializeException e ) {
92
93
e .printStackTrace ();
93
94
}
94
95
}
@@ -121,10 +122,10 @@ public Object serialize(KsonContext ksonContext, HashMap<?, ?> value) {
121
122
Object valueObject = value .get (keyObject );
122
123
123
124
try {
124
- Object tryFromObject = ksonContext .addFromObjectStack (keyObject );
125
- Object tryFromObject2 = ksonContext .addFromObjectStack (valueObject );
126
- ksonObject .put (tryFromObject , tryFromObject2 );
127
- } catch (IllegalArgumentException | IllegalAccessException | IOException e ) {
125
+ Object keyKson = ksonContext .addFromObjectStack (keyObject );
126
+ Object valueKson = ksonContext .addFromObjectStack (valueObject );
127
+ ksonObject .put (keyKson , valueKson );
128
+ } catch (SerializeException e ) {
128
129
e .printStackTrace ();
129
130
}
130
131
}
@@ -196,14 +197,6 @@ private boolean isNeedSerialize(Class<?> clazz) {
196
197
return true ;
197
198
}
198
199
199
- public <T > T toObject (Class <T > clazz , Object object ) throws Exception {
200
- if (object instanceof KsonValue ) {
201
- return (T ) this .addToObjectStack (clazz , (KsonValue ) object );
202
- }
203
-
204
- return (T ) object ;
205
- }
206
-
207
200
public Transformer <?> getTransformer (Class <?> type ) {
208
201
if (!this .transformers .containsKey (type )) {
209
202
boolean matched = false ;
@@ -246,11 +239,19 @@ public Field getPrimaryKeyField(Class<?> type) {
246
239
return this .primaryKeys .get (type );
247
240
}
248
241
249
- public Object addToObjectStack (Object object ) throws Exception {
242
+ public <T > T toObject (Class <T > clazz , Object object ) throws DeserializeException {
243
+ if (object instanceof KsonValue ) {
244
+ return (T ) this .addToObjectStack (clazz , (KsonValue ) object );
245
+ }
246
+
247
+ return (T ) object ;
248
+ }
249
+
250
+ public Object addToObjectStack (Object object ) throws DeserializeException {
250
251
return this .addToObjectStack (Object .class , object );
251
252
}
252
253
253
- public Object addToObjectStack (Class <?> clazz , Object object ) throws Exception {
254
+ public Object addToObjectStack (Class <?> clazz , Object object ) throws DeserializeException {
254
255
boolean needLoop = this .objectStack .isEmpty ();
255
256
256
257
Object result = this .createAtToObject (true , clazz , object );
@@ -266,7 +267,11 @@ public Object addToObjectStack(Class<?> clazz, Object object) throws Exception {
266
267
KsonObject ksonValue = (KsonObject ) targetKson ;
267
268
268
269
for (Field field : this .getAccessibleFields (targetObjectClass )) {
269
- field .set (targetObject , createAtToObject (false , field .getType (), ksonValue .get (field .getName ())));
270
+ try {
271
+ field .set (targetObject , createAtToObject (false , field .getType (), ksonValue .get (field .getName ())));
272
+ } catch (IllegalArgumentException | IllegalAccessException e ) {
273
+ throw new DeserializeException ("Deserialize failed because can't access the field." );
274
+ }
270
275
}
271
276
} else {
272
277
KsonArray ksonValue = (KsonArray ) targetKson ;
@@ -291,14 +296,18 @@ public Object addToObjectStack(Class<?> clazz, Object object) throws Exception {
291
296
return result ;
292
297
}
293
298
294
- private Object createAtToObject (boolean first , Class <?> type , Object originalValue ) throws Exception {
299
+ private Object createAtToObject (boolean first , Class <?> type , Object originalValue ) throws DeserializeException {
295
300
Object primaryId = null ;
296
301
297
302
if (originalValue instanceof KsonObject ) {
298
303
KsonObject wrappingObject = (KsonObject ) originalValue ;
299
304
300
305
if (wrappingObject .containsKey ("#class" )) {
301
- type = Class .forName (wrappingObject .get ("#class" ).toString ());
306
+ try {
307
+ type = Class .forName (wrappingObject .get ("#class" ).toString ());
308
+ } catch (ClassNotFoundException e ) {
309
+ throw new DeserializeException ("Deserialize failed because can't find target class." );
310
+ }
302
311
originalValue = wrappingObject .get ("#data" );
303
312
} else if (wrappingObject .containsKey ("@id" )) {
304
313
primaryId = wrappingObject .get ("@id" );
@@ -336,7 +345,11 @@ private Object createAtToObject(boolean first, Class<?> type, Object originalVal
336
345
convertedValue = Array .newInstance (componentType , ksonArray .size ());
337
346
} else if (convertedValue instanceof KsonObject ) {
338
347
if (primaryId == null ) {
339
- convertedValue = UnsafeAllocator .newInstance (type );
348
+ try {
349
+ convertedValue = UnsafeAllocator .newInstance (type );
350
+ } catch (Exception e ) {
351
+ throw new DeserializeException ("Deserialize failed because can't allocation object." );
352
+ }
340
353
} else {
341
354
if (!this .primaryObjects .containsKey (type )) {
342
355
this .primaryObjects .put (type , new HashMap <Object , Object >());
@@ -345,7 +358,11 @@ private Object createAtToObject(boolean first, Class<?> type, Object originalVal
345
358
HashMap <Object , Object > hashMap = this .primaryObjects .get (type );
346
359
347
360
if (!hashMap .containsKey (primaryId )) {
348
- hashMap .put (primaryId , UnsafeAllocator .newInstance (type ));
361
+ try {
362
+ hashMap .put (primaryId , UnsafeAllocator .newInstance (type ));
363
+ } catch (Exception e ) {
364
+ throw new DeserializeException ("Deserialize failed because can't allocation primary object." );
365
+ }
349
366
}
350
367
351
368
convertedValue = hashMap .get (primaryId );
@@ -362,15 +379,15 @@ private Object createAtToObject(boolean first, Class<?> type, Object originalVal
362
379
return convertedValue ;
363
380
}
364
381
365
- public KsonValue fromObject (Object object ) throws IOException , IllegalArgumentException , IllegalAccessException {
382
+ public KsonValue fromObject (Object object ) throws SerializeException {
366
383
if (this .objectStack .isEmpty ()) {
367
384
return (KsonValue ) addFromObjectStack (object );
368
385
} else {
369
- throw new IllegalAccessException ("This context already running parse !" );
386
+ throw new SerializeException ("This context already running serialize !" );
370
387
}
371
388
}
372
389
373
- public Object addFromObjectStack (Object object ) throws IOException , IllegalArgumentException , IllegalAccessException {
390
+ public Object addFromObjectStack (Object object ) throws SerializeException {
374
391
boolean needLoop = this .objectStack .isEmpty ();
375
392
376
393
Object result = null ;
@@ -386,7 +403,11 @@ public Object addFromObjectStack(Object object) throws IOException, IllegalArgum
386
403
KsonObject ksonValue = (KsonObject ) targetKson ;
387
404
388
405
for (Field field : this .getAccessibleFields (targetObject .getClass ())) {
389
- ksonValue .put (field .getName (), this .createAtFromObject (false , field .getType (), field .get (targetObject )));
406
+ try {
407
+ ksonValue .put (field .getName (), this .createAtFromObject (false , field .getType (), field .get (targetObject )));
408
+ } catch (IllegalArgumentException | IllegalAccessException e ) {
409
+ throw new SerializeException ("Serialize failed because object could can't get from field." );
410
+ }
390
411
}
391
412
} else {
392
413
KsonArray ksonValue = (KsonArray ) targetKson ;
@@ -413,7 +434,7 @@ public Object addFromObjectStack(Object object) throws IOException, IllegalArgum
413
434
return result ;
414
435
}
415
436
416
- private Object createAtFromObject (boolean first , Class <?> type , Object originalValue ) {
437
+ private Object createAtFromObject (boolean first , Class <?> type , Object originalValue ) throws SerializeException {
417
438
if (originalValue == null )
418
439
return null ;
419
440
@@ -429,15 +450,15 @@ private Object createAtFromObject(boolean first, Class<?> type, Object originalV
429
450
Field primaryKeyField = getPrimaryKeyField (originalValueType );
430
451
431
452
if (primaryKeyField != null ) {
432
- try {
433
- KsonObject wrappingObject = new KsonObject ();
453
+ KsonObject wrappingObject = new KsonObject ();
434
454
455
+ try {
435
456
wrappingObject .put ("@id" , primaryKeyField .get (originalValue ));
436
-
437
- originalValue = wrappingObject ;
438
457
} catch (IllegalArgumentException | IllegalAccessException e ) {
439
- e . printStackTrace ( );
458
+ throw new SerializeException ( "Serialize failed because primary key can't get from field." );
440
459
}
460
+
461
+ originalValue = wrappingObject ;
441
462
}
442
463
}
443
464
0 commit comments