View Javadoc

1   package com.atlassian.messagequeue.internal.core;
2   
3   import com.atlassian.messagequeue.MessageSerializationException;
4   import com.google.gson.Gson;
5   import com.google.gson.GsonBuilder;
6   import com.google.gson.JsonSyntaxException;
7   
8   /**
9    * Implementation using Gson (https://github.com/google/gson).
10   */
11  public class GsonNestedMessageSerializer implements NestedMessageSerializer {
12      private final Gson gson;
13  
14      public GsonNestedMessageSerializer() {
15          this.gson = new GsonBuilder().serializeNulls().create();
16      }
17  
18      @Override
19      public String serialize(NestedMessage message) {
20          try {
21              return gson.toJson(message);
22          } catch (Exception e) {
23              throw new MessageSerializationException("Serialization error", e);
24          }
25      }
26  
27      @Override
28      public NestedMessage deserialize(String string) {
29          try {
30              return gson.fromJson(string, NestedMessage.class);
31          } catch (JsonSyntaxException e) {
32              throw new MessageSerializationException("Deserialization error", e);
33          }
34      }
35  }