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:io.weba.processor.flink.event.gson.factory.EventFactory.java

License:Open Source License

public Gson create() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Id.class, new IdSerializer());
    gsonBuilder.registerTypeAdapter(Type.class, new TypeSerializer());
    gsonBuilder.registerTypeAdapter(Visitor.class, new VisitorSerializer());
    gsonBuilder.registerTypeAdapter(Session.class, new SessionSerializer());
    gsonBuilder.registerTypeAdapter(Map.class, new MapSerializer());
    gsonBuilder.registerTypeAdapter(Payload.class, new MapSerializer());
    gsonBuilder.serializeNulls();/*from   w  ww.  j a v  a2  s.  c o m*/
    gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    gsonBuilder.setLongSerializationPolicy(LongSerializationPolicy.STRING);
    gsonBuilder.enableComplexMapKeySerialization();

    return gsonBuilder.create();
}

From source file:jp.jun_nama.droidkaigi2017.qiitabrowsersample.MainApplication.java

License:Apache License

private synchronized void initRetrofit() {
    if (retrofit == null) {
        Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                .create();// w w w . j av  a2s  .  c o  m
        retrofit = new Retrofit.Builder().baseUrl(QiitaService.BASE_URL).client(getRetrofitOkHttpClient())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
    }
}

From source file:jqiita.QiitaClient.java

License:Apache License

@SuppressWarnings("unchecked")
public <T> T resource(Class<T> clazz) {
    RestAdapter builder = (RestAdapter) objectCache.computeIfAbsent(getKey(RestAdapter.class, accessToken),
            (key) -> {//w w  w . ja v  a  2  s .c  om
                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssX")
                        .registerTypeAdapter(OffsetDateTime.class, new OffsetDateTimeConverter())
                        .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
                return new RestAdapter.Builder().setEndpoint(host + "/api/" + API_VERSION)
                        .setConverter(new GsonConverter(gson))
                        .setLogLevel(RestAdapter.LogLevel.valueOf(logLevel))
                        .setErrorHandler(new QiitaErrorHandler()).setRequestInterceptor((request) -> {
                            if (accessToken != null && !accessToken.isEmpty()) {
                                request.addHeader("Authorization", "Bearer " + accessToken);
                            }
                        }).build();
            });
    return (T) objectCache.computeIfAbsent(getKey(clazz, accessToken), (key) -> builder.create(clazz));
}

From source file:mashup.fm.github.BaseService.java

License:Apache License

/**
 * Gets the gson builder.//from   w w  w. j  a  v a 2  s.  c o m
 * 
 * @return the gson builder
 */
private static GsonBuilder getGsonBuilder() {
    // Init Builder
    GsonBuilder builder = new GsonBuilder();

    // This is the code that will try to parse dates
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        @Override
        public Date deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            try {
                if (arg0.getAsString().indexOf(' ') == 0) {
                    // 2011-03-23T05:14:20-07:00
                    DateFormat df = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ss-mm:mm");
                    return df.parse(arg0.getAsString());
                }
                DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss Z");
                return df.parse(arg0.getAsString());

            } catch (Throwable t) {
                Logger.warn(ExceptionUtil.getStackTrace(t));
                return null;
            }
        }
    });

    // We use camel case and the api returns lower case with underscore -
    // fieldId versus field_id
    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);

    // Return Builder
    return builder;
}

From source file:me.ixfan.wechatkit.common.WeChatApiResult.java

License:Open Source License

public static WeChatApiResult instanceOf(JsonObject jsonResponse) {
    Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
    return gson.fromJson(jsonResponse, WeChatApiResult.class);
}

From source file:me.ixfan.wechatkit.material.MaterialManager.java

License:Open Source License

/**
 * ??//  ww w  . j  av  a 2s.  c  o m
 *
 * @param articles ??
 * @return ?? {@link WeChatApiResult#getMediaId()} ??? <code>media_id</code>
 * ? {@link WeChatApiResult#getErrcode()}  {@link WeChatApiResult#getErrmsg()}
 * ??
 */
public WeChatApiResult uploadPermanentArticles(ArticleForUpload... articles) {
    Map<String, ArticleForUpload[]> data = new HashMap<>();
    data.put("articles", articles);
    try {
        Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                .create();
        JsonObject jsonResponse = HttpClientUtil.sendPostRequestWithJsonBody(
                WeChatConstants.WECHAT_POST_MATERIAL_UPLOAD_PERMANENT_NEWS.replace("${ACCESS_TOKEN}",
                        super.getTokenManager().getAccessToken()),
                gson.toJson(data));
        return WeChatApiResult.instanceOf(jsonResponse);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:me.ixfan.wechatkit.material.MaterialManager.java

License:Open Source License

/**
 * ??// w  ww.j a va  2  s  .c om
 *
 * @param articles ??
 * @return ?? {@link WeChatApiResult#getMediaId()} ??? <code>media_id</code>
 * ? {@link WeChatApiResult#getErrcode()}  {@link WeChatApiResult#getErrmsg()}
 * ??
 */
public WeChatApiResult uploadArticlesForMassMessage(ArticleForUpload... articles) {
    Map<String, ArticleForUpload[]> data = new HashMap<>();
    data.put("articles", articles);
    try {
        Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                .create();
        JsonObject jsonResponse = HttpClientUtil.sendPostRequestWithJsonBody(
                WeChatConstants.WECHAT_POST_MESSAGE_UPLOAD_NEWS.replace("${ACCESS_TOKEN}",
                        super.getTokenManager().getAccessToken()),
                gson.toJson(data));
        return WeChatApiResult.instanceOf(jsonResponse);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:me.ixfan.wechatkit.message.MessageManager.java

License:Open Source License

/**
 * ?????//from   www  .j av  a  2  s  .c  om
 * @return ????
 * @throws WeChatApiErrorException API??
 */
public List<MessageTemplate> retrieveMessageTemplates() throws WeChatApiErrorException {
    final String url = WeChatConstants.WECHAT_GET_MESSAGE_TEMPLATES.replace("${ACCESS_TOKEN}",
            super.tokenManager.getAccessToken());
    JsonObject jsonResponse;
    try {
        jsonResponse = HttpClientUtil.sendGetRequestAndGetJsonResponse(url);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    if (!jsonResponse.has("template_list")) {
        throw new WeChatApiErrorException(jsonResponse.get("errcode").getAsInt(),
                jsonResponse.get("errmsg").getAsString());
    } else {
        JsonArray teplArray = jsonResponse.getAsJsonArray("template_list");
        if (teplArray.size() == 0) {
            return Collections.emptyList();
        }
        Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                .create();
        return gson.fromJson(teplArray, new TypeToken<ArrayList<MessageTemplate>>() {
        }.getType());
    }

}

From source file:me.ixfan.wechatkit.user.model.WeChatFollower.java

License:Open Source License

public static WeChatFollower fromJson(JsonObject jsonObject) {
    Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
    return gson.fromJson(jsonObject, WeChatFollower.class);
}

From source file:me.ixfan.wechatkit.user.model.WeChatFollower.java

License:Open Source License

public static List<WeChatFollower> batchFromJson(JsonArray jsonArray) {
    Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
    Type type = new TypeToken<List<WeChatFollower>>() {
    }.getType();/*w  ww. ja  v a  2  s . c  om*/
    return gson.fromJson(jsonArray, type);
}