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.google.samples.apps.iosched.server.schedule.model.DataExtractor.java

License:Open Source License

private void setVideoPropertiesInSession(JsonObject origin, JsonObject dest) {
    boolean isLivestream = isLivestreamed(origin);
    set(new JsonPrimitive(isLivestream), dest, OutputJsonKeys.Sessions.isLivestream);

    JsonPrimitive vid = null;

    if (isLivestream) {
        vid = getVideoFromTopicInfo(origin, InputJsonKeys.VendorAPISource.Topics.INFO_STREAM_VIDEO_ID,
                Config.VIDEO_LIVESTREAMURL_FOR_EMPTY);
    } else {//from  w  ww  .ja va  2  s.co  m
        vid = getMapValue(get(origin, InputJsonKeys.VendorAPISource.Topics.info),
                InputJsonKeys.VendorAPISource.Topics.INFO_VIDEO_URL, Converters.YOUTUBE_URL, null);
    }
    if (vid != null && !vid.getAsString().isEmpty()) {
        set(vid, dest, OutputJsonKeys.Sessions.youtubeUrl);
    }
}

From source file:com.google.samples.apps.iosched.server.schedule.model.DataExtractor.java

License:Open Source License

private JsonPrimitive getVideoFromTopicInfo(JsonObject origin, String sourceInfoKey, String defaultVideoUrl) {
    JsonPrimitive result = null;/*from   ww  w  .  j av  a  2  s .  com*/

    if (!obfuscate) {
        JsonPrimitive vid = getMapValue(get(origin, InputJsonKeys.VendorAPISource.Topics.info), sourceInfoKey,
                null, defaultVideoUrl);
        if (vid != null && !vid.getAsString().isEmpty()) {
            result = vid;
        }
    }
    return (result == null && defaultVideoUrl != null) ? new JsonPrimitive(defaultVideoUrl) : result;
}

From source file:com.google.samples.apps.iosched.server.schedule.model.validator.SessionURLConverter.java

License:Open Source License

@Override
public JsonPrimitive convert(JsonPrimitive sessionId) {
    if (sessionId == null) {
        return null;
    }//from   w  ww  . j  av  a  2 s . co  m
    // TODO: Replace with URL shortener and move this processing to a taskqueue.
    return new JsonPrimitive(SESSION_BASE_URL + sessionId.getAsString());
}

From source file:com.google.samples.apps.iosched.server.schedule.model.validator.TwitterURLConverter.java

License:Open Source License

/**
 * Takes a user inputted twitter profile as a JsonPrimitive and returns a properly
 * formatted/cleaned URL to that twitter profile.
 *//*from  w w w  .  j av  a  2s  .c om*/
@Override
public JsonPrimitive convert(JsonPrimitive value) {
    if (value == null) {
        return null;
    }
    String str = value.getAsString();
    if (str.isEmpty()) {
        return value;
    }

    // If they didn't enter it as a URL, format it as one.
    for (Pattern p : twitterRecognizedPatterns) {
        Matcher m = p.matcher(str);
        if (m.find()) {
            return new JsonPrimitive(twitterFormat.format(new String[] { m.group(0) }));
        }
    }

    // If URL starts with http/https:
    if (acceptableUrlPattern.matcher(str).matches()) {
        return value;
    }

    // Otherwise, just add https://:
    str = "https://" + str;
    return new JsonPrimitive(str);
}

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;/*from www . j  a v  a2 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)) {
                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;/*from   w  ww . j  a  v a  2s . 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 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 w w .  j av a  2  s .  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;//www . ja va2  s  .  co  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 = 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/*from ww w.jav a 2  s .co m*/
 *
 * 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 ww.j  av  a  2s  . 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;
}