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

License:Open Source License

public JsonArray extractTags(JsonDataSources sources) {
    JsonArray result = new JsonArray();
    JsonDataSource source = sources.getSource(InputJsonKeys.VendorAPISource.MainTypes.categories.name());
    JsonDataSource tagCategoryMappingSource = sources
            .getSource(InputJsonKeys.ExtraSource.MainTypes.tag_category_mapping.name());
    JsonDataSource tagsConfSource = sources.getSource(InputJsonKeys.ExtraSource.MainTypes.tag_conf.name());

    categoryToTagMap = new HashMap<String, JsonObject>();

    // Only for checking duplicates.
    HashSet<String> originalTagNames = new HashSet<String>();

    if (source != null) {
        for (JsonObject origin : source) {
            JsonObject dest = new JsonObject();

            // set tag category, looking for parentid in the tag_category_mapping data source
            JsonElement parentId = get(origin, InputJsonKeys.VendorAPISource.Categories.parentid);

            // Ignore categories with null parents, because they are roots (tag categories).
            if (parentId != null && !parentId.getAsString().equals("")) {
                JsonElement category = null;
                if (tagCategoryMappingSource != null) {
                    JsonObject categoryMapping = tagCategoryMappingSource
                            .getElementById(parentId.getAsString());
                    if (categoryMapping != null) {
                        category = get(categoryMapping, InputJsonKeys.ExtraSource.CategoryTagMapping.tag_name);
                        JsonPrimitive isDefault = (JsonPrimitive) get(categoryMapping,
                                InputJsonKeys.ExtraSource.CategoryTagMapping.is_default);
                        if (isDefault != null && isDefault.getAsBoolean()) {
                            mainCategory = category;
                        }//from  ww  w . j  a  v a 2s .  c  o  m
                    }
                    set(category, dest, OutputJsonKeys.Tags.category);
                }

                // Ignore categories unrecognized parents (no category)
                if (category == null) {
                    continue;
                }

                // Tag name is by convention: "TAGCATEGORY_TAGNAME"
                JsonElement name = get(origin, InputJsonKeys.VendorAPISource.Categories.name);
                JsonElement tagName = new JsonPrimitive(
                        category.getAsString() + "_" + Converters.TAG_NAME.convert(name).getAsString());
                JsonElement originalTagName = tagName;

                if (obfuscate) {
                    name = Converters.OBFUSCATE.convert(name);
                    tagName = new JsonPrimitive(
                            category.getAsString() + "_" + Converters.TAG_NAME.convert(name).getAsString());
                }

                set(tagName, dest, OutputJsonKeys.Tags.tag);
                set(name, dest, OutputJsonKeys.Tags.name);
                set(origin, InputJsonKeys.VendorAPISource.Categories.id, dest, OutputJsonKeys.Tags.original_id);
                set(origin, InputJsonKeys.VendorAPISource.Categories.description, dest,
                        OutputJsonKeys.Tags._abstract, obfuscate ? Converters.OBFUSCATE : null);

                if (tagsConfSource != null) {
                    JsonObject tagConf = tagsConfSource.getElementById(originalTagName.getAsString());
                    if (tagConf != null) {
                        set(tagConf, InputJsonKeys.ExtraSource.TagConf.order_in_category, dest,
                                OutputJsonKeys.Tags.order_in_category);
                        set(tagConf, InputJsonKeys.ExtraSource.TagConf.color, dest, OutputJsonKeys.Tags.color);
                        set(tagConf, InputJsonKeys.ExtraSource.TagConf.hashtag, dest,
                                OutputJsonKeys.Tags.hashtag);
                    }
                }

                categoryToTagMap.put(get(origin, InputJsonKeys.VendorAPISource.Categories.id).getAsString(),
                        dest);
                if (originalTagNames.add(originalTagName.getAsString())) {
                    result.add(dest);
                }
            }
        }
    }
    if (Config.DEBUG_FIX_DATA) {
        DebugDataExtractorHelper.changeCategories(categoryToTagMap, result);
    }
    return result;
}

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

