Example usage for com.google.gson JsonObject remove

List of usage examples for com.google.gson JsonObject remove

Introduction

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

Prototype

public JsonElement remove(String property) 

Source Link

Document

Removes the property from this JsonObject .

Usage

From source file:au.edu.ausstage.tweetgatherer.MessageProcessor.java

License:Open Source License

/**
 * A method that is run in the thread/*from   w w w . j a  v a2  s  .c  o  m*/
 * Takes a tweet from the queue and processes it
 */
public void run() {
    STweet tweet = null;

    try {

        // infinite loop to keep the thread running
        while (true) {

            // take a tweet from the queue
            tweet = newTweets.take();

            // get the tweet id number
            String tweetId = Long.toString(tweet.getStatusId());

            // get a hash of the tweet id
            String tweetIdHash = HashUtils.hashValue(tweetId);

            // get the user
            STweetUser user = tweet.getUser();

            // get the user id
            String userId = Long.toString(user.getUserId());

            // get a hash of the user id
            String userIdHash = HashUtils.hashValue(userId);

            // get the JSON value of the tweet
            JsonObject jsonTweetObject = sanitiseMessage(tweet.getJSON(), tweetIdHash);

            // get the JSON value of the user
            JsonObject jsonUserObject = sanitiseUser(user.getJSON(), userIdHash);

            // replace the user with the sanitised version
            jsonTweetObject.remove("user");
            jsonTweetObject.addProperty("user", "");

            /*
             * Write the file
             */

            if (FileUtils.writeNewFile(logFiles + "/" + tweetIdHash,
                    jsonTweetObject.toString() + "\n" + jsonUserObject.toString()) == false) {
                System.err.println("ERROR: Unable to write file to the specified log file");
                System.err.println("       twitter message with id '" + tweetId + "' is now lost");
            } else {
                System.out.println("INFO: New Message written to the log directory:");
                System.out.println("      " + tweetIdHash);
            }

            /*
             * get the hash tags from this message
             */

            // get the list of hash tags from the tweet
            JsonElement elem = jsonTweetObject.get("text");
            List<String> hashtags = extractHashTags.extractHashtags(elem.getAsString());

            // convert all of the found hash tags to lower case
            ListIterator<String> hashtagsIterator = hashtags.listIterator();

            while (hashtagsIterator.hasNext() == true) {

                // get the next tage in the list
                String tmp = (String) hashtagsIterator.next();

                tmp = tmp.toLowerCase();

                // replace the value we retrieved with this updated one
                hashtagsIterator.set(tmp);
            }

            // see if this is a company performance
            if (isCompanyPerformance(hashtags, jsonTweetObject, jsonUserObject, tweetIdHash) == false) {
                // this isn't a company performance
                // is it using a performance specific id?
                if (isPerformance(hashtags, jsonTweetObject, jsonUserObject, tweetIdHash) == false) {
                    //this isn't a valid performance
                    System.err.println("ERROR: Unable to find a matching performance for this message");

                    // send an exception report
                    if (emailManager.sendMessageWithAttachment(EMAIL_SUBJECT, EMAIL_MESSAGE,
                            logFiles + "/" + tweetIdHash) == false) {
                        System.err.println("ERROR: Unable to send the exception report");
                    }
                }
            }
        }

    } catch (InterruptedException ex) {
        // thread has been interrupted
        System.out.println("INFO: The Message processing thread has stopped");
    }
}

From source file:au.edu.ausstage.tweetgatherer.MessageProcessor.java

License:Open Source License

/**
 * A private method to sanitise the message
 *
 * @param jsonTweetObject the original message object
 * @param idHash          the hash of the message id
 *
 * @return                the sanitised message object
 *//*from w w w . j  a va 2 s.c  o  m*/
