Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.hazelcast.nio.DataSerializable;
- import java.io.DataInput;
- import java.io.DataOutput;
- import java.io.IOException;
- import javax.inject.Inject;
- import javax.inject.Named;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- /**
- * Common serialization/deserialization using Jackson (JSON).
- * Uses binary (Smile) format to implement {@link DataSerializable}.
- * Exposes both JSON- and Smile-oriented ObjectMappers to subclasses.
- */
- public class JsonDataSerializable implements DataSerializable {
- final static Logger logger = LoggerFactory.getLogger(JsonDataSerializable.class);
- public void readData(DataInput input) throws IOException {
- int length = input.readInt();
- byte[] bytes = new byte[length];
- input.readFully(bytes);
- try {
- smileObjectMapper.readerForUpdating(this).readValue(bytes);
- } catch (IOException ex) {
- logger.warn("Error reading DataSerializable from this tree: " +
- objectMapper
- .writerWithDefaultPrettyPrinter()
- .writeValueAsString(smileObjectMapper.readTree(bytes))
- );
- throw ex;
- }
- }
- public void writeData(DataOutput output) throws IOException {
- byte[] bytes = smileObjectMapper.writeValueAsBytes(this);
- output.writeInt(bytes.length);
- output.write(bytes);
- }
- protected static ObjectMapper objectMapper() {
- return objectMapper;
- }
- protected static ObjectMapper smileObjectMapper() {
- return smileObjectMapper;
- }
- @Inject static volatile ObjectMapper objectMapper;
- @Inject @Named(JsonModule.SMILE) static volatile ObjectMapper smileObjectMapper;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement