mirror of
https://github.com/fankes/moshi.git
synced 2025-10-18 23:49:21 +08:00
Merge pull request #10 from square/jwilson_0812_arrays
Implement an array adapter.
This commit is contained in:
69
moshi/src/main/java/com/squareup/moshi/ArrayJsonAdapter.java
Normal file
69
moshi/src/main/java/com/squareup/moshi/ArrayJsonAdapter.java
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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.Array;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Converts arrays to JSON arrays containing their converted contents. This
|
||||
* supports both primitive and object arrays.
|
||||
*/
|
||||
class ArrayJsonAdapter extends JsonAdapter<Object> {
|
||||
public static final Factory FACTORY = new Factory() {
|
||||
@Override public JsonAdapter<?> create(Type type, AnnotatedElement annotations, Moshi moshi) {
|
||||
Type elementType = Types.arrayComponentType(type);
|
||||
if (elementType == null) return null;
|
||||
Class<?> elementClass = Types.getRawType(elementType);
|
||||
JsonAdapter<Object> elementAdapter = moshi.adapter(elementType);
|
||||
return new ArrayJsonAdapter(elementClass, elementAdapter).nullSafe();
|
||||
}
|
||||
};
|
||||
|
||||
private final Class<?> elementClass;
|
||||
private final JsonAdapter<Object> elementAdapter;
|
||||
|
||||
ArrayJsonAdapter(Class<?> elementClass, JsonAdapter<Object> elementAdapter) {
|
||||
this.elementClass = elementClass;
|
||||
this.elementAdapter = elementAdapter;
|
||||
}
|
||||
|
||||
@Override public Object fromJson(JsonReader reader) throws IOException {
|
||||
List<Object> list = new ArrayList<Object>();
|
||||
reader.beginArray();
|
||||
while (reader.hasNext()) {
|
||||
list.add(elementAdapter.fromJson(reader));
|
||||
}
|
||||
reader.endArray();
|
||||
Object array = Array.newInstance(elementClass, list.size());
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
Array.set(array, i, list.get(i));
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
@Override public void toJson(JsonWriter writer, Object value) throws IOException {
|
||||
writer.beginArray();
|
||||
for (int i = 0, size = Array.getLength(value); i < size; i++) {
|
||||
elementAdapter.toJson(writer, Array.get(value, i));
|
||||
}
|
||||
writer.endArray();
|
||||
}
|
||||
}
|
@@ -30,9 +30,9 @@ abstract class CollectionJsonAdapter<C extends Collection<T>, T> extends JsonAda
|
||||
@Override public JsonAdapter<?> create(Type type, AnnotatedElement annotations, Moshi moshi) {
|
||||
Class<?> rawType = Types.getRawType(type);
|
||||
if (rawType == List.class || rawType == Collection.class) {
|
||||
return newArrayListAdapter(type, annotations, moshi).nullSafe();
|
||||
return newArrayListAdapter(type, moshi).nullSafe();
|
||||
} else if (rawType == Set.class) {
|
||||
return newLinkedHashSetAdapter(type, annotations, moshi).nullSafe();
|
||||
return newLinkedHashSetAdapter(type, moshi).nullSafe();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -44,10 +44,9 @@ abstract class CollectionJsonAdapter<C extends Collection<T>, T> extends JsonAda
|
||||
this.elementAdapter = elementAdapter;
|
||||
}
|
||||
|
||||
private static <T> JsonAdapter<Collection<T>> newArrayListAdapter(
|
||||
Type type, AnnotatedElement annotations, Moshi moshi) {
|
||||
private static <T> JsonAdapter<Collection<T>> newArrayListAdapter(Type type, Moshi moshi) {
|
||||
Type elementType = Types.collectionElementType(type, Collection.class);
|
||||
JsonAdapter<T> elementAdapter = moshi.adapter(elementType, annotations);
|
||||
JsonAdapter<T> elementAdapter = moshi.adapter(elementType);
|
||||
return new CollectionJsonAdapter<Collection<T>, T>(elementAdapter) {
|
||||
@Override Collection<T> newCollection() {
|
||||
return new ArrayList<T>();
|
||||
@@ -55,10 +54,9 @@ abstract class CollectionJsonAdapter<C extends Collection<T>, T> extends JsonAda
|
||||
};
|
||||
}
|
||||
|
||||
private static <T> JsonAdapter<Set<T>> newLinkedHashSetAdapter(
|
||||
Type type, AnnotatedElement annotations, Moshi moshi) {
|
||||
private static <T> JsonAdapter<Set<T>> newLinkedHashSetAdapter(Type type, Moshi moshi) {
|
||||
Type elementType = Types.collectionElementType(type, Collection.class);
|
||||
JsonAdapter<T> elementAdapter = moshi.adapter(elementType, annotations);
|
||||
JsonAdapter<T> elementAdapter = moshi.adapter(elementType);
|
||||
return new CollectionJsonAdapter<Set<T>, T>(elementAdapter) {
|
||||
@Override Set<T> newCollection() {
|
||||
return new LinkedHashSet<T>();
|
||||
|
@@ -32,6 +32,7 @@ public final class Moshi {
|
||||
factories.addAll(builder.factories);
|
||||
factories.add(StandardJsonAdapters.FACTORY);
|
||||
factories.add(CollectionJsonAdapter.FACTORY);
|
||||
factories.add(ArrayJsonAdapter.FACTORY);
|
||||
this.factories = Collections.unmodifiableList(factories);
|
||||
}
|
||||
|
||||
|
@@ -245,13 +245,17 @@ final class Types {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the component type of this array type.
|
||||
* @throws ClassCastException if this type is not an array.
|
||||
* Returns the element type of {@code type} if it is an array type, or null if it is not an
|
||||
* array type.
|
||||
*/
|
||||
public static Type arrayComponentType(Type array) {
|
||||
return array instanceof GenericArrayType
|
||||
? ((GenericArrayType) array).getGenericComponentType()
|
||||
: ((Class<?>) array).getComponentType();
|
||||
public static Type arrayComponentType(Type type) {
|
||||
if (type instanceof GenericArrayType) {
|
||||
return ((GenericArrayType) type).getGenericComponentType();
|
||||
} else if (type instanceof Class) {
|
||||
return ((Class<?>) type).getComponentType();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -147,7 +147,7 @@ public final class MoshiTest {
|
||||
@Uppercase
|
||||
static List<String> uppercaseStrings;
|
||||
|
||||
@Test public void collectionsKeepAnnotations() throws Exception {
|
||||
@Test public void collectionsDoNotKeepAnnotations() throws Exception {
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.add(new UppercaseAdapterFactory())
|
||||
.build();
|
||||
@@ -155,8 +155,22 @@ public final class MoshiTest {
|
||||
Field uppercaseStringsField = MoshiTest.class.getDeclaredField("uppercaseStrings");
|
||||
JsonAdapter<List<String>> adapter = moshi.adapter(uppercaseStringsField.getGenericType(),
|
||||
uppercaseStringsField);
|
||||
assertThat(adapter.toJson(Arrays.asList("a"))).isEqualTo("[\"A\"]");
|
||||
assertThat(adapter.fromJson("[\"b\"]")).isEqualTo(Arrays.asList("B"));
|
||||
assertThat(adapter.toJson(Arrays.asList("a"))).isEqualTo("[\"a\"]");
|
||||
assertThat(adapter.fromJson("[\"b\"]")).isEqualTo(Arrays.asList("b"));
|
||||
}
|
||||
|
||||
@Test public void objectArray() throws Exception {
|
||||
Moshi moshi = new Moshi.Builder().build();
|
||||
JsonAdapter<String[]> adapter = moshi.adapter(String[].class);
|
||||
assertThat(adapter.toJson(new String[] {"a", "b"})).isEqualTo("[\"a\",\"b\"]");
|
||||
assertThat(adapter.fromJson("[\"a\",\"b\"]")).containsExactly("a", "b");
|
||||
}
|
||||
|
||||
@Test public void primitiveArray() throws Exception {
|
||||
Moshi moshi = new Moshi.Builder().build();
|
||||
JsonAdapter<int[]> adapter = moshi.adapter(int[].class);
|
||||
assertThat(adapter.toJson(new int[] {1, 2})).isEqualTo("[1,2]");
|
||||
assertThat(adapter.fromJson("[2,3]")).containsExactly(2, 3);
|
||||
}
|
||||
|
||||
static class Pizza {
|
||||
|
Reference in New Issue
Block a user