Example usage for com.google.gson JsonObject has

List of usage examples for com.google.gson JsonObject has

Introduction

In this page you can find the example usage for com.google.gson JsonObject has.

Prototype

public boolean has(String memberName) 

Source Link

Document

Convenience method to check if a member with the specified name is present in this object.

Usage

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

License:Apache License

public String extractMonthDayFormatParameter(final JsonObject element) {
    String value = null;/*from w ww. jav a  2 s.c  om*/
    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;/*www  . j  a  va  2 s.  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 String[] extractArrayNamed(final String parameterName, final JsonElement element,
        final Set<String> parametersPassedInRequest) {
    String[] arrayValue = null;/* ww  w  .  java  2 s  .co  m*/
    if (element.isJsonObject()) {
        final JsonObject object = element.getAsJsonObject();
        if (object.has(parameterName)) {
            parametersPassedInRequest.add(parameterName);
            final JsonArray array = object.get(parameterName).getAsJsonArray();
            arrayValue = new String[array.size()];
            for (int i = 0; i < array.size(); i++) {
                arrayValue[i] = array.get(i).getAsString();
            }
        }
    }
    return arrayValue;
}

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

License:Apache License

public JsonArray extractJsonArrayNamed(final String parameterName, final JsonElement element) {
    JsonArray jsonArray = null;/*from  w  w w. j a  v a2 s . c om*/

    if (element.isJsonObject()) {
        final JsonObject object = element.getAsJsonObject();
        if (object.has(parameterName)) {
            jsonArray = object.get(parameterName).getAsJsonArray();
        }
    }

    return jsonArray;
}

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

License:Apache License

public JsonObject extractJsonObjectNamed(final String parameterName, final JsonElement element) {
    JsonObject jsonObject = null;//  ww  w  .  ja va 2  s. co  m

    if (element.isJsonObject()) {
        final JsonObject object = element.getAsJsonObject();
        if (object.has(parameterName)) {
            jsonObject = object.get(parameterName).getAsJsonObject();
        }
    }

    return jsonObject;
}

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

License:Apache License

/**
 * Used with the local date is in array format
 *//*from  w ww .  j  a  va  2 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 JsonObject element,
        final String dateFormat, final Locale clientApplicationLocale) {
    MonthDay value = null;//from w  ww .j a  v  a 2  s .c  o 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  w  w. ja va2s  .co  m
    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  w w  .j  a v a2  s  . 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;// www.j a va  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)) {
                value = Double.valueOf(stringValue);
            }
        }
    }
    return value;
}