Example usage for com.google.gson JsonPrimitive getAsString

List of usage examples for com.google.gson JsonPrimitive getAsString

Introduction

In this page you can find the example usage for com.google.gson JsonPrimitive getAsString.

Prototype

@Override
public String getAsString() 

Source Link

Document

convenience method to get this element as a String.

Usage

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

License:Apache License

public String extractTimeFormatParameter(final JsonObject element) {
    String value = null;//from   w  w  w. j av  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;
}

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

License:Apache License

public String extractMonthDayFormatParameter(final JsonObject element) {
    String value = null;/* w  w w.j  a v  a2  s .  com*/
    if (element.isJsonObject()) {
        final JsonObject object = element.getAsJsonObject();

        final String monthDayFormatParameter = "monthDayFormat";
        if (object.has(monthDayFormatParameter) && object.get(monthDayFormatParameter).isJsonPrimitive()) {
            final JsonPrimitive primitive = object.get(monthDayFormatParameter).getAsJsonPrimitive();
            value = primitive.getAsString();
        }
    }
    return value;
}

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

License:Apache License

public Locale extractLocaleParameter(final JsonObject element) {
    Locale clientApplicationLocale = null;
    if (element.isJsonObject()) {
        final JsonObject object = element.getAsJsonObject();

        String locale = null;//from   w  w w.j  a v  a  2s . c o m
        final String localeParameter = "locale";
        if (object.has(localeParameter) && object.get(localeParameter).isJsonPrimitive()) {
            final JsonPrimitive primitive = object.get(localeParameter).getAsJsonPrimitive();
            locale = primitive.getAsString();
            clientApplicationLocale = localeFromString(locale);
        }
    }
    return clientApplicationLocale;
}

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

License:Apache License

public MonthDay extractMonthDayNamed(final String parameterName, final JsonObject element,
        final String dateFormat, final Locale clientApplicationLocale) {
    MonthDay value = null;/*from w ww  . ja v  a 2s .  co m*/
    if (element.isJsonObject()) {
        final JsonObject object = element.getAsJsonObject();

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

            final JsonPrimitive primitive = object.get(parameterName).getAsJsonPrimitive();
            final String valueAsString = primitive.getAsString();
            if (StringUtils.isNotBlank(valueAsString)) {
                try {
                    final DateTimeFormatter formatter = DateTimeFormat.forPattern(dateFormat)
                            .withLocale(clientApplicationLocale);
                    value = MonthDay.parse(valueAsString.toLowerCase(clientApplicationLocale), formatter);
                } catch (final IllegalArgumentException e) {
                    final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
                    final ApiParameterError error = ApiParameterError.parameterError(
                            "validation.msg.invalid.month.day",
                            "The parameter " + parameterName + " is invalid based on the monthDayFormat: '"
                                    + dateFormat + "' and locale: '" + clientApplicationLocale + "' provided:",
                            parameterName, valueAsString, dateFormat);
                    dataValidationErrors.add(error);

                    throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
                            "Validation errors exist.", dataValidationErrors);
                }
            }
        }

    }
    return value;
}

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

License:Apache License

public LocalDateTime extractLocalTimeNamed(final String parameterName, final JsonObject element,
        final String timeFormat, final Locale clientApplicationLocale,
        final Set<String> parametersPassedInCommand) {
    LocalDateTime value = null;/*from   w  ww .  j  a v  a2 s.com*/
    String timeValueAsString = null;
    if (element.isJsonObject()) {
        final JsonObject object = element.getAsJsonObject();
        if (object.has(parameterName) && object.get(parameterName).isJsonPrimitive()) {
            parametersPassedInCommand.add(parameterName);

            try {
                DateTimeFormatter timeFormtter = DateTimeFormat.forPattern(timeFormat);
                final JsonPrimitive primitive = object.get(parameterName).getAsJsonPrimitive();
                timeValueAsString = primitive.getAsString();
                if (StringUtils.isNotBlank(timeValueAsString)) {
                    value = LocalDateTime.parse(timeValueAsString, timeFormtter);
                }
            } catch (IllegalArgumentException e) {
                final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
                final String defaultMessage = new StringBuilder(
                        "The parameter '" + timeValueAsString + "' is not in correct format.").toString();
                final ApiParameterError error = ApiParameterError
                        .parameterError("validation.msg.invalid.TimeFormat", defaultMessage, parameterName);
                dataValidationErrors.add(error);
                throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
                        "Validation errors exist.", dataValidationErrors);
            }

        }
    }
    return value;
}

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

License:Apache License

