@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { Type type = typeToken.getType(); Class<? super T> rawType = typeToken.getRawType(); if (!Collection.class.isAssignableFrom(rawType)) { return null; } Type elementType = $Gson$Types.getCollectionElementType(type, rawType); TypeAdapter<?> elementTypeAdapter = gson.getAdapter(TypeToken.get(elementType)); ObjectConstructor<T> constructor = constructorConstructor.get(typeToken); @SuppressWarnings({"unchecked", "rawtypes"}) // create() doesn't define a type parameter TypeAdapter<T> result = new Adapter(gson, elementType, elementTypeAdapter, constructor); return result; }
public Collection deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonNull()) { return null; } // Use ObjectConstructor to create instance instead of hard-coding a specific type. // This handles cases where users are using their own subclass of Collection. Collection collection = constructCollectionType(typeOfT, context); Type childType = $Gson$Types.getCollectionElementType(typeOfT, $Gson$Types.getRawType(typeOfT)); for (JsonElement childElement : json.getAsJsonArray()) { if (childElement == null || childElement.isJsonNull()) { collection.add(null); } else { Object value = context.deserialize(childElement, childType); collection.add(value); } } return collection; }
public JsonElement serialize(Collection src, Type typeOfSrc, JsonSerializationContext context) { if (src == null) { return JsonNull.INSTANCE; } JsonArray array = new JsonArray(); Type childGenericType = null; if (typeOfSrc instanceof ParameterizedType) { Class<?> rawTypeOfSrc = $Gson$Types.getRawType(typeOfSrc); childGenericType = $Gson$Types.getCollectionElementType(typeOfSrc, rawTypeOfSrc); } for (Object child : src) { if (child == null) { array.add(JsonNull.INSTANCE); } else { Type childType = (childGenericType == null || childGenericType == Object.class) ? child.getClass() : childGenericType; JsonElement element = context.serialize(child, childType, false, false); array.add(element); } } return array; }
public <T> TypeAdapter<T> create(MiniGson context, TypeToken<T> typeToken) { Type type = typeToken.getType(); Class<? super T> rawType = typeToken.getRawType(); if (!Collection.class.isAssignableFrom(rawType)) { return null; } Type elementType = $Gson$Types.getCollectionElementType(type, rawType); TypeAdapter<?> elementTypeAdapter = context.getAdapter(TypeToken.get(elementType)); Class<?> constructorType; if (rawType == List.class || rawType == Collection.class) { constructorType = ArrayList.class; } else if (rawType == Set.class) { constructorType = LinkedHashSet.class; } else { constructorType = rawType; } Constructor<?> constructor; try { constructor = constructorType.getConstructor(); } catch (NoSuchMethodException e) { return null; } @SuppressWarnings("unchecked") // create() doesn't define a type parameter TypeAdapter<T> result = new CollectionTypeAdapter(context, elementType, elementTypeAdapter, constructor); return result; } };
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { Type type = typeToken.getType(); Class<? super T> rawType = typeToken.getRawType(); if (!Collection.class.isAssignableFrom(rawType)) { return null; } Type elementType = $Gson$Types.getCollectionElementType(type, rawType); TypeAdapter<?> elementTypeAdapter = gson.getAdapter(TypeToken.get(elementType)); ObjectConstructor<T> constructor = constructorConstructor.get(typeToken); @SuppressWarnings({"unchecked", "rawtypes"}) // create() doesn't define a type parameter TypeAdapter<T> result = new Adapter(gson, elementType, elementTypeAdapter, constructor); return result; }
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { Type type = typeToken.getType(); Class<? super T> rawType = typeToken.getRawType(); if (!Collection.class.isAssignableFrom(rawType)) { return null; } Type elementType = $Gson$Types.getCollectionElementType(type, rawType); TypeAdapter<?> elementTypeAdapter = gson.getAdapter(TypeToken.get(elementType)); ObjectConstructor<T> constructor = constructorConstructor.get(typeToken); @SuppressWarnings({"unchecked", "rawtypes"}) // create() doesn't define a type parameter TypeAdapter<T> result = new Adapter(gson, elementType, elementTypeAdapter, constructor); return result; }
public static Type getCollectionElementType(Type context, Class<?> contextRawType) { return $Gson$Types.getCollectionElementType(context, contextRawType); }
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { Type type = typeToken.getType(); Class<? super T> rawType = typeToken.getRawType(); if (!Collection.class.isAssignableFrom(rawType)) { return null; } Type elementType = $Gson$Types.getCollectionElementType(type, rawType); TypeAdapter<?> elementTypeAdapter = gson.getAdapter(TypeToken.get(elementType)); ObjectConstructor<T> constructor = constructorConstructor.get(typeToken); @SuppressWarnings({ "unchecked", "rawtypes" }) TypeAdapter<T> result = new Adapter(elementTypeAdapter, constructor); return result; }
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { Type type = typeToken.getType(); Class<? super T> rawType = typeToken.getRawType(); if (!Collection.class.isAssignableFrom(rawType)) { return null; } Type elementType = $Gson$Types.getCollectionElementType(type, rawType); TypeAdapter<?> elementTypeAdapter = gson.getAdapter(TypeToken.get(elementType)); ObjectConstructor<T> constructor = constructorConstructor.get(typeToken); @SuppressWarnings({"unchecked", "rawtypes"}) // create() doesn't define a type parameter TypeAdapter<T> result = new Adapter(gson, elementType, elementTypeAdapter, constructor); return result; }
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { Type type = typeToken.getType(); Class<? super T> rawType = typeToken.getRawType(); if (!Collection.class.isAssignableFrom(rawType)) { return null; } Type elementType = $Gson$Types.getCollectionElementType(type, rawType); TypeAdapter<?> elementTypeAdapter = gson.getAdapter(TypeToken.get(elementType)); ObjectConstructor<T> constructor = constructorConstructor.getConstructor(typeToken); @SuppressWarnings({"unchecked", "rawtypes"}) // create() doesn't define a type parameter TypeAdapter<T> result = new Adapter(gson, elementType, elementTypeAdapter, constructor); return result; }
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { Type type = typeToken.getType(); Class<? super T> rawType = typeToken.getRawType(); if (!Collection.class.isAssignableFrom(rawType)) { return null; } Type elementType = $Gson$Types.getCollectionElementType(type, rawType); TypeAdapter<?> elementTypeAdapter = gson.getAdapter(TypeToken.get(elementType)); ObjectConstructor<T> constructor = constructorConstructor.get(typeToken); @SuppressWarnings({"unchecked", "rawtypes"}) // create() doesn't define a type parameter TypeAdapter<T> result = new Adapter(gson, elementType, elementTypeAdapter, constructor); return result; }
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { Type type = typeToken.getType(); Class<? super T> rawType = typeToken.getRawType(); if (!Collection.class.isAssignableFrom(rawType)) { return null; } Type elementType = $Gson$Types.getCollectionElementType(type, rawType); TypeAdapter<?> elementTypeAdapter = gson.getAdapter(TypeToken.get(elementType)); ObjectConstructor<T> constructor = constructorConstructor.get(typeToken); @SuppressWarnings({"unchecked", "rawtypes"}) // create() doesn't define a type parameter TypeAdapter<T> result = new Adapter(gson, elementType, elementTypeAdapter, constructor); return result; }