Skip to content

Instantly share code, notes, and snippets.

@jamiehuson
Created September 16, 2013 21:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiehuson/6586885 to your computer and use it in GitHub Desktop.
Save jamiehuson/6586885 to your computer and use it in GitHub Desktop.
A Jackson Converter for use with Square's Tape queue library
public class JacksonConverter<T> implements FileObjectQueue.Converter<T> {
private final ObjectMapper mMapper;
private final Class<T> mType;
public JacksonConverter(ObjectMapper mapper, Class<T> type) {
this.mType = type;
this.mMapper = mapper;
}
@Override
public T from(byte[] bytes) {
if (bytes == null) {
Log.e(JacksonConverter.class.getName(), "byte[] is null");
return null;
}
if (bytes.length == 0) {
Log.w(JacksonConverter.class.getName(), "byte[] length is 0");
return null;
}
try {
return mMapper.readValue(bytes, mType);
} catch (IOException e) {
Log.e(JacksonConverter.class.getName(), "IOE " + e.getMessage());
}
return null;
}
@Override
public void toStream(T t, OutputStream outputStream) {
try {
mMapper.writeValue(outputStream, t);
} catch (IOException e) {
Log.e(JacksonConverter.class.getName(), "IOE " + e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment