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.
{
  "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"
      }
    }
  }
}
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;
    }
}