private JsonObject sanitiseMessage(JsonObject jsonTweetObject, String idHash) {

    // remove the id and replace it with the hash
    jsonTweetObject.remove("id");
    jsonTweetObject.addProperty("id", idHash);

    // replace the "in_reply_to_user_id" field with a hash if present
    if (jsonTweetObject.has("in_reply_to_user_id") == true) {

        // get a hash of the value
        JsonElement elem = jsonTweetObject.get("in_reply_to_user_id");
        if (elem.isJsonNull() == false) {
            String hash = HashUtils.hashValue(elem.getAsString());

            // update the value
            jsonTweetObject.remove("in_reply_to_user_id");
            jsonTweetObject.addProperty("in_reply_to_user_id", hash);
        }
    }

    // replace the "in_reply_to_status_id" field with a hash if present
    if (jsonTweetObject.has("in_reply_to_status_id") == true) {

        // get a hash of the value
        JsonElement elem = jsonTweetObject.get("in_reply_to_status_id");
        if (elem.isJsonNull() == false) {
            String hash = HashUtils.hashValue(elem.getAsString());

            // update the value
            jsonTweetObject.remove("in_reply_to_status_id");
            jsonTweetObject.addProperty("in_reply_to_status_id", hash);
        }
    }

    // replace the "retweeted_status" field with a hash if present
    if (jsonTweetObject.has("retweeted_status") == true) {

        // get a hash of the value
        JsonElement elem = jsonTweetObject.get("retweeted_status");
        if (elem.isJsonNull() == false) {
            String hash = HashUtils.hashValue(elem.getAsString());

            // update the value
            jsonTweetObject.remove("retweeted_status");
            jsonTweetObject.addProperty("retweeted_status", hash);
        }
    }

    // return the sanitised object
    return jsonTweetObject;

}

From source file:au.edu.ausstage.tweetgatherer.MessageProcessor.java

License:Open Source License

/**
 * A private method to sanitise the message
 *
 * @param jsonTweetObject the original user object
 * @param idHash          the hash of the user id
 *
 * @return                the sanitised user object
 *///  w ww  . ja  va2 s  .  com
private JsonObject sanitiseUser(JsonObject jsonTweetObject, String idHash) {

    // remove the id and replace it with the hash
    jsonTweetObject.remove("id");
    jsonTweetObject.addProperty("id", idHash);

    // loop through the list of sanitised fields
    for (int i = 0; i < SANITISED_FIELDS.length; i++) {
        if (jsonTweetObject.has(SANITISED_FIELDS[i]) == true) {
            jsonTweetObject.remove(SANITISED_FIELDS[i]);
            jsonTweetObject.addProperty(SANITISED_FIELDS[i], "");
        }
    }

    // return the sanitised object
    return jsonTweetObject;
}

From source file:br.unicamp.cst.bindings.soar.SOARPlugin.java

License:Open Source License

public void removeBranchFromJson(String pathToOldBranch, JsonObject json) {
    String[] oldNodes = pathToOldBranch.split("\\.");
    if (oldNodes.length > 1) {
        if (json.has(oldNodes[0])) {
            removeBranchFromJson(pathToOldBranch.substring(oldNodes[0].length() + 1),
                    json.getAsJsonObject(oldNodes[0]));
        }/*  w w  w  .j  a  v a 2  s.  c o m*/
    } else {
        json.remove(oldNodes[0]);
    }
}

From source file:ccm.pay2spawn.types.guis.HelperGuiBase.java

License:Open Source License

public void storeValue(String key, JsonObject jsonObject, Object value) {
    if (key == null || jsonObject == null)
        return;// w  w  w . j  a v  a 2s . c om
    if (value == null) {
        jsonObject.add(key, JsonNull.INSTANCE);
        return;
    }
    if (Strings.isNullOrEmpty(value.toString()))
        jsonObject.remove(key);
    else
        jsonObject.addProperty(key,
                typeMap != null && typeMap.containsKey(key) ? typeMap.get(key) + ":" + value.toString()
                        : value.toString());
}

From source file:classes.analysis.Analysis.java

License:Open Source License

