Skip to content

Instantly share code, notes, and snippets.

@richarth
Created February 9, 2017 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richarth/476f16f63a80ef064752b88532623996 to your computer and use it in GitHub Desktop.
Save richarth/476f16f63a80ef064752b88532623996 to your computer and use it in GitHub Desktop.
Sample code to demonstrate cannot serialize abstract class issue with Auto Value and Moshi
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.1'
compile 'com.android.support:support-v4:25.1.1'
compile 'com.android.support:recyclerview-v7:25.1.1'
compile 'com.android.support:design:25.1.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.google.dagger:dagger:2.9'
compile 'com.annimon:stream:1.1.5'
compile 'com.squareup.retrofit2:converter-moshi:2.1.0'
provided 'com.google.auto.value:auto-value:1.3' // needed for Android Studio
apt 'com.google.auto.value:auto-value:1.3'
apt 'com.ryanharter.auto.value:auto-value-moshi:0.4.2'
provided 'com.ryanharter.auto.value:auto-value-moshi-annotations:0.4.2'
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:2.7.1'
}
@RunWith(JUnit4.class)
public class ModelTests {
@Test
public void verifyJsonConvertsToModel() throws Exception {
String sampleMovieJson = "{\"adult\":false,\"backdrop_path\":\"/lubzBMQLLmG88CLQ4F3TxZr2Q7N.jpg\",\"belongs_to_collection\":{\"id\":427084,\"name\":\"The Secret Life of Pets Collection\",\"poster_path\":\"/aDNbXvuRiuYxk8qCwXNQQ7UEHau.jpg\",\"backdrop_path\":null},\"budget\":75000000,\"genres\":[{\"id\":12,\"name\":\"Adventure\"},{\"id\":16,\"name\":\"Animation\"},{\"id\":35,\"name\":\"Comedy\"},{\"id\":10751,\"name\":\"Family\"}],\"homepage\":\"http://www.thesecretlifeofpets.com/\",\"id\":328111,\"imdb_id\":\"tt2709768\",\"original_language\":\"en\",\"original_title\":\"The Secret Life of Pets\",\"overview\":\"The quiet life of a terrier named Max is upended when his owner takes in Duke, a stray whom Max instantly dislikes.\",\"popularity\":129.924005,\"poster_path\":\"/WLQN5aiQG8wc9SeKwixW7pAR8K.jpg\",\"production_companies\":[{\"name\":\"Universal Pictures\",\"id\":33},{\"name\":\"Dentsu\",\"id\":6452},{\"name\":\"Illumination Entertainment\",\"id\":6704}],\"production_countries\":[{\"iso_3166_1\":\"US\",\"name\":\"United States of America\"}],\"release_date\":\"2016-06-18\",\"revenue\":874333497,\"runtime\":87,\"spoken_languages\":[{\"iso_639_1\":\"en\",\"name\":\"English\"}],\"status\":\"Released\",\"tagline\":\"Think this is what they do all day?\",\"title\":\"The Secret Life of Pets\",\"video\":false,\"vote_average\":5.8,\"vote_count\":2078}";
Movie sampleMovie = Movie.create(328111, "The Secret Life of Pets", "/aDNbXvuRiuYxk8qCwXNQQ7UEHau.jpg", "The quiet life of a terrier named Max is upended when his owner takes in Duke, a stray whom Max instantly dislikes.", 5.8, "2016-06-18");
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<Movie> adapter = moshi.adapter(Movie.class).lenient();
assertEquals(adapter.fromJson(sampleMovieJson), sampleMovie);
}
@Test
public void verifyJsonConvertsToModel2() throws Exception {
String sampleMovieJson = "{\"adult\":false,\"backdrop_path\":\"/lubzBMQLLmG88CLQ4F3TxZr2Q7N.jpg\",\"belongs_to_collection\":{\"id\":427084,\"name\":\"The Secret Life of Pets Collection\",\"poster_path\":\"/aDNbXvuRiuYxk8qCwXNQQ7UEHau.jpg\",\"backdrop_path\":null},\"budget\":75000000,\"genres\":[{\"id\":12,\"name\":\"Adventure\"},{\"id\":16,\"name\":\"Animation\"},{\"id\":35,\"name\":\"Comedy\"},{\"id\":10751,\"name\":\"Family\"}],\"homepage\":\"http://www.thesecretlifeofpets.com/\",\"id\":328111,\"imdb_id\":\"tt2709768\",\"original_language\":\"en\",\"original_title\":\"The Secret Life of Pets\",\"overview\":\"The quiet life of a terrier named Max is upended when his owner takes in Duke, a stray whom Max instantly dislikes.\",\"popularity\":129.924005,\"poster_path\":\"/WLQN5aiQG8wc9SeKwixW7pAR8K.jpg\",\"production_companies\":[{\"name\":\"Universal Pictures\",\"id\":33},{\"name\":\"Dentsu\",\"id\":6452},{\"name\":\"Illumination Entertainment\",\"id\":6704}],\"production_countries\":[{\"iso_3166_1\":\"US\",\"name\":\"United States of America\"}],\"release_date\":\"2016-06-18\",\"revenue\":874333497,\"runtime\":87,\"spoken_languages\":[{\"iso_639_1\":\"en\",\"name\":\"English\"}],\"status\":\"Released\",\"tagline\":\"Think this is what they do all day?\",\"title\":\"The Secret Life of Pets\",\"video\":false,\"vote_average\":5.8,\"vote_count\":2078}";
Movie2 sampleMovie = new Movie2();
sampleMovie.setId(328111);
sampleMovie.setName("The Secret Life of Pets");
sampleMovie.setPosterUrl("/WLQN5aiQG8wc9SeKwixW7pAR8K.jpg");
sampleMovie.setPlotSynopsis("The quiet life of a terrier named Max is upended when his owner takes in Duke, a stray whom Max instantly dislikes.");
sampleMovie.setAverageRating(5.8);
sampleMovie.setReleaseDate("2016-06-18");
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<Movie2> adapter = moshi.adapter(Movie2.class).lenient();
Movie2 convertedMovie = adapter.fromJson(sampleMovieJson);
assertEquals(sampleMovie.getId(), convertedMovie.getId());
assertEquals(sampleMovie.getName(), convertedMovie.getName());
assertEquals(sampleMovie.getPosterUrl(), convertedMovie.getPosterUrl());
assertEquals(sampleMovie.getPlotSynopsis(), convertedMovie.getPlotSynopsis());
assertEquals(sampleMovie.getAverageRating(), convertedMovie.getAverageRating(), 0);
assertEquals(sampleMovie.getReleaseDate(), convertedMovie.getReleaseDate());
}
}
@AutoValue
public abstract class Movie {
public abstract int id();
@Json(name = "original_title")
public abstract String name();
@Json(name = "poster_path")
public abstract String posterUrl();
@Json(name = "overview")
public abstract String plotSynopsis();
@Json(name = "vote_average")
public abstract double averageRating();
@Json(name = "release_date")
public abstract String releaseDate();
public static Movie create(int id, @NonNull String name, @NonNull String posterUrl, @NonNull String plotSynopsis, double averageRating, @NonNull String releaseDate) {
return new AutoValue_Movie(id, name, posterUrl, plotSynopsis, averageRating, releaseDate);
}
public static JsonAdapter<Movie> jsonAdapter(Moshi moshi) {
return new AutoValue_Movie.MoshiJsonAdapter(moshi);
}
}
public class Movie2 {
private int id;
@Json(name = "original_title")
private String name;
@Json(name = "poster_path")
private String posterUrl;
@Json(name = "overview")
private String plotSynopsis;
@Json(name = "vote_average")
private double averageRating;
@Json(name = "release_date")
private String releaseDate;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPosterUrl() {
return posterUrl;
}
public void setPosterUrl(String posterUrl) {
this.posterUrl = posterUrl;
}
public String getPlotSynopsis() {
return plotSynopsis;
}
public void setPlotSynopsis(String plotSynopsis) {
this.plotSynopsis = plotSynopsis;
}
public double getAverageRating() {
return averageRating;
}
public void setAverageRating(double averageRating) {
this.averageRating = averageRating;
}
public String getReleaseDate() {
return releaseDate;
}
public void setReleaseDate(String releaseDate) {
this.releaseDate = releaseDate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment