@@ -442,52 +442,59 @@ public static void serialize(TypeLiteral typeLiteral, Object obj, OutputStream o
442
442
}
443
443
444
444
public static void serialize (Type type , Object obj , OutputStream out ) {
445
- JsonStream stream = JsonStreamPool .borrowJsonStream ();
446
- try {
447
- try {
448
- stream .reset (out );
449
- stream .writeVal (type , obj );
450
- } finally {
451
- stream .close ();
452
- }
453
- } catch (IOException e ) {
454
- throw new JsonException (e );
455
- } finally {
456
- JsonStreamPool .returnJsonStream (stream );
457
- }
445
+ serialize (type , obj , out , false );
458
446
}
459
447
460
448
public static String serialize (Config config , Object obj ) {
461
- JsoniterSpi .setCurrentConfig (config );
462
- try {
463
- return serialize (config .escapeUnicode (), obj .getClass (), obj );
464
- } finally {
465
- JsoniterSpi .clearCurrentConfig ();
466
- }
449
+ return serialize (config , obj .getClass (), obj );
467
450
}
468
451
469
452
public static String serialize (Object obj ) {
470
- return serialize (JsoniterSpi . getCurrentConfig (). escapeUnicode (), obj .getClass (), obj );
453
+ return serialize (obj .getClass (), obj );
471
454
}
472
455
473
456
public static String serialize (Config config , TypeLiteral typeLiteral , Object obj ) {
457
+ return serialize (config , typeLiteral .getType (), obj );
458
+ }
459
+
460
+ private static String serialize (Config config , Type type , Object obj ) {
461
+ final Config configBackup = JsoniterSpi .getCurrentConfig ();
462
+ // Set temporary config
474
463
JsoniterSpi .setCurrentConfig (config );
475
464
try {
476
- return serialize (config . escapeUnicode (), typeLiteral . getType () , obj );
465
+ return serialize (type , obj );
477
466
} finally {
478
- JsoniterSpi .clearCurrentConfig ();
467
+ // Revert old config
468
+ JsoniterSpi .setCurrentConfig (configBackup );
479
469
}
480
470
}
481
471
482
472
public static String serialize (TypeLiteral typeLiteral , Object obj ) {
483
- return serialize (JsoniterSpi . getCurrentConfig (). escapeUnicode (), typeLiteral .getType (), obj );
473
+ return serialize (typeLiteral .getType (), obj );
484
474
}
485
475
486
476
public static String serialize (boolean escapeUnicode , Type type , Object obj ) {
487
- JsonStream stream = JsonStreamPool .borrowJsonStream ();
477
+ final Config currentConfig = JsoniterSpi .getCurrentConfig ();
478
+ return serialize (currentConfig .copyBuilder ().escapeUnicode (escapeUnicode ).build (), type , obj );
479
+ }
480
+
481
+ private static String serialize (Type type , Object obj ) {
482
+ return serialize (type , obj , null , true );
483
+ }
484
+
485
+ private static String serialize (Type type , Object obj , OutputStream out , boolean returnObjAsString ) {
486
+ final JsonStream stream = JsonStreamPool .borrowJsonStream ();
487
+ final boolean escapeUnicode = JsoniterSpi .getCurrentConfig ().escapeUnicode ();
488
488
try {
489
- stream .reset (null );
490
- stream .writeVal (type , obj );
489
+ try {
490
+ stream .reset (out );
491
+ stream .writeVal (type , obj );
492
+ } finally {
493
+ stream .close ();
494
+ }
495
+ if (!returnObjAsString ) {
496
+ return "" ;
497
+ }
491
498
if (escapeUnicode ) {
492
499
return new String (stream .buf , 0 , stream .count );
493
500
} else {
0 commit comments