private static Analysis parseAnalysisGalaxyData(String origin, String emsuser, JsonObject analysisData) {
    JsonParser parser = new JsonParser();
    JsonArray provenance = (JsonArray) parser.parse(analysisData.get("provenance").getAsString());

    //STEP 1. Find the associations between the steps (inputs and outputs)
    HashMap<String, JsonElement> outputs = new HashMap<String, JsonElement>();
    JsonObject stepJSONobject;
    for (JsonElement step_json : provenance) {
        stepJSONobject = step_json.getAsJsonObject();
        for (JsonElement output : stepJSONobject.getAsJsonArray("outputs")) {
            outputs.put(output.getAsJsonObject().get("id").getAsString(), step_json);
        }/*from  w  ww  .ja v a2 s  .c om*/

        if ("upload1".equalsIgnoreCase(stepJSONobject.get("tool_id").getAsString())) {
            stepJSONobject.remove("step_type");
            stepJSONobject.add("step_type", new JsonPrimitive("external_source"));
        } else {
            stepJSONobject.add("step_type", new JsonPrimitive("processed_data"));
        }
    }
    for (JsonElement step_json : provenance) {
        stepJSONobject = step_json.getAsJsonObject();
        for (JsonElement input : stepJSONobject.getAsJsonArray("inputs")) {
            String id = input.getAsJsonObject().get("id").getAsString();
            if (outputs.containsKey(id)) {
                if (!"external_source"
                        .equalsIgnoreCase(outputs.get(id).getAsJsonObject().get("step_type").getAsString())) {
                    outputs.get(id).getAsJsonObject().remove("step_type");
                    outputs.get(id).getAsJsonObject().add("step_type", new JsonPrimitive("intermediate_data"));
                }

                if (!stepJSONobject.has("used_data")) {
                    stepJSONobject.add("used_data", new JsonArray());
                }
                ((JsonArray) stepJSONobject.get("used_data")).add(new JsonPrimitive(
                        "STxxxx." + outputs.get(id).getAsJsonObject().get("id").getAsString()));
            }
        }
    }

    //STEP 2. Create the instances for the steps
    ArrayList<NonProcessedData> nonProcessedDataList = new ArrayList<NonProcessedData>();
    ArrayList<ProcessedData> processedDataList = new ArrayList<ProcessedData>();
    for (JsonElement step_json : provenance) {
        stepJSONobject = step_json.getAsJsonObject();
        if ("external_source".equalsIgnoreCase(stepJSONobject.get("step_type").getAsString())) {
            nonProcessedDataList.add(ExternalData.parseStepGalaxyData(stepJSONobject, analysisData, emsuser));
        } else if ("intermediate_data".equalsIgnoreCase(stepJSONobject.get("step_type").getAsString())) {
            nonProcessedDataList
                    .add(IntermediateData.parseStepGalaxyData(stepJSONobject, analysisData, emsuser));
        } else if ("processed_data".equalsIgnoreCase(stepJSONobject.get("step_type").getAsString())) {
            processedDataList.add(ProcessedData.parseStepGalaxyData(stepJSONobject, analysisData, emsuser));
        } else {
            throw new InstantiationError("Unknown step type");
        }
    }

    Collections.sort(nonProcessedDataList);
    Collections.sort(processedDataList);

    //STEP 3. Create the instance of analysis
    Analysis analysis = new Analysis();
    analysis.setAnalysisName(analysisData.get("ems_analysis_name").getAsString());
    analysis.setAnalysisType("Galaxy workflow");
    analysis.setNonProcessedData(nonProcessedDataList.toArray(new NonProcessedData[] {}));
    analysis.setProcessedData(processedDataList.toArray(new ProcessedData[] {}));
    analysis.setTags(new String[] { "imported" });
    analysis.setStatus("pending");

    return analysis;
}

From source file:classes.analysis.Analysis.java

License:Open Source License

public String export(String tmpDir, String format, String templatesDir) throws Exception {
    String content = "";
    if ("json".equalsIgnoreCase(format)) {
        content = this.toJSON();
    } else if ("xml".equalsIgnoreCase(format)) {
        content = "<?xml version=\"1.0\"?>\n";
        content += "<analysis id=\"" + this.getAnalysisID() + "\">\n";
        JsonObject analysis = new JsonParser().parse(this.toJSON()).getAsJsonObject();
        JsonElement non_processed_data = analysis.remove("non_processed_data");
        JsonElement processed_data = analysis.remove("processed_data");

        content += this.generateXMLContent(analysis, 1);
        content += "\t<steps>\n";
        content += "\t\t<non_processed_data>\n";
        for (JsonElement subelement : non_processed_data.getAsJsonArray()) {
            content += "\t\t<step>\n";
            content += this.generateXMLContent(subelement, 4);
            content += "\t\t\t</step>\n";
        }/*from w w w  . j a va 2  s.co m*/
        content += "\t\t</non_processed_data>\n";
        content += "\t\t<processed_data>\n";
        for (JsonElement subelement : processed_data.getAsJsonArray()) {
            content += "\t\t<step>\n";
            content += this.generateXMLContent(subelement, 4);
            content += "\t\t\t</step>\n";
        }
        content += "\t\t</processed_data>\n";
        content += "\t</steps>\n";
        content += "</analysis>\n";
    } else {
        ArrayList<Pair> elements = this.processElementContent(templatesDir, this.toJSON());

        if ("html".equals(format)) {
            content = this.generateHTMLContent(elements);
        } else {
            throw new Exception(format + " is not a valid format");
        }
    }

    File file = new File(tmpDir + File.separator + this.analysis_id + "." + format);
    PrintWriter writer = new PrintWriter(file);
    writer.println(content);
    writer.close();
    return file.getAbsolutePath();
}

