Search in sources :

Example 16 with JsonElement

use of com.google.gson.JsonElement in project iosched by google.

the class DataExtractor method extractTags.

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;
                        }
                    }
                    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);
                    }
                }
                // the images to be attached to the Tag/Track rather than the session.
                if (tagName.getAsString().startsWith(TRACK)) {
                    // Extract photo urls from topics for TRACK tags.
                    String objectId = extractTrackPhotoObjectId(sources, dest.get(OutputJsonKeys.Tags.original_id.name()).getAsString());
                    if (!objectId.isEmpty()) {
                        dest.addProperty(OutputJsonKeys.Tags.photoUrl.name(), Converters.SESSION_PHOTO_URL.convert(new JsonPrimitive(objectId)).getAsString());
                    }
                    // Add background colors for TRACK tags.
                    String trackColor = getTrackColor(tagName.getAsString().substring(6));
                    if (!trackColor.isEmpty()) {
                        dest.addProperty(OutputJsonKeys.Tags.color.name(), trackColor);
                    }
                }
                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;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) HashSet(java.util.HashSet)

Example 17 with JsonElement

use of com.google.gson.JsonElement in project iosched by google.

the class DataExtractor method extractRooms.

public JsonArray extractRooms(JsonDataSources sources) {
    HashSet<String> ids = new HashSet<String>();
    JsonArray result = new JsonArray();
    JsonDataSource source = sources.getSource(InputJsonKeys.VendorAPISource.MainTypes.rooms.name());
    if (source != null) {
        for (JsonObject origin : source) {
            JsonObject dest = new JsonObject();
            JsonElement originalId = get(origin, InputJsonKeys.VendorAPISource.Rooms.Id);
            String id = Config.ROOM_MAPPING.getRoomId(originalId.getAsString());
            if (!ids.contains(id)) {
                String title = Config.ROOM_MAPPING.getTitle(id, get(origin, InputJsonKeys.VendorAPISource.Rooms.Name).getAsString());
                set(new JsonPrimitive(id), dest, OutputJsonKeys.Rooms.id);
                set(originalId, dest, OutputJsonKeys.Rooms.original_id);
                set(new JsonPrimitive(title), dest, OutputJsonKeys.Rooms.name);
                result.add(dest);
                ids.add(id);
            }
        }
    }
    if (Config.DEBUG_FIX_DATA) {
        DebugDataExtractorHelper.changeRooms(result);
    }
    return result;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) HashSet(java.util.HashSet)

Example 18 with JsonElement

use of com.google.gson.JsonElement in project iosched by google.

the class DebugDataExtractorHelper method changeSession.

public static final void changeSession(JsonObject session, Set<String> usedTags) {
    int hash = session.get(OutputJsonKeys.Sessions.id.name()).getAsString().hashCode();
    r.setSeed(hash);
    // 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
    // not the most efficient, but good enough and avoid duplicates
    Collections.shuffle(topicTags, r);
    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);
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar)

Example 19 with JsonElement

use of com.google.gson.JsonElement in project SimpleNews by liuling07.

the class NewsJsonUtils method readJsonNewsDetailBeans.

public static NewsDetailBean readJsonNewsDetailBeans(String res, String docId) {
    NewsDetailBean newsDetailBean = null;
    try {
        JsonParser parser = new JsonParser();
        JsonObject jsonObj = parser.parse(res).getAsJsonObject();
        JsonElement jsonElement = jsonObj.get(docId);
        if (jsonElement == null) {
            return null;
        }
        newsDetailBean = JsonUtils.deserialize(jsonElement.getAsJsonObject(), NewsDetailBean.class);
    } catch (Exception e) {
        LogUtils.e(TAG, "readJsonNewsBeans error", e);
    }
    return newsDetailBean;
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) NewsDetailBean(com.lauren.simplenews.beans.NewsDetailBean) JsonParser(com.google.gson.JsonParser)

Example 20 with JsonElement

use of com.google.gson.JsonElement in project SimpleNews by liuling07.

the class WeatherJsonUtils method getCity.

/**
     * 从定位的json字串中获取城市
     * @param json
     * @return
     */
public static String getCity(String json) {
    JsonParser parser = new JsonParser();
    JsonObject jsonObj = parser.parse(json).getAsJsonObject();
    JsonElement status = jsonObj.get("status");
    if (status != null && "OK".equals(status.getAsString())) {
        JsonObject result = jsonObj.getAsJsonObject("result");
        if (result != null) {
            JsonObject addressComponent = result.getAsJsonObject("addressComponent");
            if (addressComponent != null) {
                JsonElement cityElement = addressComponent.get("city");
                if (cityElement != null) {
                    return cityElement.getAsString().replace("市", "");
                }
            }
        }
    }
    return null;
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser)

Aggregations

JsonElement (com.google.gson.JsonElement)682 JsonObject (com.google.gson.JsonObject)381 JsonArray (com.google.gson.JsonArray)166 JsonPrimitive (com.google.gson.JsonPrimitive)107 JsonParser (com.google.gson.JsonParser)105 Gson (com.google.gson.Gson)69 Map (java.util.Map)63 ArrayList (java.util.ArrayList)51 HashMap (java.util.HashMap)50 IOException (java.io.IOException)48 Test (org.testng.annotations.Test)45 JsonReader (com.google.gson.stream.JsonReader)31 InputStreamReader (java.io.InputStreamReader)28 GsonBuilder (com.google.gson.GsonBuilder)26 Pager (com.willshex.blogwt.shared.api.Pager)26 Type (java.lang.reflect.Type)25 StringReader (java.io.StringReader)21 Test (org.junit.Test)21 User (com.willshex.blogwt.shared.api.datatype.User)19 JsonParseException (com.google.gson.JsonParseException)17