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.keydap.sparrow.SparrowClient.java

License:Apache License

/**
 * Creates an instance of the client/*from  w  w  w  . ja v a  2  s.c om*/
 * 
 * @param baseApiUrl the API URL of the SCIM server
 * @param authenticator authenticator instance, optional
 * @param sslCtx the SSL context, mandatory only when the service is accessible over HTTPS
 */
public SparrowClient(String baseApiUrl, String baseOauthUrl, Authenticator authenticator, SSLContext sslCtx) {
    this.baseApiUrl = baseApiUrl;
    this.baseOauthUrl = baseOauthUrl;

    // if authenticator is not given then use a null authenticator
    if (authenticator == null) {
        authenticator = new Authenticator() {
            public void saveHeaders(HttpResponse resp) {
            }

            public void authenticate(String baseUrl, CloseableHttpClient client) throws Exception {
            }

            public void addHeaders(HttpUriRequest req) {
            }
        };
    }

    this.authenticator = authenticator;

    boolean isHttps = baseApiUrl.toLowerCase().startsWith("https");

    builder = HttpClientBuilder.create().useSystemProperties();

    if (isHttps) {
        if (sslCtx == null) {
            LOG.warn(
                    "********************** No SSLContext instance is provided, creating a cstom SSLContext that trusts all certificates **********************");
            try {
                sslCtx = SSLContext.getInstance("TLS");
                sslCtx.init(null, new X509TrustManager[] { new AllowAllTrustManager() }, null);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

            builder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
        }

        builder.setSSLContext(sslCtx);
    }

    client = builder.build();

    GsonBuilder gb = new GsonBuilder();

    Type dt = new TypeToken<Date>() {
    }.getType();
    gb.registerTypeAdapter(dt, new DateTimeSerializer());

    serializer = gb.create();
}

From source file:com.kolich.havalo.entities.HavaloEntity.java

License:Open Source License

/**
 * Get a new {@link GsonBuilder} instance, configured accordingly.
 * @return/*from w  w  w.j  av a2s  .c o  m*/
 */
public static final GsonBuilder getHavaloGsonBuilder() {
    final GsonBuilder builder = new GsonBuilder().serializeNulls();
    // Register a type adapter for the HavaloUUID entity type.
    builder.registerTypeAdapter(new TypeToken<HavaloUUID>() {
    }.getType(), new HavaloUUID.HavaloUUIDTypeAdapter());
    builder.registerTypeAdapter(new TypeToken<Date>() {
    }.getType(), new KolichDefaultDateTypeAdapter(iso8601Format__));
    builder.registerTypeAdapter(new TypeToken<File>() {
    }.getType(), new Repository.FileTypeAdapter());
    builder.registerTypeAdapter(new TypeToken<Trie<String, HashedFileObject>>() {
    }.getType(), new Repository.TrieTypeAdapter());
    builder.registerTypeAdapter(new TypeToken<Exception>() {
    }.getType(), new HavaloError.ExceptionTypeAdapter());
    return builder;
}

From source file:com.kurento.kmf.content.jsonrpc.GsonUtils.java

License:Open Source License

/**
 * Gson object accessor (getter)./*from ww w  .j  a va 2 s  . c o m*/
 * 
 * @return son object
 */
public static Gson getGson() {
    if (gson != null) {
        return gson;
    }

    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(JsonRpcRequest.class, new JsonRpcRequestDeserializer());

    builder.registerTypeAdapter(JsonRpcResponse.class, new JsonRpcResponseDeserializer());

    gson = builder.create();

    return gson;
}

From source file:com.kurento.kmf.jsonrpcconnector.JsonUtils.java

License:Open Source License

/**
 * Gson object accessor (getter).//from   ww  w. j  a  v a 2  s. c  om
 * 
 * @return son object
 */
public static Gson getGson() {
    if (gson != null) {
        return gson;
    }

    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Request.class, new JsonRpcRequestDeserializer());

    builder.registerTypeAdapter(Response.class, new JsonRpcResponseDeserializer());

    builder.registerTypeAdapter(Props.class, new JsonPropsAdapter());

    gson = builder.create();

    return gson;
}

From source file:com.l.notel.notel.org.redpin.android.json.GsonFactory.java

License:Open Source License

/**
 * Gets a configured {@link Gson} instance
 * /*w w w.ja  v  a  2s . c  o m*/
 * @return {@link Gson} instance
 */
public synchronized static Gson getGsonInstance() {
    if (gson == null) {
        GsonBuilder builder = new GsonBuilder();

        // needed to get proper sub type after deserialization
        builder.registerTypeAdapter(org.redpin.base.core.Fingerprint.class, new BaseFingerprintTypeAdapter());
        builder.registerTypeAdapter(org.redpin.base.core.Location.class, new BaseLocationTypeAdapter());
        builder.registerTypeAdapter(org.redpin.base.core.Map.class, new BaseMapTypeAdapter());
        builder.registerTypeAdapter(org.redpin.base.core.Measurement.class, new BaseMeasurementTypeAdapter());

        // needed in order to deserialize proper the measurement vectors
        builder.registerTypeAdapter(Measurement.class, new MeasurementTypeAdapter());

        gson = builder.create();

    }

    return gson;
}

From source file:com.liferay.mobile.sdk.json.JSONParser.java

License:Open Source License

protected synchronized static Gson gson() {
    if (gson == null) {
        GsonBuilder builder = new GsonBuilder();

        for (Entry<Type, Object> entry : adapters.entrySet()) {
            builder.registerTypeAdapter(entry.getKey(), entry.getValue());
        }/* w w  w . j av  a  2 s .co  m*/

        gson = builder.create();
    }

    return gson;
}

From source file:com.luan.thermospy.android.core.rest.CameraControlReq.java

License:Open Source License

private JSONObject getJsonObject() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder = gsonBuilder.registerTypeAdapter(CameraControlAction.class,
            new CameraControlActionDeserializer());
    Gson gson = gsonBuilder.create();//  w  w w.  java  2 s.  c om

    try {
        String jsonStr = gson.toJson(mCameraControlAction, Action.class);
        return new JSONObject(jsonStr);
    } catch (JSONException | JsonIOException e) {
        Log.e(LOG_TAG, "Failed to create json object of Camera Control Action object!", e);
        return null;
    }
}

From source file:com.luan.thermospy.android.core.rest.CameraControlReq.java

License:Open Source License

@Override
public void onOkResponse(JSONObject response) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder = gsonBuilder.registerTypeAdapter(CameraControlAction.class,
            new CameraControlActionDeserializer());
    Gson gson = gsonBuilder.create();//from w  w  w  .j  a v a 2s  .  com
    try {
        Action t = gson.fromJson(response.toString(), Action.class);
        mListener.onCameraControlResp(t);
    } catch (JsonSyntaxException ex) {
        mListener.onCameraControlError();
    }
}

From source file:com.luan.thermospy.android.core.rest.GetActiveLogSessionReq.java

License:Open Source License

@Override
public void onOkResponse(JSONObject response) {
    GsonBuilder builder = new GsonBuilder();

    // Register an adapter to manage the date types as long values
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }//  w w w. ja va  2s  .  c o  m
    });

    Gson gson = builder.create();
    mListener.onActiveLogSessionRecv(gson.fromJson(response.toString(), LogSession.class));
}

From source file:com.luan.thermospy.android.core.rest.GetLogSessionListReq.java

License:Open Source License

@Override
public void onOkResponse(JSONArray response) {
    List<LogSession> logSessionsList = new ArrayList<LogSession>();
    // Creates the json object which will manage the information received
    GsonBuilder builder = new GsonBuilder();

    // Register an adapter to manage the date types as long values
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }/*from   w  ww  .j  a v  a2 s  .c o m*/
    });

    Gson gson = builder.create();
    try {
        for (int i = 0; i < response.length(); i++) {
            logSessionsList.add(gson.fromJson(response.getJSONObject(i).toString(), LogSession.class));
        }
        mListener.onLogSessionsRecv(logSessionsList);
    } catch (JSONException ex) {
        mListener.onLogSessionsError();
    }
}