@Override public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) { Pair<ElementAt, Set<Annotation>> nextAnnotations = nextAnnotations(annotations, ElementAt.class); if (nextAnnotations == null || !nextAnnotations.second.isEmpty()) return null; return new ElementAtJsonAdapter<>(type, moshi, nextAnnotations.first.index()); } };
@Override public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) { Pair<Wrapped, Set<Annotation>> nextAnnotations = nextAnnotations(annotations, Wrapped.class); if (nextAnnotations == null) return null; JsonAdapter<Object> adapter = moshi.adapter(type, nextAnnotations.second); Wrapped wrapped = nextAnnotations.first; return new WrappedJsonAdapter<>(adapter, wrapped.path(), wrapped.failOnNotFound()); } };
private static class Data2 { @FirstElement @Custom String str; } }
private static class WrapsChar implements Wrapper<Character> { @FallbackOnNull char first; @FallbackOnNull(fallbackChar = 'a') char second; @Override public Character first() { return first; } @Override public Character second() { return second; } }
private static class AnotherInt { @FallbackOnNull(fallbackInt = 2) @Multiply int willFallback; @FallbackOnNull(fallbackInt = 2) @Multiply int willMultiply; }
private static class Data2 { @LastElement @Custom String str; } }
/** * The name of the method will be taken as the first key, then the path provided with the * annotation. */ @Wrapped(path = {"1"}) abstract Meta meta();
@SerializeOnly @Nullable abstract Integer bar(); }
@Test public void firstElementJsonAdapter() throws Exception { assertResponse(service.firstElement(), "[\n" + " \"expected\",\n" + " \"ignored\"\n" + "]", "expected"); }
@Test public void elementAtJsonAdapter() throws Exception { assertResponse(service.elementAt(), "[\n" + " \"one\",\n" + " \"two\",\n" + " \"three\"\n" + "]", "three"); }
@Test public void unwrapFirstElement() throws Exception { assertResponse(service.unwrapFirstElement(), "{\n" + " \"one\": {\n" + " \"two\": [\n" + " \"first\"\n" + " ]\n" + " }\n" + "}", "first"); }
private static class Data2 { @Custom @SerializeNulls String data; } }
private static class WrapsShort implements Wrapper<Short> { @FallbackOnNull short first; @FallbackOnNull(fallbackShort = 121) short second; @Override public Short first() { return first; } @Override public Short second() { return second; } }
private static class WrapsFloat implements Wrapper<Float> { @FallbackOnNull float first; @FallbackOnNull(fallbackFloat = 16.0f) float second; @Override public Float first() { return first; } @Override public Float second() { return second; } }
private static class WrapsBool implements Wrapper<Boolean> { @FallbackOnNull boolean first; @FallbackOnNull(fallbackBoolean = true) boolean second; @Override public Boolean first() { return first; } @Override public Boolean second() { return second; } }
private static class WrapsInt implements Wrapper<Integer> { @FallbackOnNull int first; @FallbackOnNull(fallbackInt = -1) int second; @Override public Integer first() { return first; } @Override public Integer second() { return second; } }
private static class WrapsLong implements Wrapper<Long> { @FallbackOnNull long first; @FallbackOnNull(fallbackLong = -113) long second; @Override public Long first() { return first; } @Override public Long second() { return second; } }
private static class WrapsByte implements Wrapper<Byte> { @FallbackOnNull byte first; @FallbackOnNull(fallbackByte = 42) byte second; @Override public Byte first() { return first; } @Override public Byte second() { return second; } }
private static class WrapsDouble implements Wrapper<Double> { @FallbackOnNull double first; @FallbackOnNull(fallbackDouble = 12.0) double second; @Override public Double first() { return first; } @Override public Double second() { return second; } }