Java Code Examples for org.codehaus.jackson.JsonNode#isArray()

The following examples show how to use org.codehaus.jackson.JsonNode#isArray() . 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: MyqDevice.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
public MyqDevice(int deviceId, String deviceType, int deviceTypeID, JsonNode deviceJson) {
    this.deviceId = deviceId;
    this.deviceType = deviceType;
    this.deviceTypeID = deviceTypeID;

    deviceAttributes.put("MyQDeviceId", Integer.toString(deviceId));
    deviceAttributes.put("MyQDeviceTypeName", deviceType);
    deviceAttributes.put("MyQDeviceTypeId", Integer.toString(deviceTypeID));
    deviceAttributes.put("SerialNumber", deviceJson.get("SerialNumber").asText());

    JsonNode otherAttributes = deviceJson.get("Attributes");
    if (otherAttributes.isArray()) {
        int attributesSize = otherAttributes.size();
        for (int j = 0; j < attributesSize; j++) {
            String attributeName = otherAttributes.get(j).get("AttributeDisplayName").asText();
            String attributeValue = otherAttributes.get(j).get("Value").asText();
            logger.trace("AttributeName: {} AttributeValue: {}", attributeName, attributeValue);
            deviceAttributes.put(attributeName, attributeValue);
        }
    }
}
 
Example 2
Source File: LampDevice.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
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: JsonUtils.java    From Cubert with Apache License 2.0 6 votes vote down vote up
public static String[] asArray(JsonNode node)
{
    if (node == null)
        throw new IllegalArgumentException("Specified JsonNode is null");

    if (node.isArray())
    {
        ArrayNode anode = (ArrayNode) node;
        int nelements = anode.size();
        String[] array = new String[nelements];
        for (int i = 0; i < nelements; i++)
        {
            array[i] = anode.get(i).getTextValue();
        }
        return array;
    }
    else
    {
        return new String[] { node.getTextValue() };
    }
}
 
Example 4
Source File: BlockgenLineageAnalyzer.java    From Cubert with Apache License 2.0 6 votes vote down vote up
@Override
public void visitInput(JsonNode json)
{
    JsonNode pathJson = json.get("path");
    String path;
    if (pathJson.isArray())
        path = JsonUtils.encodePath(pathJson.get(0));
    else
        path = JsonUtils.encodePath(pathJson);

    // blockgenId related
    if (getText(json, "type").equalsIgnoreCase("RUBIX"))
    {
        currentBlockgenId = this.blockgenIdMap.get(path);
        if (currentBlockgenId == null)
            error(json,
                  "Attempting to load a rubix file that was not created by BLOCKGEN or BLOCKGEN BY INDEX");
    }
}
 
Example 5
Source File: APIDashboardTask.java    From kardio with Apache License 2.0 6 votes vote down vote up
/**
 * @param envInfo
 * @param appName
 * @return
 * @throws Exception
 */
private static String getMarathonAppLaunchDate(EnvironmentVO envInfo,String appName) throws Exception {
	String appVerUrl = envInfo.getMarathonUrl()+"//" + appName + "/versions";
	String appVersionJson = null;
	try{
		appVersionJson = RestCommunicationHandler.getResponse(appVerUrl, true, "Basic ", envInfo.getMarathonCred());
	}catch(FileNotFoundException ex){
		logger.error("Unable to get Version of API : AppName: " + appName + "; URL :"+ appVerUrl, ex);
	}

	if(appVersionJson == null){
		return null;
	}
	ObjectMapper mapper = new ObjectMapper();
	JsonNode rootNode = mapper.readTree(appVersionJson).get("versions");
	String launchDate = null; 
	if(rootNode.isArray()){
		launchDate = rootNode.get(rootNode.size() - 1).asText();
	} else {
		logger.info("Launch Date for "+ appName +" is null");
	}
	return launchDate;
}
 
Example 6
Source File: JsonRestSourceHandler.java    From ingestion with Apache License 2.0 6 votes vote down vote up
@Override public List<Event> getEvents(String body, Map<String, String> headers) {

        List<Event> events = new ArrayList<Event>(0);
        ObjectMapper mapper = new ObjectMapper();
        try {
            JsonNode jsonNode = mapper.readTree(body);
            if (jsonNode.isObject()) {
                events.add(buildSingleEvent(headers, findValue(jsonNode, path)));
            }
            if (jsonNode.isArray()) {
                final Iterator<JsonNode> elements = jsonNode.getElements();
                JsonNode element;
                while (elements.hasNext()) {
                    element = elements.next();
                    events.add(buildSingleEvent(headers, findValue(element, path)));
                }
                
            }

        } catch (Exception e) {
            LOG.warn("An error ocurred while response parsing. Then content is not valid: " + body);
        }

        return events;

    }
 