License:Open Source License

public JsonArray extractSessions(JsonDataSources sources) {
    if (videoSessionsById == null) {
        throw new IllegalStateException(
                "You need to extract video sessions before attempting to extract sessions");
    }//from   w  w  w  . ja  v  a 2 s.  com
    if (categoryToTagMap == null) {
        throw new IllegalStateException("You need to extract tags before attempting to extract sessions");
    }

    JsonArray result = new JsonArray();
    JsonDataSource source = sources.getSource(InputJsonKeys.VendorAPISource.MainTypes.topics.name());
    if (source != null) {
        for (JsonObject origin : source) {
            if (isVideoSession(origin)) {
                // Sessions with the Video tag are processed as video library content
                continue;
            }
            if (isHiddenSession(origin)) {
                // Sessions with a "Hidden from schedule" flag should be ignored
                continue;
            }
            JsonElement title = get(origin, InputJsonKeys.VendorAPISource.Topics.title);
            // Since the CMS returns an empty keynote as a session, we need to ignore it
            if (title != null && title.isJsonPrimitive() && "keynote".equalsIgnoreCase(title.getAsString())) {
                continue;
            }
            JsonObject dest = new JsonObject();

            // Some sessions require a special ID, so we replace it here...
            if (title != null && title.isJsonPrimitive()
                    && "after hours".equalsIgnoreCase(title.getAsString())) {
                set(new JsonPrimitive("__afterhours__"), dest, OutputJsonKeys.Sessions.id);
            } else {
                set(origin, InputJsonKeys.VendorAPISource.Topics.id, dest, OutputJsonKeys.Sessions.id);
            }
            set(origin, InputJsonKeys.VendorAPISource.Topics.id, dest, OutputJsonKeys.Sessions.url,
                    Converters.SESSION_URL);
            set(origin, InputJsonKeys.VendorAPISource.Topics.title, dest, OutputJsonKeys.Sessions.title,
                    obfuscate ? Converters.OBFUSCATE : null);
            set(origin, InputJsonKeys.VendorAPISource.Topics.description, dest,
                    OutputJsonKeys.Sessions.description, obfuscate ? Converters.OBFUSCATE : null);
            set(origin, InputJsonKeys.VendorAPISource.Topics.start, dest,
                    OutputJsonKeys.Sessions.startTimestamp, Converters.DATETIME);
            set(origin, InputJsonKeys.VendorAPISource.Topics.finish, dest, OutputJsonKeys.Sessions.endTimestamp,
                    Converters.DATETIME);
            set(new JsonPrimitive(isFeatured(origin)), dest, OutputJsonKeys.Sessions.isFeatured);

            JsonElement documents = get(origin, InputJsonKeys.VendorAPISource.Topics.documents);
            if (documents != null && documents.isJsonArray() && documents.getAsJsonArray().size() > 0) {
                // Note that the input for SessionPhotoURL is the entity ID. We simply ignore the original
                // photo URL, because that will be processed by an offline cron script, resizing the
                // photos and saving them to a known location with the entity ID as its base name.
                set(origin, InputJsonKeys.VendorAPISource.Topics.id, dest, OutputJsonKeys.Sessions.photoUrl,
                        Converters.SESSION_PHOTO_URL);
            }

            setVideoPropertiesInSession(origin, dest);
            setRelatedContent(origin, dest);

            JsonElement mainTag = null;
            JsonElement hashtag = null;
            JsonElement mainTagColor = null;
            JsonArray categories = origin
                    .getAsJsonArray(InputJsonKeys.VendorAPISource.Topics.categoryids.name());
            JsonArray tags = new JsonArray();
            for (JsonElement category : categories) {
                JsonObject tag = categoryToTagMap.get(category.getAsString());
                if (tag != null) {
                    JsonElement tagName = get(tag, OutputJsonKeys.Tags.tag);
                    tags.add(tagName);
                    usedTags.add(tagName.getAsString());

                    if (mainTag == null) {
                        // check if the tag is from a "default" category. For example, if THEME is the default
                        // category, all sessions will have a "mainTag" property set to the first tag of type THEME
                        JsonElement tagCategory = get(tag, OutputJsonKeys.Tags.category); // THEME, TYPE or TOPIC
                        if (tagCategory.equals(mainCategory)) {
                            mainTag = tagName;
                            mainTagColor = get(tag, OutputJsonKeys.Tags.color);
                        }
                        if (hashtag == null && isHashtag(tag)) {
                            hashtag = get(tag, OutputJsonKeys.Tags.hashtag);
                            if (hashtag == null || hashtag.getAsString() == null
                                    || hashtag.getAsString().isEmpty()) {
                                // If no hashtag set in the tagsconf file, we will convert the tagname to find one:
                                hashtag = new JsonPrimitive(
                                        get(tag, OutputJsonKeys.Tags.name, Converters.TAG_NAME).getAsString()
                                                .toLowerCase());
                            }
                        }
                    }
                }
            }
            set(tags, dest, OutputJsonKeys.Sessions.tags);
            if (mainTag != null) {
                set(mainTag, dest, OutputJsonKeys.Sessions.mainTag);
            }
            if (mainTagColor != null) {
                set(mainTagColor, dest, OutputJsonKeys.Sessions.color);
            }
            if (hashtag != null) {
                set(hashtag, dest, OutputJsonKeys.Sessions.hashtag);
            }

            JsonArray speakers = getAsArray(origin, InputJsonKeys.VendorAPISource.Topics.speakerids);
            if (speakers != null)
                for (JsonElement speaker : speakers) {
                    String speakerId = speaker.getAsString();
                    usedSpeakers.add(speakerId);
                }
            set(speakers, dest, OutputJsonKeys.Sessions.speakers);

            JsonArray sessions = origin.getAsJsonArray(InputJsonKeys.VendorAPISource.Topics.sessions.name());
            if (sessions != null && sessions.size() > 0) {
                String roomId = get(sessions.get(0).getAsJsonObject(),
                        InputJsonKeys.VendorAPISource.Sessions.roomid).getAsString();
                roomId = Config.ROOM_MAPPING.getRoomId(roomId);
                set(new JsonPrimitive(roomId), dest, OutputJsonKeys.Sessions.room);

                // captions URL is set based on the session room, so keep it here.
                String captionsURL = Config.ROOM_MAPPING.getCaptions(roomId);
                if (captionsURL != null) {
                    set(new JsonPrimitive(captionsURL), dest, OutputJsonKeys.Sessions.captionsUrl);
                }
            }

            if (Config.DEBUG_FIX_DATA) {
                DebugDataExtractorHelper.changeSession(dest, usedTags);
            }
            result.add(dest);
        }
    }
    return result;
}

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

