Search in sources :

Example 1 with JsonAdapter

use of com.squareup.moshi.JsonAdapter in project jsonschema2pojo by joelittlejohn.

the class Moshi1IT method enumValuesAreSerializedCorrectly.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void enumValuesAreSerializedCorrectly() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/enum/typeWithEnumProperty.json", "com.example", config("annotationStyle", "moshi1", "propertyWordDelimiters", "_"));
    Class generatedType = resultsClassLoader.loadClass("com.example.TypeWithEnumProperty");
    Class enumType = resultsClassLoader.loadClass("com.example.TypeWithEnumProperty$EnumProperty");
    Object instance = generatedType.newInstance();
    Method setter = generatedType.getMethod("setEnumProperty", enumType);
    setter.invoke(instance, enumType.getEnumConstants()[3]);
    JsonAdapter jsonAdapter = moshi.adapter(generatedType);
    String json = jsonAdapter.toJson(instance);
    Map<String, String> jsonAsMap = new Gson().fromJson(json, Map.class);
    assertThat(jsonAsMap.get("enum_Property"), is("4 ! 1"));
}
Also used : Gson(com.google.gson.Gson) Method(java.lang.reflect.Method) JsonAdapter(com.squareup.moshi.JsonAdapter) Test(org.junit.Test)

Example 2 with JsonAdapter

use of com.squareup.moshi.JsonAdapter 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

JsonAdapter (com.squareup.moshi.JsonAdapter)2 Gson (com.google.gson.Gson)1 Moshi (com.squareup.moshi.Moshi)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 Before (org.junit.Before)1 Test (org.junit.Test)1 Retrofit (retrofit2.Retrofit)1