Example 7
Source File: GarageDoorDevice.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
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 8
Source File: MyqDeviceData.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * 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 9
Source File: MLOBIInboundTransport.java    From defense-solutions-proofs-of-concept with Apache License 2.0 5 votes vote down vote up
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 10
Source File: ShuffleRewriter.java    From Cubert with Apache License 2.0 5 votes vote down vote up
private void rewriteGroupByAggregateForCube(JsonNode aggregates)
{
    // modify the aggregates JsonNode object (to be used in SHUFFLE, and GBY in
    // reducer):
    // a) if it is dual agg, use the outer aggregator
    // b) if it is COUNT or COUNT_DISTINCT agg, switch it to SUM
    // c) use the original output column name as input column name
    for (JsonNode aggNode : aggregates)
    {
        String type;
        JsonNode typeJson = aggNode.get("type");
        if (typeJson.isArray())
            type = typeJson.get(0).getTextValue();
        else
            type = typeJson.getTextValue();

        String outputColName = getText(aggNode, "output");

        ObjectNode onode = (ObjectNode) aggNode;
        // see if the aggregation type has to be changed
        if (type.equals("COUNT") || type.equals("COUNT_DISTINCT"))
            onode.put("type", "SUM");
        else
            onode.put("type", type);

        // change the input column name
        onode.put("input", outputColName);
    }
}
 
Example 11
Source File: SemanticAnalyzer.java    From Cubert with Apache License 2.0 5 votes vote down vote up
@Override
public void visitInput(JsonNode json)
{
    JsonNode pathJson = json.get("path");
    String path;
    if (pathJson.isArray())
        path = JsonUtils.encodePath(pathJson.get(0));
    else
        path = JsonUtils.encodePath(pathJson);

    // determine the postcondition of input
    PostCondition inputCondition = datasetConditions.get(path);
    if (inputCondition == null)
    {
        error(null, pathJson, "Cannot determine schema at path " + path);
    }

    // put the schema in json
    ((ObjectNode) json).put("schema", inputCondition.getSchema().toJson());

    Node node = new Node(json, inputCondition);

    // If the linkedListHead is not null, it implies there was a previous multimapper
    // In which case, store the Node in a list for now.
    // We will validate the postconditions in visitShuffle()
    if (linkedListHead != null)
    {
        mapOutputNodes.add(linkedListHead);
    }

    linkedListHead = node;
    operatorMap.clear();
    operatorMap.put(getText(json, "name"), node);
}
 
Example 12
Source File: JobExecutor.java    From Cubert with Apache License 2.0 5 votes vote down vote up
public static JsonNode asArray(JsonNode node, String property)
{
    JsonNode n = node.get(property);
    if (n.isArray())
        return node.path(property);
    else
    {
        singletonArray.removeAll();
        singletonArray.add(n);
        return singletonArray;
    }
}
 
Example 13
Source File: DescribePlan.java    From Cubert with Apache License 2.0 4 votes vote down vote up
@Override
public void visitOutput(JsonNode json)
{
    JsonNode pathJson = json.get("path");
    String path;
    if (pathJson.isArray())
        pathJson = pathJson.get(0);

    if (pathJson.isTextual())
    {
        path = pathJson.getTextValue();
    }
    else
    {
        path =
                String.format("(%s, %s, %s)",
                              getText(pathJson, "root"),
                              getText(pathJson, "startDate"),
                              getText(pathJson, "endDate"));
    }

    if (pathsSeen.contains(path))
        return;

    pathsSeen.add(path);

    String type = getText(json, "type");
    JsonNode schemaJson = json.get("schema");

    BlockSchema schema = new BlockSchema(schemaJson);
    print.f("%3d. (%s) %s %s", pathsSeen.size(), type, path, schema.toString());

    if (type.equalsIgnoreCase("CubertStore"))
    {
        print.f("\tPARTITIONED ON %s",
                Arrays.toString(JsonUtils.asArray(json, "partitionKeys")));
        print.f("\tSORTED ON %s",
                Arrays.toString(JsonUtils.asArray(json, "sortKeys")));
        print.f("\tBLOCKGEN ID: %s", JsonUtils.getText(json, "blockgenId"));
    }
}
 
Example 14
Source File: JsonSchemaInference.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean isArray(final JsonNode value) {
    return value.isArray();
}
 
