List of usage examples for com.google.gson JsonElement isJsonNull
public boolean isJsonNull()
From source file:com.ibm.watson.developer_cloud.util.DateDeserializer.java
License:Open Source License
@Override public synchronized Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonNull() || json.getAsString().isEmpty()) { return null; }/*w w w . j a va 2 s .co m*/ String dateAsString = json.getAsJsonPrimitive().getAsString().replaceAll("Z$", "+0000"); ParseException e = null; for (SimpleDateFormat format : dateFormatters) { try { return format.parse(dateAsString); } catch (ParseException e1) { e = e1; } } Pattern isJustNumber = Pattern.compile("^\\d+$"); Matcher foundMatch = isJustNumber.matcher(dateAsString); if (foundMatch.find()) { Long timeAsLong = Long.parseLong(dateAsString); Long msCheck = 100000000000L; // are we ms or seconds maybe? if (timeAsLong < msCheck) { // assuming in seconds timeAsLong = timeAsLong * 1000; } return new Date(timeAsLong); } LOG.log(Level.SEVERE, "Error parsing: " + dateAsString, e); return null; }
From source file:com.ibm.watson.movieapp.dialog.rest.UtilityFunctions.java
License:Open Source License
/** * Returns a property's value from a JSON * <p>//from ww w . jav a 2 s. c o m * This returns a property's value from a JSON object.</p> * * @param json the JSON object to be searched * @param propName the name of the property key * @return the value of the property */ public static String getPropValue(JsonObject json, String propName) { if (json != null && propName != null) { if (json.has(propName)) { if (json.get(propName) != null) { JsonElement element = json.get(propName); if (!element.isJsonNull()) { return json.get(propName).getAsString(); } } } } return null; }
From source file:com.iheart.quickio.client.QuickIo.java
License:Open Source License
private void routeCallback(final String evPath, final QuickIoCallback cb, final JsonElement json) { if (!json.isJsonObject()) { return;// w ww . j ava 2 s . co m } long cbId = Long.parseLong(evPath.substring(QuickIo.EV_CALLBACK.length())); QuickIoCallback evCb = null; synchronized (this) { evCb = this.cbs.remove(cbId); } if (evCb == null) { return; } JsonObject obj = json.getAsJsonObject(); int code = obj.get("code").getAsInt(); String errMsg = ""; if (obj.has("err_msg")) { JsonElement errObj = obj.get("err_msg"); errMsg = errObj.isJsonNull() ? null : errObj.getAsString(); } evCb.onCallback(obj.get("data"), cb, code, errMsg); }
From source file:com.inkubator.hrm.service.impl.BioDataServiceImpl.java
@Override @Transactional(readOnly = false, isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Exception.class) public String saveBiodataRevisionWithApproval(Object modifiedEntity, String dataType, EmpData empData) throws Exception { //Jika Approval Definition untuk proses revisi biodata belum di buat, lempar BussinessException if (isApprovalDefinitionNotFound()) { throw new BussinessException("biodata.biodata_revision_doesnt_have_approval_def"); }//from w w w. j a v a 2 s. co m HrmUser requestUser = hrmUserDao.getByEmpDataId(empData.getId()); //Dapatkan List Pending Approval Activity List<ApprovalActivity> listPreviousActivities = approvalActivityDao .getPendingRequest(requestUser.getUserId()); //Filter hanya yang berasal dari proses request revisi biodata listPreviousActivities = Lambda.select(listPreviousActivities, Lambda.having(Lambda.on(ApprovalActivity.class).getApprovalDefinition().getName(), Matchers.equalTo(HRMConstant.BIO_DATA_EDIT))); //Looping satu persatu for (ApprovalActivity approvalActivity : listPreviousActivities) { String pendingData = approvalActivity.getPendingData(); Gson gson = JsonUtil.getHibernateEntityGsonBuilder().create(); // dapatkan nilai dataType (jenis kelompok formulir biodata) dari pending approval activity JsonElement jsonElementDataType = gson.fromJson(pendingData, JsonObject.class).get("dataType"); if (!jsonElementDataType.isJsonNull()) { String dataTypeFromJson = jsonElementDataType.getAsString(); //Jika kelompok formulir dari pending Request sama dengan yang akan di ajukan sekarang, maka lempar BusinessException, //karena tidak boleh mengajukan perubahan biodata pada satu jenis kelompok formulir, //jika sebelumnya sudah pernah ada pengajuan revisi pada jenis kelompok formulir tsb dan statusnya masih pending. if (StringUtils.equals(dataType, dataTypeFromJson)) { throw new BussinessException("biodata.error_revision_same_data_type_which_still_pending_found"); } } } return this.saveRevision(modifiedEntity, dataType, empData, Boolean.FALSE, null); }
From source file:com.inkubator.hrm.service.impl.BioDataServiceImpl.java
@Override protected String getDetailSmsContentOfActivity(ApprovalActivity appActivity) { StringBuffer detail = new StringBuffer(); HrmUser requester = hrmUserDao.getByUserId(appActivity.getRequestBy()); String revisionAt = StringUtils.EMPTY; detail.append("Pengajuan Revisi Biodata oleh " + requester.getEmpData().getBioData().getFullName() + ". "); String pendingData = appActivity.getPendingData(); Gson gson = JsonUtil.getHibernateEntityGsonBuilder().create(); // dapatkan nilai dataType (jenis kelompok formulir biodata) dari pending approval activity JsonElement jsonElementDataType = gson.fromJson(pendingData, JsonObject.class).get("dataType"); if (!jsonElementDataType.isJsonNull()) { String dataType = jsonElementDataType.getAsString(); switch (dataType) { case HRMConstant.BIO_REV_DETAIL_BIO_DATA: revisionAt = "Data Detail Biodata"; break; case HRMConstant.BIO_REV_ADDRESS: revisionAt = "Data Alamat"; break; case HRMConstant.BIO_REV_CONTACT: revisionAt = "Data Kontak"; break; case HRMConstant.BIO_REV_ID_CARD: revisionAt = "Data Kartu Identitas"; break; case HRMConstant.BIO_REV_FAMILY: revisionAt = "Data Keluarga"; break; case HRMConstant.BIO_REV_COMPANY_RELATION: revisionAt = "Data Relasi Perusahaan"; break; case HRMConstant.BIO_REV_EDUCATION: revisionAt = "Data Pendidikan"; break; case HRMConstant.BIO_REV_SKILL: revisionAt = "Data Keahlian"; break; case HRMConstant.BIO_REV_SPESIFICATION_ABILITY: revisionAt = "Data Spesifikasi Kemampuan"; break; case HRMConstant.BIO_REV_INTEREST: revisionAt = "Data Minat"; break; default://from w w w . j ava 2 s. c om break; } } detail.append("Jenis kelompok Biodata : " + revisionAt + ". "); return detail.toString(); }
From source file:com.inkubator.hrm.web.reimbursement.RmbsApplicationApprovalFormController.java
@PostConstruct @Override// w ww .j ava 2 s . co m public void initialization() { try { super.initialization(); /** initial data for approval activity tracking */ String id = FacesUtil.getRequestParameter("execution"); currentActivity = approvalActivityService.getEntiyByPK(Long.parseLong(id.substring(1))); askingRevisedActivity = approvalActivityService.getEntityByActivityNumberAndSequence( currentActivity.getActivityNumber(), currentActivity.getSequence() - 1); isWaitingApproval = currentActivity.getApprovalStatus() == HRMConstant.APPROVAL_STATUS_WAITING_APPROVAL; isWaitingRevised = currentActivity.getApprovalStatus() == HRMConstant.APPROVAL_STATUS_WAITING_REVISED; isApprover = StringUtils.equals(UserInfoUtil.getUserName(), currentActivity.getApprovedBy()); isRequester = StringUtils.equals(UserInfoUtil.getUserName(), currentActivity.getRequestBy()); /** start binding data that needed (from json) to object */ Gson gson = JsonUtil.getHibernateEntityGsonBuilder().create(); rmbsApplication = gson.fromJson(currentActivity.getPendingData(), RmbsApplication.class); //relational object EmpData empData = empDataService.getByEmpIdWithDetail(rmbsApplication.getEmpData().getId()); rmbsApplication.setEmpData(empData); RmbsType rmbsType = rmbsTypeService.getEntiyByPK(rmbsApplication.getRmbsType().getId()); rmbsApplication.setRmbsType(rmbsType); Currency currency = currencyService.getEntiyByPK(rmbsApplication.getCurrency().getId()); rmbsApplication.setCurrency(currency); //additional information RmbsSchemaListOfEmp rmbsSchemaListOfEmp = rmbsSchemaListOfEmpService .getEntityByEmpDataId(empData.getId()); rmbsSchema = rmbsSchemaListOfEmp.getRmbsSchema(); rmbsSchemaListOfType = rmbsSchemaListOfTypeService .getEntityByPk(new RmbsSchemaListOfTypeId(rmbsType.getId(), rmbsSchema.getId())); totalRequestThisMoth = rmbsApplicationService.getTotalNominalForOneMonth( rmbsApplication.getApplicationDate(), empData.getId(), rmbsType.getId()); //attachment file JsonObject jsonObject = gson.fromJson(currentActivity.getPendingData(), JsonObject.class); JsonElement elReimbursementFileName = jsonObject.get("reimbursementFileName"); isHaveAttachment = !elReimbursementFileName.isJsonNull(); } catch (Exception ex) { LOGGER.error("Error", ex); } }
From source file:com.jayway.jsonpath.internal.spi.json.GsonJsonProvider.java
License:Apache License
public static Object unwrap(Object o) { if (o == null) { return null; }/*from w w w . j a v a2 s . c om*/ if (!(o instanceof JsonElement)) { return o; } JsonElement e = (JsonElement) o; if (e.isJsonNull()) { return null; } else if (e.isJsonPrimitive()) { JsonPrimitive p = e.getAsJsonPrimitive(); if (p.isString()) { return p.getAsString(); } else if (p.isBoolean()) { return p.getAsBoolean(); } else if (p.isNumber()) { return unwrapNumber(p.getAsNumber()); } } return o; }
From source file:com.jayway.jsonpath.internal.spi.mapper.GsonMapper.java
License:Apache License
@Override public Object convert(Object src, Class<?> srcType, Class<?> targetType, Configuration conf) { assertValidConversion(src, srcType, targetType); if (src == null || src.getClass().equals(JsonNull.class)) { return null; }/* w w w .j a v a 2s . c om*/ if (JsonPrimitive.class.isAssignableFrom(srcType)) { JsonPrimitive primitive = (JsonPrimitive) src; if (targetType.equals(Long.class)) { return primitive.getAsLong(); } else if (targetType.equals(Integer.class)) { return primitive.getAsInt(); } else if (targetType.equals(BigInteger.class)) { return primitive.getAsBigInteger(); } else if (targetType.equals(Byte.class)) { return primitive.getAsByte(); } else if (targetType.equals(BigDecimal.class)) { return primitive.getAsBigDecimal(); } else if (targetType.equals(Double.class)) { return primitive.getAsDouble(); } else if (targetType.equals(Float.class)) { return primitive.getAsFloat(); } else if (targetType.equals(String.class)) { return primitive.getAsString(); } else if (targetType.equals(Boolean.class)) { return primitive.getAsBoolean(); } else if (targetType.equals(Date.class)) { if (primitive.isNumber()) { return new Date(primitive.getAsLong()); } else if (primitive.isString()) { try { return DateFormat.getInstance().parse(primitive.getAsString()); } catch (ParseException e) { throw new MappingException(e); } } } } else if (JsonObject.class.isAssignableFrom(srcType)) { JsonObject srcObject = (JsonObject) src; if (targetType.equals(Map.class)) { Map<String, Object> targetMap = new LinkedHashMap<String, Object>(); for (Map.Entry<String, JsonElement> entry : srcObject.entrySet()) { Object val = null; JsonElement element = entry.getValue(); if (element.isJsonPrimitive()) { val = GsonJsonProvider.unwrap(element); } else if (element.isJsonArray()) { val = convert(element, element.getClass(), List.class, conf); } else if (element.isJsonObject()) { val = convert(element, element.getClass(), Map.class, conf); } else if (element.isJsonNull()) { val = null; } targetMap.put(entry.getKey(), val); } return targetMap; } } else if (JsonArray.class.isAssignableFrom(srcType)) { JsonArray srcArray = (JsonArray) src; if (targetType.equals(List.class)) { List<Object> targetList = new ArrayList<Object>(); for (JsonElement element : srcArray) { if (element.isJsonPrimitive()) { targetList.add(GsonJsonProvider.unwrap(element)); } else if (element.isJsonArray()) { targetList.add(convert(element, element.getClass(), List.class, conf)); } else if (element.isJsonObject()) { targetList.add(convert(element, element.getClass(), Map.class, conf)); } else if (element.isJsonNull()) { targetList.add(null); } } return targetList; } } throw new MappingException("Can not map: " + srcType.getName() + " to: " + targetType.getName()); }
From source file:com.jayway.jsonpath.spi.json.GsonJsonProvider.java
License:Apache License
public Object unwrap(final Object o) { if (o == null) { return null; }/* ww w. ja va2s .c om*/ if (!(o instanceof JsonElement)) { return o; } JsonElement e = (JsonElement) o; if (e.isJsonNull()) { return null; } else if (e.isJsonPrimitive()) { JsonPrimitive p = e.getAsJsonPrimitive(); if (p.isString()) { return p.getAsString(); } else if (p.isBoolean()) { return p.getAsBoolean(); } else if (p.isNumber()) { return unwrapNumber(p.getAsNumber()); } } return o; }
From source file:com.kodokux.github.api.GithubApiUtil.java
License:Apache License
@NotNull private static ResponsePage request(@NotNull GithubAuthData auth, @NotNull String path, @Nullable String requestBody, @NotNull Collection<Header> headers, @NotNull HttpVerb verb) throws IOException { HttpMethod method = null;/*from w w w . j av a 2 s.c om*/ try { String uri = GithubUrlUtil.getApiUrl(auth.getHost()) + path; method = doREST(auth, uri, requestBody, headers, verb); checkStatusCode(method); InputStream resp = method.getResponseBodyAsStream(); if (resp == null) { return new ResponsePage(); } JsonElement ret = parseResponse(resp); if (ret.isJsonNull()) { return new ResponsePage(); } Header header = method.getResponseHeader("Link"); if (header != null) { String value = header.getValue(); int end = value.indexOf(">; rel=\"next\""); int begin = value.lastIndexOf('<', end); if (begin >= 0 && end >= 0) { String newPath = GithubUrlUtil.removeProtocolPrefix(value.substring(begin + 1, end)); int index = newPath.indexOf('/'); return new ResponsePage(ret, newPath.substring(index)); } } return new ResponsePage(ret); } finally { if (method != null) { method.releaseConnection(); } } }