@@ -442,52 +442,59 @@ public static void serialize(TypeLiteral typeLiteral, Object obj, OutputStream o
442442 }
443443
444444 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 );
458446 }
459447
460448 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 );
467450 }
468451
469452 public static String serialize (Object obj ) {
470- return serialize (JsoniterSpi . getCurrentConfig (). escapeUnicode (), obj .getClass (), obj );
453+ return serialize (obj .getClass (), obj );
471454 }
472455
473456 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
474463 JsoniterSpi .setCurrentConfig (config );
475464 try {
476- return serialize (config . escapeUnicode (), typeLiteral . getType () , obj );
465+ return serialize (type , obj );
477466 } finally {
478- JsoniterSpi .clearCurrentConfig ();
467+ // Revert old config
468+ JsoniterSpi .setCurrentConfig (configBackup );
479469 }
480470 }
481471
482472 public static String serialize (TypeLiteral typeLiteral , Object obj ) {
483- return serialize (JsoniterSpi . getCurrentConfig (). escapeUnicode (), typeLiteral .getType (), obj );
473+ return serialize (typeLiteral .getType (), obj );
484474 }
485475
486476 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 ();
488488 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+ }
491498 if (escapeUnicode ) {
492499 return new String (stream .buf , 0 , stream .count );
493500 } else {
0 commit comments