1.16.5 Custom Gson serializer not working

Discussion in 'Spigot Plugin Development' started by JoeyPlayzTV, Jun 14, 2021.

  1. Hey guys, i try to implement a custom Gson serializer.

    Here is my code:
    Code (Java):
    import com.google.gson.*;
    import net.proxysocke.gsontest.IJob;

    import java.lang.reflect.Type;

    public class JobSerializer implements JsonSerializer<IJob> {

        @Override
        public JsonElement serialize(IJob iJob, Type type, JsonSerializationContext jsonSerializationContext) {
            System.out.println("Huhu");
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("type", iJob.getClass().getName());
            jsonObject.add("data", jsonSerializationContext.serialize(iJob));
            return jsonObject;
        }
    }
    Here my test method:
    Code (Java):
    private static void test(){
            Map<String, IJob> jobs = new HashMap<>();
            jobs.put("farmer", new MinerJob());
            jobs.put("fisher", new FisherJob());

            GsonBuilder builder = new GsonBuilder();
            Type t = new TypeToken<Map<String, IJob>>(){}.getType();
            builder.registerTypeAdapter(t, new JobSerializer());
            Gson g = builder.create();
            System.out.println(g.toJson(jobs));
        }

    My problem is that my custom serializer will not be called. it give me a default serialisation and it does not print "Huhu". Can anyone help me, why my custom serializer is not called? I implemented it in the correct way but dont work ;(
     
  2. SteelPhoenix

    Moderator

    Your serializer is for IJob, not for a Map
     
  3. Ah ok i will try to fix that. i wnat to serialize a map of IJobs
     
  4. Dont work i have implemented this for map now but dont work. it gets not even called.

    Code (Java):
    import com.google.gson.*;
    import net.proxysocke.gsontest.IJob;

    import java.lang.reflect.Type;
    import java.util.Map;

    public class JobSerializer implements JsonSerializer<Map<String, IJob>> {

        @Override
        public JsonElement serialize(Map<String, IJob> o, Type type, JsonSerializationContext jsonSerializationContext) {
            System.out.println("Huhu");
            JsonObject serializedMap = new JsonObject();
            for(Map.Entry<String, IJob> e : o.entrySet()){
                JsonObject job = new JsonObject();
                job.addProperty("type", e.getValue().getClass().getName());
                job.add("data", jsonSerializationContext.serialize(e.getValue()));
                serializedMap.add(e.getKey(), job);
            }
            return serializedMap;
        }
    }

    Code (Java):
    private static void test(){
            Map<String, IJob> jobs = new HashMap<>();
            jobs.put("farmer", new MinerJob());
            jobs.put("fisher", new FisherJob());

            GsonBuilder builder = new GsonBuilder();
            Type t = new TypeToken<Map<String, IJob>>(){}.getType();
            builder.registerTypeAdapter(t, new JobSerializer());

            Gson g = builder.create();
            System.out.println(g.toJson(jobs));
        }