View Javadoc

1   package com.atlassian.messagequeue.internal.core;
2   
3   import com.atlassian.messagequeue.MessageSerializationException;
4   import org.codehaus.jackson.annotate.JsonAutoDetect;
5   import org.codehaus.jackson.annotate.JsonMethod;
6   import org.codehaus.jackson.map.ObjectMapper;
7   
8   import java.io.IOException;
9   
10  /**
11   * Implementation using Jackson (https://github.com/FasterXML/jackson).
12   */
13  public class JacksonNestedMessageSerializer implements NestedMessageSerializer {
14      private final ObjectMapper objectMapper;
15  
16      public JacksonNestedMessageSerializer() {
17          this.objectMapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, JsonAutoDetect.Visibility.ANY);
18  
19      }
20  
21      @Override
22      public String serialize(NestedMessage nestedMessage) {
23          try {
24              return objectMapper.writeValueAsString(nestedMessage);
25          } catch (IOException e) {
26              throw new MessageSerializationException("Error serializing nested message", e);
27          }
28      }
29  
30      @Override
31      public NestedMessage deserialize(String string) {
32          try {
33              return objectMapper.readValue(string, NestedMessage.class);
34          } catch (IOException e) {
35              throw new MessageSerializationException("Error deserializing nested message", e);
36          }
37      }
38  }