List of usage examples for com.google.gson JsonObject getAsJsonArray
public JsonArray getAsJsonArray(String memberName)
From source file:gobblin.example.wikipedia.WikipediaExtractor.java
License:Apache License
private Queue<JsonElement> retrievePageRevisions(Map<String, String> query) throws IOException, URISyntaxException { Queue<JsonElement> retrievedRevisions = new LinkedList<>(); JsonElement jsonElement = performHttpQuery(this.rootUrl, query); if (jsonElement == null || !jsonElement.isJsonObject()) { return retrievedRevisions; }//from ww w .j a v a 2s . com JsonObject jsonObj = jsonElement.getAsJsonObject(); if (jsonObj == null || !jsonObj.has(JSON_MEMBER_QUERY)) { return retrievedRevisions; } JsonObject queryObj = jsonObj.getAsJsonObject(JSON_MEMBER_QUERY); if (!queryObj.has(JSON_MEMBER_PAGES)) { return retrievedRevisions; } JsonObject pagesObj = queryObj.getAsJsonObject(JSON_MEMBER_PAGES); if (pagesObj.entrySet().isEmpty()) { return retrievedRevisions; } JsonObject pageIdObj = pagesObj.getAsJsonObject(pagesObj.entrySet().iterator().next().getKey()); if (!pageIdObj.has(JSON_MEMBER_REVISIONS)) { return retrievedRevisions; } //retrieve revisions of the current pageTitle JsonArray jsonArr = pageIdObj.getAsJsonArray(JSON_MEMBER_REVISIONS); for (JsonElement revElement : jsonArr) { JsonObject revObj = revElement.getAsJsonObject(); /*'pageid' and 'title' are associated with the parent object * of all revisions. Add them to each individual revision. */ if (pageIdObj.has(JSON_MEMBER_PAGEID)) { revObj.add(JSON_MEMBER_PAGEID, pageIdObj.get(JSON_MEMBER_PAGEID)); } if (pageIdObj.has(JSON_MEMBER_TITLE)) { revObj.add(JSON_MEMBER_TITLE, pageIdObj.get(JSON_MEMBER_TITLE)); } retrievedRevisions.add(revObj); } LOG.info(retrievedRevisions.size() + " record(s) retrieved for title " + this.requestedTitle); return retrievedRevisions; }
From source file:gobblin.salesforce.SalesforceExtractor.java
License:Apache License
@Override public JsonArray getSchema(CommandOutput<?, ?> response) throws SchemaException { log.info("Get schema from salesforce"); String output;//from ww w. j av a 2s. com Iterator<String> itr = (Iterator<String>) response.getResults().values().iterator(); if (itr.hasNext()) { output = itr.next(); } else { throw new SchemaException("Failed to get schema from salesforce; REST response has no output"); } JsonArray fieldJsonArray = new JsonArray(); JsonElement element = GSON.fromJson(output, JsonObject.class); JsonObject jsonObject = element.getAsJsonObject(); try { JsonArray array = jsonObject.getAsJsonArray("fields"); for (JsonElement columnElement : array) { JsonObject field = columnElement.getAsJsonObject(); Schema schema = new Schema(); schema.setColumnName(field.get("name").getAsString()); String dataType = field.get("type").getAsString(); String elementDataType = "string"; List<String> mapSymbols = null; JsonObject newDataType = this.convertDataType(field.get("name").getAsString(), dataType, elementDataType, mapSymbols); log.debug("ColumnName:" + field.get("name").getAsString() + "; old datatype:" + dataType + "; new datatype:" + newDataType); schema.setDataType(newDataType); schema.setLength(field.get("length").getAsLong()); schema.setPrecision(field.get("precision").getAsInt()); schema.setScale(field.get("scale").getAsInt()); schema.setNullable(field.get("nillable").getAsBoolean()); schema.setFormat(null); schema.setComment((field.get("label").isJsonNull() ? null : field.get("label").getAsString())); schema.setDefaultValue( (field.get("defaultValue").isJsonNull() ? null : field.get("defaultValue").getAsString())); schema.setUnique(field.get("unique").getAsBoolean()); String jsonStr = GSON.toJson(schema); JsonObject obj = GSON.fromJson(jsonStr, JsonObject.class).getAsJsonObject(); fieldJsonArray.add(obj); } } catch (Exception e) { throw new SchemaException("Failed to get schema from salesforce; error - " + e.getMessage(), e); } return fieldJsonArray; }
From source file:gobblin.salesforce.SalesforceExtractor.java
License:Apache License
@Override public long getHighWatermark(CommandOutput<?, ?> response, String watermarkColumn, String format) throws HighWatermarkException { log.info("Get high watermark from salesforce"); String output;/*w ww. j a v a 2s. c om*/ Iterator<String> itr = (Iterator<String>) response.getResults().values().iterator(); if (itr.hasNext()) { output = itr.next(); } else { throw new HighWatermarkException( "Failed to get high watermark from salesforce; REST response has no output"); } JsonElement element = GSON.fromJson(output, JsonObject.class); long high_ts; try { JsonObject jsonObject = element.getAsJsonObject(); JsonArray jsonArray = jsonObject.getAsJsonArray("records"); if (jsonArray.size() == 0) { return -1; } String value = jsonObject.getAsJsonArray("records").get(0).getAsJsonObject().get(watermarkColumn) .getAsString(); if (format != null) { SimpleDateFormat inFormat = new SimpleDateFormat(format); Date date = null; try { date = inFormat.parse(value); } catch (ParseException e) { log.error("ParseException: " + e.getMessage(), e); } SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss"); high_ts = Long.parseLong(outFormat.format(date)); } else { high_ts = Long.parseLong(value); } } catch (Exception e) { throw new HighWatermarkException( "Failed to get high watermark from salesforce; error - " + e.getMessage(), e); } return high_ts; }
From source file:gobblin.salesforce.SalesforceExtractor.java
License:Apache License
@Override public Iterator<JsonElement> getData(CommandOutput<?, ?> response) throws DataRecordException { log.debug("Get data records from response"); String output;/*from w w w . j ava 2 s . c o m*/ Iterator<String> itr = (Iterator<String>) response.getResults().values().iterator(); if (itr.hasNext()) { output = itr.next(); } else { throw new DataRecordException("Failed to get data from salesforce; REST response has no output"); } List<JsonElement> rs = Lists.newArrayList(); JsonElement element = GSON.fromJson(output, JsonObject.class); JsonArray partRecords; try { JsonObject jsonObject = element.getAsJsonObject(); partRecords = jsonObject.getAsJsonArray("records"); if (jsonObject.get("done").getAsBoolean()) { setPullStatus(false); } else { setNextUrl(this.sfConnector.getFullUri(jsonObject.get("nextRecordsUrl").getAsString() .replaceAll(this.sfConnector.getServicesDataEnvPath(), ""))); } JsonArray array = Utils.removeElementFromJsonArray(partRecords, "attributes"); Iterator<JsonElement> li = array.iterator(); while (li.hasNext()) { JsonElement recordElement = li.next(); rs.add(recordElement); } return rs.iterator(); } catch (Exception e) { throw new DataRecordException("Failed to get records from salesforce; error - " + e.getMessage(), e); } }
From source file:gobblin.salesforce.SalesforceSource.java
License:Apache License
private static Set<SourceEntity> getSourceEntities(String response) { Set<SourceEntity> result = Sets.newHashSet(); JsonObject jsonObject = new Gson().fromJson(response, JsonObject.class).getAsJsonObject(); JsonArray array = jsonObject.getAsJsonArray("sobjects"); for (JsonElement element : array) { String sourceEntityName = element.getAsJsonObject().get("name").getAsString(); result.add(SourceEntity.fromSourceEntityName(sourceEntityName)); }//from ww w . java 2 s.co m return result; }
From source file:gsonTests.jsonTojava.ParseTreeExample6.java
public static void main(String[] args) throws MalformedURLException, IOException { String url = "http://freemusicarchive.org/api/get/albums.json?api_key=60BLHNQCAOUFPIBZ&limit=5"; String json = IOUtils.toString(new URL(url)); JsonParser parser = new JsonParser(); // The JsonElement is the root node. It can be an object, array, null or // java primitive. JsonElement element = parser.parse(json); // use the isxxx methods to find out the type of jsonelement. In our // example we know that the root object is the Albums object and // contains an array of dataset objects if (element.isJsonObject()) { JsonObject albums = element.getAsJsonObject(); System.out.println(albums.get("title").getAsString()); JsonArray datasets = albums.getAsJsonArray("dataset"); for (int i = 0; i < datasets.size(); i++) { JsonObject dataset = datasets.get(i).getAsJsonObject(); System.out.println(dataset.get("album_title").getAsString()); }/*from w w w. ja v a 2 s.c om*/ } }
From source file:guru.qas.martini.report.column.ExceptionColumn.java
License:Apache License
@Override public void addResult(State state, Cell cell, JsonObject o) { JsonArray array = o.getAsJsonArray(KEY_STEPS); int size = array.size(); String value = null;//from w ww. j a va 2s. c o m for (int i = 0; null == value && i < size; i++) { JsonElement element = array.get(i); JsonObject step = element.getAsJsonObject(); JsonElement keyElement = step.get(KEY_EXCEPTION); JsonPrimitive primitive = null != keyElement && keyElement.isJsonPrimitive() ? keyElement.getAsJsonPrimitive() : null; String stackTrace = null == primitive ? null : primitive.getAsString().trim(); value = null != stackTrace && !stackTrace.isEmpty() ? stackTrace : null; } RichTextString richTextString = new XSSFRichTextString(value); cell.setCellValue(richTextString); }
From source file:guru.qas.martini.report.column.TagColumn.java
License:Apache License
@Override public void addResult(State state, Cell cell, JsonObject o) { JsonArray array = o.getAsJsonArray(KEY_TAGS); if (null != array) { addResult(cell, array);/* w w w. j a va2 s . co m*/ } }
From source file:guru.qas.martini.report.column.ThemeColumn.java
License:Apache License
@Override public void addResult(State state, Cell cell, JsonObject o) { JsonArray categories = o.getAsJsonArray(KEY); if (null != categories) { addResult(state, cell, categories); }/*from w w w. j av a 2 s . c o m*/ }
From source file:guru.qas.martini.report.DefaultState.java
License:Apache License
@Override public void updateSuites(Sheet sheet) { int lastRowNum = sheet.getLastRowNum(); Row row = sheet.createRow(0 == lastRowNum ? 0 : lastRowNum + 1); row.createCell(0, CellType.STRING).setCellValue("ID"); row.createCell(1, CellType.STRING).setCellValue("Date"); row.createCell(2, CellType.STRING).setCellValue("Name"); row.createCell(3, CellType.STRING).setCellValue("Hostname"); row.createCell(4, CellType.STRING).setCellValue("IP"); row.createCell(5, CellType.STRING).setCellValue("Username"); row.createCell(6, CellType.STRING).setCellValue("Profiles"); row.createCell(7, CellType.STRING).setCellValue("Environment Variables"); for (Map.Entry<String, JsonObject> mapEntry : suites.entrySet()) { row = sheet.createRow(sheet.getLastRowNum() + 1); String id = mapEntry.getKey(); row.createCell(0, CellType.STRING).setCellValue(id); JsonObject suite = mapEntry.getValue(); JsonPrimitive primitive = suite.getAsJsonPrimitive("startTimestamp"); Long timestamp = null == primitive ? null : primitive.getAsLong(); Cell cell = row.createCell(1);// w w w .j av a 2s .c o m if (null != timestamp) { Workbook workbook = sheet.getWorkbook(); CellStyle cellStyle = workbook.createCellStyle(); CreationHelper creationHelper = workbook.getCreationHelper(); cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("m/d/yy h:mm")); cellStyle.setVerticalAlignment(VerticalAlignment.TOP); cell.setCellValue(new Date(timestamp)); cell.setCellStyle(cellStyle); } cell = row.createCell(2); primitive = suite.getAsJsonPrimitive("name"); cell.setCellValue(null == primitive ? "" : primitive.getAsString()); cell = row.createCell(3); JsonObject host = suite.getAsJsonObject("host"); primitive = null == host ? null : host.getAsJsonPrimitive("name"); cell.setCellValue(null == primitive ? "" : primitive.getAsString()); cell = row.createCell(4); primitive = null == host ? null : host.getAsJsonPrimitive("ip"); cell.setCellValue(null == primitive ? "" : primitive.getAsString()); cell = row.createCell(5); primitive = null == host ? null : host.getAsJsonPrimitive("username"); cell.setCellValue(null == primitive ? "" : primitive.getAsString()); cell = row.createCell(6); JsonArray array = suite.getAsJsonArray("profiles"); List<String> profiles = Lists.newArrayList(); if (null != array) { int size = array.size(); for (int i = 0; i < size; i++) { JsonElement element = array.get(i); String profile = null == element ? null : element.getAsString(); profiles.add(profile); } String profilesValue = Joiner.on('\n').skipNulls().join(profiles); cell.setCellValue(profilesValue); } cell = row.createCell(7); JsonObject environmentVariables = suite.getAsJsonObject("environment"); Map<String, String> index = new TreeMap<>(); if (null != environmentVariables) { Set<Map.Entry<String, JsonElement>> entries = environmentVariables.entrySet(); for (Map.Entry<String, JsonElement> environmentEntry : entries) { String key = environmentEntry.getKey(); JsonElement element = environmentEntry.getValue(); String value = null == element ? "" : element.getAsString(); index.put(key, value); } String variablesValue = Joiner.on('\n').withKeyValueSeparator('=').useForNull("").join(index); cell.setCellValue(variablesValue); } } for (int i = 0; i < 8; i++) { sheet.autoSizeColumn(i, false); } }