All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ssf4j.jackson.JacksonSerializer Maven / Gradle / Ivy

There is a newer version: 0.4
Show newest version
package org.ssf4j.jackson;

import java.io.IOException;
import java.io.OutputStream;

import org.ssf4j.Serializer;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonSerializer implements Serializer {

	protected JacksonSerialization serde;
	protected OutputStream out;
	protected ObjectMapper mapper;
	
	public JacksonSerializer(JacksonSerialization serde, OutputStream out, ObjectMapper mapper) throws IOException {
		this.serde = serde;
		this.out = out;
		this.mapper = mapper != null ? mapper : new ObjectMapper();
	}
	
	@Override
	public void flush() throws IOException {
		out.flush();
	}

	@Override
	public void close() throws IOException {
		flush();
		out.close();
	}

	@Override
	public void write(T object) throws IOException {
		serde.getLock().lock();
		try {
			mapper.writeValue(out, object);
		} finally {
			serde.getLock().unlock();
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy