Example usage for com.google.gson JsonElement isJsonObject

List of usage examples for com.google.gson JsonElement isJsonObject

Introduction

In this page you can find the example usage for com.google.gson JsonElement isJsonObject.

Prototype

public boolean isJsonObject() 

Source Link

Document

provides check for verifying if this element is a Json object or not.

Usage

From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java

License:Apache License

/**
 * Used with the local date is in array format
 *///from   ww w .j  a va2  s  .  c  o  m
public LocalDate extractLocalDateAsArrayNamed(final String parameterName, final JsonElement element,
        final Set<String> parametersPassedInCommand) {
    LocalDate value = null;
    if (element.isJsonObject()) {
        final JsonObject object = element.getAsJsonObject();

        if (object.has(parameterName) && object.get(parameterName).isJsonArray()) {

            parametersPassedInCommand.add(parameterName);

            final JsonArray dateArray = object.get(parameterName).getAsJsonArray();

            final Integer year = dateArray.get(0).getAsInt();
            final Integer month = dateArray.get(1).getAsInt();
            final Integer day = dateArray.get(2).getAsInt();

            value = new LocalDate().withYearOfEra(year).withMonthOfYear(month).withDayOfMonth(day);
        }

    }
    return value;
}

From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java

License:Apache License

public MonthDay extractMonthDayNamed(final String parameterName, final JsonElement element) {

    MonthDay value = null;// ww  w . j ava  2 s.c  o  m

    if (element.isJsonObject()) {
        final JsonObject object = element.getAsJsonObject();

        final String monthDayFormat = extractMonthDayFormatParameter(object);
        final Locale clientApplicationLocale = extractLocaleParameter(object);
        value = extractMonthDayNamed(parameterName, object, monthDayFormat, clientApplicationLocale);
    }
    return value;
}

From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java

License:Apache License

public LocalDate extractLocalDateNamed(final String parameterName, final JsonElement element,
        final Set<String> parametersPassedInCommand) {

    LocalDate value = null;//from w  ww . j a v  a  2 s .  c  o m

    if (element.isJsonObject()) {
        final JsonObject object = element.getAsJsonObject();

        final String dateFormat = extractDateFormatParameter(object);
        final Locale clientApplicationLocale = extractLocaleParameter(object);
        value = extractLocalDateNamed(parameterName, object, dateFormat, clientApplicationLocale,
                parametersPassedInCommand);
    }
    return value;
}

From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java

License:Apache License

public LocalDateTime extractLocalTimeNamed(final String parameterName, final JsonElement element,
        final Set<String> parametersPassedInCommand) {

    LocalDateTime value = null;/* w  w w.j  a v a  2  s  . com*/

    if (element.isJsonObject()) {
        final JsonObject object = element.getAsJsonObject();

        final String timeFormat = extractTimeFormatParameter(object);
        final Locale clientApplicationLocale = extractLocaleParameter(object);
        value = extractLocalTimeNamed(parameterName, object, timeFormat, clientApplicationLocale,
                parametersPassedInCommand);
    }
    return value;
}

From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java

License:Apache License

public Double extractDoubleNamed(final String parameterName, final JsonElement element,
        final Set<String> parametersPassedInRequest) {
    Double value = null;/*from w ww  .j  av  a2  s  . 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)) {
                value = Double.valueOf(stringValue);
            }
        }
    }
    return value;
}

From source file:com.gst.portfolio.collateral.service.CollateralAssembler.java

License:Apache License

public Set<LoanCollateral> fromParsedJson(final JsonElement element) {

    final Set<LoanCollateral> collateralItems = new HashSet<>();

    if (element.isJsonObject()) {
        final JsonObject topLevelJsonElement = element.getAsJsonObject();

        if (topLevelJsonElement.has("collateral") && topLevelJsonElement.get("collateral").isJsonArray()) {
            final JsonArray array = topLevelJsonElement.get("collateral").getAsJsonArray();
            final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement);
            for (int i = 0; i < array.size(); i++) {

                final JsonObject collateralItemElement = array.get(i).getAsJsonObject();

                final Long id = this.fromApiJsonHelper.extractLongNamed("id", collateralItemElement);
                final Long collateralTypeId = this.fromApiJsonHelper.extractLongNamed("type",
                        collateralItemElement);
                final CodeValue collateralType = this.codeValueRepository
                        .findOneWithNotFoundDetection(collateralTypeId);
                final String description = this.fromApiJsonHelper.extractStringNamed("description",
                        collateralItemElement);
                final BigDecimal value = this.fromApiJsonHelper.extractBigDecimalNamed("value",
                        collateralItemElement, locale);

                if (id == null) {
                    collateralItems.add(LoanCollateral.from(collateralType, value, description));
                } else {
                    final LoanCollateral loanCollateralItem = this.loanCollateralRepository.findOne(id);
                    if (loanCollateralItem == null) {
                        throw new CollateralNotFoundException(id);
                    }// ww w .ja  v  a2 s.c  o m

                    loanCollateralItem.assembleFrom(collateralType, value, description);

                    collateralItems.add(loanCollateralItem);
                }
            }
        } else {
            // no collaterals passed, use existing ones against loan
        }

    }

    return collateralItems;
}

From source file:com.gst.portfolio.collectionsheet.data.CollectionSheetTransactionDataValidator.java

License:Apache License

private void validateAttendanceDetails(final JsonElement element,
        final DataValidatorBuilder baseDataValidator) {
    final JsonObject topLevelJsonElement = element.getAsJsonObject();
    if (element.isJsonObject()) {
        if (topLevelJsonElement.has(clientsAttendanceParamName)
                && topLevelJsonElement.get(clientsAttendanceParamName).isJsonArray()) {
            final JsonArray array = topLevelJsonElement.get(clientsAttendanceParamName).getAsJsonArray();
            for (int i = 0; i < array.size(); i++) {
                final JsonObject attendanceElement = array.get(i).getAsJsonObject();
                final Long clientId = this.fromApiJsonHelper.extractLongNamed(clientIdParamName,
                        attendanceElement);
                final Long attendanceType = this.fromApiJsonHelper.extractLongNamed(attendanceTypeParamName,
                        attendanceElement);
                baseDataValidator.reset()
                        .parameter(clientsAttendanceParamName + "[" + i + "]." + clientIdParamName)
                        .value(clientId).notNull().integerGreaterThanZero();
                baseDataValidator.reset()
                        .parameter(clientsAttendanceParamName + "[" + i + "]." + attendanceTypeParamName)
                        .value(attendanceType).notNull().integerGreaterThanZero();
            }/*  w ww .  j a va 2 s  .c o m*/
        }
    }
}

