Last active
December 10, 2015 12:08
-
-
Save dinigo/4431586 to your computer and use it in GitHub Desktop.
Deserializing JSON polimorfic clases with gson
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 Articulo { | |
| enum TipoArticulo {ENTREVISTA, BANNER}; | |
| } |
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
| class ArticuloAdapter implements JsonDeserializer<Articulo> { | |
| @Override | |
| public Articulo deserialize(JsonElement jsonElement, Type tipo, JsonDeserializationContext context) throws JsonParseException { | |
| JsonObject object = jsonElement.getAsJsonObject(); | |
| Gson gson = new Gson(); | |
| JsonElement titular = object.get("titular"); | |
| if (titular != null) { | |
| return gson.fromJson(jsonElement, ArticuloTexto.class); | |
| } | |
| JsonElement banner = object.get("banner"); | |
| if (banner != null) { | |
| return gson.fromJson(jsonElement, ArticuloBanner.class); | |
| } | |
| return null; | |
| } | |
| } |
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
| class ArticuloTexto implements Articulo | |
| { | |
| private static final TipoArticulo tipo = TipoArticulo.ENTREVISTA; | |
| private String titular; | |
| private String autor; | |
| public ArticuloTexto(String titular, String autor, List<Elemento> articulos) { | |
| this.titular = titular; | |
| this.autor = autor; | |
| this.articulos = articulos; | |
| } | |
| private List<Elemento> articulos; | |
| } |
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 static void constructorRevistaPolimorfico(String revista) throws JsonSyntaxException, JsonIOException, FileNotFoundException{ | |
| Gson gson = new GsonBuilder(). | |
| setPrettyPrinting(). | |
| registerTypeHierarchyAdapter(Articulo.class, new ArticuloAdapter()). | |
| create(); | |
| ArrayList<Articulo> revistar = gson.fromJson(revista, new TypeToken<List<Articulo>>(){}.getType()); | |
| System.out.println(gson.toJson(revistar)); | |
| } |
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
| class Elemento | |
| { | |
| private String tipo; | |
| private String contenido; | |
| public Elemento(String tipo, String contenido) { | |
| this.tipo = tipo; | |
| this.contenido = contenido; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment