Example usage for com.google.gson JsonArray JsonArray

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

Introduction

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

Prototype

public JsonArray() 

Source Link

Document

Creates an empty JsonArray.

Usage

From source file:com.google.gwtjsonrpc.server.MapDeserializer.java

License:Apache License

public JsonElement serialize(final Map<Object, Object> src, final Type typeOfSrc,
        final JsonSerializationContext context) {
    final Type kt = ((ParameterizedType) typeOfSrc).getActualTypeArguments()[0];
    final Type vt = ((ParameterizedType) typeOfSrc).getActualTypeArguments()[1];

    if (src == null) {
        return new JsonNull();
    }//from  ww w .  j  a v  a 2 s  . c  o  m

    if (kt == String.class) {
        final JsonObject r = new JsonObject();
        for (final Map.Entry<Object, Object> e : src.entrySet()) {
            r.add(e.getKey().toString(), context.serialize(e.getValue(), vt));
        }
        return r;
    } else {
        final JsonArray r = new JsonArray();
        for (final Map.Entry<Object, Object> e : src.entrySet()) {
            r.add(context.serialize(e.getKey(), kt));
            r.add(context.serialize(e.getValue(), vt));
        }
        return r;
    }
}

From source file:com.google.identitytoolkit.RpcHelper.java

License:Open Source License

/**
 * Using 2-Leg Oauth (i.e. Service Account).
 *///from w w w  .  ja v  a2  s  . co  m
public JsonObject getAccountInfoById(String localId) throws GitkitClientException, GitkitServerException {
    JsonObject params = new JsonObject();
    JsonArray localIdArray = new JsonArray();
    localIdArray.add(new JsonPrimitive(localId));
    params.add("localId", localIdArray);
    return invokeGoogle2LegOauthApi("getAccountInfo", params);
}

From source file:com.google.identitytoolkit.RpcHelper.java

License:Open Source License

/**
 * Using 2-Leg Oauth (i.e. Service Account).
 *///from ww w.  ja v  a 2  s  .  c om
public JsonObject getAccountInfoByEmail(String email) throws GitkitClientException, GitkitServerException {
    JsonObject params = new JsonObject();
    JsonArray emailArray = new JsonArray();
    emailArray.add(new JsonPrimitive(email));
    params.add("email", emailArray);
    return invokeGoogle2LegOauthApi("getAccountInfo", params);
}

From source file:com.google.identitytoolkit.RpcHelper.java

License:Open Source License

private static JsonArray toJsonArray(List<GitkitUser> accounts) {
    JsonArray infos = new JsonArray();
    for (GitkitUser account : accounts) {
        JsonObject user = new JsonObject();
        user.addProperty("email", account.getEmail());
        user.addProperty("localId", account.getLocalId());
        if (account.getHash() != null) {
            user.addProperty("passwordHash", BaseEncoding.base64Url().encode(account.getHash()));
        }// www.j av a2s.c  o m
        if (account.getSalt() != null) {
            user.addProperty("salt", BaseEncoding.base64Url().encode(account.getSalt()));
        }
        if (account.getProviders() != null) {
            JsonArray providers = new JsonArray();
            for (GitkitUser.ProviderInfo idpInfo : account.getProviders()) {
                JsonObject provider = new JsonObject();
                provider.addProperty("federatedId", idpInfo.getFederatedId());
                provider.addProperty("providerId", idpInfo.getProviderId());
                providers.add(provider);
            }
            user.add("providerUserInfo", providers);
        }
        infos.add(user);
    }
    return infos;
}

From source file:com.google.iosched.input.fetcher.RemoteJsonHelper.java

License:Open Source License

public static JsonObject mergeJsonFiles(JsonObject target, String... filenames) throws IOException {
    if (target == null) {
        target = new JsonObject();
    }/*w w  w  .j  ava  2s .co  m*/
    for (String filename : filenames) {
        String url = Config.CLOUD_STORAGE_BASE_URL + filename;
        JsonObject obj = fetchJsonFromPublicURL(url);
        if (obj == null) {
            throw new FileNotFoundException(url);
        } else {
            for (Entry<String, JsonElement> entry : obj.entrySet()) {
                if (entry.getValue().isJsonArray()) {
                    // tries to merge an array with the existing one, if it's the case:
                    JsonArray existing = target.getAsJsonArray(entry.getKey());
                    if (existing == null) {
                        existing = new JsonArray();
                        target.add(entry.getKey(), existing);
                    }
                    existing.addAll(entry.getValue().getAsJsonArray());
                } else {
                    target.add(entry.getKey(), entry.getValue());
                }
            }
        }
    }
    return target;
}

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

License:Open Source License

private JsonObject clone(JsonObject source) {
    JsonObject dest = new JsonObject();
    for (Map.Entry<String, JsonElement> entry : source.entrySet()) {
        JsonArray values = entry.getValue().getAsJsonArray();
        JsonArray cloned = new JsonArray();
        cloned.addAll(values);/*from  w  ww  .j  a  v a  2 s  .c o  m*/
        dest.add(entry.getKey(), cloned);
    }
    return dest;
}

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

License:Open Source License

