Friday, August 29, 2014

Creating Custom Serializer and Deserializer with Gson

Gson is a cool JSON library by Google. It can convert Java objects into their JSON representation and vice versa. In this blog, I'm going to show how to write custom serializer and deserializer with Gson for the following JSON structure.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
  "root": {
    "node1-a": {
      "node2-a": {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3"
      },
      "node2-b": {
        "key4": "value4",
        "key5": "value5"
      }
    },
    "node1-b": {
      "node2-a": {
        "key1": "value1",
        "key2": "value2"
      }
    },
    "node1-c": {
      "node2-a": {
        "key1": "value1"
      }
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
public class Root {
    public final Map<String, Node1> map = new LinkedHashMap<String, Node1>();
 
    public Root put(String key, Node1 node) {
        map.put(key, node);
        return this;
    }
 
    /**
     * This is the next at level 1
     */
    public static class Node1 {
        public final Map<String, Node2> map = new LinkedHashMap<String, Node2>();
 
        public Node1 put(String key, Node2 node) {
            map.put(key, node);
            return this;
        }
 
        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder();
            builder.append("Node1 [map=");
            builder.append(map);
            builder.append("]");
            return builder.toString();
        }
    }
 
    /**
     * This is the node at level 2
     */
    public static class Node2 {
        public final Map<String, String> map = new LinkedHashMap<String, String>();
 
        public Node2 put(String key, String value) {
            map.put(key, value);
            return this;
        }
 
        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder();
            builder.append("Node2 [map=");
            builder.append(map);
            builder.append("]");
            return builder.toString();
        }
    }
 
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Root [map=");
        builder.append(map);
        builder.append("]");
        return builder.toString();
    }
}
 
public class Main {
    public static void main(String[] args) throws Exception {
        Gson gson = new GsonBuilder().setPrettyPrinting()
            .registerTypeAdapter(Root.class, new RootSerializer())
            .registerTypeAdapter(Root.class, new RootDeserializer())
            .create();
         
        Root root = new Root()
            .put("node1-a", new Node1()
                .put("node2-a", new Node2()
                    .put("key1", "value1")
                    .put("key2", "value2")
                    .put("key3", "value3"))
                .put("node2-b", new Node2()
                    .put("key4", "value4")
                    .put("key5", "value5")))
            .put("node1-b", new Node1()
                .put("node2-a", new Node2()
                    .put("key1", "value1")
                    .put("key2", "value2")))
            .put("node1-c", new Node1()
                .put("node2-a", new Node2()
                    .put("key1", "value1")));
         
        gson.toJson(root, System.out);
         
        System.out.println();
         
        String json = String.join("\n", Files.readAllLines(Paths.get("test.json")));
        root = gson.fromJson(json, Root.class);
        System.out.println(root);
    }
}
 
public class RootSerializer implements JsonSerializer<Root> {
    @Override
    public JsonElement serialize(Root root, Type typeOfSrc,
        JsonSerializationContext context) {
        JsonObject node1JsonObj = new JsonObject();
        for (Map.Entry<String, Node1> n1Entry : root.map.entrySet()) {
            JsonObject node2JsonObj = new JsonObject();
            for (Map.Entry<String, Node2> n2Entry : n1Entry.getValue().map.entrySet()) {
                JsonObject jsonObj = new JsonObject();
                for (Map.Entry<String, String> e : n2Entry.getValue().map.entrySet()) {
                    jsonObj.addProperty(e.getKey(), e.getValue());
                }
                node2JsonObj.add(n2Entry.getKey(), jsonObj);
            }
            node1JsonObj.add(n1Entry.getKey(), node2JsonObj);
        }
         
        JsonObject rootJsonObject = new JsonObject();
        rootJsonObject.add("root", node1JsonObj);
        return rootJsonObject;
    }
}
 
public class RootDeserializer implements JsonDeserializer<Root> {
    @Override
    public Root deserialize(JsonElement rootJson, Type typeOfT,
        JsonDeserializationContext context) throws JsonParseException {
        Root root = new Root();
        JsonObject rootJsonObj = rootJson.getAsJsonObject();
        for (Map.Entry<String, JsonElement> rootJsonEntry : rootJsonObj.entrySet()) {
            JsonObject node1JsonObj = rootJsonEntry.getValue().getAsJsonObject();
            Node1 node1 = new Node1();
            for (Map.Entry<String, JsonElement> n1JsonEntry : node1JsonObj.entrySet()) {
                JsonObject node2JsonObj = n1JsonEntry.getValue().getAsJsonObject();
                Node2 node2 = new Node2();
                for (Map.Entry<String, JsonElement> n2JsonEntry : node2JsonObj.entrySet()) {
                    JsonObject jsonObj = n2JsonEntry.getValue().getAsJsonObject();
                    for (Map.Entry<String, JsonElement> e : jsonObj.entrySet()) {
                        node2.put(e.getKey(), e.getValue().getAsString());
                    }
                }
                node1.put(n1JsonEntry.getKey(), node2);
            }
            root.put(rootJsonEntry.getKey(), node1);
        }
        return root;
    }
}