From a0b177136dea220c7cb454dfcf7f964e116d5fab Mon Sep 17 00:00:00 2001 From: Adam Koski Date: Sun, 12 Apr 2015 19:27:38 -0700 Subject: [PATCH] Squashed commit of the following: commit f4e6b9aab54b270ca703794fde9370d00821dfca Author: Adam Koski Date: Sun Apr 12 19:25:49 2015 -0700 Add copyright headers and remove redundant test commit 064dfbb5ec7c6ed8e60c91776ce0a4e90a29ff9e Author: Adam Koski Date: Sun Apr 12 13:10:17 2015 -0700 Fix error preventing merging commit d80c00ef2b7220397dd074dd6ceb03b9be9ec416 Author: Adam Koski Date: Sun Apr 12 12:59:46 2015 -0700 Switch to using name() commit 7e68f21537a58843c0426c5e73028e12b0522b96 Author: Adam Koski Date: Sat Apr 11 22:37:25 2015 -0700 Optimize enum value retrieval commit 1422abd632ca147252f5b0698c8b19dc77ca3fac Author: Adam Koski Date: Sat Apr 11 22:35:06 2015 -0700 Add enum test for custom names commit 8a7fefe28a98576f8db849fe83fb7973fefd1f12 Author: Adam Koski Date: Fri Apr 10 19:32:46 2015 -0700 Add javadoc commit 920b7c6883664df39d99eb206b933c4929157d84 Author: Adam Koski Date: Fri Apr 10 19:21:18 2015 -0700 Rename class to match convention commit 461e87aa02552e09d0c4a0feaf2971e6050aa994 Author: Adam Koski Date: Thu Apr 9 23:20:05 2015 -0700 Add initial support for enums --- .../com/squareup/moshi/EnumJsonAdapter.java | 62 +++++++++++++++++++ .../main/java/com/squareup/moshi/Moshi.java | 1 + .../squareup/moshi/EnumJsonAdapterTest.java | 48 ++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 moshi/src/main/java/com/squareup/moshi/EnumJsonAdapter.java create mode 100644 moshi/src/test/java/com/squareup/moshi/EnumJsonAdapterTest.java 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