Example usage for com.google.gson GsonBuilder registerTypeAdapter

List of usage examples for com.google.gson GsonBuilder registerTypeAdapter

Introduction

In this page you can find the example usage for com.google.gson GsonBuilder registerTypeAdapter.

Prototype

@SuppressWarnings({ "unchecked", "rawtypes" })
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) 

Source Link

Document

Configures Gson for custom serialization or deserialization.

Usage

From source file:com.google.nigori.common.MessageLibrary.java

License:Apache License

private static Gson initializeGson() {

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(GetRequest.class, new TypeAdapterProtobuf());
    gsonBuilder.registerTypeAdapter(GetResponse.class, new TypeAdapterProtobuf());
    gsonBuilder.registerTypeAdapter(GetIndicesRequest.class, new TypeAdapterProtobuf());
    gsonBuilder.registerTypeAdapter(GetIndicesResponse.class, new TypeAdapterProtobuf());
    gsonBuilder.registerTypeAdapter(GetRevisionsRequest.class, new TypeAdapterProtobuf());
    gsonBuilder.registerTypeAdapter(GetRevisionsResponse.class, new TypeAdapterProtobuf());
    gsonBuilder.registerTypeAdapter(PutRequest.class, new TypeAdapterProtobuf());
    gsonBuilder.registerTypeAdapter(DeleteRequest.class, new TypeAdapterProtobuf());
    gsonBuilder.registerTypeAdapter(RegisterRequest.class, new TypeAdapterProtobuf());
    gsonBuilder.registerTypeAdapter(UnregisterRequest.class, new TypeAdapterProtobuf());
    gsonBuilder.registerTypeAdapter(AuthenticateRequest.class, new TypeAdapterProtobuf());
    gsonBuilder.registerTypeAdapter(RevisionValue.class, new TypeAdapterProtobuf());
    gsonBuilder.registerTypeAdapter(ByteString.class, new TypeAdapterByteString());
    gsonBuilder.setPrettyPrinting();//from   w w  w .jav  a2 s.  c  om
    return gsonBuilder.create();
}

From source file:com.google.research.ic.ferret.data.LogLoader.java

License:Open Source License

private LogLoader() {
    GsonBuilder gBuilder = new GsonBuilder();
    gBuilder.registerTypeAdapter(Event.class, new JsonEventDeserializer<Event>());
    gBuilder.registerTypeAdapter(Attribute.class, new JsonAttributeDeserializer<Attribute>());
    gson = gBuilder.create();/*from  w  w  w  .  ja va  2 s  .  c om*/
}

From source file:com.google.shipshape.util.rpc.Protocol.java

License:Open Source License

/** Returns a {@link Gson} capable of en/decoding {@link Protocol} messages. */
public static Gson constructGson(GsonBuilder builder) {
    if (builder == null) {
        builder = new GsonBuilder();
    }/* w  w w .j a  va2s.  co m*/
    return builder.registerTypeAdapter(Error.class, new ErrorTypeAdapter())
            .registerTypeAdapter(Request.class, new RequestTypeAdapter())
            .registerTypeAdapter(Response.class, new ResponseTypeAdapter())
            .registerTypeAdapter(Method.Info.class, new MethodInfoSerializer())
            .registerTypeAdapter(Version.class, new VersionTypeAdapter())
            .registerTypeHierarchyAdapter(ProtocolMessageEnum.class, new ProtoEnumTypeAdapter())
            .registerTypeHierarchyAdapter(GeneratedMessage.class, new ProtoTypeAdapter())
            .registerTypeHierarchyAdapter(ByteString.class, new ByteStringTypeAdapter())
            .registerTypeAdapter(byte[].class, new ByteArrayTypeAdapter()).create();
}

From source file:com.google.wave.api.impl.GsonFactory.java

License:Apache License