From source file:com.gst.portfolio.collectionsheet.data.CollectionSheetTransactionDataValidator.java

License:Apache License

private void validateDisbursementTransactions(final JsonElement element,
        final DataValidatorBuilder baseDataValidator) {
    final JsonObject topLevelJsonElement = element.getAsJsonObject();
    final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement);
    if (element.isJsonObject()) {
        if (topLevelJsonElement.has(bulkDisbursementTransactionsParamName)
                && topLevelJsonElement.get(bulkDisbursementTransactionsParamName).isJsonArray()) {
            final JsonArray array = topLevelJsonElement.get(bulkDisbursementTransactionsParamName)
                    .getAsJsonArray();// w ww .j  a  va  2  s.c  o  m

            for (int i = 0; i < array.size(); i++) {
                final JsonObject loanTransactionElement = array.get(i).getAsJsonObject();
                final Long loanId = this.fromApiJsonHelper.extractLongNamed(loanIdParamName,
                        loanTransactionElement);
                final BigDecimal disbursementAmount = this.fromApiJsonHelper
                        .extractBigDecimalNamed(transactionAmountParamName, loanTransactionElement, locale);

                baseDataValidator.reset().parameter("bulktransaction" + "[" + i + "].loan.id").value(loanId)
                        .notNull().integerGreaterThanZero();
                baseDataValidator.reset().parameter("bulktransaction" + "[" + i + "].disbursement.amount")
                        .value(disbursementAmount).notNull().zeroOrPositiveAmount();
            }
        }
    }
}

From source file:com.gst.portfolio.collectionsheet.data.CollectionSheetTransactionDataValidator.java

License:Apache License

private void validateRepaymentTransactions(final JsonElement element,
        final DataValidatorBuilder baseDataValidator) {
    final JsonObject topLevelJsonElement = element.getAsJsonObject();
    final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement);
    if (element.isJsonObject()) {
        if (topLevelJsonElement.has(bulkRepaymentTransactionsParamName)
                && topLevelJsonElement.get(bulkRepaymentTransactionsParamName).isJsonArray()) {
            final JsonArray array = topLevelJsonElement.get(bulkRepaymentTransactionsParamName)
                    .getAsJsonArray();// w  w w.ja  va2  s.  c o  m

            for (int i = 0; i < array.size(); i++) {
                final JsonObject loanTransactionElement = array.get(i).getAsJsonObject();
                final Long loanId = this.fromApiJsonHelper.extractLongNamed(loanIdParamName,
                        loanTransactionElement);
                final BigDecimal disbursementAmount = this.fromApiJsonHelper
                        .extractBigDecimalNamed(transactionAmountParamName, loanTransactionElement, locale);

                baseDataValidator.reset().parameter("bulktransaction" + "[" + i + "].loan.id").value(loanId)
                        .notNull().integerGreaterThanZero();
                baseDataValidator.reset().parameter("bulktransaction" + "[" + i + "].disbursement.amount")
                        .value(disbursementAmount).notNull().zeroOrPositiveAmount();

                validatePaymentDetails(baseDataValidator, loanTransactionElement, locale);
            }
        }
    }
}

From source file:com.gst.portfolio.collectionsheet.data.CollectionSheetTransactionDataValidator.java

License:Apache License

private void validateSavingsDueTransactions(final JsonElement element,
        final DataValidatorBuilder baseDataValidator) {
    final JsonObject topLevelJsonElement = element.getAsJsonObject();
    final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement);
    if (element.isJsonObject()) {
        if (topLevelJsonElement.has(bulkSavingsDueTransactionsParamName)
                && topLevelJsonElement.get(bulkSavingsDueTransactionsParamName).isJsonArray()) {
            final JsonArray array = topLevelJsonElement.get(bulkSavingsDueTransactionsParamName)
                    .getAsJsonArray();//from   ww  w  .  j a  v a  2  s  . c o m

            for (int i = 0; i < array.size(); i++) {
                final JsonObject savingsTransactionElement = array.get(i).getAsJsonObject();
                final Long savingsId = this.fromApiJsonHelper.extractLongNamed(savingsIdParamName,
                        savingsTransactionElement);
                final BigDecimal dueAmount = this.fromApiJsonHelper
                        .extractBigDecimalNamed(transactionAmountParamName, savingsTransactionElement, locale);

                baseDataValidator.reset().parameter("bulktransaction" + "[" + i + "].savings.id")
                        .value(savingsId).notNull().integerGreaterThanZero();
                baseDataValidator.reset().parameter("bulktransaction" + "[" + i + "].due.amount")
                        .value(dueAmount).notNull().zeroOrPositiveAmount();
                validatePaymentDetails(baseDataValidator, savingsTransactionElement, locale);
            }
        }
    }
}