public JsonArray extractRooms(JsonDataSources sources) {
    HashSet<String> ids = new HashSet<String>();
    JsonArray result = new JsonArray();
    JsonDataSource source = sources.getSource(VendorAPISource.MainTypes.rooms.name());
    if (source != null) {
        for (JsonObject origin : source) {
            JsonObject dest = new JsonObject();
            JsonElement originalId = get(origin, VendorAPISource.Rooms.id);
            String id = Config.ROOM_MAPPING.getRoomId(originalId.getAsString());
            if (!ids.contains(id)) {
                String title = Config.ROOM_MAPPING.getTitle(id,
                        get(origin, 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);/*  w w w .j av  a 2 s.c  om*/
                ids.add(id);
            }
        }
    }
    return result;
}

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

License:Open Source License

public JsonArray extractTags(JsonDataSources sources) {
    JsonArray result = new JsonArray();
    JsonDataSource source = sources.getSource(VendorAPISource.MainTypes.categories.name());
    JsonDataSource tagCategoryMappingSource = sources
            .getSource(ExtraSource.MainTypes.tag_category_mapping.name());
    JsonDataSource tagsConfSource = sources.getSource(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, 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, ExtraSource.CategoryTagMapping.tag_name);
                        JsonPrimitive isDefault = (JsonPrimitive) get(categoryMapping,
                                ExtraSource.CategoryTagMapping.is_default);
                        if (isDefault != null && isDefault.getAsBoolean()) {
                            mainCategory = category;
                        }/*w ww . j a  v  a  2  s  . c  om*/
                    }
                    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, VendorAPISource.Categories.name);
                JsonElement tagName = new JsonPrimitive(
                        category.getAsString() + "_" + Converters.TAG_NAME.convert(name).getAsString());
                JsonElement originalTagName = tagName;

                set(tagName, dest, OutputJsonKeys.Tags.tag);
                set(name, dest, OutputJsonKeys.Tags.name);
                set(origin, VendorAPISource.Categories.id, dest, OutputJsonKeys.Tags.original_id);
                set(origin, VendorAPISource.Categories.description, dest, OutputJsonKeys.Tags._abstract, null);

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

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

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

License:Open Source License

public JsonArray extractSpeakers(JsonDataSources sources) {
    speakersById = new HashMap<String, JsonObject>();
    JsonArray result = new JsonArray();
    JsonDataSource source = sources.getSource(VendorAPISource.MainTypes.speakers.name());
    if (source != null) {
        for (JsonObject origin : source) {
            JsonObject dest = new JsonObject();
            JsonElement id = get(origin, VendorAPISource.Speakers.id);
            set(id, dest, OutputJsonKeys.Speakers.id);
            set(origin, VendorAPISource.Speakers.name, dest, OutputJsonKeys.Speakers.name, null);
            set(origin, VendorAPISource.Speakers.bio, dest, OutputJsonKeys.Speakers.bio, null);
            set(origin, VendorAPISource.Speakers.companyname, dest, OutputJsonKeys.Speakers.company, null);
            JsonElement originalPhoto = get(origin, VendorAPISource.Speakers.photo);
            if (originalPhoto != null && !"".equals(originalPhoto.getAsString())) {
                // Note that the input for SPEAKER_PHOTO_ID converter 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, VendorAPISource.Speakers.id, dest, OutputJsonKeys.Speakers.thumbnailUrl,
                        Converters.SPEAKER_PHOTO_URL);
            }/*from ww  w  . j ava2  s  .c om*/
            JsonElement info = origin.get(VendorAPISource.Speakers.info.name());
            JsonPrimitive plusUrl = getMapValue(info,
                    InputJsonKeys.VendorAPISource.Speakers.INFO_PUBLIC_PLUS_ID, Converters.GPLUS_URL, null);
            if (plusUrl != null) {
                set(plusUrl, dest, OutputJsonKeys.Speakers.plusoneUrl);
            }
            result.add(dest);
            speakersById.put(id.getAsString(), dest);
        }
    }
    return result;
}

From source file:com.google.iosched.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");
    }/* w ww . j  av 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(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, 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();
            set(origin, VendorAPISource.Topics.id, dest, OutputJsonKeys.Sessions.id);
            set(origin, VendorAPISource.Topics.id, dest, OutputJsonKeys.Sessions.url, Converters.SESSION_URL);
            set(origin, VendorAPISource.Topics.title, dest, OutputJsonKeys.Sessions.title, null);
            set(origin, VendorAPISource.Topics.description, dest, OutputJsonKeys.Sessions.description, null);
            set(origin, VendorAPISource.Topics.start, dest, OutputJsonKeys.Sessions.startTimestamp,
                    Converters.DATETIME);
            set(origin, VendorAPISource.Topics.finish, dest, OutputJsonKeys.Sessions.endTimestamp,
                    Converters.DATETIME);

            JsonElement documents = get(origin, 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, VendorAPISource.Topics.id, dest, OutputJsonKeys.Sessions.photoUrl,
                        Converters.SESSION_PHOTO_URL);
            }

            setVideoPropertiesInSession(origin, dest);
            setRelatedVideos(origin, dest);

            JsonElement mainTag = null;
            JsonElement hashtag = null;
            JsonElement mainTagColor = null;
            JsonArray categories = origin.getAsJsonArray(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, 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(VendorAPISource.Topics.sessions.name());
            if (sessions != null && sessions.size() > 0) {
                String roomId = get(sessions.get(0).getAsJsonObject(), 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);
                }
            }

            result.add(dest);
        }
    }
    return result;
}