discomarathon / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

cannot cleanly override enum deserializer #271

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
There's a constructor for Gson that accepts a list of serializers/deserializers 
for default types.  I can't seem to figure out a way to override Enum without 
copy/pasting the 900 line DefaultTypeAdapters class and changing it, as all of 
the default serializers are marked private in a package private class.  Any 
ideas on a cleaner way?

Original issue reported on code.google.com by adrian.f...@gmail.com on 6 Dec 2010 at 2:06

GoogleCodeExporter commented 9 years ago
here's a way I've found:

open up the package (com.google.gson) and add the type hierarchy you want with 
the package private registerTypeHierarchyAdapter method.

package com.google.gson;

public class JcloudsGsonPackageAccessor {
   public static GsonBuilder registerTypeHierarchyAdapter(GsonBuilder builder, Class<?> baseType, Object typeAdapter) {
      return builder.registerTypeHierarchyAdapter(baseType, typeAdapter);
   }
}

...

      JcloudsGsonPackageAccessor.registerTypeHierarchyAdapter(builder, Enum.class, new MyEnumTypeAdapter());

Original comment by adrian.f...@gmail.com on 6 Dec 2010 at 2:19

GoogleCodeExporter commented 9 years ago
Fixed in r709

Original comment by inder123 on 24 Jan 2011 at 10:21