List of usage examples for com.google.gson FieldNamingPolicy LOWER_CASE_WITH_DASHES
FieldNamingPolicy LOWER_CASE_WITH_DASHES
To view the source code for com.google.gson FieldNamingPolicy LOWER_CASE_WITH_DASHES.
Click Source Link
From source file:ca.oson.json.Oson.java
License:Open Source License
public Gson getGson() { if (gson == null) { GsonBuilder gsonBuilder = new GsonBuilder(); switch (getDefaultType()) { case ALWAYS: gsonBuilder.serializeNulls(); break; case NON_NULL: break; case NON_EMPTY: break; case DEFAULT: gsonBuilder.serializeNulls(); break; default://from w w w. j ava 2 s .c o m gsonBuilder.serializeNulls(); break; } switch (getFieldNaming()) { case FIELD: // original field name: someField_name gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY); break; case LOWER: // somefield_name -> some_field_name gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); break; case UPPER: //SOMEFIELD_NAME -> SomeFieldName gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE); break; case CAMELCASE: // someFieldName gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY); break; case UPPER_CAMELCASE: // SomeFieldName gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE); break; case UNDERSCORE_CAMELCASE: // some_Field_Name gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); break; case UNDERSCORE_UPPER_CAMELCASE: // Some_Field_Name gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); break; case UNDERSCORE_LOWER: // some_field_name gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); break; case UNDERSCORE_UPPER: // SOME_FIELD_NAME gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); break; case SPACE_CAMELCASE: // some Field Name gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES); break; case SPACE_UPPER_CAMELCASE: // Some Field Name gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES); break; case SPACE_LOWER: // some field name gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES); break; case SPACE_UPPER: // SOME FIELD NAME gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES); break; case DASH_CAMELCASE: // some-Field-Name gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES); break; case DASH_UPPER_CAMELCASE: // Some-Field-Name gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES); break; case DASH_LOWER: // some-field-name gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES); break; case DASH_UPPER: // SOME-FIELD-NAME gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES); break; default: gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY); break; } if (getPrettyPrinting() && getIndentation() > 0) { gsonBuilder.setPrettyPrinting(); } gsonBuilder.setDateFormat(options.getSimpleDateFormat()); Set<FieldMapper> mappers = getFieldMappers(); Map<Class, ClassMapper> classMappers = getClassMappers(); List<ExclusionStrategy> strategies = new ArrayList<>(); if (mappers != null) { gsonBuilder.setFieldNamingStrategy(new FieldNamingStrategy() { @Override public String translateName(Field f) { String fieldName = f.getName(); String serializedName = java2Json(f); if (!fieldName.equalsIgnoreCase(serializedName)) { // if null returned, this field is ignored return serializedName; } return json2Java(f); } }); for (FieldMapper mapper : mappers) { if (mapper.java == null || mapper.json == null || mapper.ignore) { strategies.add(new ExclusionStrategy() { @Override public boolean shouldSkipField(FieldAttributes f) { String name = f.getName(); Class cls = f.getClass(); if (mapper.java == null) { if (mapper.json.equals(name)) { if (mapper.getType() == null || cls.equals(mapper.getType())) { return true; } } } else if (mapper.json == null || mapper.ignore) { if (mapper.java.equals(name)) { if (mapper.getType() == null || cls.equals(mapper.getType())) { return true; } } } return false; } @Override public boolean shouldSkipClass(Class<?> clazz) { return false; } }); } } } if (classMappers != null) { for (Entry<Class, ClassMapper> entry : classMappers.entrySet()) { ClassMapper mapper = entry.getValue(); if (mapper.getType() == null) { mapper.setType(entry.getKey()); } if (mapper.ignore != null && mapper.ignore) { strategies.add(new ExclusionStrategy() { @Override public boolean shouldSkipField(FieldAttributes f) { return false; } @Override public boolean shouldSkipClass(Class<?> clazz) { if (clazz.equals(mapper.getType())) { return true; } return false; } }); } } for (Entry<Class, ClassMapper> entry : classMappers.entrySet()) { ClassMapper mapper = entry.getValue(); if (!mapper.ignore && mapper.constructor != null) { gsonBuilder.registerTypeAdapter(entry.getKey(), mapper.constructor); } } } int size = strategies.size(); if (size > 0) { gsonBuilder.setExclusionStrategies(strategies.toArray(new ExclusionStrategy[size])); } Double version = getVersion(); if (version != null) { gsonBuilder.setVersion(version); } if (isUseGsonExpose()) { gsonBuilder.excludeFieldsWithoutExposeAnnotation(); } if (!DefaultValue.isDefault(getPatterns())) { gsonBuilder.setLenient(); } gson = gsonBuilder.create(); } return gson; }
From source file:com.mediamath.terminalone.service.GetService.java
License:Apache License
/** * parses error response of Get operation. * // w ww .j av a2 s. c om * @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. * /*from w w w .j av a2 s.co 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.mediamath.terminalone.utils.T1JsonToObjParser.java
License:Apache License
/** * parses 3pas creative upload response string to TPASCreativeUpload object. * /* ww w . j a va 2s . 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. * //from www .j a v a2s . c o 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 w w w .j a va2 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:org.hyperic.hq.plugin.jboss7.JBossAdminHttp.java
License:Open Source License
private Object query(HttpRequestBase req, String api, Type type) throws PluginException { Object res = null;/*from ww w. jav a 2 s . co m*/ try { HttpResponse response = client.execute(req, localcontext); int statusCode = response.getStatusLine().getStatusCode(); // response must be read in order to "close" the connection. // https://jira.hyperic.com/browse/HHQ-5063#comment-154101 String responseBody = readInputString(response.getEntity().getContent()); if (log.isDebugEnabled()) { log.debug("[" + api + "] -(" + req.getURI() + ")-> " + responseBody); } if (statusCode != 200) { throw new PluginException("[" + req.getURI() + "] http error code: '" + statusCode + "' msg='" + response.getStatusLine().getReasonPhrase() + "'"); } GsonBuilder gsb = new GsonBuilder(); if (!((type instanceof Class) && ((Class) type).getCanonicalName().equals(Connector.class.getCanonicalName()))) { gsb.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES); } Gson gson = gsb.create(); res = gson.fromJson(responseBody, type); if (log.isDebugEnabled()) { if (res.getClass().isArray()) { log.debug("[" + api + "] -(" + statusCode + ")*> " + Arrays.asList((Object[]) res)); } else { log.debug("[" + api + "] -(" + statusCode + ")-> " + res); } } } catch (JsonParseException ex) { log.debug(ex.getMessage(), ex); throw new PluginException(ex.getMessage(), ex); } catch (IOException ex) { log.debug(ex.getMessage(), ex); throw new PluginException(ex.getMessage(), ex); } return res; }
From source file:org.onebusaway.admin.json.LowerCaseWDashesGsonJsonTool.java
License:Apache License
private Gson buildGsonObject() { GsonBuilder gbuilder = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES); setTypeAdapters(gbuilder);//from www . j a v a2s .c o m if (prettyPrintOutput) gbuilder.setPrettyPrinting(); Gson gson = gbuilder.create(); return gson; }
From source file:org.onebusaway.admin.service.bundle.impl.BundleBuildingUtil.java
License:Apache License
public String generateJsonMetadata(BundleBuildRequest request, BundleBuildResponse response, String bundleId) { File bundleDir = new File(response.getBundleDataDirectory()); List<BundleFile> files = getBundleFilesWithSumsForDirectory(bundleDir, bundleDir); Gson gson = new GsonBuilder().serializeNulls() .registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter()) .registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter()) .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).setPrettyPrinting().create(); Bundle bundle = new Bundle(); if (bundleId != null) { bundle.setId(bundleId); // Use id from previous build } else {/*from w w w . ja v a2s .co m*/ bundle.setId(getBundleId(response.getBundleRootDirectory() + File.separator + "data" + File.separator + "metadata.json")); } bundle.setDataset(request.getBundleDirectory()); bundle.setName(request.getBundleName()); bundle.setServiceDateFrom(request.getBundleStartDate()); bundle.setServiceDateTo(request.getBundleEndDate()); DateTime now = new DateTime(); bundle.setCreated(now); bundle.setUpdated(now); List<String> applicableAgencyIds = new ArrayList<String>(); // TODO this should come from somewhere //applicableAgencyIds.add("MTA NYCT"); bundle.setApplicableAgencyIds(applicableAgencyIds); bundle.setFiles(files); String output = gson.toJson(bundle); String outputFilename = response.getBundleRootDirectory() + File.separator + META_DATA_FILE; File outputFile = new File(outputFilename); _log.info("creating metadata file=" + outputFilename); PrintWriter writer = null; try { writer = new PrintWriter(outputFile); writer.print(output); } catch (Exception any) { _log.error(any.toString(), any); response.setException(any); } finally { writer.close(); } return outputFilename; }