SafeEnumJsonAdapter extends JsonAdapter<T> { @Override public T fromJson(JsonReader reader) throws IOException { String name = reader.nextString(); return nameConstantMap.get(name); } SafeEnumJsonAdapter(Class<T> enumType); @Override T fromJson(JsonReader reader); @Override void toJson(JsonWriter writer, T value); @Override String toString(); static final JsonAdapter.Factory FACTORY; }
@Test public void invalidEnum() throws Exception { JsonAdapter<Roshambo> adapter = moshi.adapter(Roshambo.class).lenient(); assertThat(adapter.fromJson("\"SPOCK\"")).isNull(); }