Example 15
Source File: AbstractSiteToSiteReportingTask.java    From nifi with Apache License 2.0 4 votes vote down vote up
protected Object getRawNodeValue(final JsonNode fieldNode, final DataType dataType) throws IOException {
    if (fieldNode == null || fieldNode.isNull()) {
        return null;
    }

    if (fieldNode.isNumber()) {
        return fieldNode.getNumberValue();
    }

    if (fieldNode.isBinary()) {
        return fieldNode.getBinaryValue();
    }

    if (fieldNode.isBoolean()) {
        return fieldNode.getBooleanValue();
    }

    if (fieldNode.isTextual()) {
        return fieldNode.getTextValue();
    }

    if (fieldNode.isArray()) {
        final ArrayNode arrayNode = (ArrayNode) fieldNode;
        final int numElements = arrayNode.size();
        final Object[] arrayElements = new Object[numElements];
        int count = 0;

        final DataType elementDataType;
        if (dataType != null && dataType.getFieldType() == RecordFieldType.ARRAY) {
            final ArrayDataType arrayDataType = (ArrayDataType) dataType;
            elementDataType = arrayDataType.getElementType();
        } else {
            elementDataType = null;
        }

        for (final JsonNode node : arrayNode) {
            final Object value = getRawNodeValue(node, elementDataType);
            arrayElements[count++] = value;
        }

        return arrayElements;
    }

    if (fieldNode.isObject()) {
        RecordSchema childSchema;
        if (dataType != null && RecordFieldType.RECORD == dataType.getFieldType()) {
            final RecordDataType recordDataType = (RecordDataType) dataType;
            childSchema = recordDataType.getChildSchema();
        } else {
            childSchema = null;
        }

        if (childSchema == null) {
            childSchema = new SimpleRecordSchema(Collections.emptyList());
        }

        final Iterator<String> fieldNames = fieldNode.getFieldNames();
        final Map<String, Object> childValues = new HashMap<>();
        while (fieldNames.hasNext()) {
            final String childFieldName = fieldNames.next();
            final Object childValue = getRawNodeValue(fieldNode.get(childFieldName), dataType);
            childValues.put(childFieldName, childValue);
        }

        final MapRecord record = new MapRecord(childSchema, childValues);
        return record;
    }

    return null;
}
 
Example 16
Source File: SchemaValidator.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * validation logic taken out of class {@link Schema} with adaptations
 * @param schema schema (type) of a field
 * @param defaultValue default value provided for said field in the parent schema
 * @throws SchemaParseException is name is invalid
 */
private static boolean isValidDefault(Schema schema, JsonNode defaultValue) {
  if (defaultValue == null) {
    return false;
  }
  switch (schema.getType()) {
    case STRING:
    case BYTES:
    case ENUM:
    case FIXED:
      return defaultValue.isTextual();
    case INT:
    case LONG:
    case FLOAT:
    case DOUBLE:
      return defaultValue.isNumber();
    case BOOLEAN:
      return defaultValue.isBoolean();
    case NULL:
      return defaultValue.isNull();
    case ARRAY:
      if (!defaultValue.isArray()) {
        return false;
      }
      for (JsonNode element : defaultValue) {
        if (!isValidDefault(schema.getElementType(), element)) {
          return false;
        }
      }
      return true;
    case MAP:
      if (!defaultValue.isObject()) {
        return false;
      }
      for (JsonNode value : defaultValue) {
        if (!isValidDefault(schema.getValueType(), value)) {
          return false;
        }
      }
      return true;
    case UNION: // union default: first branch
      return isValidDefault(schema.getTypes().get(0), defaultValue);
    case RECORD:
      if (!defaultValue.isObject()) {
        return false;
      }
      for (Schema.Field field : schema.getFields()) {
        if (!isValidDefault(
              field.schema(),
              defaultValue.get(field.name()) != null ? defaultValue.get(field.name()) : field.defaultValue()
        )) {
          return false;
        }
      }
      return true;
    default:
      return false;
  }
}
 
