2 * Copyright (C) 2011 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com
.google
.gson
.internal
.bind
;
19 import java
.io
.IOException
;
20 import java
.lang
.reflect
.Array
;
21 import java
.lang
.reflect
.GenericArrayType
;
22 import java
.lang
.reflect
.Type
;
23 import java
.util
.ArrayList
;
24 import java
.util
.List
;
26 import com
.google
.gson
.Gson
;
27 import com
.google
.gson
.TypeAdapter
;
28 import com
.google
.gson
.TypeAdapterFactory
;
29 import com
.google
.gson
.internal
.$Gson$Types
;
30 import com
.google
.gson
.reflect
.TypeToken
;
31 import com
.google
.gson
.stream
.JsonReader
;
32 import com
.google
.gson
.stream
.JsonToken
;
33 import com
.google
.gson
.stream
.JsonWriter
;
36 * Adapt an array of objects.
38 public final class ArrayTypeAdapter
<E
> extends TypeAdapter
<Object
> {
39 public static final TypeAdapterFactory FACTORY
= new TypeAdapterFactory() {
40 @SuppressWarnings({"unchecked", "rawtypes"})
41 public <T
> TypeAdapter
<T
> create(Gson gson
, TypeToken
<T
> typeToken
) {
42 Type type
= typeToken
.getType();
43 if (!(type
instanceof GenericArrayType
|| type
instanceof Class
&& ((Class
<?
>) type
).isArray())) {
47 Type componentType
= $Gson$Types
.getArrayComponentType(type
);
48 TypeAdapter
<?
> componentTypeAdapter
= gson
.getAdapter(TypeToken
.get(componentType
));
49 return new ArrayTypeAdapter(
50 gson
, componentTypeAdapter
, $Gson$Types
.getRawType(componentType
));
54 private final Class
<E
> componentType
;
55 private final TypeAdapter
<E
> componentTypeAdapter
;
57 public ArrayTypeAdapter(Gson context
, TypeAdapter
<E
> componentTypeAdapter
, Class
<E
> componentType
) {
58 this.componentTypeAdapter
=
59 new TypeAdapterRuntimeTypeWrapper
<E
>(context
, componentTypeAdapter
, componentType
);
60 this.componentType
= componentType
;
63 public Object
read(JsonReader in
) throws IOException
{
64 if (in
.peek() == JsonToken
.NULL
) {
69 List
<E
> list
= new ArrayList
<E
>();
71 while (in
.hasNext()) {
72 E instance
= componentTypeAdapter
.read(in
);
76 Object array
= Array
.newInstance(componentType
, list
.size());
77 for (int i
= 0; i
< list
.size(); i
++) {
78 Array
.set(array
, i
, list
.get(i
));
83 @SuppressWarnings("unchecked")
84 @Override public void write(JsonWriter out
, Object array
) throws IOException
{
91 for (int i
= 0, length
= Array
.getLength(array
); i
< length
; i
++) {
92 E value
= (E
) Array
.get(array
, i
);
93 componentTypeAdapter
.write(out
, value
);