gson-comments/gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java

107 lines
3.4 KiB
Java
Raw Normal View History

2011-07-12 00:26:53 +02:00
/*
* Copyright (C) 2011 Google 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.
*/
2011-08-03 02:25:10 +02:00
package com.google.gson.internal.bind;
2011-07-12 00:26:53 +02:00
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.internal.$Gson$Types;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
2011-07-12 00:26:53 +02:00
/**
* Adapt an array of objects.
*/
public final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
@SuppressWarnings({"unchecked", "rawtypes"})
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
2011-07-12 00:26:53 +02:00
Type type = typeToken.getType();
if (!(type instanceof GenericArrayType || type instanceof Class && ((Class<?>) type).isArray())) {
return null;
}
Type componentType = $Gson$Types.getArrayComponentType(type);
2011-12-02 23:57:30 +01:00
TypeAdapter<?> componentTypeAdapter = gson.getAdapter(TypeToken.get(componentType));
return new ArrayTypeAdapter(
2011-12-02 23:57:30 +01:00
gson, componentTypeAdapter, $Gson$Types.getRawType(componentType));
2011-07-12 00:26:53 +02:00
}
};
private final Class<E> componentType;
private final TypeAdapter<E> componentTypeAdapter;
public ArrayTypeAdapter(Gson context, TypeAdapter<E> componentTypeAdapter, Class<E> componentType) {
this.componentTypeAdapter =
new TypeAdapterRuntimeTypeWrapper<>(context, componentTypeAdapter, componentType);
2011-07-12 00:26:53 +02:00
this.componentType = componentType;
}
@Override public Object read(JsonReader in) throws IOException {
2011-12-03 20:46:25 +01:00
if (in.peek() == JsonToken.NULL) {
in.nextNull();
2011-07-12 00:26:53 +02:00
return null;
}
2022-05-17 21:20:10 +02:00
Object array;
if (in.isLenient() && in.peek() != JsonToken.BEGIN_ARRAY) {
// Coerce
array = Array.newInstance(componentType, 1);
Array.set(array, 0, componentTypeAdapter.read(in));
return array;
}
2011-07-12 00:26:53 +02:00
List<E> list = new ArrayList<>();
2011-12-03 20:46:25 +01:00
in.beginArray();
while (in.hasNext()) {
E instance = componentTypeAdapter.read(in);
2011-07-12 00:26:53 +02:00
list.add(instance);
}
2011-12-03 20:46:25 +01:00
in.endArray();
int size = list.size();
2022-05-17 21:20:10 +02:00
array = Array.newInstance(componentType, size);
for (int i = 0; i < size; i++) {
2011-07-12 00:26:53 +02:00
Array.set(array, i, list.get(i));
}
return array;
}
2011-09-16 08:03:16 +02:00
@SuppressWarnings("unchecked")
2011-12-03 20:46:25 +01:00
@Override public void write(JsonWriter out, Object array) throws IOException {
2011-07-12 00:26:53 +02:00
if (array == null) {
2011-12-03 20:46:25 +01:00
out.nullValue();
2011-07-12 00:26:53 +02:00
return;
}
2011-12-03 20:46:25 +01:00
out.beginArray();
2011-07-12 00:26:53 +02:00
for (int i = 0, length = Array.getLength(array); i < length; i++) {
E value = (E) Array.get(array, i);
2011-12-03 20:46:25 +01:00
componentTypeAdapter.write(out, value);
2011-07-12 00:26:53 +02:00
}
2011-12-03 20:46:25 +01:00
out.endArray();
2011-07-12 00:26:53 +02:00
}
}