mirror of
https://github.com/fankes/moshi.git
synced 2025-10-18 23:49:21 +08:00
CollectionsJsonAdapter.
This commit is contained in:
@@ -0,0 +1,88 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/** Converts collection types to JSON arrays containing their converted contents. */
|
||||||
|
abstract class CollectionJsonAdapter<C extends Collection<T>, T> extends JsonAdapter<C> {
|
||||||
|
public static final JsonAdapter.Factory FACTORY = new JsonAdapter.Factory() {
|
||||||
|
@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();
|
||||||
|
} else if (rawType == Set.class) {
|
||||||
|
return newLinkedHashSetAdapter(type, annotations, moshi).nullSafe();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private final JsonAdapter<T> elementAdapter;
|
||||||
|
|
||||||
|
private CollectionJsonAdapter(JsonAdapter<T> elementAdapter) {
|
||||||
|
this.elementAdapter = elementAdapter;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> JsonAdapter<Collection<T>> newArrayListAdapter(
|
||||||
|
Type type, AnnotatedElement annotations, Moshi moshi) {
|
||||||
|
Type elementType = Types.collectionElementType(type, Collection.class);
|
||||||
|
JsonAdapter<T> elementAdapter = moshi.adapter(elementType, annotations);
|
||||||
|
return new CollectionJsonAdapter<Collection<T>, T>(elementAdapter) {
|
||||||
|
@Override Collection<T> newCollection() {
|
||||||
|
return new ArrayList<T>();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> JsonAdapter<Set<T>> newLinkedHashSetAdapter(
|
||||||
|
Type type, AnnotatedElement annotations, Moshi moshi) {
|
||||||
|
Type elementType = Types.collectionElementType(type, Collection.class);
|
||||||
|
JsonAdapter<T> elementAdapter = moshi.adapter(elementType, annotations);
|
||||||
|
return new CollectionJsonAdapter<Set<T>, T>(elementAdapter) {
|
||||||
|
@Override Set<T> newCollection() {
|
||||||
|
return new LinkedHashSet<T>();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract C newCollection();
|
||||||
|
|
||||||
|
@Override public C fromJson(JsonReader reader) throws IOException {
|
||||||
|
C result = newCollection();
|
||||||
|
reader.beginArray();
|
||||||
|
while (reader.hasNext()) {
|
||||||
|
result.add(elementAdapter.fromJson(reader));
|
||||||
|
}
|
||||||
|
reader.endArray();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public void toJson(JsonWriter writer, C value) throws IOException {
|
||||||
|
writer.beginArray();
|
||||||
|
for (T element : value) {
|
||||||
|
elementAdapter.toJson(writer, element);
|
||||||
|
}
|
||||||
|
writer.endArray();
|
||||||
|
}
|
||||||
|
}
|
@@ -19,7 +19,6 @@ import java.io.Closeable;
|
|||||||
import java.io.Flushable;
|
import java.io.Flushable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import okio.BufferedSink;
|
import okio.BufferedSink;
|
||||||
import okio.Okio;
|
|
||||||
import okio.Sink;
|
import okio.Sink;
|
||||||
|
|
||||||
import static com.squareup.moshi.JsonScope.DANGLING_NAME;
|
import static com.squareup.moshi.JsonScope.DANGLING_NAME;
|
||||||
@@ -183,11 +182,11 @@ public class JsonWriter implements Closeable, Flushable {
|
|||||||
/**
|
/**
|
||||||
* Creates a new instance that writes a JSON-encoded stream to {@code sink}.
|
* Creates a new instance that writes a JSON-encoded stream to {@code sink}.
|
||||||
*/
|
*/
|
||||||
public JsonWriter(Sink sink) {
|
public JsonWriter(BufferedSink sink) {
|
||||||
if (sink == null) {
|
if (sink == null) {
|
||||||
throw new NullPointerException("sink == null");
|
throw new NullPointerException("sink == null");
|
||||||
}
|
}
|
||||||
this.sink = Okio.buffer(sink);
|
this.sink = sink;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -30,7 +30,8 @@ public final class Moshi {
|
|||||||
private Moshi(Builder builder) {
|
private Moshi(Builder builder) {
|
||||||
List<JsonAdapter.Factory> factories = new ArrayList<JsonAdapter.Factory>();
|
List<JsonAdapter.Factory> factories = new ArrayList<JsonAdapter.Factory>();
|
||||||
factories.addAll(builder.factories);
|
factories.addAll(builder.factories);
|
||||||
factories.add(new StandardJsonAdapterFactory());
|
factories.add(StandardJsonAdapters.FACTORY);
|
||||||
|
factories.add(CollectionJsonAdapter.FACTORY);
|
||||||
this.factories = Collections.unmodifiableList(factories);
|
this.factories = Collections.unmodifiableList(factories);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -19,7 +19,22 @@ import java.io.IOException;
|
|||||||
import java.lang.reflect.AnnotatedElement;
|
import java.lang.reflect.AnnotatedElement;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
final class StandardJsonAdapterFactory implements JsonAdapter.Factory {
|
final class StandardJsonAdapters {
|
||||||
|
public static final JsonAdapter.Factory FACTORY = new JsonAdapter.Factory() {
|
||||||
|
@Override public JsonAdapter<?> create(
|
||||||
|
Type type, AnnotatedElement annotations, Moshi moshi) {
|
||||||
|
// TODO: support all 8 primitive types.
|
||||||
|
if (type == boolean.class) return BOOLEAN_JSON_ADAPTER;
|
||||||
|
if (type == double.class) return DOUBLE_JSON_ADAPTER;
|
||||||
|
if (type == int.class) return INTEGER_JSON_ADAPTER;
|
||||||
|
if (type == Boolean.class) return BOOLEAN_JSON_ADAPTER.nullSafe();
|
||||||
|
if (type == Double.class) return DOUBLE_JSON_ADAPTER.nullSafe();
|
||||||
|
if (type == Integer.class) return INTEGER_JSON_ADAPTER.nullSafe();
|
||||||
|
if (type == String.class) return STRING_JSON_ADAPTER.nullSafe();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
static final JsonAdapter<Boolean> BOOLEAN_JSON_ADAPTER = new JsonAdapter<Boolean>() {
|
static final JsonAdapter<Boolean> BOOLEAN_JSON_ADAPTER = new JsonAdapter<Boolean>() {
|
||||||
@Override public Boolean fromJson(JsonReader reader) throws IOException {
|
@Override public Boolean fromJson(JsonReader reader) throws IOException {
|
||||||
return reader.nextBoolean();
|
return reader.nextBoolean();
|
||||||
@@ -55,17 +70,4 @@ final class StandardJsonAdapterFactory implements JsonAdapter.Factory {
|
|||||||
writer.value(value);
|
writer.value(value);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@Override public JsonAdapter<?> create(
|
|
||||||
Type type, AnnotatedElement annotations, Moshi moshi) {
|
|
||||||
// TODO: support all 8 primitive types.
|
|
||||||
if (type == boolean.class) return BOOLEAN_JSON_ADAPTER;
|
|
||||||
if (type == double.class) return DOUBLE_JSON_ADAPTER;
|
|
||||||
if (type == int.class) return INTEGER_JSON_ADAPTER;
|
|
||||||
if (type == Boolean.class) return BOOLEAN_JSON_ADAPTER.nullSafe();
|
|
||||||
if (type == Double.class) return DOUBLE_JSON_ADAPTER.nullSafe();
|
|
||||||
if (type == Integer.class) return INTEGER_JSON_ADAPTER.nullSafe();
|
|
||||||
if (type == String.class) return STRING_JSON_ADAPTER.nullSafe();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
@@ -18,8 +18,15 @@ package com.squareup.moshi;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.reflect.AnnotatedElement;
|
import java.lang.reflect.AnnotatedElement;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.Set;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
@@ -106,6 +113,52 @@ public final class MoshiTest {
|
|||||||
assertThat(adapter.fromJson("\"b\"")).isEqualTo("B");
|
assertThat(adapter.fromJson("\"b\"")).isEqualTo("B");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test public void listJsonAdapter() throws Exception {
|
||||||
|
Moshi moshi = new Moshi.Builder().build();
|
||||||
|
JsonAdapter<List<String>> adapter = moshi.adapter(new TypeLiteral<List<String>>() {
|
||||||
|
}.getType());
|
||||||
|
assertThat(adapter.toJson(Arrays.asList("a", "b"))).isEqualTo("[\"a\",\"b\"]");
|
||||||
|
assertThat(adapter.fromJson("[\"a\",\"b\"]")).isEqualTo(Arrays.asList("a", "b"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void setJsonAdapter() throws Exception {
|
||||||
|
Set<String> set = new LinkedHashSet<String>();
|
||||||
|
set.add("a");
|
||||||
|
set.add("b");
|
||||||
|
|
||||||
|
Moshi moshi = new Moshi.Builder().build();
|
||||||
|
JsonAdapter<Set<String>> adapter = moshi.adapter(new TypeLiteral<Set<String>>() {}.getType());
|
||||||
|
assertThat(adapter.toJson(set)).isEqualTo("[\"a\",\"b\"]");
|
||||||
|
assertThat(adapter.fromJson("[\"a\",\"b\"]")).isEqualTo(set);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void collectionJsonAdapter() throws Exception {
|
||||||
|
Collection<String> collection = new ArrayDeque<String>();
|
||||||
|
collection.add("a");
|
||||||
|
collection.add("b");
|
||||||
|
|
||||||
|
Moshi moshi = new Moshi.Builder().build();
|
||||||
|
JsonAdapter<Collection<String>> adapter = moshi.adapter(
|
||||||
|
new TypeLiteral<Collection<String>>() {}.getType());
|
||||||
|
assertThat(adapter.toJson(collection)).isEqualTo("[\"a\",\"b\"]");
|
||||||
|
assertThat(adapter.fromJson("[\"a\",\"b\"]")).containsExactly("a", "b");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Uppercase
|
||||||
|
static List<String> uppercaseStrings;
|
||||||
|
|
||||||
|
@Test public void collectionsKeepAnnotations() throws Exception {
|
||||||
|
Moshi moshi = new Moshi.Builder()
|
||||||
|
.add(new UppercaseAdapterFactory())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
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"));
|
||||||
|
}
|
||||||
|
|
||||||
static class Pizza {
|
static class Pizza {
|
||||||
final int diameter;
|
final int diameter;
|
||||||
final boolean extraCheese;
|
final boolean extraCheese;
|
||||||
|
Reference in New Issue
Block a user