@Override public byte[] mapRecord(TridentTuple tuple) { JSONObject obj = new JSONObject(); if (this.columnFields != null) { for (String field : this.columnFields) { obj.put(field, tuple.getValueByField(field)); } } return obj.toJSONString().getBytes(); }
@SuppressWarnings("unchecked") @Override public String toJSONString() { JSONArray jsonArray = new JSONArray(); for(String kw : keywords) { JSONObject data = new JSONObject(); data.put("name", kw); data.put("value", kw); jsonArray.add(data); } return jsonArray.toJSONString(); }
public static void main(String[] args) { // convert Java to json JSONObject root = new JSONObject(); root.put("message", "Hi"); JSONObject place = new JSONObject(); place.put("name", "World!"); root.put("place", place); String json = root.toJSONString(); System.out.println(json); System.out.println(); // convert json to Java JSONObject obj = (JSONObject) JSONValue.parse(json); String message = (String) obj.get("message"); place = (JSONObject) obj.get("place"); String name = (String) place.get("name"); System.out.println(message + " " + name); } }
J4pVersionResponse(J4pVersionRequest pRequest, JSONObject pResponse) { super(pRequest,pResponse); JSONObject value = (JSONObject) getValue(); agentVersion = (String) value.get("agent"); protocolVersion = (String) value.get("protocol"); info = (JSONObject) value.get("info"); if (info == null) { info = new JSONObject(); } }
public JSONArray storeRecentSearch( String recentSearch ) { JSONArray recentSearches = getRecentSearches(); try { if ( recentSearch == null || recentSearches.contains( recentSearch ) ) { return recentSearches; } recentSearches.add( recentSearch ); if ( recentSearches.size() > 5 ) { recentSearches.remove( 0 ); } PropsUI props = PropsUI.getInstance(); String jsonValue = props.getRecentSearches(); JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = jsonValue != null ? (JSONObject) jsonParser.parse( jsonValue ) : new JSONObject(); jsonObject.put( getLogin(), recentSearches ); props.setRecentSearches( jsonObject.toJSONString() ); } catch ( Exception e ) { e.printStackTrace(); } return recentSearches; }
@Override public JSONObject joinMessages(Map<String, Tuple> streamMessageMap, MessageGetStrategy messageGetStrategy) { JSONObject message = new JSONObject(); for (String key : streamMessageMap.keySet()) { Tuple tuple = streamMessageMap.get(key); JSONObject obj = (JSONObject) messageGetStrategy.get(tuple); message.putAll(obj); } List<Object> emptyKeys = new ArrayList<>(); for(Object key : message.keySet()) { Object value = message.get(key); if(value == null || value.toString().length() == 0) { emptyKeys.add(key); } } for(Object o : emptyKeys) { message.remove(o); } message.put(getClass().getSimpleName().toLowerCase() + ".joiner.ts", "" + System.currentTimeMillis()); return message; }
JSONObject envJSON = (JSONObject) testCaseJSONObj.get("env"); JSONObject execJSON = (JSONObject) testCaseJSONObj.get("exec"); JSONObject preJSON = (JSONObject) testCaseJSONObj.get("pre"); JSONObject postJSON = new JSONObject(); if (testCaseJSONObj.containsKey("post")) { postJSON = (JSONObject) testCaseJSONObj.get("post"); JSONArray callCreates = new JSONArray(); if (testCaseJSONObj.containsKey("callcreates")) callCreates = (JSONArray) testCaseJSONObj.get("callcreates"); Object logsJSON = new JSONArray(); if (testCaseJSONObj.containsKey("logs")) logsJSON = testCaseJSONObj.get("logs");
@SuppressWarnings( "unchecked" ) private void outPutRow( Object[] rowData ) throws KettleStepException { // We can now output an object data.jg = new JSONObject(); data.jg.put( data.realBlocName, data.ja ); String value = data.jg.toJSONString(); if ( data.outputValue && data.outputRowMeta != null ) { Object[] outputRowData = RowDataUtil.addValueData( rowData, data.inputRowMetaSize, value ); incrementLinesOutput(); putRow( data.outputRowMeta, outputRowData ); } if ( data.writeToFile && !data.ja.isEmpty() ) { // Open a file if ( !openNewFile() ) { throw new KettleStepException( BaseMessages.getString( PKG, "JsonOutput.Error.OpenNewFile", buildFilename() ) ); } // Write data to file try { data.writer.write( value ); } catch ( Exception e ) { throw new KettleStepException( BaseMessages.getString( PKG, "JsonOutput.Error.Writing" ), e ); } // Close file closeFile(); } // Data are safe data.rowsAreSafe = true; data.ja = new JSONArray(); }
@SuppressWarnings("unchecked") private void addResult(JSONArray ja, SearchResult result) { JSONObject document = new JSONObject(); document.putAll(result.getFields()); document.put("docid", result.getDocId()); document.put("query_relevance_score", result.getScore()); for(Entry<Integer, Double> entry: result.getVariables().entrySet()) { document.put("variable_" + entry.getKey(), entry.getValue()); } for(Entry<String, String> entry: result.getCategories().entrySet()) { document.put("category_" + entry.getKey(), entry.getValue()); } ja.add(document); }
@SuppressWarnings({"unchecked"}) public JSONObject getJSONObject() { JSONObject errorMessage = new JSONObject(); errorMessage.put(Constants.GUID, UUID.randomUUID().toString()); errorMessage.put(Constants.SENSOR_TYPE, "error"); if (sensorTypes.size() == 1) { errorMessage.put(ErrorFields.FAILED_SENSOR_TYPE.getName(), sensorTypes.iterator().next()); } else { errorMessage .put(ErrorFields.FAILED_SENSOR_TYPE.getName(), new JSONArray().addAll(sensorTypes)); } errorMessage.put(ErrorFields.ERROR_TYPE.getName(), errorType.getType()); addMessageString(errorMessage); addStacktrace(errorMessage); addTimestamp(errorMessage); addHostname(errorMessage); addRawMessages(errorMessage); addErrorHash(errorMessage); return errorMessage; }
private static JSONObject join(JSONObject left, JSONObject right) { JSONObject message = new JSONObject(); message.putAll(left); message.putAll(right); List<Object> emptyKeys = new ArrayList<>(); for(Object key : message.keySet()) { Object value = message.get(key); if(value == null || value.toString().length() == 0) { emptyKeys.add(key); } } for(Object o : emptyKeys) { message.remove(o); } return message; }
/** * Saves to the file. */ @SuppressWarnings("unchecked") protected void save() { JSONArray array = new JSONArray(); for (BaseEntry entry : entries) { JSONObject obj = new JSONObject(); for (Entry<String, String> mapEntry : entry.write().entrySet()) { obj.put(mapEntry.getKey(), mapEntry.getValue()); } array.add(obj); } try (Writer writer = new FileWriter(file)) { array.writeJSONString(writer); } catch (Exception ex) { GlowServer.logger.log(Level.SEVERE, "Error writing to: " + file, ex); } }
public Number connect(Map conf, TopologyContext context) throws IOException, NoOutputException { JSONObject setupInfo = new JSONObject(); setupInfo.put("pidDir", context.getPIDDir()); setupInfo.put("conf", conf); setupInfo.put("context", context); writeMessage(setupInfo); return (Number) ((JSONObject) readMessage()).get("pid"); }
@Override public byte[] mapRecord(Tuple tuple) { JSONObject obj = new JSONObject(); if (this.columnFields != null) { for (String field : this.columnFields) { obj.put(field, tuple.getValueByField(field)); } } return obj.toJSONString().getBytes(); }
private TupleBasedDocument createDocument(JSONObject message, Tuple tuple, String sensorType, FieldNameConverter fieldNameConverter) { // transform the message fields to the source fields of the indexed document JSONObject source = new JSONObject(); for(Object k : message.keySet()){ copyField(k.toString(), message, source, fieldNameConverter); } // define the document id String guid = ConversionUtils.convert(source.get(Constants.GUID), String.class); if(guid == null) { LOG.warn("Missing '{}' field; document ID will be auto-generated.", Constants.GUID); } // define the document timestamp Long timestamp = null; Object value = source.get(TIMESTAMP.getName()); if(value != null) { timestamp = Long.parseLong(value.toString()); } else { LOG.warn("Missing '{}' field; timestamp will be set to system time.", TIMESTAMP.getName()); } return new TupleBasedDocument(source, guid, sensorType, timestamp, tuple); }
JSONObject jo = new JSONObject(); jo.put( outputField.getElementName(), data.inputRowMeta.getBoolean( row, data.fieldIndexes[i] ) ); break; case ValueMetaInterface.TYPE_INTEGER: jo.put( outputField.getElementName(), data.inputRowMeta.getInteger( row, data.fieldIndexes[i] ) ); break; case ValueMetaInterface.TYPE_NUMBER: jo.put( outputField.getElementName(), data.inputRowMeta.getNumber( row, data.fieldIndexes[i] ) ); break; case ValueMetaInterface.TYPE_BIGNUMBER: break; data.ja.add( jo );
Match gm = grok.match(originalMessage); gm.captures(); JSONObject message = new JSONObject(); message.putAll(gm.toMap()); errors.put(originalMessage, rte); } else { message.put("original_string", originalMessage); for (String timeField : timeFields) { String fieldValue = (String) message.get(timeField); if (fieldValue != null) { message.put(timeField, toEpoch(fieldValue)); message.put(Constants.Fields.TIMESTAMP.getName(), formatTimestamp(message.get(timestampField)));
private void createEmptyFile() { try { FileWriter fileWriter = new FileWriter(HISTORY_FILE); JSONObject root = new JSONObject(); root.put("messages", new JSONArray()); fileWriter.write(root.toJSONString()); fileWriter.flush(); fileWriter.close(); } catch (IOException e) { logger.error("Error during creating history file: ", e); } }
@SuppressWarnings( "unchecked" ) public String getDatabases() { JSONArray list = new JSONArray(); for ( int i = 0; i < repositoriesMeta.nrDatabases(); i++ ) { JSONObject databaseJSON = new JSONObject(); databaseJSON.put( "name", repositoriesMeta.getDatabase( i ).getName() ); list.add( databaseJSON ); } return list.toString(); }
JSONObject json = new JSONObject(); for ( Header header : headers ) { Object previousValue = json.get( header.getName() ); if ( previousValue == null ) { json.put( header.getName(), header.getValue() ); } else if ( previousValue instanceof List ) { List<String> list = (List<String>) previousValue; list.add( (String) previousValue ); list.add( header.getValue() ); json.put( header.getName(), list );