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"));
}
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);
}
Aggregations