public <T> int serialize(T obj, Class<T> type, OutputStream out) throws IOException {
out = new CountingOutputStream(new BufferedOutputStream(out, getBufferSize()));
try {
ObjectWriter writer = _objectMapper.writerWithType(type);
if (isPrettyPrint()) {
writer = writer.withDefaultPrettyPrinter();
}
writer.writeValue(out, obj);
} finally {
if (isCloseEnabled()) {
out.close();