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

The following examples show how to use org.codehaus.jackson.JsonNode#isBinary() . 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: 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 2
Source File: JsonSchemaInference.java    From nifi with Apache License 2.0 4 votes vote down vote up
protected DataType getDataType(final JsonNode jsonNode) {
    if (jsonNode.isTextual()) {
        final String text = jsonNode.getTextValue();
        if (text == null) {
            return RecordFieldType.STRING.getDataType();
        }

        final Optional<DataType> timeDataType = timeValueInference.getDataType(text);
        return timeDataType.orElse(RecordFieldType.STRING.getDataType());
    }

    if (jsonNode.isObject()) {
        final RecordSchema schema = createSchema(jsonNode);
        return RecordFieldType.RECORD.getRecordDataType(schema);
    }

    if (jsonNode.isIntegralNumber()) {
        if (jsonNode.isBigInteger()) {
            return RecordFieldType.BIGINT.getDataType();
        }
        return RecordFieldType.LONG.getDataType();
    }

    if (jsonNode.isBigDecimal()) {
        final DecimalNode decimalNode = (DecimalNode) jsonNode;
        final BigDecimal value = decimalNode.getDecimalValue();
        return RecordFieldType.DECIMAL.getDecimalDataType(value.precision(), value.scale());
    }

    if (jsonNode.isFloatingPointNumber()) {
        return RecordFieldType.DOUBLE.getDataType();
    }
    if (jsonNode.isBinary()) {
        return RecordFieldType.ARRAY.getArrayDataType(RecordFieldType.BYTE.getDataType());
    }
    if (jsonNode.isBoolean()) {
        return RecordFieldType.BOOLEAN.getDataType();
    }

    return null;
}