@Test(expected = JsonSyntaxException.class) public void nonEnumRead() throws IOException { final TypeAdapter<DistributionType> typeAdapter = this.typeAdapterFactory.create(this.gson, TypeToken.get(DistributionType.class)); when(this.reader.nextString()).thenReturn("fakeDistribution"); typeAdapter.read(this.reader); }
@Override public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) { @SuppressWarnings("unchecked") final Class<T> rawType = (Class<T>) type.getRawType(); if (!rawType.isEnum()) { return null; } return new TypeAdapter<T>() { @Override public void write(final JsonWriter out, final T value) throws IOException { out.value(value.toString().toLowerCase(Locale.US)); } @Override @SuppressWarnings("unchecked") public T read(final JsonReader in) throws IOException { final String s = in.nextString().toUpperCase(Locale.US); for (final Object enumEntry : rawType.getEnumConstants()) { if (enumEntry.toString().equals(s)) { return (T) enumEntry; } } throw new JsonSyntaxException(String.format("Could not parse into enum [%s]", s)); } }.nullSafe(); }
CaseInsensitiveEnumTypeAdapterFactory implements TypeAdapterFactory { @Override public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) { @SuppressWarnings("unchecked") final Class<T> rawType = (Class<T>) type.getRawType(); if (!rawType.isEnum()) { return null; } return new TypeAdapter<T>() { @Override public void write(final JsonWriter out, final T value) throws IOException { out.value(value.toString().toLowerCase(Locale.US)); } @Override @SuppressWarnings("unchecked") public T read(final JsonReader in) throws IOException { final String s = in.nextString().toUpperCase(Locale.US); for (final Object enumEntry : rawType.getEnumConstants()) { if (enumEntry.toString().equals(s)) { return (T) enumEntry; } } throw new JsonSyntaxException(String.format("Could not parse into enum [%s]", s)); } }.nullSafe(); } }
CaseInsensitiveEnumTypeAdapterFactory implements TypeAdapterFactory { @Override public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) { @SuppressWarnings("unchecked") final Class<T> rawType = (Class<T>) type.getRawType(); if (!rawType.isEnum()) { return null; } return new TypeAdapter<T>() { @Override public void write(final JsonWriter out, final T value) throws IOException { out.value(value.toString().toLowerCase(Locale.US)); } @Override @SuppressWarnings("unchecked") public T read(final JsonReader in) throws IOException { final String s = in.nextString().toUpperCase(Locale.US); for (final Object enumEntry : rawType.getEnumConstants()) { if (enumEntry.toString().equals(s)) { return (T) enumEntry; } } throw new JsonSyntaxException(String.format("Could not parse into enum [%s]", s)); } }.nullSafe(); }  }
CaseInsensitiveEnumTypeAdapterFactory implements TypeAdapterFactory { @Override public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) { @SuppressWarnings("unchecked") final Class<T> rawType = (Class<T>) type.getRawType(); if (!rawType.isEnum()) { return null; } return new TypeAdapter<T>() { @Override public void write(final JsonWriter out, final T value) throws IOException { out.value(value.toString().toLowerCase(Locale.US)); } @Override @SuppressWarnings("unchecked") public T read(final JsonReader in) throws IOException { final String s = in.nextString().toUpperCase(Locale.US); for (final Object enumEntry : rawType.getEnumConstants()) { if (enumEntry.toString().equals(s)) { return (T) enumEntry; } } throw new JsonSyntaxException(String.format("Could not parse into enum [%s]", s)); } }.nullSafe(); }  @Override TypeAdapter<T> create(final Gson gson, final TypeToken<T> type); }
CaseInsensitiveEnumTypeAdapterFactory implements TypeAdapterFactory { @Override public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) { @SuppressWarnings("unchecked") final Class<T> rawType = (Class<T>) type.getRawType(); if (!rawType.isEnum()) { return null; } return new TypeAdapter<T>() { @Override public void write(final JsonWriter out, final T value) throws IOException { out.value(value.toString().toLowerCase(Locale.US)); } @Override @SuppressWarnings("unchecked") public T read(final JsonReader in) throws IOException { final String s = in.nextString().toUpperCase(Locale.US); for (final Object enumEntry : rawType.getEnumConstants()) { if (enumEntry.toString().equals(s)) { return (T) enumEntry; } } throw new JsonSyntaxException(String.format("Could not parse into enum [%s]", s)); } }.nullSafe(); }  @Override TypeAdapter<T> create(final Gson gson, final TypeToken<T> type);  }