mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
62
moshi/src/main/java/com/squareup/moshi/EnumJsonAdapter.java
Normal file
62
moshi/src/main/java/com/squareup/moshi/EnumJsonAdapter.java
Normal file
@@ -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 <T> Enum class to adapt
|
||||||
|
*/
|
||||||
|
final class EnumJsonAdapter<T extends Enum<T>> extends JsonAdapter<T> {
|
||||||
|
public static final Factory FACTORY = new Factory() {
|
||||||
|
@Override public JsonAdapter<? extends Enum> create(Type type, AnnotatedElement annotations,
|
||||||
|
Moshi moshi) {
|
||||||
|
Class<?> rawType = Types.getRawType(type);
|
||||||
|
if (Enum.class.isAssignableFrom(rawType)) {
|
||||||
|
//noinspection unchecked
|
||||||
|
Class<? extends Enum> enumType = (Class<? extends Enum>) 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());
|
||||||
|
}
|
||||||
|
}
|
@@ -34,6 +34,7 @@ public final class Moshi {
|
|||||||
List<JsonAdapter.Factory> factories = new ArrayList<>();
|
List<JsonAdapter.Factory> factories = new ArrayList<>();
|
||||||
factories.addAll(builder.factories);
|
factories.addAll(builder.factories);
|
||||||
factories.add(StandardJsonAdapters.FACTORY);
|
factories.add(StandardJsonAdapters.FACTORY);
|
||||||
|
factories.add(EnumJsonAdapter.FACTORY);
|
||||||
factories.add(CollectionJsonAdapter.FACTORY);
|
factories.add(CollectionJsonAdapter.FACTORY);
|
||||||
factories.add(MapJsonAdapter.FACTORY);
|
factories.add(MapJsonAdapter.FACTORY);
|
||||||
factories.add(ArrayJsonAdapter.FACTORY);
|
factories.add(ArrayJsonAdapter.FACTORY);
|
||||||
|
@@ -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<Roshambo> 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<Roshambo> adapter = moshi.adapter(Roshambo.class).lenient();
|
||||||
|
assertThat(adapter.fromJson("\"Lizard\"")).isNull();
|
||||||
|
assertThat(adapter.fromJson("\"SPOCK\"")).isNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void nulls() throws Exception {
|
||||||
|
JsonAdapter<Roshambo> adapter = moshi.adapter(Roshambo.class).lenient();
|
||||||
|
assertThat(adapter.fromJson("null")).isNull();
|
||||||
|
assertThat(adapter.toJson(null)).isEqualTo("null");
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user