public static String toJsonString ( Object obj , ClassLoader classLoader ) { if ( obj == null ) { return "null" ; } ClassLoader oldClassLoader = Thread . currentThread ( ) . getContextClassLoader ( ) ; if ( classLoader != null ) { Thread . currentThread ( ) . setContextClassLoader ( classLoader ) ; } try { ObjectMapper mapper = poolMapper . borrowObject ( ) ; if ( mapper != null ) { try { return mapper . writeValueAsString ( obj ) ; } finally { poolMapper . returnObject ( mapper ) ; } } throw new SerializationException ( "No ObjectMapper instance avaialble!" ) ; } catch ( Exception e ) { throw e instanceof SerializationException ? ( SerializationException ) e : new SerializationException ( e ) ; } finally { Thread . currentThread ( ) . setContextClassLoader ( oldClassLoader ) ; } }
Serialize an object to JSON string with a custom class loader .