Skip to content

Instantly share code, notes, and snippets.

@ktchernov
Last active April 22, 2016 02:57
Show Gist options
  • Save ktchernov/cd3520cebf8aa56214b5155a648d38d4 to your computer and use it in GitHub Desktop.
Save ktchernov/cd3520cebf8aa56214b5155a648d38d4 to your computer and use it in GitHub Desktop.
package io.github.ktchernov.moshiautovaluebug;
import com.squareup.moshi.Moshi;
import org.junit.Before;
import org.junit.Test;
import io.github.ktchernov.moshiautovaluebug.TestClassA.TestClassB;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class MoshiAutoValueTest {
private Moshi moshi;
@Before public void setUp() {
moshi = new Moshi.Builder()
.add(TestClassB.typeAdapterFactory()).build();
}
@Test
public void parseValid() throws Exception {
TestClassA testClassA =
moshi.adapter(TestClassA.class).fromJson("{ \"data\": { \"someValue\": 1 }");
assertEquals(1, testClassA.data.someValue());
}
@Test
public void parseWithNull() throws Exception {
TestClassA testClassA =
moshi.adapter(TestClassA.class).fromJson("{ \"data\": null }");
assertNull(testClassA.data);
}
}
package io.github.ktchernov.moshiautovaluebug;
import com.google.auto.value.AutoValue;
import android.support.annotation.Nullable;
import com.squareup.moshi.JsonAdapter;
public class TestClassA {
@Nullable public TestClassB data;
@AutoValue public static abstract class TestClassB {
public abstract int someValue();
public static JsonAdapter.Factory typeAdapterFactory() {
return AutoValue_TestClassA_TestClassB.typeAdapterFactory();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment