Skip to content

Instantly share code, notes, and snippets.

@ZacSweers
Last active February 15, 2021 06:12
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 ZacSweers/0ef2c5f0e3f61f82063dd8c67d75879f to your computer and use it in GitHub Desktop.
Save ZacSweers/0ef2c5f0e3f61f82063dd8c67d75879f to your computer and use it in GitHub Desktop.
Sample bug report for Moshi
package test;
import static com.google.common.truth.Truth.assertThat;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import org.junit.Test;
import java.io.IOException;
public class MoshiBugReport {
@Test public void test() throws IOException {
String json = "{\"value\":1}";
Moshi moshi = new Moshi.Builder()
.build();
JsonAdapter<ExampleClass> adapter = moshi.adapter(ExampleClass.class);
ExampleClass instance = adapter.fromJson(json);
assertThat(instance.value).isEqualTo(1);
assertThat(adapter.toJson(instance)).isEqualTo(json);
}
public static class ExampleClass {
public int value;
}
}
package test
import com.google.common.truth.Truth.assertThat
import com.squareup.moshi.JsonClass
import com.squareup.moshi.Moshi
import com.squareup.moshi.adapter
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import org.junit.Test
class MoshiBugReport {
@Test fun test() {
val json = "{\"value\":1}"
val moshi = Moshi.Builder()
// Add any custom adapters here
.add(KotlinJsonAdapterFactory()) // Omit if using code gen
.build()
val adapter = moshi.adapter<ExampleClass>()
val instance = adapter.fromJson(json)!!
assertThat(instance.value).isEqualTo(1)
assertThat(adapter.toJson(instance)).isEqualTo(json)
}
@JsonClass(generateAdapter = true)
class ExampleClass(val value: Int)
}
@ZacSweers
Copy link
Author

🛑 Contrary to popular belief, commenting here is not a substitute for a bug report. Please don't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment