Example usage for com.google.gson GsonBuilder create

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

Introduction

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

Prototype

public Gson create() 

Source Link

Document

Creates a Gson instance based on the current configuration.

Usage

From source file:com.logisticsShop.utils.JsonUtil.java

License:Apache License

/**
 *  {@code JSON} ???/*w ww. j  a va  2 s.c om*/
 * 
 * @param <T>
 *            ???
 * @param json
 *            ?{@code JSON} ?
 * @param token
 *            {@code com.google.gson.reflect.TypeToken} ?
 * @param datePattern
 *            ???
 * @return ?{@code JSON} ?
 * @since 1.0
 */
public static <T> T fromJson(String json, TypeToken<T> token, String datePattern) {
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();
    try {
        return gson.fromJson(json, token.getType());
    } catch (Exception ex) {
        return null;
    }
}

From source file:com.logisticsShop.utils.JsonUtil.java

License:Apache License

/**
 *  {@code JSON} ???<b>??? {@code JavaBean} ?/b>
 * // w ww. jav  a2  s.  c  o  m
 * @param <T>
 *            ???
 * @param json
 *            ?{@code JSON} ?
 * @param clazz
 *            ???
 * @param datePattern
 *            ???
 * @return ?{@code JSON} ?
 * @since 1.0
 */
public static <T> T fromJson(String json, Class<T> clazz, String datePattern) {
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();
    try {
        return gson.fromJson(json, clazz);
    } catch (Exception ex) {
        return null;
    }
}

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();

    try {//from   www.  jav a  2s . c o m
        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();
    try {//w w w .  j a  va  2 s . c  o  m
        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());
        }/*from   w  w  w.ja va2s  .  co  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());
        }// w  w w  . j a va2  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();
    }
}

From source file:com.luan.thermospy.android.core.rest.GetLogSessionReq.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());
        }//from w w w  .ja v a2s  .  c  om
    });

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

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

License:Open Source License

@Override
public void onOkResponse(JSONObject response) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder = gsonBuilder.registerTypeAdapter(ServerStatus.class, new ServerStatusSerializer());
    Gson gson = gsonBuilder.create();
    try {//from   ww  w .j a  v  a2  s.  c o m
        ServiceStatus t = gson.fromJson(response.toString(), ServiceStatus.class);
        mListener.onServiceStatusRecv(t);
    } catch (JsonSyntaxException ex) {
        mListener.onServiceStatusError();
    }
}

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

License:Open Source License

@Override
public void onOkResponse(JSONArray response) {
    List<TemperatureEntry> logSessionsList = new ArrayList<TemperatureEntry>();
    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 ww  w. j av a 2 s  .co m
    });

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

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

License:Open Source License

private JSONObject getJsonObject() {
    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());
        }/* ww w .j  ava2  s  . co  m*/
    });

    Gson gson = builder.create();

    try {
        return new JSONObject(gson.toJson(mLogSession, LogSession.class));
    } catch (JSONException | JsonIOException e) {
        return null;
    }
}