List of usage examples for com.google.gson JsonObject has
public boolean has(String memberName)
From source file:com.gsaul.AethonSimulator.RuntimeTypeAdapterFactory.java
License:Apache License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (type.getRawType() != baseType) { return null; }//from ww w . j a v a2 s .c o m final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>(); final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<Class<?>, TypeAdapter<?>>(); for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) { TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue())); labelToDelegate.put(entry.getKey(), delegate); subtypeToDelegate.put(entry.getValue(), delegate); } return new TypeAdapter<R>() { @Override public R read(JsonReader in) throws IOException { JsonElement jsonElement = Streams.parse(in); JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName); //used to be .remove(typeFieldName); if (labelJsonElement == null) { throw new JsonParseException("cannot deserialize " + baseType + " because it does not define a field named " + typeFieldName); } String label = labelJsonElement.getAsString(); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } return delegate.fromJsonTree(jsonElement); } @Override public void write(JsonWriter out, R value) throws IOException { Class<?> srcType = value.getClass(); String label = subtypeToLabel.get(srcType); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType); if (delegate == null) { throw new JsonParseException( "cannot serialize " + srcType.getName() + "; did you forget to register a subtype?"); } JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject(); if (jsonObject.has(typeFieldName)) { throw new JsonParseException("cannot serialize " + srcType.getName() + " because it already defines a field named " + typeFieldName); } JsonObject clone = new JsonObject(); clone.add(typeFieldName, new JsonPrimitive(label)); for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) { clone.add(e.getKey(), e.getValue()); } Streams.write(clone, out); } }.nullSafe(); }
From source file:com.gst.accounting.journalentry.serialization.JournalEntryCommandFromApiJsonDeserializer.java
License:Apache License
@Override public JournalEntryCommand commandFromApiJson(final String json) { if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); }//from w w w. ja v a 2 s.c om final Type typeOfMap = new TypeToken<Map<String, Object>>() { }.getType(); final Set<String> supportedParameters = JournalEntryJsonInputParams.getAllValues(); this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParameters); final JsonElement element = this.fromApiJsonHelper.parse(json); final Long officeId = this.fromApiJsonHelper .extractLongNamed(JournalEntryJsonInputParams.OFFICE_ID.getValue(), element); final String currencyCode = this.fromApiJsonHelper .extractStringNamed(JournalEntryJsonInputParams.CURRENCY_CODE.getValue(), element); final String comments = this.fromApiJsonHelper .extractStringNamed(JournalEntryJsonInputParams.COMMENTS.getValue(), element); final LocalDate transactionDate = this.fromApiJsonHelper .extractLocalDateNamed(JournalEntryJsonInputParams.TRANSACTION_DATE.getValue(), element); final String referenceNumber = this.fromApiJsonHelper .extractStringNamed(JournalEntryJsonInputParams.REFERENCE_NUMBER.getValue(), element); final Long accountingRuleId = this.fromApiJsonHelper .extractLongNamed(JournalEntryJsonInputParams.ACCOUNTING_RULE.getValue(), element); final JsonObject topLevelJsonElement = element.getAsJsonObject(); final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement); final BigDecimal amount = this.fromApiJsonHelper .extractBigDecimalNamed(JournalEntryJsonInputParams.AMOUNT.getValue(), element, locale); final Long paymentTypeId = this.fromApiJsonHelper .extractLongNamed(JournalEntryJsonInputParams.PAYMENT_TYPE_ID.getValue(), element); final String accountNumber = this.fromApiJsonHelper .extractStringNamed(JournalEntryJsonInputParams.ACCOUNT_NUMBER.getValue(), element); final String checkNumber = this.fromApiJsonHelper .extractStringNamed(JournalEntryJsonInputParams.CHECK_NUMBER.getValue(), element); final String receiptNumber = this.fromApiJsonHelper .extractStringNamed(JournalEntryJsonInputParams.RECEIPT_NUMBER.getValue(), element); final String bankNumber = this.fromApiJsonHelper .extractStringNamed(JournalEntryJsonInputParams.BANK_NUMBER.getValue(), element); final String routingCode = this.fromApiJsonHelper .extractStringNamed(JournalEntryJsonInputParams.ROUTING_CODE.getValue(), element); SingleDebitOrCreditEntryCommand[] credits = null; SingleDebitOrCreditEntryCommand[] debits = null; if (element.isJsonObject()) { if (topLevelJsonElement.has(JournalEntryJsonInputParams.CREDITS.getValue()) && topLevelJsonElement.get(JournalEntryJsonInputParams.CREDITS.getValue()).isJsonArray()) { credits = populateCreditsOrDebitsArray(topLevelJsonElement, locale, credits, JournalEntryJsonInputParams.CREDITS.getValue()); } if (topLevelJsonElement.has(JournalEntryJsonInputParams.DEBITS.getValue()) && topLevelJsonElement.get(JournalEntryJsonInputParams.DEBITS.getValue()).isJsonArray()) { debits = populateCreditsOrDebitsArray(topLevelJsonElement, locale, debits, JournalEntryJsonInputParams.DEBITS.getValue()); } } return new JournalEntryCommand(officeId, currencyCode, transactionDate, comments, credits, debits, referenceNumber, accountingRuleId, amount, paymentTypeId, accountNumber, checkNumber, receiptNumber, bankNumber, routingCode); }
From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java
License:Apache License
public Boolean extractBooleanNamed(final String parameterName, final JsonElement element, final Set<String> requestParamatersDetected) { Boolean value = null;/*from www.j a va2 s .c o m*/ if (element.isJsonObject()) { final JsonObject object = element.getAsJsonObject(); if (object.has(parameterName) && object.get(parameterName).isJsonPrimitive()) { requestParamatersDetected.add(parameterName); final JsonPrimitive primitive = object.get(parameterName).getAsJsonPrimitive(); value = primitive.getAsBoolean(); } } return value; }
From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java
License:Apache License
public Long extractLongNamed(final String parameterName, final JsonElement element, final Set<String> parametersPassedInRequest) { Long longValue = null;/* w w w. ja v a 2s . co m*/ if (element.isJsonObject()) { final JsonObject object = element.getAsJsonObject(); if (object.has(parameterName) && object.get(parameterName).isJsonPrimitive()) { parametersPassedInRequest.add(parameterName); final JsonPrimitive primitive = object.get(parameterName).getAsJsonPrimitive(); final String stringValue = primitive.getAsString(); if (StringUtils.isNotBlank(stringValue)) { longValue = Long.valueOf(stringValue); } } } return longValue; }
From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java
License:Apache License
public String extractStringNamed(final String parameterName, final JsonElement element, final Set<String> parametersPassedInRequest) { String stringValue = null;//w ww. ja va2 s .com if (element.isJsonObject()) { final JsonObject object = element.getAsJsonObject(); if (object.has(parameterName) && object.get(parameterName).isJsonPrimitive()) { parametersPassedInRequest.add(parameterName); final JsonPrimitive primitive = object.get(parameterName).getAsJsonPrimitive(); final String valueAsString = primitive.getAsString(); if (StringUtils.isNotBlank(valueAsString)) { stringValue = valueAsString; } } } return stringValue; }
From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java
License:Apache License
public BigDecimal extractBigDecimalNamed(final String parameterName, final JsonObject element, final Locale locale, final Set<String> modifiedParameters) { BigDecimal value = null;// w ww . j a va2s . c o m if (element.isJsonObject()) { final JsonObject object = element.getAsJsonObject(); if (object.has(parameterName) && object.get(parameterName).isJsonPrimitive()) { modifiedParameters.add(parameterName); final JsonPrimitive primitive = object.get(parameterName).getAsJsonPrimitive(); final String valueAsString = primitive.getAsString(); if (StringUtils.isNotBlank(valueAsString)) { value = convertFrom(valueAsString, parameterName, locale); } } } return value; }
From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java
License:Apache License
public Integer extractIntegerNamed(final String parameterName, final JsonElement element, final Locale locale, final Set<String> modifiedParameters) { Integer value = null;//from w w w . j a va 2 s .c om if (element.isJsonObject()) { final JsonObject object = element.getAsJsonObject(); if (object.has(parameterName) && object.get(parameterName).isJsonPrimitive()) { modifiedParameters.add(parameterName); final JsonPrimitive primitive = object.get(parameterName).getAsJsonPrimitive(); final String valueAsString = primitive.getAsString(); if (StringUtils.isNotBlank(valueAsString)) { value = convertToInteger(valueAsString, parameterName, locale); } } } return value; }
From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java
License:Apache License
/** * Method used to extract integers from unformatted strings. Ex: "1" , * "100002" etc/* w ww . j av a 2 s .c om*/ * * Please note that this method does not support extracting Integers from * locale specific formatted strings Ex "1,000" etc * * @param parameterName * @param element * @param parametersPassedInRequest * @return */ public Integer extractIntegerSansLocaleNamed(final String parameterName, final JsonElement element, final Set<String> parametersPassedInRequest) { Integer intValue = null; if (element.isJsonObject()) { final JsonObject object = element.getAsJsonObject(); if (object.has(parameterName) && object.get(parameterName).isJsonPrimitive()) { parametersPassedInRequest.add(parameterName); final JsonPrimitive primitive = object.get(parameterName).getAsJsonPrimitive(); final String stringValue = primitive.getAsString(); if (StringUtils.isNotBlank(stringValue)) { intValue = convertToIntegerSanLocale(stringValue, parameterName); } } } return intValue; }
From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java
License:Apache License
public String extractDateFormatParameter(final JsonObject element) { String value = null;// w w w .jav a2s. co m if (element.isJsonObject()) { final JsonObject object = element.getAsJsonObject(); final String dateFormatParameter = "dateFormat"; if (object.has(dateFormatParameter) && object.get(dateFormatParameter).isJsonPrimitive()) { final JsonPrimitive primitive = object.get(dateFormatParameter).getAsJsonPrimitive(); value = primitive.getAsString(); } } return value; }
From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java
License:Apache License
public String extractTimeFormatParameter(final JsonObject element) { String value = null;/*from w ww . jav a 2 s. c o m*/ if (element.isJsonObject()) { final JsonObject object = element.getAsJsonObject(); final String timeFormatParameter = "timeFormat"; if (object.has(timeFormatParameter) && object.get(timeFormatParameter).isJsonPrimitive()) { final JsonPrimitive primitive = object.get(timeFormatParameter).getAsJsonPrimitive(); value = primitive.getAsString(); } } return value; }