package com.jenjinstudios.io.serialization;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.jenjinstudios.io.Message;

import java.lang.reflect.Type;

/**
 * Used to serialize Messages with Gson.
 *
 * @author Caleb Brinkman
 */
public class GsonMessageSerializer implements JsonSerializer<Message>
{
    @Override
    public JsonElement serialize(Message src, Type typeOfSrc, JsonSerializationContext context) {
        JsonElement fields = context.serialize(src, src.getClass());
        JsonObject message = new JsonObject();
        message.addProperty("class", src.getClass().getName());
        message.add("fields", fields);
        return message;
    }
}

@@ -238,190 +238,1813 @@
 ge;%0A
-%0Aimport java.lang.reflect.Type;%0A%0A/**%0A * Used to serialize Messages with Gson.%0A *%0A * @author Caleb Brinkman%0A */%0Apublic class GsonMessageSerializer implements JsonSerializer%3CMessage%3E%0A%7B
+import com.jenjinstudios.io.annotations.MessageAdapter;%0Aimport org.reflections.Reflections;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport java.lang.annotation.Annotation;%0Aimport java.lang.reflect.Type;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.Set;%0A%0A/**%0A * Used to serialize Messages with Gson.%0A *%0A * @author Caleb Brinkman%0A */%0Apublic class GsonMessageSerializer implements JsonSerializer%3CMessage%3E%0A%7B%0A    private static final Logger LOGGER = LoggerFactory.getLogger(GsonMessageSerializer.class);%0A    private static final Map%3CClass, String%3E ADAPT_TO = new HashMap%3C%3E(10);%0A%0A    static %7B%0A        LOGGER.debug(%22Scanning for message classes%22);%0A        Reflections reflections = new Reflections(%22%22);%0A        final Set%3CClass%3C? extends Message%3E%3E messageClasses = reflections.getSubTypesOf(Message.class);%0A        for (Class messageClass : messageClasses) %7B%0A            LOGGER.debug(%22Registering message class: %22 + messageClass.getName());%0A            Annotation annotation = messageClass.getAnnotation(MessageAdapter.class);%0A            if (annotation != null) %7B%0A                final MessageAdapter messageAdapter = (MessageAdapter) annotation;%0A                String adaptTo = messageAdapter.adaptTo();%0A                if (!adaptTo.isEmpty()) %7B%0A                    LOGGER.debug(%22Registering adapter class: %22 + adaptTo);%0A                    try %7B%0A                        Class.forName(adaptTo);%0A                        ADAPT_TO.put(messageClass, adaptTo);%0A                    %7D catch (ClassNotFoundException e) %7B%0A                        LOGGER.warn(%22Encountered ClassNotFoundException when registering message adapter%22, e);%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A        LOGGER.debug(%22Registered %5C%22adaptTo%5C%22 Message Classes: %7B%7D%22, ADAPT_TO);%0A    %7D%0A
 %0A   
@@ -2148,24 +2148,90 @@
  context) %7B%0A
+        final Class%3C? extends Message%3E srcClass = src.getClass();%0A
         Json
@@ -2273,27 +2273,21 @@
 src, src
-.get
 Class
-()
 );%0A     
@@ -2324,24 +2324,131 @@
 onObject();%0A
+%0A        String className = ADAPT_TO.containsKey(srcClass) ? ADAPT_TO.get(srcClass) : srcClass.getName();%0A%0A
         mess
@@ -2476,32 +2476,17 @@
 s%22, 
-src.getClass().get
+class
 Name
-()
 );%0A 

Implement adaptTo in GsonMessageSerializer