Java Code Examples for org.codehaus.jackson.JsonNode#size()
The following examples show how to use
org.codehaus.jackson.JsonNode#size() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CubeOperator.java From Cubert with Apache License 2.0 | 7 votes |
private static Object instantiateObject(Class<?> cls, JsonNode constructorArgs) throws InstantiationException, IllegalAccessException, IllegalArgumentException, SecurityException, InvocationTargetException, NoSuchMethodException { if (constructorArgs == null || constructorArgs.isNull()) return cls.newInstance(); Object[] args = new Object[constructorArgs.size()]; Class<?>[] argClasses = new Class[args.length]; for (int i = 0; i < args.length; i++) { args[i] = JsonUtils.asObject(constructorArgs.get(i)); argClasses[i] = args[i].getClass(); } return cls.getConstructor(argClasses).newInstance(args); }
Example 2
Source File: LampDevice.java From openhab1-addons with Eclipse Public License 2.0 | 6 votes |
public LampDevice(int deviceId, String deviceType, int deviceTypeID, JsonNode deviceJson) { super(deviceId, deviceType, deviceTypeID, deviceJson); JsonNode attributes = deviceJson.get("Attributes"); if (attributes.isArray()) { int attributesSize = attributes.size(); for (int j = 0; j < attributesSize; j++) { String attributeName = attributes.get(j).get("AttributeDisplayName").asText(); if (attributeName.contains("lightstate")) { int lightstate = attributes.get(j).get("Value").asInt(); deviceAttributes.put("UpdatedDate", attributes.get(j).get("UpdatedDate").asText()); this.state = getLampState(lightstate); logger.debug("Lamp DeviceID: {} DeviceType: {} Lightstate : {}", deviceId, deviceType, lightstate); break; } } } }
Example 3
Source File: GarageDoorDevice.java From openhab1-addons with Eclipse Public License 2.0 | 6 votes |
public GarageDoorDevice(int deviceId, String deviceType, int deviceTypeID, JsonNode deviceJson) { super(deviceId, deviceType, deviceTypeID, deviceJson); JsonNode attributes = deviceJson.get("Attributes"); if (attributes.isArray()) { int attributesSize = attributes.size(); for (int j = 0; j < attributesSize; j++) { String attributeName = attributes.get(j).get("AttributeDisplayName").asText(); if (attributeName.contains("doorstate")) { int doorstate = attributes.get(j).get("Value").asInt(); this.status = GarageDoorStatus.GetDoorStatus(doorstate); deviceAttributes.put("UpdatedDate", attributes.get(j).get("UpdatedDate").asText()); logger.debug("GarageDoorOpener DeviceID: {} DeviceType: {} Doorstate : {}", deviceId, deviceType, doorstate); break; } } } }
Example 4
Source File: MyqDeviceData.java From openhab1-addons with Eclipse Public License 2.0 | 6 votes |
/** * Constructor of the MyqDeviceData. * * @param rootNode * The Json node returned from the myq website. */ public MyqDeviceData(JsonNode rootNode) throws IOException { if (rootNode.has("Devices")) { JsonNode node = rootNode.get("Devices"); if (node.isArray()) { logger.debug("Chamberlain MyQ Devices:"); int arraysize = node.size(); for (int i = 0; i < arraysize; i++) { int deviceId = node.get(i).get("MyQDeviceId").asInt(); String deviceType = node.get(i).get("MyQDeviceTypeName").asText(); int deviceTypeId = node.get(i).get("MyQDeviceTypeId").asInt(); // GarageDoorOpener have MyQDeviceTypeId of 2,5,7,17 if (deviceTypeId == 2 || deviceTypeId == 5 || deviceTypeId == 7 || deviceTypeId == 17) { devices.add(new GarageDoorDevice(deviceId, deviceType, deviceTypeId, node.get(i))); } else if (deviceTypeId == 3) { // Light have MyQDeviceTypeId of 3 devices.add(new LampDevice(deviceId, deviceType, deviceTypeId, node.get(i))); } } } } }
Example 5
Source File: GenerateOperator.java From Cubert with Apache License 2.0 | 6 votes |
private static BlockSchema buildFunctionTree(FunctionTree tree, JsonNode json) throws PreconditionException { int numOutputColumns = json.size(); ColumnType[] outputTypes = new ColumnType[numOutputColumns]; for (int i = 0; i < numOutputColumns; i++) { JsonNode outputColJson = json.get(i); String colName = JsonUtils.getText(outputColJson, "col_name"); tree.addFunctionTree(outputColJson.get("expression")); ColumnType type = tree.getType(i); ColumnType copy = new ColumnType(colName, type.getType()); copy.setColumnSchema(type.getColumnSchema()); outputTypes[i] = copy; } return new BlockSchema(outputTypes); }
Example 6
Source File: GenericDataTSV.java From iow-hadoop-streaming with Apache License 2.0 | 6 votes |
private Array<java.io.Serializable> createArray(Schema type, String t) throws IOException, JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); JsonNode node = mapper.readTree(t); Iterator <JsonNode> i = node.iterator(); Array<java.io.Serializable> arr = new GenericData.Array<java.io.Serializable>(node.size(), Schema.createArray(type)); while(i.hasNext()) { switch (type.getType()) { case INT: arr.add(i.next().getIntValue()); break; case FLOAT: case DOUBLE: arr.add(i.next().getDoubleValue()); break; default: arr.add(i.next().getTextValue()); // No array-of-objects! } } return arr; }
Example 7
Source File: JSONRPCParser.java From bitcoin-transaction-explorer with MIT License | 5 votes |
public static ArrayList<String> getTransactionList(final InputStream jsonData) throws JsonProcessingException, IOException { final JsonNode tree = getResultNode(jsonData); // Array of txs final ArrayList<String> txs = new ArrayList<>(); final JsonNode txNode = tree.get("tx"); for (int i = 0; i < txNode.size(); i++) { txs.add(txNode.get(i).getTextValue()); } return txs; }
Example 8
Source File: GeoJsonParser.java From arcgis-runtime-demo-java with Apache License 2.0 | 5 votes |
/** * Parses a point * Example: * [101.0, 0.0]. * @param parser * @return a point. * @throws Exception */ private Point parsePointCoordinates(JsonNode node) { Point p = new Point(); p.setXY(node.get(0).asDouble(), node.get(1).asDouble()); if (node.size() == 3) { p.setZ(node.get(2).asDouble()); } return p; }
Example 9
Source File: AssignableInstanceManager.java From helix with Apache License 2.0 | 5 votes |
public void logQuotaProfileJSON(boolean onlyDisplayIfFull) { // Create a String to use as the log for quota status ObjectMapper mapper = new ObjectMapper(); JsonNode instanceNode = mapper.createObjectNode(); // Loop through all instances for (Map.Entry<String, AssignableInstance> instanceEntry : _assignableInstanceMap.entrySet()) { AssignableInstance assignableInstance = instanceEntry.getValue(); boolean capacityFull = false; JsonNode resourceTypeNode = mapper.createObjectNode(); for (Map.Entry<String, Map<String, Integer>> capacityEntry : assignableInstance .getTotalCapacity().entrySet()) { String resourceType = capacityEntry.getKey(); Map<String, Integer> quotaTypeMap = capacityEntry.getValue(); JsonNode quotaTypeNode = mapper.createObjectNode(); for (Map.Entry<String, Integer> typeEntry : quotaTypeMap.entrySet()) { String quotaType = typeEntry.getKey(); int totalCapacity = typeEntry.getValue(); int usedCapacity = assignableInstance.getUsedCapacity().get(resourceType).get(quotaType); if (!capacityFull) { capacityFull = totalCapacity <= usedCapacity; } String capacityString = String.format("%d/%d", usedCapacity, totalCapacity); ((ObjectNode) quotaTypeNode).put(quotaType, capacityString); } ((ObjectNode) resourceTypeNode).put(resourceType, quotaTypeNode); } // If onlyDisplayIfFull, do not add the JsonNode to the parent node if (onlyDisplayIfFull && !capacityFull) { continue; } ((ObjectNode) instanceNode).put(instanceEntry.getKey(), resourceTypeNode); } if (instanceNode.size() > 0) { LOG.info("Current quota capacity: {}", instanceNode.toString()); } }
Example 10
Source File: BitMEXExchange.java From libdynticker with GNU Lesser General Public License v3.0 | 5 votes |
@Override protected String getTicker(Pair pair) throws IOException { JsonNode node = readJsonFromUrl(API_URL + "/instrument?symbol=" + pair.getMarket() + "&columns=lastPrice"); if(node.size() != 1) throw new NoMarketDataException(pair); return parseTicker(node, pair); }
Example 11
Source File: GatecoinExchange.java From libdynticker with GNU Lesser General Public License v3.0 | 5 votes |
@Override public String parseTicker(JsonNode node, Pair pair) throws IOException { JsonNode transactions = node.get("transactions"); if(transactions.size() > 0) return transactions.get(0).get("price").asText(); else return "NaN"; }
Example 12
Source File: CoinsBankExchange.java From libdynticker with GNU Lesser General Public License v3.0 | 5 votes |
@Override protected String getTicker(Pair pair) throws IOException { // https://coinsbank.com/api/bitcoincharts/trades/EURAUD JsonNode node = readJsonFromUrl( "https://coinsbank.com/api/bitcoincharts/trades/" + pair.getCoin() + pair.getExchange()); if(node.size() > 0) return parseTicker(node, pair); else throw new IOException(); }
Example 13
Source File: CustomFieldDeSerializer.java From jira-rest-client with Apache License 2.0 | 5 votes |
@Override public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { logger.info("Deserialize..."); ObjectCodec oc = jp.getCodec(); JsonNode node = oc.readTree(jp); for (int i = 0; i < node.size(); i++) { JsonNode child = node.get(i); if (child == null) { logger.info(i + "th Child node is null"); continue; } //String if (child.isTextual()) { Iterator<String> it = child.getFieldNames(); while (it.hasNext()) { String field = it.next(); logger.info("in while loop " + field); if (field.startsWith("customfield")) { } } } } return null; }
Example 14
Source File: BlockSchema.java From Cubert with Apache License 2.0 | 5 votes |
public BlockSchema(JsonNode json) { int ncols = json.size(); columnTypes = new ColumnType[ncols]; for (int i = 0; i < ncols; i++) columnTypes[i] = new ColumnType(json.get(i)); }
Example 15
Source File: ClusterStatusTask.java From exhibitor with Apache License 2.0 | 5 votes |
private ServerStatus getStatus(ServerSpec spec) { if ( spec.equals(us) ) { InstanceStateTypes state = exhibitor.getMonitorRunningInstance().getCurrentInstanceState(); return new ServerStatus(spec.getHostname(), state.getCode(), state.getDescription(), exhibitor.getMonitorRunningInstance().isCurrentlyLeader()); } try { RemoteInstanceRequest request = new RemoteInstanceRequest(exhibitor, spec.getHostname()); RemoteInstanceRequest.Result result = request.makeRequest(exhibitor.getRemoteInstanceRequestClient(), "getStatus"); ObjectMapper mapper = new ObjectMapper(); JsonNode value = mapper.readTree(mapper.getJsonFactory().createJsonParser(result.remoteResponse)); if ( value.size() == 0 ) { return new ServerStatus(spec.getHostname(), InstanceStateTypes.DOWN.getCode(), InstanceStateTypes.DOWN.getDescription(), false); } int code = value.get("state").getValueAsInt(); String description = value.get("description").getTextValue(); return new ServerStatus(spec.getHostname(), code, description, value.get("isLeader").getBooleanValue()); } catch ( IOException e ) { log.error("Getting remote server status", e); throw new RuntimeException(e); } }
Example 16
Source File: MLOBIInboundTransport.java From defense-solutions-proofs-of-concept with Apache License 2.0 | 5 votes |
private void updateLastTimestamp(JsonNode root) throws ParseException { JsonNode features = root.get("features"); String format ="yyyy-MM-dd'T'HH:mm:ss.SSS"; if (features.isArray()) { for (int i = 0; i < features.size(); i++) { JsonNode feature = features.get(i); JsonNode attributes = feature.get("attributes"); JsonNode time = attributes.get("LastUpdatedDateTime"); if (feature.get("attributes").get("LastUpdatedDateTime") != null) { String timeString = time.toString(); timeString = timeString.substring(1, timeString.length()-4); TimeZone tz = TimeZone.getTimeZone("UTC"); SimpleDateFormat dateFormat = new SimpleDateFormat(format); dateFormat.setTimeZone(tz); Date d = dateFormat.parse(timeString); long ts = d.getTime(); if (ts > lastTimestamp) lastTimestamp = ts; } else { LOGGER.warn("NO_TIME_FIELD_FOUND", newFeaturesTimeFieldName, root.toString()); } } } }
Example 17
Source File: ScrollElasticsearchHttp.java From localization_nifi with Apache License 2.0 | 4 votes |
private void getPage(final Response getResponse, final URL url, final ProcessContext context, final ProcessSession session, FlowFile flowFile, final ComponentLog logger, final long startNanos) throws IOException { final int statusCode = getResponse.code(); if (isSuccess(statusCode)) { ResponseBody body = getResponse.body(); final byte[] bodyBytes = body.bytes(); JsonNode responseJson = parseJsonResponse(new ByteArrayInputStream(bodyBytes)); String scrollId = responseJson.get("_scroll_id").asText(); StringBuilder builder = new StringBuilder(); builder.append("{ \"hits\" : ["); JsonNode hits = responseJson.get("hits").get("hits"); if (hits.size() == 0) { finishQuery(context.getStateManager()); session.remove(flowFile); return; } for(int i = 0; i < hits.size(); i++) { JsonNode hit = hits.get(i); String retrievedIndex = hit.get("_index").asText(); String retrievedType = hit.get("_type").asText(); JsonNode source = hit.get("_source"); flowFile = session.putAttribute(flowFile, "es.index", retrievedIndex); flowFile = session.putAttribute(flowFile, "es.type", retrievedType); flowFile = session.putAttribute(flowFile, "mime.type", "application/json"); builder.append(source.toString()); if (i < hits.size() - 1) { builder.append(", "); } } builder.append("] }"); logger.debug("Elasticsearch retrieved " + responseJson.size() + " documents, routing to success"); flowFile = session.write(flowFile, out -> { out.write(builder.toString().getBytes()); }); session.transfer(flowFile, REL_SUCCESS); saveScrollId(context.getStateManager(), scrollId); // emit provenance event final long millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos); session.getProvenanceReporter().receive(flowFile, url.toExternalForm(), millis); } else { // 5xx -> RETRY, but a server error might last a while, so yield if (statusCode / 100 == 5) { logger.warn("Elasticsearch returned code {} with message {}, removing the flow file. This is likely a server problem, yielding...", new Object[]{statusCode, getResponse.message()}); session.remove(flowFile); context.yield(); } else { logger.warn("Elasticsearch returned code {} with message {}", new Object[]{statusCode, getResponse.message()}); session.remove(flowFile); } } }
Example 18
Source File: ShuffleRewriter.java From Cubert with Apache License 2.0 | 4 votes |
private JsonNode rewriteJoin(JsonNode job) { ObjectNode newJob = (ObjectNode) cloneNode(job); ObjectNode shuffle = (ObjectNode) newJob.get("shuffle"); JsonNode joinKeys = shuffle.get("joinKeys"); String blockName = getText(shuffle, "name"); // make sure there are two mappers in the job JsonNode mapJsons = newJob.get("map"); if (mapJsons.size() != 2) { throw new RuntimeException("There must be exactly two multimappers for JOIN shuffle command."); } // Add the Map side operator in each of the mappers // tag = 1, for the first mapper (non dimensional) // tag = 0, for the second dimensional mapper int tag = 1; for (JsonNode mapJson: mapJsons) { if (!mapJson.has("operators") || mapJson.get("operators").isNull()) ((ObjectNode) mapJson).put("operators", mapper.createArrayNode()); ArrayNode operators = (ArrayNode) mapJson.get("operators"); // we need unique references for all blockIndexJoin operators.add(createObjectNode("operator", "REDUCE_JOIN_MAPPER", "input", createArrayNode(blockName), "output", blockName, "joinKeys", joinKeys, "tag", tag)); tag --; } // create the reduce side operator ObjectNode reducerOperator = createObjectNode("operator", "REDUCE_JOIN", "input", createArrayNode(blockName), "output", blockName, "joinKeys", joinKeys); if (shuffle.has("joinType")) reducerOperator.put("joinType", shuffle.get("joinType")); // add the reduce side operator if (!newJob.has("reduce") || newJob.get("reduce").isNull()) { newJob.put("reduce", mapper.createArrayNode()); } ArrayNode reduce = (ArrayNode) newJob.get("reduce"); reduce.insert(0, reducerOperator); // Fix the shuffle json if (shuffle.has("partitionKeys")) { String[] partitionKeys = JsonUtils.asArray(shuffle, "partitionKeys"); String[] joinKeyNames = JsonUtils.asArray(shuffle, "joinKeys"); // make sure that partitionKeys is prefix of joinKeys if (!CommonUtils.isPrefix(joinKeyNames, partitionKeys)) { throw new RuntimeException("Partition key must be a prefix of join keys"); } } else { shuffle.put("partitionKeys", shuffle.get("joinKeys")); } // We will sort on (joinKeys + ___tag) JsonNode pivotKeys = cloneNode(shuffle.get("joinKeys")); ((ArrayNode) pivotKeys).add("___tag"); shuffle.put("type", "SHUFFLE"); shuffle.put("join", true); shuffle.put("pivotKeys", pivotKeys); shuffle.remove("joinKeys"); return newJob; }
Example 19
Source File: CubeDimensions.java From Cubert with Apache License 2.0 | 4 votes |
public CubeDimensions(BlockSchema inputSchema, BlockSchema outputSchema, String[] dimensions, JsonNode groupingSetsJson) { // create the arrays inputIndex = new int[dimensions.length]; outputIndex = new int[dimensions.length]; dimensionTypes = new DataType[dimensions.length]; dimensionOffsets = new int[dimensions.length]; dictionaries = new CodeDictionary[dimensions.length]; // intialize the above arrays int idx = 0; int offset = 0; for (String dim : dimensions) { inputIndex[idx] = inputSchema.getIndex(dim); outputIndex[idx] = outputSchema.getIndex(dim); dimensionTypes[idx] = inputSchema.getType(inputIndex[idx]); dimensionOffsets[idx] = offset; offset++; // pad one more int if the data type is long ("encoded" as 2 ints) if (dimensionTypes[idx] == DataType.LONG) offset++; // create dictionary if the dimension is string if (dimensionTypes[idx] == DataType.STRING) dictionaries[idx] = new CodeDictionary(); idx++; } // the "last" int in the dimension key is used to store the null bit vector nullBitVectorIndex = offset; // create the dimension key key = new DimensionKey(nullBitVectorIndex); key.getArray()[nullBitVectorIndex] = 0; // determine if this is a full cube isFullCube = (groupingSetsJson == null) || (groupingSetsJson.isNull()) || groupingSetsJson.size() == 0; // determine the number of ancestors if (isFullCube) numAncestors = (int) Math.pow(2, dimensions.length); else numAncestors = groupingSetsJson.size(); // allocate the ancestors ancestors = new DimensionKey[numAncestors]; for (int i = 0; i < numAncestors; i++) ancestors[i] = new DimensionKey(nullBitVectorIndex); // pre-assign null bit vector for the ancestors assignNullBitVector(dimensions, groupingSetsJson); // assign zeroedDimIndex for the ancestors zeroedDimArrayLength = new int[numAncestors]; zeroedDimIndex = new byte[numAncestors][64]; assignZeroedDimensions(dimensions); }
Example 20
Source File: QueryElasticsearchHttp.java From localization_nifi with Apache License 2.0 | 4 votes |
private int getPage(final Response getResponse, final URL url, final ProcessContext context, final ProcessSession session, FlowFile flowFile, final ComponentLog logger, final long startNanos, boolean targetIsContent) throws IOException { List<FlowFile> page = new ArrayList<>(); final int statusCode = getResponse.code(); if (isSuccess(statusCode)) { ResponseBody body = getResponse.body(); final byte[] bodyBytes = body.bytes(); JsonNode responseJson = parseJsonResponse(new ByteArrayInputStream(bodyBytes)); JsonNode hits = responseJson.get("hits").get("hits"); for(int i = 0; i < hits.size(); i++) { JsonNode hit = hits.get(i); String retrievedId = hit.get("_id").asText(); String retrievedIndex = hit.get("_index").asText(); String retrievedType = hit.get("_type").asText(); FlowFile documentFlowFile = null; if (flowFile != null) { documentFlowFile = targetIsContent ? session.create(flowFile) : session.clone(flowFile); } else { documentFlowFile = session.create(); } JsonNode source = hit.get("_source"); documentFlowFile = session.putAttribute(documentFlowFile, "es.id", retrievedId); documentFlowFile = session.putAttribute(documentFlowFile, "es.index", retrievedIndex); documentFlowFile = session.putAttribute(documentFlowFile, "es.type", retrievedType); if (targetIsContent) { documentFlowFile = session.putAttribute(documentFlowFile, "filename", retrievedId); documentFlowFile = session.putAttribute(documentFlowFile, "mime.type", "application/json"); documentFlowFile = session.write(documentFlowFile, out -> { out.write(source.toString().getBytes()); }); } else { Map<String, String> attributes = new HashMap<>(); for(Iterator<Entry<String, JsonNode>> it = source.getFields(); it.hasNext(); ) { Entry<String, JsonNode> entry = it.next(); attributes.put(ATTRIBUTE_PREFIX + entry.getKey(), entry.getValue().asText()); } documentFlowFile = session.putAllAttributes(documentFlowFile, attributes); } page.add(documentFlowFile); } logger.debug("Elasticsearch retrieved " + responseJson.size() + " documents, routing to success"); session.transfer(page, REL_SUCCESS); } else { try { // 5xx -> RETRY, but a server error might last a while, so yield if (statusCode / 100 == 5) { throw new RetryableException(String.format("Elasticsearch returned code %s with message %s, transferring flow file to retry. This is likely a server problem, yielding...", statusCode, getResponse.message())); } else if (context.hasIncomingConnection()) { // 1xx, 3xx, 4xx -> NO RETRY throw new UnretryableException(String.format("Elasticsearch returned code %s with message %s, transferring flow file to failure", statusCode, getResponse.message())); } else { logger.warn("Elasticsearch returned code {} with message {}", new Object[]{statusCode, getResponse.message()}); } } finally { if (!page.isEmpty()) { session.remove(page); page.clear(); } } } // emit provenance event final long millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos); if (!page.isEmpty()) { if (context.hasNonLoopConnection()) { page.forEach(f -> session.getProvenanceReporter().fetch(f, url.toExternalForm(), millis)); } else { page.forEach(f -> session.getProvenanceReporter().receive(f, url.toExternalForm(), millis)); } } return page.size(); }