Java Code Examples for com.squareup.moshi.Types#getRawType()

The following examples show how to use com.squareup.moshi.Types#getRawType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonApiConverterFactory.java    From moshi-jsonapi with MIT License 6 votes vote down vote up
private JsonAdapter<?> getAdapterFromType(Type type) {
    Class<?> rawType = Types.getRawType(type);
    JsonAdapter<?> adapter;
    if (rawType.isArray() && ResourceIdentifier.class.isAssignableFrom(rawType.getComponentType())) {
        adapter = moshi.adapter(Types.newParameterizedType(Document.class, rawType.getComponentType()));
    } else if (List.class.isAssignableFrom(rawType) && type instanceof ParameterizedType) {
        Type typeParameter = ((ParameterizedType) type).getActualTypeArguments()[0];
        if (typeParameter instanceof Class<?> && ResourceIdentifier.class.isAssignableFrom((Class<?>) typeParameter)) {
            adapter = moshi.adapter(Types.newParameterizedType(Document.class, typeParameter));
        } else {
            return null;
        }
    } else if (ResourceIdentifier.class.isAssignableFrom(rawType)) {
        adapter = moshi.adapter(Types.newParameterizedType(Document.class, rawType));
    } else if (Document.class.isAssignableFrom(rawType)) {
        adapter = moshi.adapter(Types.newParameterizedType(Document.class, Resource.class));
    } else {
        return null;
    }
    return adapter;
}
 
Example 2
Source File: PolymorphicJsonAdapterFactory.java    From moshi with Apache License 2.0 6 votes vote down vote up
@Override
public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) {
  if (Types.getRawType(type) != baseType || !annotations.isEmpty()) {
    return null;
  }

  List<JsonAdapter<Object>> jsonAdapters = new ArrayList<>(subtypes.size());
  for (int i = 0, size = subtypes.size(); i < size; i++) {
    jsonAdapters.add(moshi.adapter(subtypes.get(i)));
  }

  return new PolymorphicJsonAdapter(labelKey,
      labels,
      subtypes,
      jsonAdapters,
      defaultValue,
      defaultValueSet
  ).nullSafe();
}
 
Example 3
Source File: Util.java    From moshi with Apache License 2.0 6 votes vote down vote up
public ParameterizedTypeImpl(@Nullable Type ownerType, Type rawType, Type... typeArguments) {
  // Require an owner type if the raw type needs it.
  if (rawType instanceof Class<?>) {
    Class<?> enclosingClass = ((Class<?>) rawType).getEnclosingClass();
    if (ownerType != null) {
      if (enclosingClass == null || Types.getRawType(ownerType) != enclosingClass) {
        throw new IllegalArgumentException(
            "unexpected owner type for " + rawType + ": " + ownerType);
      }
    } else if (enclosingClass != null) {
      throw new IllegalArgumentException(
          "unexpected owner type for " + rawType + ": null");
    }
  }

  this.ownerType = ownerType == null ? null : canonicalize(ownerType);
  this.rawType = canonicalize(rawType);
  this.typeArguments = typeArguments.clone();
  for (int t = 0; t < this.typeArguments.length; t++) {
    if (this.typeArguments[t] == null) throw new NullPointerException();
    checkNotPrimitive(this.typeArguments[t]);
    this.typeArguments[t] = canonicalize(this.typeArguments[t]);
  }
}
 
Example 4
Source File: JsonApiConverterFactory.java    From moshi-jsonapi with MIT License 4 votes vote down vote up
MoshiResponseBodyConverter(JsonAdapter<Document> adapter, Type type) {
    this.adapter = adapter;
    this.rawType = (Class<R>) Types.getRawType(type);
}
 
Example 5
Source File: JsonApiConverterFactory.java    From moshi-jsonapi with MIT License 4 votes vote down vote up
MoshiRequestBodyConverter(JsonAdapter<Document> adapter, Type type) {
    this.adapter = adapter;
    this.rawType = (Class<T>) Types.getRawType(type);
}