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
|
|
|
|
2012-10-18 04:37:43 +02:00
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.TypeAdapter;
|
|
|
|
import com.google.gson.TypeAdapterFactory;
|
2011-08-03 03:19:26 +02:00
|
|
|
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;
|
2022-08-22 16:22:32 +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;
|
2011-08-03 03:19:26 +02:00
|
|
|
|
2011-07-12 00:26:53 +02:00
|
|
|
/**
|
|
|
|
* Adapt an array of objects.
|
|
|
|
*/
|
2011-08-03 03:19:26 +02:00
|
|
|
public final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
|
2011-12-23 19:27:13 +01:00
|
|
|
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
|
2015-11-05 03:52:20 +01:00
|
|
|
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
|
2011-07-12 00:26:53 +02:00
|
|
|
Type type = typeToken.getType();
|
2023-02-15 14:18:43 +01:00
|
|
|
if (!(type instanceof GenericArrayType || (type instanceof Class && ((Class<?>) type).isArray()))) {
|
2011-07-12 00:26:53 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Type componentType = $Gson$Types.getArrayComponentType(type);
|
2011-12-02 23:57:30 +01:00
|
|
|
TypeAdapter<?> componentTypeAdapter = gson.getAdapter(TypeToken.get(componentType));
|
2022-08-27 02:36:18 +02:00
|
|
|
|
|
|
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
|
|
|
TypeAdapter<T> arrayAdapter = new ArrayTypeAdapter(
|
2011-12-02 23:57:30 +01:00
|
|
|
gson, componentTypeAdapter, $Gson$Types.getRawType(componentType));
|
2022-08-27 02:36:18 +02:00
|
|
|
return arrayAdapter;
|
2011-07-12 00:26:53 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private final Class<E> componentType;
|
|
|
|
private final TypeAdapter<E> componentTypeAdapter;
|
|
|
|
|
2011-11-20 16:23:08 +01:00
|
|
|
public ArrayTypeAdapter(Gson context, TypeAdapter<E> componentTypeAdapter, Class<E> componentType) {
|
2011-08-05 02:41:24 +02:00
|
|
|
this.componentTypeAdapter =
|
2022-04-18 00:27:21 +02:00
|
|
|
new TypeAdapterRuntimeTypeWrapper<>(context, componentTypeAdapter, componentType);
|
2011-07-12 00:26:53 +02:00
|
|
|
this.componentType = componentType;
|
|
|
|
}
|
|
|
|
|
2015-11-05 03:52:20 +01:00
|
|
|
@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
|
|
|
if (in.isLenient() && in.peek() != JsonToken.BEGIN_ARRAY) {
|
|
|
|
// Coerce
|
2022-09-14 19:10:47 +02:00
|
|
|
Object array = Array.newInstance(componentType, 1);
|
2022-05-17 21:20:10 +02:00
|
|
|
Array.set(array, 0, componentTypeAdapter.read(in));
|
|
|
|
return array;
|
|
|
|
}
|
2011-07-12 00:26:53 +02:00
|
|
|
|
2022-07-31 23:49:02 +02:00
|
|
|
ArrayList<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();
|
2017-03-01 17:13:56 +01:00
|
|
|
|
|
|
|
int size = list.size();
|
2022-07-31 23:49:02 +02:00
|
|
|
// Have to copy primitives one by one to primitive array
|
|
|
|
if (componentType.isPrimitive()) {
|
|
|
|
Object array = Array.newInstance(componentType, size);
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
Array.set(array, i, list.get(i));
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
// But for Object[] can use ArrayList.toArray
|
|
|
|
else {
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
E[] array = (E[]) Array.newInstance(componentType, size);
|
|
|
|
return list.toArray(array);
|
2011-07-12 00:26:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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++) {
|
2022-08-27 02:36:18 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2011-11-20 22:02:26 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|