Class MoshiConverterFactory

java.lang.Object
retrofit2.Converter.Factory
retrofit2.converter.moshi.MoshiConverterFactory

public final class MoshiConverterFactory
extends retrofit2.Converter.Factory
A converter which uses Moshi for JSON.

Because Moshi is so flexible in the types it supports, this converter assumes that it can handle all types. If you are mixing JSON serialization with something else (such as protocol buffers), you must add this instance last to allow the other converters a chance to see their types.

Any @JsonQualifier-annotated annotations on the parameter will be used when looking up a request body converter and those on the method will be used when looking up a response body converter.

  • Method Summary

    Modifier and Type Method Description
    MoshiConverterFactory asLenient()
    Return a new factory which uses lenient adapters.
    static MoshiConverterFactory create()
    Create an instance using a default Moshi instance for conversion.
    static MoshiConverterFactory create​(com.squareup.moshi.Moshi moshi)
    Create an instance using moshi for conversion.
    MoshiConverterFactory failOnUnknown()
    Return a new factory which uses JsonAdapter.failOnUnknown() adapters.
    retrofit2.Converter<?,​okhttp3.RequestBody> requestBodyConverter​(java.lang.reflect.Type type, java.lang.annotation.Annotation[] parameterAnnotations, java.lang.annotation.Annotation[] methodAnnotations, retrofit2.Retrofit retrofit)  
    retrofit2.Converter<okhttp3.ResponseBody,​?> responseBodyConverter​(java.lang.reflect.Type type, java.lang.annotation.Annotation[] annotations, retrofit2.Retrofit retrofit)  
    MoshiConverterFactory withNullSerialization()
    Return a new factory which includes null values into the serialized JSON.

    Methods inherited from class retrofit2.Converter.Factory

    getParameterUpperBound, getRawType, stringConverter

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • create

      public static MoshiConverterFactory create()
      Create an instance using a default Moshi instance for conversion.
    • create

      public static MoshiConverterFactory create​(com.squareup.moshi.Moshi moshi)
      Create an instance using moshi for conversion.
    • asLenient

      public MoshiConverterFactory asLenient()
      Return a new factory which uses lenient adapters.
    • failOnUnknown

      public MoshiConverterFactory failOnUnknown()
      Return a new factory which uses JsonAdapter.failOnUnknown() adapters.
    • withNullSerialization

      public MoshiConverterFactory withNullSerialization()
      Return a new factory which includes null values into the serialized JSON.
    • responseBodyConverter

      public retrofit2.Converter<okhttp3.ResponseBody,​?> responseBodyConverter​(java.lang.reflect.Type type, java.lang.annotation.Annotation[] annotations, retrofit2.Retrofit retrofit)
      Overrides:
      responseBodyConverter in class retrofit2.Converter.Factory
    • requestBodyConverter

      public retrofit2.Converter<?,​okhttp3.RequestBody> requestBodyConverter​(java.lang.reflect.Type type, java.lang.annotation.Annotation[] parameterAnnotations, java.lang.annotation.Annotation[] methodAnnotations, retrofit2.Retrofit retrofit)
      Overrides:
      requestBodyConverter in class retrofit2.Converter.Factory