List of usage examples for org.json.simple JSONObject entrySet
Set<Map.Entry<K, V>> entrySet();
From source file:com.rackspacecloud.blueflood.outputs.serializers.JSONBasicRollupsOutputSerializer.java
private JSONObject toJSON(long timestamp, Points.Point point, String unit, Set<MetricStat> filterStats) throws SerializationException { final JSONObject object = new JSONObject(); object.put("timestamp", timestamp); JSONObject filterStatsObject = null; long numPoints = 1; // todo: adding getCount() to Rollup interface will simplify this block. // because of inheritance, GaugeRollup needs to come before BasicRollup. sorry. if (point.getData() instanceof BluefloodGaugeRollup) { BluefloodGaugeRollup rollup = (BluefloodGaugeRollup) point.getData(); numPoints += rollup.getCount();//w ww. ja va 2 s . c om filterStatsObject = getFilteredStatsForRollup(rollup, filterStats); } else if (point.getData() instanceof BasicRollup) { numPoints = ((BasicRollup) point.getData()).getCount(); filterStatsObject = getFilteredStatsForRollup((BasicRollup) point.getData(), filterStats); } else if (point.getData() instanceof SimpleNumber) { numPoints = 1; filterStatsObject = getFilteredStatsForFullRes(point.getData(), filterStats); } else if (point.getData() instanceof String) { numPoints = 1; filterStatsObject = getFilteredStatsForString((String) point.getData()); } else if (point.getData() instanceof Boolean) { numPoints = 1; filterStatsObject = getFilteredStatsForBoolean((Boolean) point.getData()); } else if (point.getData() instanceof BluefloodSetRollup) { BluefloodSetRollup rollup = (BluefloodSetRollup) point.getData(); numPoints += rollup.getCount(); filterStatsObject = getFilteredStatsForRollup(rollup, filterStats); } else if (point.getData() instanceof BluefloodTimerRollup) { BluefloodTimerRollup rollup = (BluefloodTimerRollup) point.getData(); numPoints += rollup.getCount(); filterStatsObject = getFilteredStatsForRollup(rollup, filterStats); } else if (point.getData() instanceof BluefloodCounterRollup) { BluefloodCounterRollup rollup = (BluefloodCounterRollup) point.getData(); numPoints += rollup.getCount().longValue(); filterStatsObject = getFilteredStatsForRollup(rollup, filterStats); } else if (point.getData() instanceof BluefloodEnumRollup) { BluefloodEnumRollup rollup = (BluefloodEnumRollup) point.getData(); numPoints += rollup.getCount(); filterStatsObject = getFilteredStatsForRollup(rollup, filterStats); } else { String errString = String.format("Unsupported datatype for Point %s", point.getData().getClass()); log.error(errString); throw new SerializationException(errString); } // Set all filtered stats to null if numPoints is 0 if (numPoints == 0) { final Set<Map.Entry<String, Object>> statsSet = filterStatsObject.entrySet(); for (Map.Entry<String, Object> stat : statsSet) { if (!stat.getKey().equals("numPoints")) { stat.setValue(null); } } } // Add filtered stats to main object final Set<Map.Entry<String, Object>> statsSet = filterStatsObject.entrySet(); for (Map.Entry<String, Object> stat : statsSet) { object.put(stat.getKey(), stat.getValue()); } return object; }
From source file:nl.vumc.biomedbridges.galaxy.GalaxyWorkflow.java
/** * Parses the JSON / GA-file of a Galaxy workflow. * * // todo: use the GalaxyWorkflowMetadata class instead. *///ww w.j a v a 2s.com public void parseJson() { try { inputsMetadata = new ArrayList<>(); outputsMetadata = new ArrayList<>(); final String jsonContent = getJsonContent(); if (jsonContent != null) { final JSONObject workflowJson = (JSONObject) jsonParser.parse(jsonContent); final JSONObject stepsMapJson = (JSONObject) workflowJson.get("steps"); logger.info("This workflow contains " + stepsMapJson.size() + " step" + (stepsMapJson.size() != 1 ? "s" : "") + ":"); // Sort the steps to have a well defined order. final SortedMap<Integer, JSONObject> sortedStepsMap = new TreeMap<>(); for (final Object stepObject : stepsMapJson.entrySet()) if (stepObject instanceof Map.Entry) { final Map.Entry stepEntry = (Map.Entry) stepObject; final int stepId = Integer.parseInt((String) stepEntry.getKey()); sortedStepsMap.put(stepId, (JSONObject) stepEntry.getValue()); } for (final JSONObject stepJson : sortedStepsMap.values()) { addJsonInputs((JSONArray) stepJson.get("inputs")); addJsonOutputs((JSONArray) stepJson.get("outputs")); } } } catch (final ParseException e) { logger.error("Exception while parsing json design in workflow file {}.", getJsonFilename(), e); } }
From source file:nl.vumc.biomedbridges.galaxy.GalaxyWorkflow.java
/** * Create a list of maps from a json array. * * @param jsonArray the json array.//from w w w . ja v a2 s .c o m * @return the list of maps. */ private List<Map<String, String>> createListOfMaps(final JSONArray jsonArray) { final List<Map<String, String>> listOfMaps = new ArrayList<>(); for (final Object object : jsonArray) { final JSONObject jsonObject = (JSONObject) object; //logger.trace("jsonObject: " + jsonObject); final Map<String, String> propertyMap = new HashMap<>(); for (final Object entry : jsonObject.entrySet()) if (entry instanceof Map.Entry) { final Map.Entry mapEntry = (Map.Entry) entry; propertyMap.put((String) mapEntry.getKey(), (String) mapEntry.getValue()); } listOfMaps.add(propertyMap); } return listOfMaps; }
From source file:nl.vumc.biomedbridges.galaxy.metadata.GalaxyWorkflowMetadata.java
/** * Create a Galaxy workflow metadata object from a json object. * * @param workflowJson the workflow json object. *///from w w w .j a v a2s. c o m public GalaxyWorkflowMetadata(final JSONObject workflowJson) { this.aGalaxyWorkflow = "true".equals(workflowJson.get("a_galaxy_workflow")); this.annotation = workflowJson.get("annotation").toString(); this.formatVersion = workflowJson.get("format-version").toString(); this.name = workflowJson.get("name").toString(); this.steps = new ArrayList<>(); final JSONObject stepsMapJson = (JSONObject) workflowJson.get("steps"); // Sort the steps to have a well defined order. final SortedMap<Integer, JSONObject> sortedStepsMap = new TreeMap<>(); for (final Object stepObject : stepsMapJson.entrySet()) if (stepObject instanceof Map.Entry) { final Map.Entry stepEntry = (Map.Entry) stepObject; final int stepId = Integer.parseInt((String) stepEntry.getKey()); sortedStepsMap.put(stepId, (JSONObject) stepEntry.getValue()); } for (final JSONObject stepJson : sortedStepsMap.values()) steps.add(new GalaxyWorkflowStep(stepJson)); }
From source file:nl.vumc.biomedbridges.galaxy.metadata.GalaxyWorkflowStep.java
/** * Create a Galaxy workflow step from a step json object. * * @param stepJson the json step object that contains the data for this step. */// ww w . ja v a 2s . co m public GalaxyWorkflowStep(final JSONObject stepJson) { this.id = JsonUtilities.getJsonLong(stepJson, "id"); this.name = JsonUtilities.getJsonString(stepJson, "name"); this.type = JsonUtilities.getJsonString(stepJson, "type"); this.toolId = JsonUtilities.getJsonString(stepJson, "tool_id"); this.toolVersion = JsonUtilities.getJsonString(stepJson, "tool_version"); this.annotation = JsonUtilities.getJsonString(stepJson, "annotation"); this.position = new GalaxyStepPosition((JSONObject) stepJson.get("position")); this.toolErrors = new HashMap<>(); // Initialize inputConnections. this.inputConnections = new HashMap<>(); final JSONObject inputConnectionsMap = (JSONObject) stepJson.get("input_connections"); for (final Object inputConnectionObject : inputConnectionsMap.entrySet()) { final Map.Entry inputConnectionEntry = (Map.Entry) inputConnectionObject; final JSONObject inputConnectionJson = (JSONObject) inputConnectionEntry.getValue(); final GalaxyStepInputConnection inputConnection = new GalaxyStepInputConnection(inputConnectionJson); this.inputConnections.put((String) inputConnectionEntry.getKey(), inputConnection); } // Initialize inputs. this.inputs = new ArrayList<>(); final JSONArray inputsArray = (JSONArray) stepJson.get("inputs"); for (final Object input : inputsArray) this.inputs.add(new GalaxyStepInput((JSONObject) input)); // Initialize outputs. this.outputs = new ArrayList<>(); final JSONArray outputsArray = (JSONArray) stepJson.get("outputs"); for (final Object output : outputsArray) this.outputs.add(new GalaxyStepOutput((JSONObject) output)); // Initialize toolState. this.toolState = new HashMap<>(); fillToolState(stepJson.get("tool_state")); // Initialize userOutputs. this.userOutputs = new ArrayList<>(); }
From source file:nl.vumc.biomedbridges.galaxy.metadata.GalaxyWorkflowStep.java
/** * Fill the tool state map based on the values in the tool state json object. * * @param toolStateObject the tool state json object. *///from ww w . j av a2 s . c o m private void fillToolState(final Object toolStateObject) { try { if (toolStateObject != null) { final JSONObject toolStateJson = (JSONObject) new JSONParser().parse(toolStateObject.toString()); for (final Object parameterObject : toolStateJson.entrySet()) { final Map.Entry parameterEntry = (Map.Entry) parameterObject; final Object parameterValue = parameterEntry.getValue(); final Object toolStateValue = parameterValue != null ? getToolStateValue(parameterValue) : null; logger.trace(parameterEntry.getKey() + " -> " + toolStateValue + (toolStateValue != null ? " (" + toolStateValue.getClass().getName() + ")" : "")); this.toolState.put((String) parameterEntry.getKey(), toolStateValue); } } } catch (final ParseException e) { e.printStackTrace(); } }
From source file:org.dia.kafka.isatools.producer.ISAToolsKafkaProducer.java
private static JSONObject adjustUnifiedSchema(JSONObject parse) { JSONObject jsonObject = new JSONObject(); List invNames = new ArrayList<String>(); List invMid = new ArrayList<String>(); List invLastNames = new ArrayList<String>(); Set<Map.Entry> set = parse.entrySet(); for (Map.Entry entry : set) { String jsonKey = SolrKafkaConsumer.updateCommentPreffix(entry.getKey().toString()); String solrKey = ISA_SOLR.get(jsonKey); // System.out.println("solrKey " + solrKey); if (solrKey != null) { // System.out.println("jsonKey: " + jsonKey + " -> solrKey: " + solrKey); if (jsonKey.equals("Study_Person_First_Name")) { invNames.addAll(((JSONArray) JSONValue.parse(entry.getValue().toString()))); } else if (jsonKey.equals("Study_Person_Mid_Initials")) { invMid.addAll(((JSONArray) JSONValue.parse(entry.getValue().toString()))); } else if (jsonKey.equals("Study_Person_Last_Name")) { invLastNames.addAll(((JSONArray) JSONValue.parse(entry.getValue().toString()))); }// w ww.j a v a 2s. c o m jsonKey = solrKey; } else { jsonKey = jsonKey.replace(" ", "_"); } jsonObject.put(jsonKey, entry.getValue()); } JSONArray jsonArray = new JSONArray(); for (int cnt = 0; cnt < invLastNames.size(); cnt++) { StringBuilder sb = new StringBuilder(); if (!StringUtils.isEmpty(invNames.get(cnt).toString())) sb.append(invNames.get(cnt)).append(" "); if (!StringUtils.isEmpty(invMid.get(cnt).toString())) sb.append(invMid.get(cnt)).append(" "); if (!StringUtils.isEmpty(invLastNames.get(cnt).toString())) sb.append(invLastNames.get(cnt)); jsonArray.add(sb.toString()); } if (!jsonArray.isEmpty()) { jsonObject.put("Investigator", jsonArray.toJSONString()); } return jsonObject; }
From source file:org.exoplatform.addons.es.client.ElasticIndexingClient.java
private void logBulkResponseItem(JSONObject item, long executionTime) { for (Map.Entry operation : (Set<Map.Entry>) item.entrySet()) { String operationName = operation.getKey() == null ? null : (String) operation.getKey(); if (operation.getValue() != null) { JSONObject operationDetails = (JSONObject) operation.getValue(); String index = operationDetails.get("_index") == null ? null : (String) operationDetails.get("_index"); String type = operationDetails.get("_type") == null ? null : (String) operationDetails.get("_type"); String id = operationDetails.get("_id") == null ? null : (String) operationDetails.get("_id"); Long status = operationDetails.get("status") == null ? null : (Long) operationDetails.get("status"); String error = operationDetails.get("error") == null ? null : (String) ((JSONObject) operationDetails.get("error")).get("reason"); Integer httpStatusCode = status == null ? null : status.intValue(); if (ElasticIndexingAuditTrail.isError(httpStatusCode)) { auditTrail.logRejectedDocumentBulkOperation(operationName, id, index, type, httpStatusCode, error, executionTime); } else { if (auditTrail.isFullLogEnabled()) { auditTrail.logAcceptedBulkOperation(operationName, id, index, type, httpStatusCode, error, executionTime); }/*from ww w. j ava 2s .c o m*/ } } } }
From source file:org.graylog2.gelf.GELFProcessor.java
private LogMessage parse(String message) { TimerContext tcx = gelfParsedTime.time(); JSONObject json; LogMessage lm = new LogMessage(); try {/* w w w . java 2s . co m*/ json = getJSON(message); } catch (Exception e) { LOG.error("Could not parse JSON!", e); json = null; } if (json == null) { throw new IllegalStateException("JSON is null/could not be parsed (invalid JSON)"); } // Add standard fields. lm.setHost(this.jsonToString(json.get("host"))); lm.setShortMessage(this.jsonToString(json.get("short_message"))); lm.setFullMessage(this.jsonToString(json.get("full_message"))); lm.setFile(this.jsonToString(json.get("file"))); lm.setLine(this.jsonToInt(json.get("line"))); // Level is set by server if not specified by client. int level = this.jsonToInt(json.get("level")); if (level > -1) { lm.setLevel(level); } else { lm.setLevel(LogMessage.STANDARD_LEVEL); } // Facility is set by server if not specified by client. String facility = this.jsonToString(json.get("facility")); if (facility == null) { lm.setFacility(LogMessage.STANDARD_FACILITY); } else { lm.setFacility(facility); } // Set createdAt to provided timestamp - Set to current time if not set. double timestamp = this.jsonToDouble(json.get("timestamp")); if (timestamp <= 0) { lm.setCreatedAt(Tools.getUTCTimestampWithMilliseconds()); } else { lm.setCreatedAt(timestamp); } // Add additional data if there is some. Set<Map.Entry<String, Object>> entrySet = json.entrySet(); for (Map.Entry<String, Object> entry : entrySet) { String key = entry.getKey(); Object value = entry.getValue(); // Skip standard fields. if (!key.startsWith(GELFMessage.ADDITIONAL_FIELD_PREFIX)) { continue; } // Convert lists and maps to Strings. if (value instanceof List || value instanceof Map || value instanceof Set) { value = value.toString(); } // Don't allow to override _id. (just to make sure...) if (key.equals("_id")) { LOG.warn("Client tried to override _id field! Skipped field, but still storing message."); continue; } // Add to message. lm.addAdditionalData(key, value); } // Stop metrics timer. tcx.stop(); return lm; }
From source file:org.jitsi.videobridge.rest.JSONDeserializer.java
/** * Deserializes the values of a <tt>JSONObject</tt> which are neither * <tt>JSONArray</tt>, nor <tt>JSONObject</tt> into attribute values * a <tt>AbstractPacketExtension</tt>. * * @param jsonObject the <tt>JSONObject</tt> whose values which are neither * <tt>JSONArray</tt>, nor <tt>JSONObject</tt> to deserialize into attribute * values of <tt>abstractPacketExtension</tt> * @param abstractPacketExtension the <tt>AbstractPacketExtension</tt> in * the attributes of which the values of <tt>jsonObject</tt> which are * neither <tt>JSONObject</tt>, nor <tt>JSONArray</tt> are to be * deserialized//from w ww .j ava 2 s . co m */ public static void deserializeAbstractPacketExtensionAttributes(JSONObject jsonObject, AbstractPacketExtension abstractPacketExtension) { Iterator<Map.Entry<Object, Object>> i = jsonObject.entrySet().iterator(); while (i.hasNext()) { Map.Entry<Object, Object> e = i.next(); Object key = e.getKey(); if (key != null) { String name = key.toString(); if (name != null) { Object value = e.getValue(); if (!(value instanceof JSONObject) && !(value instanceof JSONArray)) { abstractPacketExtension.setAttribute(name, value); } } } } }