public <T> T deserialize(InputStream stream, Class<T> targetClass) { if (! targetClass.equals(String.class)) { throw new IllegalArgumentException("Target class is not string"); } try { InputStreamReader reader = new InputStreamReader(stream, Charsets.UTF_8); return (T) CharStreams.toString(reader); } catch (Exception e) { throw new SerializationException("Failed while deserializing the buffer to type - " + targetClass, e); } }
@Override public void serialize(Object object, OutputStream stream) { try { stream.write(object.toString().getBytes(Charsets.UTF_8)); } catch (Exception e) { throw new SerializationException("Failed while serializing the object", e); } finally { closeStream(stream); } } }
public <T> T deserialize(InputStream stream, Class<T> targetClass) { try { return mapper.readValue(stream, targetClass); } catch (Exception e) { throw new SerializationException("Failed while deserializing the buffer to type - " + targetClass, e); } finally { closeStream(stream); } }
@Override public void serialize(Object object, OutputStream stream) { try { mapper.writeValue(stream, object); } catch (Exception e) { logger.error("Unable to serialize object", e); throw new SerializationException("Failed while serializing the object", e); } finally { closeStream(stream); } } }
@Override public <T extends Collection<E>, E> T deserializeCollection(InputStream stream, Class<T> collectionType, Class<E> elementType) { JavaType javaType = mapper.getTypeFactory().constructCollectionType(collectionType, elementType); try { return mapper.readValue(stream, javaType); } catch (Exception e) { throw new SerializationException("Failed while deserializing the buffer to type - " + javaType, e); } finally { closeStream(stream); } }