Guest User

Untitled

a guest
Sep 8th, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. package com.json;
  2.  
  3. import java.lang.reflect.Type;
  4.  
  5. import com.google.gson.JsonDeserializationContext;
  6. import com.google.gson.JsonDeserializer;
  7. import com.google.gson.JsonElement;
  8. import com.google.gson.JsonObject;
  9.  
  10. public class EntryDeserializer implements JsonDeserializer<IEntry> {
  11.  
  12. @Override
  13. public IEntry deserialize(JsonElement json, Type type, JsonDeserializationContext context) {
  14. final JsonObject jObject = json.getAsJsonObject();
  15.  
  16. try {
  17. final IEntry entry = EntryDataStore.getEntryClassFromString(jObject.get("type").getAsString()).newInstance();
  18. if(entry instanceof DinoEntry && entry != null) {
  19. DinoEntry dino = (DinoEntry)entry;
  20. dino.setID(jObject.get("id").getAsInt());
  21. dino.setTitle(jObject.get("title").getAsString());
  22. dino.setDiet(jObject.get("diet").getAsString());
  23. dino.setTemperament(jObject.get("temperament").getAsString());
  24. return dino;
  25. }
  26. else if(entry instanceof TextEntry && entry != null) {
  27. TextEntry text = (TextEntry)entry;
  28. text.setID(jObject.get("id").getAsInt());
  29. text.setTitle(jObject.get("title").getAsString());
  30. text.setText(jObject.get("text").getAsString());
  31. return text;
  32. }
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. }
  36.  
  37. return null;
  38. }
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment