Created
August 21, 2015 17:13
-
-
Save Piasy/ebaf701e506b0b3d096d to your computer and use it in GitHub Desktop.
An attempt to make it work when AutoParcel && AutoGson comes with Generic field that also annotated with AutoParcel
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
package com.github.piasy.model.entities; | |
import android.support.annotation.NonNull; | |
import auto.parcel.AutoParcel; | |
import com.github.piasy.common.utils.model.AutoGson; | |
/** | |
* Created by Piasy{github.com/Piasy} on 15/8/21. | |
*/ | |
@AutoParcel | |
@AutoGson(autoClass = AutoParcel_Inner.class) | |
public abstract class Inner { | |
@NonNull | |
public static Builder builder() { | |
return new AutoParcel_Inner.Builder(); | |
} | |
@NonNull | |
public abstract String name(); | |
public abstract int age(); | |
@AutoParcel.Builder | |
public static abstract class Builder { | |
@NonNull | |
public abstract Builder name(@NonNull String name); | |
@NonNull | |
public abstract Builder age(int age); | |
@NonNull | |
public abstract Inner bulid(); | |
} | |
} |
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
package com.github.piasy.model.entities; | |
import com.github.piasy.common.utils.model.AutoGenTypeAdapterFactory; | |
import com.google.common.reflect.TypeToken; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import java.lang.reflect.Type; | |
import junit.framework.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
/** | |
* Created by Piasy{github.com/Piasy} on 15/8/21. | |
*/ | |
public class NestedAutoParcelAutoGsonTest { | |
private Gson mGson; | |
@Before | |
public void setUp() { | |
mGson = new GsonBuilder().registerTypeAdapterFactory(new AutoGenTypeAdapterFactory()) | |
.setPrettyPrinting() | |
.create(); | |
} | |
@Test | |
public void testNestedAutoParcelAutoGsonTest() { | |
String mock = "{\"name\":\"Piasy\",\"age\":10,\"inner\":{\"name\":\"PiaSys\",\"age\":2}}"; | |
Outer outer = mGson.fromJson(mock, Outer.class); | |
Assert.assertEquals("Piasy", outer.name()); | |
Assert.assertEquals(10, outer.age()); | |
Assert.assertEquals("PiaSys", outer.inner().name()); | |
Assert.assertEquals(2, outer.inner().age()); | |
} | |
@Test | |
public void testNestedListInner() { | |
String mock = | |
"{\"name\":\"Piasy\",\"age\":10,\"inners\":[{\"name\":\"PiaSys\",\"age\":2}]}"; | |
OuterWithListInner outerWithListInner = mGson.fromJson(mock, OuterWithListInner.class); | |
Assert.assertEquals("Piasy", outerWithListInner.name()); | |
Assert.assertEquals(10, outerWithListInner.age()); | |
Assert.assertEquals(1, outerWithListInner.inners().size()); | |
Inner inner = outerWithListInner.inners().get(0); | |
Assert.assertEquals("PiaSys", inner.name()); | |
Assert.assertEquals(2, inner.age()); | |
} | |
@Test | |
public void testNestedGenericList() { | |
String mock = | |
"{\"name\":\"Piasy\",\"age\":10,\"inners\":[{\"name\":\"PiaSys\",\"age\":2}]}"; | |
Type type = new TypeToken<OuterWithGenericList<Inner>>() { | |
}.getType(); | |
OuterWithGenericList<Inner> outerWithGenericList = mGson.fromJson(mock, type); | |
Assert.assertEquals("Piasy", outerWithGenericList.name()); | |
Assert.assertEquals(10, outerWithGenericList.age()); | |
Assert.assertEquals(1, outerWithGenericList.inners().size()); | |
// inners' type will be LinkedTreeMap | |
/*Inner inner = outerWithGenericList.inners().get(0); | |
Assert.assertEquals("PiaSys", inner.name()); | |
Assert.assertEquals(2, inner.age());*/ | |
// with generated real type, the deserialize will be correct | |
Type generatedType = new TypeToken<AutoParcel_OuterWithGenericList<AutoParcel_Inner>>() { | |
}.getType(); | |
AutoParcel_OuterWithGenericList<AutoParcel_Inner> outerWithGenericList2 = | |
mGson.fromJson(mock, generatedType); | |
Assert.assertEquals("Piasy", outerWithGenericList.name()); | |
Assert.assertEquals(10, outerWithGenericList.age()); | |
Assert.assertEquals(1, outerWithGenericList.inners().size()); | |
AutoParcel_Inner inner2 = outerWithGenericList2.inners().get(0); | |
Assert.assertEquals("PiaSys", inner2.name()); | |
Assert.assertEquals(2, inner2.age()); | |
} | |
} |
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
package com.github.piasy.model.entities; | |
import android.support.annotation.NonNull; | |
import auto.parcel.AutoParcel; | |
import com.github.piasy.common.utils.model.AutoGson; | |
/** | |
* Created by Piasy{github.com/Piasy} on 15/8/21. | |
*/ | |
@AutoParcel | |
@AutoGson(autoClass = AutoParcel_Outer.class) | |
public abstract class Outer { | |
@NonNull | |
public static Builder builder() { | |
return new AutoParcel_Outer.Builder(); | |
} | |
@NonNull | |
public abstract String name(); | |
public abstract int age(); | |
@NonNull | |
public abstract Inner inner(); | |
@AutoParcel.Builder | |
public static abstract class Builder { | |
@NonNull | |
public abstract Builder name(@NonNull String name); | |
@NonNull | |
public abstract Builder age(int age); | |
@NonNull | |
public abstract Builder inner(@NonNull Inner inner); | |
@NonNull | |
public abstract Outer bulid(); | |
} | |
} |
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
package com.github.piasy.model.entities; | |
import android.support.annotation.NonNull; | |
import auto.parcel.AutoParcel; | |
import com.github.piasy.common.utils.model.AutoGson; | |
import java.util.List; | |
/** | |
* Created by Piasy{github.com/Piasy} on 15/8/21. | |
*/ | |
@AutoParcel | |
// Loss Parameter type info here, of course AutoGenTypeAdapterFactory will not work. | |
// To amend this, gson has a empty anonymous TypeToken class instance to capture | |
// its parameter type info, but annotation attribute must be constant, could not use | |
// TypeToken here. | |
// Maybe could use some other technique to achieve this purpose | |
// (ref: http://stackoverflow.com/a/14139700/3077508), but this kind of usage could be avoided, just | |
// use OuterWithListInner class, avoiding use generic for Outer class. | |
@AutoGson(autoClass = AutoParcel_OuterWithGenericList.class) | |
public abstract class OuterWithGenericList<T> { | |
@NonNull | |
public static <D> Builder<D> builder() { | |
return new AutoParcel_OuterWithGenericList.Builder<>(); | |
} | |
@NonNull | |
public abstract String name(); | |
public abstract int age(); | |
@NonNull | |
public abstract List<T> inners(); | |
@AutoParcel.Builder | |
public static abstract class Builder<T> { | |
@NonNull | |
public abstract Builder<T> name(@NonNull String name); | |
@NonNull | |
public abstract Builder<T> age(int age); | |
@NonNull | |
public abstract Builder<T> inners(@NonNull List<T> inners); | |
@NonNull | |
public abstract OuterWithGenericList<T> bulid(); | |
} | |
} |
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
package com.github.piasy.model.entities; | |
import android.support.annotation.NonNull; | |
import auto.parcel.AutoParcel; | |
import com.github.piasy.common.utils.model.AutoGson; | |
import java.util.List; | |
/** | |
* Created by Piasy{github.com/Piasy} on 15/8/21. | |
*/ | |
@AutoParcel | |
@AutoGson(autoClass = AutoParcel_OuterWithListInner.class) | |
public abstract class OuterWithListInner { | |
@NonNull | |
public static Builder builder() { | |
return new AutoParcel_OuterWithListInner.Builder(); | |
} | |
@NonNull | |
public abstract String name(); | |
public abstract int age(); | |
@NonNull | |
public abstract List<Inner> inners(); | |
@AutoParcel.Builder | |
public static abstract class Builder { | |
@NonNull | |
public abstract Builder name(@NonNull String name); | |
@NonNull | |
public abstract Builder age(int age); | |
@NonNull | |
public abstract Builder inners(@NonNull List<Inner> inners); | |
@NonNull | |
public abstract OuterWithListInner bulid(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment