List of usage examples for com.google.gson GsonBuilder setFieldNamingPolicy
public GsonBuilder setFieldNamingPolicy(FieldNamingPolicy namingConvention)
From source file:com.mediamath.terminalone.service.GetService.java
License:Apache License
/** * parses error response of Get operation. * // w w w . ja v a 2 s .co 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. * // ww w .ja v a 2 s. c om * @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.mediamath.terminalone.utils.T1JsonToObjParser.java
License:Apache License
/** * parses 3pas creative upload response string to TPASCreativeUpload object. * /*from www .j a v a2s.c o m*/ * @param json3PasCreativeResponseString * required response string * @return TPASCreativeUpload object. */ public TPASCreativeUpload parse3PasCreativeUploadResponseTOObj(String json3PasCreativeResponseString) { TPASCreativeUpload response = null; GsonBuilder builder = new GsonBuilder(); builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES); builder.setDateFormat(YYYY_MM_DD_T_HH_MM_SS); Gson gson = builder.create(); response = gson.fromJson(json3PasCreativeResponseString, TPASCreativeUpload.class); return response; }
From source file:com.mediamath.terminalone.utils.T1JsonToObjParser.java
License:Apache License
/** * parses T1AS creative asset upload response to TOneASCreativeAssetsUpload entity. * // w w w . j av a 2s . co m * @param jsonT1CAUesponse * response string. * @return TOneASCreativeAssetsUpload object. */ public TOneASCreativeAssetsUpload parseTOneASCreativeAssetsUploadResponseTOObj(String jsonT1CAUesponse) { TOneASCreativeAssetsUpload response = null; GsonBuilder builder = new GsonBuilder(); builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES); builder.setDateFormat(YYYY_MM_DD_T_HH_MM_SS); Gson gson = builder.create(); response = gson.fromJson(jsonT1CAUesponse, TOneASCreativeAssetsUpload.class); return response; }
From source file:com.mediamath.terminalone.utils.T1JsonToObjParser.java
License:Apache License
/** * parses the response of second call to T1AS creative assets approve api call. * /*from www. j ava 2 s. c o m*/ * @param pResponse * requires a response string. * @return JsonResponse<? extends T1Entity> returns an entity of type T. */ public JsonResponse<? extends T1Entity> parseTOneASCreativeAssetsApproveResponse(String pResponse) { JsonResponse<? extends T1Entity> response = null; GsonBuilder builder = new GsonBuilder(); builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES); builder.setDateFormat(YYYY_MM_DD_T_HH_MM_SS); Gson gson = builder.create(); Type type = new TypeToken<JsonResponse<ArrayList<TOneASCreativeAssetsApproveResponse>>>() { }.getType(); response = gson.fromJson(pResponse, type); return response; }
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.mzs.httpcore.client.GsonUtils.java
License:Open Source License
/** * Create the standard {@link com.google.gson.Gson} configuration * * @param serializeNulls/*from ww w . j a v a 2s . c o m*/ * whether nulls should be serialized * * @return created gson, never null */ public static final Gson createGson(final boolean serializeNulls) { final GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(Date.class, new DateFormatter()); builder.setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES); if (serializeNulls) builder.serializeNulls(); return builder.create(); }
From source file:com.nextdoor.bender.serializer.json.JsonSerializer.java
License:Apache License
public JsonSerializer(FieldNamingPolicy policy) { GsonBuilder gsonBuilder = new GsonBuilder(); this.gson = gsonBuilder.setFieldNamingPolicy(policy).create(); }
From source file:com.pkuhelper.model.base.ModelBaseProviderModule.java
License:Open Source License
@Singleton @Provides// www. j a v a 2s . c o m Gson provideGson(final GsonConfig config) { final GsonBuilder builder = new GsonBuilder(); if (config.autoGsonTypeAdapterFactory() != null) { builder.registerTypeAdapterFactory(config.autoGsonTypeAdapterFactory()); } if (config.fieldNamingPolicy() != null) { builder.setFieldNamingPolicy(config.fieldNamingPolicy()); } return builder .registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeJsonConverter(config.dateTimeFormatter())) .setDateFormat(config.dateFormatString()).setPrettyPrinting().create(); }
From source file:com.planyourexchange.rest.service.ServerService.java
License:Open Source License
public ServerService(String serviceUrl, final String userName, final String password, SharedPreferences sharedPreferences) { final TokenManager tokenManager = new TokenManager(sharedPreferences); // -- Initializing gson factory with joda converters and underscores default policy GsonBuilder builder = new GsonBuilder(); builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); Converters.registerLocalDate(builder); Converters.registerLocalTime(builder); // -- For Token Authentication Purposes OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setAuthenticator(tokenManager); RestAdapter restAdapter = new RestAdapter.Builder().setLogLevel(RestAdapter.LogLevel.FULL) .setConverter(new GsonConverter(builder.create())).setEndpoint(serviceUrl) .setRequestInterceptor(tokenManager).setClient(new OkClient(okHttpClient)).build(); this.serverApi = restAdapter.create(ServerApi.class); // -- Call login to get a new Token only WHEN it is needed tokenManager.setTokenAction(new TokenManager.TokenAction() { @Override/* ww w .j ava 2 s . c om*/ public void newToken() { // -- Getting Authentication Token serverApi.login(userName, password, tokenManager); } }); }