Example usage for com.google.gson JsonPrimitive JsonPrimitive

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

Introduction

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

Prototype

public JsonPrimitive(Character c) 

Source Link

Document

Create a primitive containing a character.

Usage

From source file:com.google.iosched.model.DataExtractor.java

License:Open Source License

private JsonPrimitive getVideoFromTopicInfo(JsonObject origin, String sourceInfoKey, String defaultVideoUrl) {
    JsonPrimitive result = null;/*  w w w.  jav  a  2  s . c om*/

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

From source file:com.google.iosched.model.DataExtractor.java

License:Open Source License

private JsonPrimitive setVideoForVideoSession(JsonObject origin, JsonObject dest) {
    JsonPrimitive vid = getVideoFromTopicInfo(origin, InputJsonKeys.VendorAPISource.Topics.INFO_VIDEO_URL,
            null);/*from w  w  w  .ja  va  2 s.  c  om*/
    if (vid != null) {
        set(vid, dest, OutputJsonKeys.VideoLibrary.vid);
        JsonPrimitive thumbnail = new JsonPrimitive(
                "http://img.youtube.com/vi/" + vid.getAsString() + "/hqdefault.jpg");
        set(thumbnail, dest, OutputJsonKeys.VideoLibrary.thumbnailUrl);
    }
    return vid;
}

From source file:com.google.iosched.model.DataExtractor.java

License:Open Source License

private void setRelatedVideos(JsonObject origin, JsonObject dest) {
    JsonArray related = getAsArray(origin, VendorAPISource.Topics.related);
    if (related == null) {
        return;/*from  w ww.  j a va2  s.  co m*/
    }
    for (JsonElement el : related) {
        if (!el.isJsonObject()) {
            continue;
        }
        JsonObject obj = el.getAsJsonObject();
        if (!obj.has("name") || !obj.has("values")) {
            continue;
        }

        if (InputJsonKeys.VendorAPISource.Topics.RELATED_NAME_VIDEO
                .equals(obj.getAsJsonPrimitive("name").getAsString())) {

            JsonElement values = obj.get("values");
            if (!values.isJsonArray()) {
                continue;
            }

            // As per the data specification, related content is formatted as
            // "video1 title1\nvideo2 title2\n..."
            StringBuilder relatedContentStr = new StringBuilder();
            for (JsonElement value : values.getAsJsonArray()) {
                String relatedSessionId = value.getAsString();
                JsonObject relatedVideo = videoSessionsById.get(relatedSessionId);
                if (relatedVideo != null) {
                    JsonElement vid = get(relatedVideo, OutputJsonKeys.VideoLibrary.vid);
                    JsonElement title = get(relatedVideo, OutputJsonKeys.VideoLibrary.title);
                    if (vid != null && title != null) {
                        relatedContentStr.append(vid.getAsString()).append(" ").append(title.getAsString())
                                .append("\n");
                    }
                }
            }
            set(new JsonPrimitive(relatedContentStr.toString()), dest, OutputJsonKeys.Sessions.relatedContent);
        }
    }
}

From source file:com.google.iosched.model.DataModelHelper.java

License:Open Source License

public static JsonPrimitive getMapValue(JsonElement map, String key, Converter converter,
        String defaultValueStr) {
    JsonPrimitive defaultValue = null;/*from  w  ww .  java 2s  .c  o m*/
    if (defaultValueStr != null) {
        defaultValue = new JsonPrimitive(defaultValueStr);
        if (converter != null)
            defaultValue = converter.convert(defaultValue);
    }
    if (map == null || !map.isJsonArray()) {
        return defaultValue;
    }
    for (JsonElement el : map.getAsJsonArray()) {
        if (!el.isJsonObject()) {
            continue;
        }
        JsonObject obj = el.getAsJsonObject();
        if (!obj.has("name") || !obj.has("value")) {
            continue;
        }
        if (key.equals(obj.getAsJsonPrimitive("name").getAsString())) {
            JsonElement value = obj.get("value");
            if (!value.isJsonPrimitive()) {
                throw new ConverterException(value, converter, "Expected a JsonPrimitive");
            }
            if (converter != null)
                value = converter.convert(value);
            return value.getAsJsonPrimitive();
        }
    }
    return defaultValue;
}

From source file:com.google.iosched.model.validator.BooleanConverter.java

License:Open Source License

@Override
public JsonPrimitive convert(JsonPrimitive value) {
    if (value == null) {
        return new JsonPrimitive(false);
    }/*from  w ww.j  ava2 s . c om*/
    if (value.isBoolean()) {
        return value;
    }
    String str = value.getAsString();
    if (str.equalsIgnoreCase("false")) {
        return new JsonPrimitive(false);
    }
    if (str.equalsIgnoreCase("true")) {
        return new JsonPrimitive(true);
    }
    throw new ConverterException(value, this);
}

From source file:com.google.iosched.model.validator.DateTimeConverter.java

License:Open Source License

@SuppressWarnings("deprecation")
@Override//from www  .  j a  v a2s. c o  m
public JsonPrimitive convert(JsonPrimitive value) {
    if (value == null) {
        return null;
    }
    Date date = null;
    Exception lastEx = null;
    for (int i = 0; i < inputFormats.length && date == null; i++) {
        try {
            date = inputFormats[i].parse(value.getAsString());
        } catch (NumberFormatException e) {
            lastEx = e;
        } catch (ParseException e) {
            lastEx = e;
        }
    }

    if (date == null) {
        throw new ConverterException(value, this, lastEx.getMessage());
    }

    if (date.getYear() < 100) {
        // hack to fix invalid dates on temporary data
        date.setYear(114);
        date.setMonth(Calendar.JUNE);
        date.setDate(25);
    }
    if (Config.TIME_TRAVEL_SHIFT != 0) {
        date = new Date(date.getTime() + Config.TIME_TRAVEL_SHIFT);
    }
    return new JsonPrimitive(outputFormat.format(date));
}

From source file:com.google.iosched.model.validator.GPlusURLConverter.java

License:Open Source License

@Override
public JsonPrimitive convert(JsonPrimitive value) {
    if (value == null) {
        return null;
    }//  ww  w .jav  a2  s.  com
    String str = value.getAsString();
    if (str.isEmpty()) {
        return value;
    }
    for (Pattern p : plusRecognizedPatterns) {
        Matcher m = p.matcher(str);
        if (m.find()) {
            return new JsonPrimitive(plusFormat.format(new String[] { m.group(1) }));
        }
    }

    // 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.google.iosched.model.validator.IntegerConverter.java

License:Open Source License

@Override
public JsonPrimitive convert(JsonPrimitive value) {
    if (value == null) {
        return new JsonPrimitive(0);
    }//from   w w w  . ja  va 2  s . co m
    if (value.isNumber()) {
        return value;
    }
    String str = value.getAsString();
    try {
        return new JsonPrimitive(Integer.parseInt(str));
    } catch (NumberFormatException ex) {
        throw new ConverterException(value, this);
    }
}

From source file:com.google.iosched.model.validator.IntegerToStringConverter.java

License:Open Source License

@Override
public JsonPrimitive convert(JsonPrimitive value) {
    if (value == null) {
        return null;
    }/*  w ww.j  a v a2 s  .  co m*/
    if (value.isNumber()) {
        return new JsonPrimitive(String.valueOf(value.getAsInt()));
    }
    try {
        Integer.parseInt(value.getAsString());
        // if it parses correctly, it is validated:
        return value;
    } catch (NumberFormatException ex) {
        throw new ConverterException(value, this);
    }
}

From source file:com.google.iosched.model.validator.PhotoURLConverter.java

License:Open Source License

@Override
public JsonPrimitive convert(JsonPrimitive value) {
    if (value == null) {
        return null;
    }/*from w w  w.ja v  a2  s .  c o  m*/
    String entityId = value.getAsString();
    return new JsonPrimitive(entityBaseUrl + entityId + ".jpg");
}