public LocalDate extractLocalDateNamed(final String parameterName, final JsonObject element,
        final String dateFormat, final Locale clientApplicationLocale,
        final Set<String> parametersPassedInCommand) {
    LocalDate value = null;// w  ww.  j  a v  a  2s.  co  m
    if (element.isJsonObject()) {
        final JsonObject object = element.getAsJsonObject();

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

            parametersPassedInCommand.add(parameterName);

            final JsonPrimitive primitive = object.get(parameterName).getAsJsonPrimitive();
            final String valueAsString = primitive.getAsString();
            if (StringUtils.isNotBlank(valueAsString)) {
                value = convertFrom(valueAsString, parameterName, dateFormat, clientApplicationLocale);
            }
        }

    }
    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 w  w  .j  av  a  2 s  . c  om*/
    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.infrastructure.core.serialization.JsonParserHelper.java

License:Apache License

private Locale extractLocaleValue(final JsonObject object) {
    Locale clientApplicationLocale = null;
    String locale = null;//  ww  w . j a va  2  s. co  m
    if (object.has("locale") && object.get("locale").isJsonPrimitive()) {
        final JsonPrimitive primitive = object.get("locale").getAsJsonPrimitive();
        locale = primitive.getAsString();
        clientApplicationLocale = localeFromString(locale);
    }
    return clientApplicationLocale;
}

From source file:com.gst.portfolio.loanaccount.domain.Loan.java

License:Apache License

private Map<String, String> getDateFormatAndLocale(final JsonCommand jsonCommand) {
    Map<String, String> returnObject = new HashMap<>();
    JsonElement jsonElement = jsonCommand.parsedJson();
    if (jsonElement.isJsonObject()) {
        JsonObject topLevel = jsonElement.getAsJsonObject();
        if (topLevel.has(LoanApiConstants.dateFormatParameterName)
                && topLevel.get(LoanApiConstants.dateFormatParameterName).isJsonPrimitive()) {
            final JsonPrimitive primitive = topLevel.get(LoanApiConstants.dateFormatParameterName)
                    .getAsJsonPrimitive();
            returnObject.put(LoanApiConstants.dateFormatParameterName, primitive.getAsString());
        }//from w w w. j av a  2  s.  c  o  m
        if (topLevel.has(LoanApiConstants.localeParameterName)
                && topLevel.get(LoanApiConstants.localeParameterName).isJsonPrimitive()) {
            final JsonPrimitive primitive = topLevel.get(LoanApiConstants.localeParameterName)
                    .getAsJsonPrimitive();
            String localeString = primitive.getAsString();
            returnObject.put(LoanApiConstants.localeParameterName, localeString);
        }
    }
    return returnObject;
}

From source file:com.gst.portfolio.loanaccount.domain.Loan.java

License:Apache License

private Map<String, Object> parseDisbursementDetails(final JsonObject jsonObject, String dateFormat,
        Locale locale) {/*from  w w  w .  j  a v  a2 s .  c  o m*/
    Map<String, Object> returnObject = new HashMap<>();
    if (jsonObject.get(LoanApiConstants.disbursementDateParameterName) != null
            && jsonObject.get(LoanApiConstants.disbursementDateParameterName).isJsonPrimitive()) {
        final JsonPrimitive primitive = jsonObject.get(LoanApiConstants.disbursementDateParameterName)
                .getAsJsonPrimitive();
        final String valueAsString = primitive.getAsString();
        if (StringUtils.isNotBlank(valueAsString)) {
            LocalDate date = JsonParserHelper.convertFrom(valueAsString,
                    LoanApiConstants.disbursementDateParameterName, dateFormat, locale);
            if (date != null) {
                returnObject.put(LoanApiConstants.disbursementDateParameterName, date.toDate());
            }
        }
    }

    if (jsonObject.get(LoanApiConstants.disbursementPrincipalParameterName).isJsonPrimitive() && StringUtils
            .isNotBlank((jsonObject.get(LoanApiConstants.disbursementPrincipalParameterName).getAsString()))) {
        BigDecimal principal = jsonObject
                .getAsJsonPrimitive(LoanApiConstants.disbursementPrincipalParameterName).getAsBigDecimal();
        returnObject.put(LoanApiConstants.disbursementPrincipalParameterName, principal);
    }

    if (jsonObject.has(LoanApiConstants.disbursementIdParameterName)
            && jsonObject.get(LoanApiConstants.disbursementIdParameterName).isJsonPrimitive() && StringUtils
                    .isNotBlank((jsonObject.get(LoanApiConstants.disbursementIdParameterName).getAsString()))) {
        Long id = jsonObject.getAsJsonPrimitive(LoanApiConstants.disbursementIdParameterName).getAsLong();
        returnObject.put(LoanApiConstants.disbursementIdParameterName, id);
    }

    if (jsonObject.has(LoanApiConstants.loanChargeIdParameterName)
            && jsonObject.get(LoanApiConstants.loanChargeIdParameterName).isJsonPrimitive() && StringUtils
                    .isNotBlank((jsonObject.get(LoanApiConstants.loanChargeIdParameterName).getAsString()))) {
        returnObject.put(LoanApiConstants.loanChargeIdParameterName,
                jsonObject.getAsJsonPrimitive(LoanApiConstants.loanChargeIdParameterName).getAsString());
    }
    return returnObject;
}