From source file:classes.samples.BioCondition.java

License:Open Source License

public String export(String tmpDir, String format, String templatesDir) throws Exception {
    String content = "";
    if ("json".equalsIgnoreCase(format)) {
        content = this.toJSON();
    } else if ("xml".equalsIgnoreCase(format)) {
        content = "<?xml version=\"1.0\"?>\n";
        content += "<biocondition id=\"" + this.getBioConditionID() + "\">\n";
        JsonObject biocondition = new JsonParser().parse(this.toJSON()).getAsJsonObject();
        JsonElement bioreplicates = biocondition.remove("associatedBioreplicates");
        biocondition.remove("associatedExperiments");

        content += this.generateXMLContent(biocondition, 1);
        content += "\t<samples>\n";
        for (JsonElement subelement : bioreplicates.getAsJsonArray()) {
            content += "\t\t<sample>\n";
            content += this.generateXMLContent(subelement, 4);
            content += "\t\t</sample>\n";
        }/*from   www. ja v a  2  s  .c  o  m*/
        content += "\t</samples>\n";
        content += "</biocondition>\n";
    } else {
        ArrayList<Pair> elements = this.processElementContent(templatesDir, this.toJSON());

        if ("html".equals(format)) {
            content = this.generateHTMLContent(elements);
        } else {
            throw new Exception(format + " is not a valid format");
        }
    }

    File file = new File(tmpDir + File.separator + this.biocondition_id + "." + format);
    PrintWriter writer = new PrintWriter(file);
    writer.println(content);
    writer.close();
    return file.getAbsolutePath();
}

From source file:co.cask.cdap.logging.gateway.handlers.LogDataOffsetCallback.java

License:Apache License

private Object modifyLogJsonElememnt(JsonElement jsonElement) {
    JsonObject jsonLogData = (JsonObject) jsonElement;
    JsonObject logData = jsonLogData.getAsJsonObject("log");

    if (!fieldsToSuppress.isEmpty()) {
        for (String field : fieldsToSuppress) {
            logData.remove(field);
        }//from   w w  w  . j  a  v a2s  . c  o m
    }

    return jsonLogData;
}

From source file:com.adobe.acs.commons.wcm.impl.RTEConfigurationServlet.java

License:Apache License

private void writeConfigResource(Resource resource, String rteName, SlingHttpServletRequest request,
        SlingHttpServletResponse response) throws IOException, ServletException {
    JsonObject widget = createEmptyWidget(rteName);

    // these two size properties seem to be necessary to get the size correct
    // in a component dialog
    widget.addProperty("width", RTE_WIDTH);
    widget.addProperty("height", RTE_HEIGHT);

    RequestParameterMap map = request.getRequestParameterMap();
    for (Map.Entry<String, RequestParameter[]> entry : map.entrySet()) {
        String key = entry.getKey();
        RequestParameter[] params = entry.getValue();
        if (params != null && (params.length > 1 || EXTERNAL_STYLESHEETS_PROPERTY.equals(key))) {
            JsonArray arr = new JsonArray();
            for (int i = 0; i < params.length; i++) {
                arr.add(new JsonPrimitive(params[i].getString()));
            }/*from   w ww.  j  a  v  a 2s.co m*/
            widget.add(key, arr);
        } else if (params != null && params.length == 1) {
            widget.addProperty(key, params[0].getString());
        }
    }

    if (widget.has("fieldLabel")) {
        widget.remove("hideLabel");
    }

    JsonObject config = toJsonObject(resource);

    if (config == null) {
        config = new JsonObject();
    }

    if (config.has("includeDefault") && config.get("includeDefault").getAsBoolean()) {
        config = underlay(config, resource.getResourceResolver().getResource(DEFAULT_CONFIG));
    }

    widget.add("rtePlugins", config);

    JsonObject parent = new JsonObject();
    parent.addProperty("xtype", "dialogfieldset");
    parent.addProperty("border", false);
    parent.addProperty("padding", 0);
    parent.add("items", widget);
    Gson gson = new Gson();
    gson.toJson(parent, response.getWriter());
}