Example usage for com.google.gson FieldNamingPolicy LOWER_CASE_WITH_UNDERSCORES

List of usage examples for com.google.gson FieldNamingPolicy LOWER_CASE_WITH_UNDERSCORES

Introduction

In this page you can find the example usage for com.google.gson FieldNamingPolicy LOWER_CASE_WITH_UNDERSCORES.

Prototype

FieldNamingPolicy LOWER_CASE_WITH_UNDERSCORES

To view the source code for com.google.gson FieldNamingPolicy LOWER_CASE_WITH_UNDERSCORES.

Click Source Link

Document

Using this naming policy with Gson will modify the Java Field name from its camel cased form to a lower case field name where each word is separated by an underscore (_).

Usage

From source file:com.ibm.og.client.ApacheClient.java

License:Open Source License

private Gson createGson() {
    return new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
            .setLongSerializationPolicy(LongSerializationPolicy.STRING)
            .registerTypeAdapter(Double.class, new TypeAdapter<Double>() {
                @Override/*from   w  ww.  ja v a 2  s . c  o  m*/
                public void write(final JsonWriter out, final Double value) throws IOException {
                    // round decimals to 2 places
                    out.value(new BigDecimal(value).setScale(2, RoundingMode.HALF_UP).doubleValue());
                }

                @Override
                public Double read(final JsonReader in) throws IOException {
                    return in.nextDouble();
                }
            }.nullSafe()).create();
}

From source file:com.ibm.watson.speech_to_text.v1.websocket.SpeechToTextWebSocketListener.java

License:Open Source License

/**
 * Builds the start message./*from  www .  j a  v  a2s. c  o  m*/
 *
 * @param options the options
 * @return the request
 */
private String buildStartMessage(RecognizeOptions options) {
    Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
    JsonObject startMessage = new JsonParser().parse(gson.toJson(options)).getAsJsonObject();
    startMessage.remove(MODEL);
    startMessage.remove(CUSTOMIZATION_ID);
    startMessage.remove(LANGUAGE_CUSTOMIZATION_ID);
    startMessage.remove(ACOUSTIC_CUSTOMIZATION_ID);
    startMessage.remove(VERSION);
    startMessage.addProperty(ACTION, START);
    return startMessage.toString();
}

From source file:com.jerrellmardis.amphitheatre.api.TMDbClient.java

License:Apache License

private static TMDbService getService() {
    Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();

    if (service == null) {
        RestAdapter restAdapter = new RestAdapter.Builder().setConverter(new GsonConverter(gson))
                .setEndpoint(ApiConstants.TMDB_SERVER_URL).setRequestInterceptor(new RequestInterceptor() {
                    @Override/*from  w w w .  j  a  va  2s  .c  o m*/
                    public void intercept(RequestFacade request) {
                        request.addQueryParam("api_key", ApiConstants.TMDB_SERVER_API_KEY);
                    }
                }).build();
        service = restAdapter.create(TMDbService.class);
    }
    return service;
}

From source file:com.jlubecki.soundcloud.webapi.android.SoundCloudAPI.java

License:Open Source License

/**
 * Creates a {@link SoundCloudService}. Serializes with JSON.
 *
 * @param clientId Client ID provided by SoundCloud.
 *///w w w  .  j  av  a 2  s.  c om
public SoundCloudAPI(String clientId) {
    this.clientId = clientId;

    Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
            .registerTypeAdapter(Date.class, new DateTypeAdapter()).create();

    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new SoundCloudInterceptor()).build();

    Retrofit adapter = new Retrofit.Builder().client(client).baseUrl(SOUNDCLOUD_API_ENDPOINT)
            .addConverterFactory(GsonConverterFactory.create(gson)).build();

    service = adapter.create(SoundCloudService.class);
}

From source file:com.kodokux.github.api.GithubApiUtil.java

License:Apache License

private static Gson initGson() {
    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    return builder.create();
}

From source file:com.mediamath.terminalone.service.GetService.java

License:Apache License

/**
 * parses error response of Get operation.
 * /* w  ww.j a v  a2  s. c  o m*/
 * @param responseStr
 *          string.
 * @return JsonPostErrorResponse object.
 */
public JsonPostErrorResponse jsonGetErrorResponseParser(String responseStr) {
    JsonParser parser1 = new JsonParser();
    JsonObject obj = parser1.parse(responseStr).getAsJsonObject();

    JsonElement errorsElement = obj.get("errors");
    JsonElement errorElement = obj.get("error");
    JsonElement metaElement = obj.get("meta");

    JsonPostErrorResponse errorResponse = null;

    if (errorsElement != null || errorElement != null) {
        errorResponse = new JsonPostErrorResponse();
        GsonBuilder builder = new GsonBuilder();
        builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES);
        builder.setDateFormat(YYYY_MM_DD_T_HH_MM_SS);

        Gson gson = builder.create();

        if (errorsElement != null) {
            if (errorsElement.isJsonNull()) {

            } else if (errorsElement.isJsonObject()) {
                T1Error errors = gson.fromJson(errorsElement, T1Error.class);
                // specific to video creatives
                if (errors != null && errors.getContent() == null && errors.getField() == null
                        && errors.getFieldError() == null && errors.getMessage() == null) {

                    GsonBuilder videoBuilder = new GsonBuilder();
                    videoBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
                    videoBuilder.setDateFormat(YYYY_MM_DD_T_HH_MM_SS);

                    Gson vidgson = videoBuilder.create();

                    errors = vidgson.fromJson(errorsElement, T1Error.class);
                }
                errorResponse.setErrors(errors);
            } else if (errorsElement.isJsonArray()) {
                JsonArray array = errorsElement.getAsJsonArray();
                JsonArray newArray = new JsonArray();

                for (int i = 0; i < array.size(); i++) {
                    if (!(array.get(i) instanceof JsonPrimitive)) {
                        newArray.add(array.get(i));

                    }
                }
                if (newArray.size() > 0) {
                    errorsElement = newArray;
                    Type type = new TypeToken<ArrayList<T1Error>>() {
                    }.getType();
                    List<T1Error> errors = gson.fromJson(errorsElement, type);
                    errorResponse.setErrors(errors);
                }
            }
        }

        if (errorElement != null) {
            T1Error error = gson.fromJson(errorElement, T1Error.class);
            errorResponse.setError(error);
        }

        if (metaElement != null) {
            T1Meta meta = gson.fromJson(metaElement, T1Meta.class);
            errorResponse.setMeta(meta);
        }
    }

    return errorResponse;
}

From source file:com.mediamath.terminalone.service.PostService.java

License:Apache License

/**
 * parses error response of a POST activity.
 * //  www. j av a2  s .  c o m
 * @param responseStr
 *          requires a response JSON string
 * @param responseObj
 *          requires a Response object.
 * @return JsonPostErrorResponse entity.
 */
public JsonPostErrorResponse jsonPostErrorResponseParser(String responseStr, Response responseObj) {
    JsonParser parser1 = new JsonParser();
    JsonObject obj = parser1.parse(responseStr).getAsJsonObject();

    JsonElement errorsElement = obj.get("errors");
    JsonElement errorElement = obj.get("error");
    JsonElement metaElement = obj.get("meta");

    JsonPostErrorResponse errorResponse = null;

    if (errorsElement != null || errorElement != null
            || (responseObj != null && responseObj.getStatus() == 403 && metaElement != null)) {
        errorResponse = new JsonPostErrorResponse();
        GsonBuilder builder = new GsonBuilder();
        builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES);
        builder.setDateFormat(YYYY_MM_DD_T_HH_MM_SS);

        Gson gson = builder.create();

        if (errorsElement != null) {
            if (errorsElement.isJsonObject()) {
                T1Error errors = gson.fromJson(errorsElement, T1Error.class);
                // specific to video creatives
                if (errors != null && errors.getContent() == null && errors.getField() == null
                        && errors.getFieldError() == null && errors.getMessage() == null) {

                    GsonBuilder videoBuilder = new GsonBuilder();
                    videoBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
                    videoBuilder.setDateFormat(YYYY_MM_DD_T_HH_MM_SS);

                    Gson vidgson = videoBuilder.create();

                    errors = vidgson.fromJson(errorsElement, T1Error.class);
                }
                errorResponse.setErrors(errors);
            } else if (errorsElement.isJsonArray()) {
                JsonArray array = errorsElement.getAsJsonArray();
                JsonArray newArray = new JsonArray();

                for (int i = 0; i < array.size(); i++) {
                    if (!(array.get(i) instanceof JsonPrimitive)) {
                        newArray.add(array.get(i));

                    }
                }
                if (newArray.size() > 0) {
                    errorsElement = newArray;
                    Type type = new TypeToken<ArrayList<T1Error>>() {
                    }.getType();
                    List<T1Error> errors = gson.fromJson(errorsElement, type);
                    errorResponse.setErrors(errors);
                }
            }
        }

        if (errorElement != null) {
            T1Error error = gson.fromJson(errorElement, T1Error.class);
            errorResponse.setError(error);
        }

        if (metaElement != null) {
            if (responseObj != null && responseObj.getStatus() == 403) {
                T1Meta meta = gson.fromJson(metaElement, T1Meta.class);
                errorResponse.setMeta(meta);
            }
        }
    }

    return errorResponse;
}

From source file:com.minecraftonline.vanillairc.VanillaIRC.java

License:Open Source License

private static Config getConfig0() {
    Config localConfig = null;/*w w  w .java 2  s . co m*/
    Gson gson = new GsonBuilder().setPrettyPrinting()
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
    try {
        localConfig = gson.fromJson(new FileReader(CONFIG_FILE), Config.class);
    } catch (FileNotFoundException e) {
        try {
            Files.copy(VanillaIRC.class.getResourceAsStream(CONFIG_FILE), Paths.get(CONFIG_FILE));
            log.warn("Configuration file \"{}\" did not exist, initialized with defaults", CONFIG_FILE);
        } catch (IOException ioe) {
            log.error("Configuration file did not exist and could not write defaults", ioe);
        }
        System.exit(1);
    } catch (JsonParseException e) {
        log.error("Error while parsing config", e);
    }
    return localConfig;
}

From source file:com.monmonja.library.server.JsonRpcRequest.java

License:Open Source License

private void makeGsonBuilder() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    gsonBuilder.registerTypeAdapter(JsonResponse.class, new JSonResponseDeserializer());
    this.gson = gsonBuilder.create();
}

From source file:com.ninetyslide.ext.popularmovies.util.GsonManager.java

License:Apache License

/**
 * Return an unique instance of Gson, performing a lazy initialization if no instance exists yet.
 *
 * @return An unique instance of Gson./*ww w .  j a  v  a 2 s.co  m*/
 */
public static Gson getGsonInstance() {
    if (gsonInstance == null) {
        synchronized (gsonLock) {
            if (gsonInstance == null) {
                gsonInstance = new GsonBuilder()
                        .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
            }
        }
    }
    return gsonInstance;
}