Example usage for com.google.gson.internal ObjectConstructor ObjectConstructor

List of usage examples for com.google.gson.internal ObjectConstructor ObjectConstructor

Introduction

In this page you can find the example usage for com.google.gson.internal ObjectConstructor ObjectConstructor.

Prototype

ObjectConstructor

Source Link

Usage

From source file:com.datastore_android_sdk.serialization.ForeignCollectionTypeAdapterFactory.java

License:Apache License

@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
    Type type = typeToken.getType();

    Class<? super T> rawType = typeToken.getRawType();
    if (!ForeignCollection.class.isAssignableFrom(rawType)) {
        return null;
    }/*from   ww  w  .  j  av  a 2  s  . c o  m*/

    final Type elementType = $Gson$Types.getCollectionElementType(type, rawType);
    TypeToken<?> elementTypeToken = TypeToken.get(elementType);

    // Specify a TypeAdapter for the element only if we can retreive a concrete type element type
    TypeAdapter<?> elementTypeAdapter = elementType instanceof TypeVariable ? null
            : gson.getAdapter(elementTypeToken);

    @SuppressWarnings({ "unchecked", "rawtypes" })
    TypeAdapter<T> result = (TypeAdapter<T>) new Adapter(gson, elementType, elementTypeAdapter) {

        @Override
        public ObjectConstructor<T> getConstructor(final ForeignCollectionDeserializationContext context) {
            return new ObjectConstructor<T>() {
                public T construct() {
                    if (creator == null) {
                        return null;
                    } else {
                        return (T) creator.createInstance(elementType, context.getParent(),
                                context.getColumnName(), context.getOrderColumnName(),
                                context.getOrderAscending());
                    }
                }
            };
        }

    };
    return result;
}

From source file:com.flipkart.batching.gson.adapters.DataCollectionTypeAdapter.java

License:Open Source License

public DataCollectionTypeAdapter(@NonNull TypeAdapter<T> typeAdapter) {
    collectionTypeAdapter = new BatchingTypeAdapters.ListTypeAdapter<>(typeAdapter,
            new ObjectConstructor<Collection<T>>() {
                @Override/*  ww  w  . j a v  a 2 s .co  m*/
                public Collection<T> construct() {
                    return new ArrayList<>();
                }
            });
}

From source file:com.flipkart.batching.gson.GsonSerializationStrategy.java

License:Open Source License

private TypeAdapter<Collection<E>> getCollectionTypeAdapter() {
    if (collectionTypeAdapter == null) {
        collectionTypeAdapter = new BatchingTypeAdapters.ListTypeAdapter<>(getDataTypeAdapter(),
                new ObjectConstructor<Collection<E>>() {
                    @Override/*from www  .  j a  v a2s .c o m*/
                    public Collection<E> construct() {
                        return new ArrayList<>();
                    }
                });
    }
    return collectionTypeAdapter;
}