Created
September 16, 2013 21:32
-
-
Save jamiehuson/6586885 to your computer and use it in GitHub Desktop.
A Jackson Converter for use with Square's Tape queue library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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