Search in sources :

Example 1 with MoshiConverterFactory

use of retrofit2.converter.moshi.MoshiConverterFactory in project retrofit by square.

the class AnnotatedConverters method main.

public static void main(String... args) throws IOException {
    MockWebServer server = new MockWebServer();
    server.start();
    server.enqueue(new MockResponse().setBody("{\"name\": \"Moshi\"}"));
    server.enqueue(new MockResponse().setBody("{\"name\": \"Gson\"}"));
    server.enqueue(new MockResponse().setBody("<user name=\"SimpleXML\"/>"));
    server.enqueue(new MockResponse().setBody("{\"name\": \"Gson\"}"));
    com.squareup.moshi.Moshi moshi = new com.squareup.moshi.Moshi.Builder().build();
    com.google.gson.Gson gson = new GsonBuilder().create();
    MoshiConverterFactory moshiConverterFactory = MoshiConverterFactory.create(moshi);
    GsonConverterFactory gsonConverterFactory = GsonConverterFactory.create(gson);
    SimpleXmlConverterFactory simpleXmlConverterFactory = SimpleXmlConverterFactory.create();
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new AnnotatedConverterFactory.Builder().add(Moshi.class, moshiConverterFactory).add(Gson.class, gsonConverterFactory).add(SimpleXml.class, simpleXmlConverterFactory).build()).addConverterFactory(gsonConverterFactory).build();
    Service service = retrofit.create(Service.class);
    Library library1 = service.exampleMoshi().execute().body();
    System.out.println("Library 1: " + library1.name);
    Library library2 = service.exampleGson().execute().body();
    System.out.println("Library 2: " + library2.name);
    Library library3 = service.exampleSimpleXml().execute().body();
    System.out.println("Library 3: " + library3.name);
    Library library4 = service.exampleDefault().execute().body();
    System.out.println("Library 4: " + library4.name);
    server.shutdown();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) SimpleXmlConverterFactory(retrofit2.converter.simplexml.SimpleXmlConverterFactory) Retrofit(retrofit2.Retrofit) MoshiConverterFactory(retrofit2.converter.moshi.MoshiConverterFactory) MockWebServer(okhttp3.mockwebserver.MockWebServer) GsonConverterFactory(retrofit2.converter.gson.GsonConverterFactory)

Example 2 with MoshiConverterFactory

use of retrofit2.converter.moshi.MoshiConverterFactory in project retrofit by square.

the class MoshiConverterFactoryTest method setUp.

@Before
public void setUp() {
    Moshi moshi = new Moshi.Builder().add(new JsonAdapter.Factory() {

        @Override
        public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) {
            for (Annotation annotation : annotations) {
                if (!annotation.annotationType().isAnnotationPresent(JsonQualifier.class)) {
                    throw new AssertionError("Non-@JsonQualifier annotation: " + annotation);
                }
            }
            return null;
        }
    }).add(new Adapters()).build();
    MoshiConverterFactory factory = MoshiConverterFactory.create(moshi);
    MoshiConverterFactory factoryLenient = factory.asLenient();
    MoshiConverterFactory factoryNulls = factory.withNullSerialization();
    MoshiConverterFactory factoryFailOnUnknown = factory.failOnUnknown();
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(factory).build();
    Retrofit retrofitLenient = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(factoryLenient).build();
    Retrofit retrofitNulls = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(factoryNulls).build();
    Retrofit retrofitFailOnUnknown = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(factoryFailOnUnknown).build();
    service = retrofit.create(Service.class);
    serviceLenient = retrofitLenient.create(Service.class);
    serviceNulls = retrofitNulls.create(Service.class);
    serviceFailOnUnknown = retrofitFailOnUnknown.create(Service.class);
}
Also used : Moshi(com.squareup.moshi.Moshi) JsonAdapter(com.squareup.moshi.JsonAdapter) Annotation(java.lang.annotation.Annotation) Retrofit(retrofit2.Retrofit) Type(java.lang.reflect.Type) Before(org.junit.Before)

Aggregations

Retrofit (retrofit2.Retrofit)2 GsonBuilder (com.google.gson.GsonBuilder)1 JsonAdapter (com.squareup.moshi.JsonAdapter)1 Moshi (com.squareup.moshi.Moshi)1 Annotation (java.lang.annotation.Annotation)1 Type (java.lang.reflect.Type)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 MockWebServer (okhttp3.mockwebserver.MockWebServer)1 Before (org.junit.Before)1 GsonConverterFactory (retrofit2.converter.gson.GsonConverterFactory)1 MoshiConverterFactory (retrofit2.converter.moshi.MoshiConverterFactory)1 SimpleXmlConverterFactory (retrofit2.converter.simplexml.SimpleXmlConverterFactory)1