Converter { static Type first(Type searchFor, String... roots) { return new CompositeType(new ListTypeImpl(searchFor, true), roots); } Converter(Moshi moshi); Moshi moshi(); @SuppressWarnings("unchecked") // Type matching is the caller's responsibility. R convertFromBody(Type type, ResponseBody body); RequestBody convertToBody(Type type, B body); @SuppressWarnings("unchecked") JsonAdapter<T> findAdapter(Type type);  }
@Test public void firstInList() throws Exception { Type compositeType = Converter.first(TestData.class, "content"); TestData data = fromJson(compositeType, "{\n" + " \"content\": [\n" + " {\n" + " \"str\": \"zero\",\n" + " \"val\": 0\n" + " },\n" + " {\n" + " \"str\": \"one\",\n" + " \"val\": 1\n" + " },\n" + " {\n" + " \"str\": \"two\",\n" + " \"val\": 2\n" + " }\n" + " ]\n" + '}'); assertThat(data).isNotNull(); assertThat(data.str).isEqualTo("zero"); assertThat(data.val).isEqualTo(0); }
@Test public void firstAsNullIfListEmpty() throws Exception { Type compositeType1 = Converter.first(TestData.class, "empty"); assertThat(fromJson(compositeType1, "{\n" + " \"empty\": null\n" + '}')).isNull(); Type compositeType2 = Converter.first(TestData.class, "empty"); assertThat(fromJson(compositeType2, "{\n" + " \"empty\": []\n" + '}')).isNull(); }