diff --git a/moshi/src/main/java/com/squareup/moshi/EnumJsonAdapter.java b/moshi/src/main/java/com/squareup/moshi/EnumJsonAdapter.java new file mode 100644 index 0000000..2f12721 --- /dev/null +++ b/moshi/src/main/java/com/squareup/moshi/EnumJsonAdapter.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2014 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.squareup.moshi; + +import java.io.IOException; +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Type; + +/** + * Convert Enum types to JSON string values + * + * @param Enum class to adapt + */ +final class EnumJsonAdapter> extends JsonAdapter { + public static final Factory FACTORY = new Factory() { + @Override public JsonAdapter create(Type type, AnnotatedElement annotations, + Moshi moshi) { + Class rawType = Types.getRawType(type); + if (Enum.class.isAssignableFrom(rawType)) { + //noinspection unchecked + Class enumType = (Class) rawType; + return new EnumJsonAdapter<>(enumType.getEnumConstants()).nullSafe(); + } + return null; + } + }; + + private final T[] values; + + private EnumJsonAdapter(T[] values) { + this.values = values; + } + + @Override + public T fromJson(JsonReader reader) throws IOException { + String name = reader.nextString(); + for (T value : values) { + if (value.name().equals(name)) { + return value; + } + } + return null; + } + + @Override + public void toJson(JsonWriter writer, T value) throws IOException { + writer.value(value.name()); + } +} diff --git a/moshi/src/main/java/com/squareup/moshi/Moshi.java b/moshi/src/main/java/com/squareup/moshi/Moshi.java index 2973883..9098f47 100644 --- a/moshi/src/main/java/com/squareup/moshi/Moshi.java +++ b/moshi/src/main/java/com/squareup/moshi/Moshi.java @@ -34,6 +34,7 @@ public final class Moshi { List factories = new ArrayList<>(); factories.addAll(builder.factories); factories.add(StandardJsonAdapters.FACTORY); + factories.add(EnumJsonAdapter.FACTORY); factories.add(CollectionJsonAdapter.FACTORY); factories.add(MapJsonAdapter.FACTORY); factories.add(ArrayJsonAdapter.FACTORY); diff --git a/moshi/src/test/java/com/squareup/moshi/EnumJsonAdapterTest.java b/moshi/src/test/java/com/squareup/moshi/EnumJsonAdapterTest.java new file mode 100644 index 0000000..4507e04 --- /dev/null +++ b/moshi/src/test/java/com/squareup/moshi/EnumJsonAdapterTest.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2014 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.squareup.moshi; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class EnumJsonAdapterTest { + private final Moshi moshi = new Moshi.Builder().build(); + + enum Roshambo { + ROCK, + PAPER, + SCISSORS + } + + @Test public void basics() throws Exception { + JsonAdapter adapter = moshi.adapter(Roshambo.class).lenient(); + assertThat(adapter.fromJson("\"ROCK\"")).isEqualTo(Roshambo.ROCK); + assertThat(adapter.toJson(Roshambo.PAPER)).isEqualTo("\"PAPER\""); + } + + @Test public void invalidInput() throws Exception { + JsonAdapter adapter = moshi.adapter(Roshambo.class).lenient(); + assertThat(adapter.fromJson("\"Lizard\"")).isNull(); + assertThat(adapter.fromJson("\"SPOCK\"")).isNull(); + } + + @Test public void nulls() throws Exception { + JsonAdapter adapter = moshi.adapter(Roshambo.class).lenient(); + assertThat(adapter.fromJson("null")).isNull(); + assertThat(adapter.toJson(null)).isEqualTo("null"); + } +} \ No newline at end of file