Example 17
Source File: PutHBaseJSON.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
protected PutFlowFile createPut(final ProcessSession session, final ProcessContext context, final FlowFile flowFile) {
    final String tableName = context.getProperty(TABLE_NAME).evaluateAttributeExpressions(flowFile).getValue();
    final String rowId = context.getProperty(ROW_ID).evaluateAttributeExpressions(flowFile).getValue();
    final String rowFieldName = context.getProperty(ROW_FIELD_NAME).evaluateAttributeExpressions(flowFile).getValue();
    final String columnFamily = context.getProperty(COLUMN_FAMILY).evaluateAttributeExpressions(flowFile).getValue();
    final boolean extractRowId = !StringUtils.isBlank(rowFieldName);
    final String complexFieldStrategy = context.getProperty(COMPLEX_FIELD_STRATEGY).getValue();
    final String fieldEncodingStrategy = context.getProperty(FIELD_ENCODING_STRATEGY).getValue();
    final String rowIdEncodingStrategy = context.getProperty(ROW_ID_ENCODING_STRATEGY).getValue();

    // Parse the JSON document
    final ObjectMapper mapper = new ObjectMapper();
    final AtomicReference<JsonNode> rootNodeRef = new AtomicReference<>(null);
    try {
        session.read(flowFile, new InputStreamCallback() {
            @Override
            public void process(final InputStream in) throws IOException {
                try (final InputStream bufferedIn = new BufferedInputStream(in)) {
                    rootNodeRef.set(mapper.readTree(bufferedIn));
                }
            }
        });
    } catch (final ProcessException pe) {
        getLogger().error("Failed to parse {} as JSON due to {}; routing to failure", new Object[]{flowFile, pe.toString()}, pe);
        return null;
    }

    final JsonNode rootNode = rootNodeRef.get();

    if (rootNode.isArray()) {
        getLogger().error("Root node of JSON must be a single document, found array for {}; routing to failure", new Object[]{flowFile});
        return null;
    }

    final Collection<PutColumn> columns = new ArrayList<>();
    final AtomicReference<String> rowIdHolder = new AtomicReference<>(null);

    // convert each field/value to a column for the put, skip over nulls and arrays
    final Iterator<String> fieldNames = rootNode.getFieldNames();
    while (fieldNames.hasNext()) {
        final String fieldName = fieldNames.next();
        final AtomicReference<byte[]> fieldValueHolder = new AtomicReference<>(null);

        final JsonNode fieldNode = rootNode.get(fieldName);
        if (fieldNode.isNull()) {
            getLogger().debug("Skipping {} because value was null", new Object[]{fieldName});
        } else if (fieldNode.isValueNode()) {
            // for a value node we need to determine if we are storing the bytes of a string, or the bytes of actual types
            if (STRING_ENCODING_VALUE.equals(fieldEncodingStrategy)) {
                final byte[] valueBytes = clientService.toBytes(fieldNode.asText());
                fieldValueHolder.set(valueBytes);
            } else {
                fieldValueHolder.set(extractJNodeValue(fieldNode));
            }
        } else {
            // for non-null, non-value nodes, determine what to do based on the handling strategy
            switch (complexFieldStrategy) {
                case FAIL_VALUE:
                    getLogger().error("Complex value found for {}; routing to failure", new Object[]{fieldName});
                    return null;
                case WARN_VALUE:
                    getLogger().warn("Complex value found for {}; skipping", new Object[]{fieldName});
                    break;
                case TEXT_VALUE:
                    // use toString() here because asText() is only guaranteed to be supported on value nodes
                    // some other types of nodes, like ArrayNode, provide toString implementations
                    fieldValueHolder.set(clientService.toBytes(fieldNode.toString()));
                    break;
                case IGNORE_VALUE:
                    // silently skip
                    break;
                default:
                    break;
            }
        }

        // if we have a field value, then see if this is the row id field, if so store the value for later
        // otherwise add a new column where the fieldName and fieldValue are the column qualifier and value
        if (fieldValueHolder.get() != null) {
            if (extractRowId && fieldName.equals(rowFieldName)) {
                rowIdHolder.set(fieldNode.asText());
            } else {
                columns.add(new PutColumn(columnFamily.getBytes(StandardCharsets.UTF_8), fieldName.getBytes(StandardCharsets.UTF_8), fieldValueHolder.get()));
            }
        }
    }

    // if we are expecting a field name to use for the row id and the incoming document doesn't have it
    // log an error message so the user can see what the field names were and return null so it gets routed to failure
    if (extractRowId && rowIdHolder.get() == null) {
        final String fieldNameStr = StringUtils.join(rootNode.getFieldNames(), ",");
        getLogger().error("Row ID field named '{}' not found in field names '{}'; routing to failure", new Object[] {rowFieldName, fieldNameStr});
        return null;
    }

    final String putRowId = (extractRowId ? rowIdHolder.get() : rowId);

    byte[] rowKeyBytes = getRow(putRowId,context.getProperty(ROW_ID_ENCODING_STRATEGY).getValue());
    return new PutFlowFile(tableName, rowKeyBytes, columns, flowFile);
}