mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
Squashed commit of the following:
commit f4e6b9aab54b270ca703794fde9370d00821dfca Author: Adam Koski <dandc87@gmail.com> Date: Sun Apr 12 19:25:49 2015 -0700 Add copyright headers and remove redundant test commit 064dfbb5ec7c6ed8e60c91776ce0a4e90a29ff9e Author: Adam Koski <dandc87@gmail.com> Date: Sun Apr 12 13:10:17 2015 -0700 Fix error preventing merging commit d80c00ef2b7220397dd074dd6ceb03b9be9ec416 Author: Adam Koski <dandc87@gmail.com> Date: Sun Apr 12 12:59:46 2015 -0700 Switch to using name() commit 7e68f21537a58843c0426c5e73028e12b0522b96 Author: Adam Koski <dandc87@gmail.com> Date: Sat Apr 11 22:37:25 2015 -0700 Optimize enum value retrieval commit 1422abd632ca147252f5b0698c8b19dc77ca3fac Author: Adam Koski <dandc87@gmail.com> Date: Sat Apr 11 22:35:06 2015 -0700 Add enum test for custom names commit 8a7fefe28a98576f8db849fe83fb7973fefd1f12 Author: Adam Koski <dandc87@gmail.com> Date: Fri Apr 10 19:32:46 2015 -0700 Add javadoc commit 920b7c6883664df39d99eb206b933c4929157d84 Author: Adam Koski <dandc87@gmail.com> Date: Fri Apr 10 19:21:18 2015 -0700 Rename class to match convention commit 461e87aa02552e09d0c4a0feaf2971e6050aa994 Author: Adam Koski <dandc87@gmail.com> Date: Thu Apr 9 23:20:05 2015 -0700 Add initial support for enums
This commit is contained in:
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<>();
|
||||
factories.addAll(builder.factories);
|
||||
factories.add(StandardJsonAdapters.FACTORY);
|
||||
factories.add(EnumJsonAdapter.FACTORY);
|
||||
factories.add(CollectionJsonAdapter.FACTORY);
|
||||
factories.add(MapJsonAdapter.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