/**
 * Creates a {@link Gson} instance, with additional type adapters for these
 * types:/*from   www  .  j a  va2  s  .c  o m*/
 * <ul>
 *   <li>{@link EventMessageBundle}</li>
 *   <li>{@link OperationRequest}</li>
 *   <li>{@link Element}</li>
 *   <li>{@link JsonRpcResponse}</li>
 * </ul>
 *
 * @param opNamespace prefix that should be prepended to the operation during
 *     serialization.
 * @return an instance of {@link Gson} with pre-registered type adapters.
 */
public Gson create(String opNamespace) {
    ElementGsonAdaptor elementGsonAdaptor = new ElementGsonAdaptor();
    GsonBuilder builder = new GsonBuilder().setExclusionStrategies(new NonSerializableExclusionStrategy())
            .registerTypeAdapter(EventMessageBundle.class, new EventMessageBundleGsonAdaptor())
            .registerTypeAdapter(OperationRequest.class, new OperationRequestGsonAdaptor(opNamespace))
            .registerTypeAdapter(Element.class, elementGsonAdaptor)
            .registerTypeAdapter(Attachment.class, elementGsonAdaptor)
            .registerTypeAdapter(JsonRpcResponse.class, new JsonRpcResponseGsonAdaptor())
            .registerTypeAdapter(Annotation.class, new AnnotationInstanceCreator())
            .registerTypeAdapter(Range.class, new RangeInstanceCreator())
            .registerTypeAdapter(BlipThread.class, new ThreadInstanceCreator())
            .registerTypeAdapter(SearchResult.class, new SearchResultInstanceCreator())
            .registerTypeAdapter(SearchResult.Digest.class, new SearchResultDigestInstanceCreator())
            .registerTypeAdapter(WaveletId.class, new WaveletIdInstanceCreator())
            .registerTypeAdapter(RawAttachmentData.class, new AttachmentDataInstanceCreator())
            .registerTypeAdapter(byte[].class, new ByteArrayGsonAdaptor());

    // Register custom type adapters.
    for (Entry<Type, Object> entry : customTypeAdapters.entrySet()) {
        builder.registerTypeAdapter(entry.getKey(), entry.getValue());
    }

    return builder.serializeNulls().create();
}

From source file:com.googleapis.ajax.services.impl.BaseGoogleSearchApiQuery.java

License:Apache License

/**
 * Gets the gson builder./*from   ww  w  . j av  a2s .  c o m*/
 * 
 * @return the gson builder
 */
protected GsonBuilder getGsonBuilder() {
    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat(ApplicationConstants.RFC822DATEFORMAT);
    builder.registerTypeAdapter(ListingType.class, new JsonDeserializer<ListingType>() {

        @Override
        public ListingType deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return ListingType.fromValue(arg0.getAsString());
        }

    });
    builder.registerTypeAdapter(PatentStatus.class, new JsonDeserializer<PatentStatus>() {

        @Override
        public PatentStatus deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return PatentStatus.fromValue(arg0.getAsString());
        }

    });
    builder.registerTypeAdapter(VideoType.class, new JsonDeserializer<VideoType>() {

        @Override
        public VideoType deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return VideoType.fromValue(arg0.getAsString());
        }

    });
    builder.registerTypeAdapter(ViewPortMode.class, new JsonDeserializer<ViewPortMode>() {

        @Override
        public ViewPortMode deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return ViewPortMode.fromValue(arg0.getAsString());
        }

    });
    builder.registerTypeAdapter(GsearchResultClass.class, new JsonDeserializer<GsearchResultClass>() {

        @Override
        public GsearchResultClass deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return GsearchResultClass.fromValue(arg0.getAsString());
        }

    });
    builder.registerTypeAdapter(PhoneNumberType.class, new JsonDeserializer<PhoneNumberType>() {

        @Override
        public PhoneNumberType deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return PhoneNumberType.fromValue(arg0.getAsString());
        }

    });
    builder.registerTypeAdapter(Language.class, new JsonDeserializer<Language>() {

        @Override
        public Language deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return Language.fromValue(arg0.getAsString());
        }
    });

    return builder;
}