License:Open Source License

public JsonArray extractVideoSessions(JsonDataSources sources) {
    videoSessionsById = new HashMap<String, JsonObject>();
    if (categoryToTagMap == null) {
        throw new IllegalStateException("You need to extract tags before attempting to extract video sessions");
    }/*w  w  w .  jav a 2  s . c om*/
    if (speakersById == null) {
        throw new IllegalStateException(
                "You need to extract speakers before attempting to extract video sessions");
    }

    JsonArray result = new JsonArray();
    JsonDataSource source = sources.getSource(InputJsonKeys.VendorAPISource.MainTypes.topics.name());
    if (source != null) {
        for (JsonObject origin : source) {

            if (!isVideoSession(origin)) {
                continue;
            }
            if (isHiddenSession(origin)) {
                // Sessions with a "Hidden from schedule" flag should be ignored
                continue;
            }

            JsonObject dest = new JsonObject();

            JsonPrimitive vid = setVideoForVideoSession(origin, dest);

            JsonElement id = get(origin, InputJsonKeys.VendorAPISource.Topics.id);
            // video library id must be the Youtube video id
            set(vid, dest, OutputJsonKeys.VideoLibrary.id);
            set(origin, InputJsonKeys.VendorAPISource.Topics.title, dest, OutputJsonKeys.VideoLibrary.title,
                    obfuscate ? Converters.OBFUSCATE : null);
            set(origin, InputJsonKeys.VendorAPISource.Topics.description, dest,
                    OutputJsonKeys.VideoLibrary.desc, obfuscate ? Converters.OBFUSCATE : null);
            set(new JsonPrimitive(Config.CONFERENCE_YEAR), dest, OutputJsonKeys.VideoLibrary.year);

            JsonElement videoTopic = null;
            JsonArray categories = origin
                    .getAsJsonArray(InputJsonKeys.VendorAPISource.Topics.categoryids.name());
            for (JsonElement category : categories) {
                JsonObject tag = categoryToTagMap.get(category.getAsString());
                if (tag != null) {
                    if (isHashtag(tag)) {
                        videoTopic = get(tag, OutputJsonKeys.Tags.name);
                        // by definition, the first tag that can be a hashtag (usually a TOPIC) is considered the video tag
                        break;
                    }
                }
            }
            if (videoTopic != null) {
                set(videoTopic, dest, OutputJsonKeys.VideoLibrary.topic);
            }

            // Concatenate speakers:
            JsonArray speakers = getAsArray(origin, InputJsonKeys.VendorAPISource.Topics.speakerids);
            StringBuilder sb = new StringBuilder();
            if (speakers != null)
                for (int i = 0; i < speakers.size(); i++) {
                    String speakerId = speakers.get(i).getAsString();
                    usedSpeakers.add(speakerId);
                    JsonObject speaker = speakersById.get(speakerId);
                    if (speaker != null) {
                        sb.append(get(speaker, OutputJsonKeys.Speakers.name).getAsString());
                        if (i < speakers.size() - 1)
                            sb.append(", ");
                    }
                }
            set(new JsonPrimitive(sb.toString()), dest, OutputJsonKeys.VideoLibrary.speakers);
            videoSessionsById.put(id.getAsString(), dest);
            result.add(dest);
        }
    }
    return result;
}

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;// www . j  ava2s . com

    if (isLivestream) {
        vid = getVideoFromTopicInfo(origin, InputJsonKeys.VendorAPISource.Topics.INFO_STREAM_VIDEO_ID,
                Config.VIDEO_LIVESTREAMURL_FOR_EMPTY);
    } else {
        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  ava 2s .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.DataExtractor.java

License:Open Source License

@Deprecated
private void setRelatedVideos(JsonObject origin, JsonObject dest) {
    JsonArray related = getAsArray(origin, InputJsonKeys.VendorAPISource.Topics.related);
    if (related == null) {
        return;/*from   w w  w.  ja v  a2s. c o 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.samples.apps.iosched.server.schedule.model.DataExtractor.java

License:Open Source License

private void setRelatedContent(JsonObject origin, JsonObject dest) {
    JsonArray related = getAsArray(origin, InputJsonKeys.VendorAPISource.Topics.related);
    JsonArray outputArray = new JsonArray();

    if (related == null) {
        return;/*  w w w . j a v  a 2s .  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_SESSIONS
                .equals(obj.getAsJsonPrimitive("name").getAsString())) {

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

            // As per the data specification, related content is formatted as
            // "video1 title1\nvideo2 title2\n..."
            for (JsonElement topic : values.getAsJsonArray()) {
                if (!topic.isJsonObject()) {
                    continue;
                }

                JsonObject topicObj = topic.getAsJsonObject();

                String id = get(topicObj, InputJsonKeys.VendorAPISource.RelatedTopics.id).getAsString();
                String title = get(topicObj, InputJsonKeys.VendorAPISource.RelatedTopics.title).getAsString();

                if (id != null && title != null) {
                    JsonObject outputObj = new JsonObject();
                    set(new JsonPrimitive(id), outputObj, OutputJsonKeys.RelatedContent.id);
                    set(new JsonPrimitive(title), outputObj, OutputJsonKeys.RelatedContent.title);
                    outputArray.add(outputObj);
                }
            }
            set(outputArray, dest, OutputJsonKeys.Sessions.relatedContent);
        }
    }
}

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

License:Open Source License

public static final void changeRooms(JsonArray allRooms) {
    for (String room : rooms) {
        JsonObject dest = new JsonObject();
        JsonPrimitive roomId = new JsonPrimitive(room);
        JsonPrimitive roomName = new JsonPrimitive("Room " + room);
        DataModelHelper.set(roomId, dest, OutputJsonKeys.Rooms.id);
        DataModelHelper.set(roomName, dest, OutputJsonKeys.Rooms.name);
        allRooms.add(dest);//from   w  w w. j  av a  2 s  .co m
    }
}

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

License:Open Source License

public static final void changeSession(JsonObject session, Set<String> usedTags) {
    int hash = session.get(OutputJsonKeys.Sessions.id.name()).getAsString().hashCode();
    r.setSeed(hash);//from w w w.j a v a2  s. co  m

    // timeslot:
    int day = days[uniform(2)];
    int[] timeSlot = sessionTimes[uniform(sessionTimes.length)];
    Calendar start = new GregorianCalendar(2014, Calendar.JUNE, day, timeSlot[0], timeSlot[1], 0);
    Calendar end = new GregorianCalendar(2014, Calendar.JUNE, day, timeSlot[2], timeSlot[3], 0);
    long offset = TimeZone.getTimeZone("PST").getOffset(start.getTimeInMillis());
    start.setTimeInMillis(start.getTimeInMillis() - offset);
    end.setTimeInMillis(end.getTimeInMillis() - offset);
    String startS = formatter.format(start.getTime());
    String endS = formatter.format(end.getTime());

    DataModelHelper.set(new JsonPrimitive(startS), session, OutputJsonKeys.Sessions.startTimestamp);
    DataModelHelper.set(new JsonPrimitive(endS), session, OutputJsonKeys.Sessions.endTimestamp);

    // Room:
    DataModelHelper.set(new JsonPrimitive(rooms[uniform(rooms.length)]), session, OutputJsonKeys.Sessions.room);

    JsonArray tags = new JsonArray();

    // 2 random topic tags
    Collections.shuffle(topicTags, r); // not the most efficient, but good enough and avoid duplicates
    if (topicTags.size() > 0)
        tags.add(topicTags.get(0).get(OutputJsonKeys.Tags.tag.name()));
    if (topicTags.size() > 1)
        tags.add(topicTags.get(1).get(OutputJsonKeys.Tags.tag.name()));

    // 1 randomly distributed theme tag
    tags.add(themeTags[roullette(themeDistribution)].get(OutputJsonKeys.Tags.tag.name()));

    // 1 randomly distributed type tag
    tags.add(typeTags[roullette(typeDistribution)].get(OutputJsonKeys.Tags.tag.name()));

    for (JsonElement tag : tags) {
        usedTags.add(tag.getAsString());
    }
    DataModelHelper.set(tags, session, OutputJsonKeys.Sessions.tags);

    // Livestream
    boolean isLiveStream = uniform(2) == 1;
    if (isLiveStream) {
        DataModelHelper.set(new JsonPrimitive("https://www.youtube.com/watch?v=dQw4w9WgXcQ"), session,
                OutputJsonKeys.Sessions.youtubeUrl);
        DataModelHelper.set(new JsonPrimitive("http://www.google.com/humans.txt"), session,
                OutputJsonKeys.Sessions.captionsUrl);
        DataModelHelper.set(new JsonPrimitive(Boolean.TRUE), session, OutputJsonKeys.Sessions.isLivestream);
    } else {
        session.remove(OutputJsonKeys.Sessions.youtubeUrl.name());
        session.remove(OutputJsonKeys.Sessions.captionsUrl.name());
        DataModelHelper.set(new JsonPrimitive(Boolean.FALSE), session, OutputJsonKeys.Sessions.isLivestream);
    }
}

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 ww w  .j a v a  2  s.  c om*/
    // TODO: Replace with URL shortener and move this processing to a taskqueue.
    return new JsonPrimitive(SESSION_BASE_URL + sessionId.getAsString());
}