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

The following examples show how to use org.codehaus.jackson.JsonNode#isTextual() . 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: MLOBIOutboundTransport.java    From defense-solutions-proofs-of-concept with Apache License 2.0 6 votes vote down vote up
private String getTrackIdAsString(JsonNode trackIDNode)
{
	String output = null;
	if (trackIDNode.isTextual())
		output = trackIDNode.getTextValue();
	else if (trackIDNode.isInt())
		output = Integer.toString(trackIDNode.getIntValue());
	else if (trackIDNode.isLong())
		output = Long.toString(trackIDNode.getLongValue());
	else if (trackIDNode.isDouble())
		output = Double.toString(trackIDNode.getDoubleValue());
	else if (trackIDNode.isFloatingPointNumber())
		output = trackIDNode.getDecimalValue().toString();

	if (!Validator.isEmpty(output))
	{
		output = output.replace("'", "''");
	}
	return output;
}
 
Example 2
Source File: GeoJsonParser.java    From arcgis-runtime-demo-java with Apache License 2.0 6 votes vote down vote up
private Map<String, Object> parseProperties(JsonNode node) {
  Map<String, Object> properties = new HashMap<String, Object>();
  Iterator<Map.Entry<String, JsonNode>> propertyInterator = node.getFields(); 
  while (propertyInterator.hasNext()) {
    Map.Entry<String, JsonNode> property = propertyInterator.next();
    JsonNode jsonValue = property.getValue();
    if (jsonValue.isInt()) {
      properties.put(property.getKey(), property.getValue().asInt());
    } else if (jsonValue.isDouble()) {
      properties.put(property.getKey(), property.getValue().asDouble());
    } else if (jsonValue.isTextual()) {
      properties.put(property.getKey(), property.getValue().asText());
    }
  }
  return properties;
}
 
Example 3
Source File: JsonUtils.java    From Cubert with Apache License 2.0 5 votes vote down vote up
public static Object asObject(JsonNode node)
{
    if (node.isTextual())
        return node.getTextValue();
    else if (node.isInt())
        return node.getIntValue();
    else if (node.isFloatingPointNumber())
        return node.getDoubleValue();
    else if (node.isBoolean())
        return node.getBooleanValue();

    return null;
}
 
Example 4
Source File: JsonUtils.java    From Cubert with Apache License 2.0 5 votes vote down vote up
public static String encodePath(JsonNode path)
{
    if (path.isTextual())
        return path.getTextValue();
    else
    {
        String root = getText(path, "root");
        int startDate = Integer.parseInt(getText(path, "startDate"));
        int endDate = Integer.parseInt(getText(path, "endDate"));
        if (path.get("origStartDate") != null)
          return String.format("%s#START%d#END%d#ORIGSTART%d", root, startDate, endDate, Integer.parseInt(getText(path, "origStartDate")));
        return String.format("%s#START%d#END%d", root, startDate, endDate);
    }
}
 
Example 5
Source File: Lineage.java    From Cubert with Apache License 2.0 5 votes vote down vote up
private static String findNamedColumn(ArrayNode argsNode)
{
    for (JsonNode arg : argsNode)
    {
      if (!arg.isTextual())
        continue;
      return arg.getTextValue();
    }
    return null;
}
 
Example 6
Source File: Lineage.java    From Cubert with Apache License 2.0 5 votes vote down vote up
public static boolean isCountDistinctAggregate(ObjectNode operatorNode)
{
    if (operatorNode.get("operator") == null)
      return false;

    String type = operatorNode.get("operator").getTextValue();
    if (!type.equals("GROUP_BY") && !type.equals("CUBE"))
        return false;

    if (!operatorNode.has("aggregates"))
        return false;

    for (JsonNode aggregateJson : operatorNode.path("aggregates"))
    {
        // Create the aggregator object
        JsonNode typeNode = aggregateJson.get("type");

        // Group by case
        if (typeNode.isTextual()){
          AggregationType aggType =
            AggregationType.valueOf(JsonUtils.getText(aggregateJson, "type"));
          String measureColumn = JsonUtils.getText(aggregateJson, "input");
          if (aggType != AggregationType.COUNT_DISTINCT)
            return false;
        }
        else if (typeNode instanceof ArrayNode){
          String[] typeArray = JsonUtils.asArray(aggregateJson, "type");
          if (!typeArray[0].equals("SUM") || !typeArray[1].equals("COUNT_TO_ONE"))
            return false;
        }
    }

    return true;
}
 
Example 7
Source File: CustomFieldDeSerializer.java    From jira-rest-client with Apache License 2.0 5 votes vote down vote up
@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 8
Source File: Protocol.java    From Android-DDP with Apache License 2.0 5 votes vote down vote up
public static Error fromJson(final JsonNode json) {
	final String error;
	if (json.has(Protocol.Field.ERROR)) {
		final JsonNode errorJson = json.get(Protocol.Field.ERROR);
		if (errorJson.isTextual()) {
			error = errorJson.getTextValue();
		}
		else if (errorJson.isNumber()) {
			error = errorJson.getNumberValue().toString();
		}
		else {
			throw new IllegalArgumentException("Unexpected data type of error.error");
		}
	}
	else {
		error = null;
	}

	final String reason;
	if (json.has(Protocol.Field.REASON)) {
		reason = json.get(Protocol.Field.REASON).getTextValue();
	}
	else {
		reason = null;
	}

	final String details;
	if (json.has(Protocol.Field.DETAILS)) {
		details = json.get(Protocol.Field.DETAILS).getTextValue();
	}
	else {
		details = null;
	}

	return new Error(error, reason, details);
}
 
Example 9
Source File: PubSub.java    From WAMPlay with MIT License 5 votes vote down vote up
/**
 * Method that truncates an event message before it's published. 
 * @param client WAMP client that sent the event
 * @param event Event to be truncated
 * @return Modified json event, null to halt publish
 */
@onPublish("truncate")
public static JsonNode truncatePublish(String sessionID, JsonNode event) {
	if (!event.isTextual()) {
		return cancel();
	}		
	String message = event.asText();
	if (message.length() > 10) {
		message = message.substring(0, MAX_MESSAGE_LENGTH);
	}
	return Json.toJson(message);
}
 
Example 10
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 11
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 12
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 13
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;
}
 
Example 14
Source File: ColumnTypeUtil.java    From Cubert with Apache License 2.0 3 votes vote down vote up
/**
 * Generates the ColumnType array for the specified JsonNode.
 * <p>
 * The JsonNode can be:
 * <ul>
 * <li>a string of form "{@literal <data type> <column name>, ...}"</li>
 * 
 * <li>a json node that conforms to the ColumnType.json schema</li>
 * </ul>
 * 
 * @param node
 *            the json node
 * @return the ColumnType array
 * @throws JsonParseException
 * @throws JsonMappingException
 * @throws IOException
 */
public static ColumnType[] getColumnTypes(JsonNode node) throws IOException
{
    if (node.isTextual())
    {
        String str = node.getTextValue();
        return getColumnTypes(str);
    }
    else
    {
        return getMapper().readValue(node.toString(), ColumnType[].class);
    }
}