static <T> JsonAdapter<Set<T>> newLinkedHashSetAdapter(Type type, Moshi moshi) { Type elementType = Types.collectionElementType(type, Collection.class); JsonAdapter<T> elementAdapter = moshi.adapter(elementType); return new CollectionJsonAdapter<Set<T>, T>(elementAdapter) { @Override Set<T> newCollection() { return new LinkedHashSet<>(); } }; }
static <T> JsonAdapter<Collection<T>> newArrayListAdapter(Type type, Moshi moshi) { Type elementType = Types.collectionElementType(type, Collection.class); JsonAdapter<T> elementAdapter = moshi.adapter(elementType); return new CollectionJsonAdapter<Collection<T>, T>(elementAdapter) { @Override Collection<T> newCollection() { return new ArrayList<>(); } }; }
boolean elementTypeIsString(Type type) { Type elementType = Types.collectionElementType(type, Collection.class); Class<?> rawElementType = Types.getRawType(elementType); return rawElementType == String.class; } };
static <T> JsonAdapter<Set<T>> newLinkedHashSetAdapter(Type type, Moshi moshi) { Type elementType = Types.collectionElementType(type, Collection.class); JsonAdapter<T> elementAdapter = moshi.adapter(elementType); return new CollectionJsonAdapter<Set<T>, T>(elementAdapter) { @Override Set<T> newCollection() { return new LinkedHashSet<>(); } }; }
static <T> JsonAdapter<Collection<T>> newArrayListAdapter(Type type, Moshi moshi) { Type elementType = Types.collectionElementType(type, Collection.class); JsonAdapter<T> elementAdapter = moshi.adapter(elementType); return new CollectionJsonAdapter<Collection<T>, T>(elementAdapter) { @Override Collection<T> newCollection() { return new ArrayList<>(); } }; }
@Override public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) { if (annotations.isEmpty() || annotations.size() != 1 // At this point we know that the set contains only one entry. || annotations.iterator().next().annotationType() != ContactPath.class) { return null; } // Check that the host type is a list. Class<?> rawType = Types.getRawType(type); if (rawType != List.class) return null; // Check that the generic parameter is a list. Type paramType = Types.collectionElementType(type, List.class); Class<?> rawParamType = Types.getRawType(paramType); if (rawParamType != List.class) return null; // Check that the generic parameter of paramType is XingUser. Type innerType = Types.collectionElementType(paramType, List.class); Class<?> rawInnerType = Types.getRawType(innerType); if (rawInnerType != XingUser.class) return null; // Create the adapter. At this point we know that paramType is the pathType. return new ContactPathJsonAdapter(moshi, paramType).nullSafe(); } };