From source file:com.googleapis.maps.services.impl.BaseGoogleMapsApiQuery.java

License:Apache License

/**
 * Gets the gson builder./*from www .  ja  va  2 s .c  om*/
 * 
 * @return the gson builder
 */
protected GsonBuilder getGsonBuilder() {
    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat(ApplicationConstants.RFC822DATEFORMAT);
    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    builder.registerTypeAdapter(LocationType.class, new JsonDeserializer<LocationType>() {
        @Override
        public LocationType deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return LocationType.fromValue(arg0.getAsString());
        }
    });
    builder.registerTypeAdapter(AddressComponentType.class, new JsonDeserializer<AddressComponentType>() {
        @Override
        public AddressComponentType deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return AddressComponentType.fromValue(arg0.getAsString());
        }
    });
    //      builder.registerTypeAdapter(TravelMode.class, new JsonDeserializer<TravelMode>() {
    //         @Override
    //         public TravelMode deserialize(JsonElement arg0, Type arg1,
    //               JsonDeserializationContext arg2) throws JsonParseException {
    //            return TravelMode.fromValue(arg0.getAsString());
    //         }
    //      });

    return builder;
}

From source file:com.googlesource.gerrit.plugins.hooks.rtc.network.RTCClient.java

License:Apache License

public RTCClient(String url, boolean sslVerify, HttpParams httpParams) throws IOException {
    super();/*from   w  w  w.  java 2 s  .  c  o  m*/

    this.baseUrl = (url.endsWith("/") ? url.substring(url.length() - 1) : url);
    if (httpParams == null) {
        httpParams = new BasicHttpParams();
    }
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    this.httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, schemeRegistry),
            httpParams);

    this.transport = new Transport(this, baseUrl, httpclient, httpParams);
    this.factory = new CachableResourcesFactory(transport);

    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(RtcWorkItem.class, new RtcWorkItemDeserializer(factory));
    builder.registerTypeAdapter(RtcComment.class, new RtcCommentDeserializer());
    builder.registerTypeAdapter(RtcEntity.class, new RtcEntityDeserializer());
    builder.registerTypeAdapter(RtcRelatedLink.class, new RtcRelatedLinkDeserializer());
    builder.registerTypeAdapter(RtcWorkflowAction.class, new RtcWorkflowActionDeserializer());
    gson = builder.create();
    transport.setGson(gson);

    setCookieStore();
    setRedirectStategy();
    setSSLTrustStrategy(sslVerify);

    sessionApi = new SessionApiImpl(this, transport);
    workItemsApi = new WorkItemsApiImpl(this, transport);
}

From source file:com.greensopinion.finance.services.persistence.PersistenceGsonProvider.java

License:Apache License

public Gson get() {
    GsonBuilder gsonBuilder = builder();
    gsonBuilder.registerTypeAdapter(Date.class, new DateTypeAdapter());
    gsonBuilder.registerTypeAdapterFactory(TransactionsTypeAdapter.factory(encryptorProviderService));
    gsonBuilder.registerTypeAdapterFactory(CategoriesTypeAdapter.factory(encryptorProviderService));
    return gsonBuilder.create();
}

From source file:com.greensopinion.finance.services.web.WebGsonProvider.java

License:Apache License

@Override
public Gson get() {
    GsonBuilder gsonBuilder = builder();
    gsonBuilder.registerTypeAdapter(Date.class, new DateTypeAdapter());
    return gsonBuilder.create();
}

From source file:com.gst.infrastructure.core.serialization.CommandProcessingResultJsonSerializer.java

License:Apache License

public CommandProcessingResultJsonSerializer() {
    final GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter());
    builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter());
    builder.registerTypeAdapter(MonthDay.class, new JodaMonthDayAdapter());
    builder.serializeNulls();/*from   w  w w.j  av a 2 s . com*/

    this.gson = builder.create();
}