Created
          May 15, 2017 11:48 
        
      - 
      
 - 
        
Save raymond852/0701284dcbdb29627b841cb5c5896e40 to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public interface IAnimal { public String sound(); } | |
| public class Cat implements IAnimal { | |
| public String name; | |
| public Cat(String name) { | |
| super(); | |
| this.name = name; | |
| } | |
| @Override | |
| public String sound() { | |
| return name + " : \"meaow\""; | |
| }; | |
| } | |
| public class Dog implements IAnimal { | |
| public String name; | |
| public int ferocity; | |
| public Dog(String name, int ferocity) { | |
| super(); | |
| this.name = name; | |
| this.ferocity = ferocity; | |
| } | |
| @Override | |
| public String sound() { | |
| return name + " : \"bark\" (ferocity level:" + ferocity + ")"; | |
| } | |
| } | |
| public class IAnimalAdapter implements JsonSerializer<IAnimal>, JsonDeserializer<IAnimal>{ | |
| private static final String CLASSNAME = "CLASSNAME"; | |
| private static final String INSTANCE = "INSTANCE"; | |
| @Override | |
| public JsonElement serialize(IAnimal src, Type typeOfSrc, | |
| JsonSerializationContext context) { | |
| JsonObject retValue = new JsonObject(); | |
| String className = src.getClass().getName(); | |
| retValue.addProperty(CLASSNAME, className); | |
| JsonElement elem = context.serialize(src); | |
| retValue.add(INSTANCE, elem); | |
| return retValue; | |
| } | |
| @Override | |
| public IAnimal deserialize(JsonElement json, Type typeOfT, | |
| JsonDeserializationContext context) throws JsonParseException { | |
| JsonObject jsonObject = json.getAsJsonObject(); | |
| JsonPrimitive prim = (JsonPrimitive) jsonObject.get(CLASSNAME); | |
| String className = prim.getAsString(); | |
| Class<?> klass = null; | |
| try { | |
| klass = Class.forName(className); | |
| } catch (ClassNotFoundException e) { | |
| e.printStackTrace(); | |
| throw new JsonParseException(e.getMessage()); | |
| } | |
| return context.deserialize(jsonObject.get(INSTANCE), klass); | |
| } | |
| } | |
| public class Test { | |
| public static void main(String[] args) { | |
| IAnimal animals[] = new IAnimal[]{new Cat("Kitty"), new Dog("Brutus", 5)}; | |
| Gson gsonExt = null; | |
| { | |
| GsonBuilder builder = new GsonBuilder(); | |
| builder.registerTypeAdapter(IAnimal.class, new IAnimalAdapter()); | |
| gsonExt = builder.create(); | |
| } | |
| for (IAnimal animal : animals) { | |
| String animalJson = gsonExt.toJson(animal, IAnimal.class); | |
| System.out.println("serialized with the custom serializer:" + animalJson); | |
| IAnimal animal2 = gsonExt.fromJson(animalJson, IAnimal.class); | |
| System.out.println(animal2.sound()); | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment