Java Code Examples for org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode#at()

The following examples show how to use org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode#at() . 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: JsonRowSchemaConverter.java    From flink with Apache License 2.0 6 votes vote down vote up
private static JsonNode resolveReference(String ref, JsonNode origin, JsonNode root) {
	if (!ref.startsWith("#")) {
		throw new IllegalArgumentException("Only JSON schemes with simple references " +
			"(one indirection in the same document) are supported yet. But was: " + ref);
	}
	final String path = ref.substring(1);
	final JsonNode foundNode = root.at(path);
	if (foundNode.isMissingNode()) {
		throw new IllegalArgumentException("Could not find reference: " + ref);
	}
	// prevent obvious cyclic references
	if (foundNode == origin) {
		throw new IllegalArgumentException("Cyclic references are not supported:" + ref);
	}
	return foundNode;
}
 
Example 2
Source File: JsonRowSchemaConverter.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private static JsonNode resolveReference(String ref, JsonNode origin, JsonNode root) {
	if (!ref.startsWith("#")) {
		throw new IllegalArgumentException("Only JSON schemes with simple references " +
			"(one indirection in the same document) are supported yet. But was: " + ref);
	}
	final String path = ref.substring(1);
	final JsonNode foundNode = root.at(path);
	if (foundNode.isMissingNode()) {
		throw new IllegalArgumentException("Could not find reference: " + ref);
	}
	// prevent obvious cyclic references
	if (foundNode == origin) {
		throw new IllegalArgumentException("Cyclic references are not supported:" + ref);
	}
	return foundNode;
}
 
Example 3
Source File: JsonRowSchemaConverter.java    From flink with Apache License 2.0 6 votes vote down vote up
private static JsonNode resolveReference(String ref, JsonNode origin, JsonNode root) {
	if (!ref.startsWith("#")) {
		throw new IllegalArgumentException("Only JSON schemes with simple references " +
			"(one indirection in the same document) are supported yet. But was: " + ref);
	}
	final String path = ref.substring(1);
	final JsonNode foundNode = root.at(path);
	if (foundNode.isMissingNode()) {
		throw new IllegalArgumentException("Could not find reference: " + ref);
	}
	// prevent obvious cyclic references
	if (foundNode == origin) {
		throw new IllegalArgumentException("Cyclic references are not supported:" + ref);
	}
	return foundNode;
}
 
Example 4
Source File: Selectors.java    From stateful-functions with Apache License 2.0 6 votes vote down vote up
public static Map<String, String> propertiesAt(JsonNode node, JsonPointer pointer) {
  node = node.at(pointer);
  if (node.isMissingNode()) {
    return Collections.emptyMap();
  }
  if (!node.isArray()) {
    throw new WrongTypeException(pointer, "not a key-value list");
  }
  Map<String, String> properties = new LinkedHashMap<>();
  for (JsonNode listElement : node) {
    Iterator<Map.Entry<String, JsonNode>> fields = listElement.fields();
    if (!fields.hasNext()) {
      throw new WrongTypeException(pointer, "not a key-value list");
    }
    Map.Entry<String, JsonNode> field = fields.next();
    if (!field.getValue().isTextual()) {
      throw new WrongTypeException(pointer, "not a key-value pair at " + field);
    }
    properties.put(field.getKey(), field.getValue().asText());
  }
  return properties;
}
 
Example 5
Source File: Selectors.java    From flink-statefun with Apache License 2.0 6 votes vote down vote up
public static Map<String, String> propertiesAt(JsonNode node, JsonPointer pointer) {
  node = node.at(pointer);
  if (node.isMissingNode()) {
    return Collections.emptyMap();
  }
  if (!node.isArray()) {
    throw new WrongTypeException(pointer, "not a key-value list");
  }
  Map<String, String> properties = new LinkedHashMap<>();
  for (JsonNode listElement : node) {
    Iterator<Map.Entry<String, JsonNode>> fields = listElement.fields();
    if (!fields.hasNext()) {
      throw new WrongTypeException(pointer, "not a key-value list");
    }
    Map.Entry<String, JsonNode> field = fields.next();
    properties.put(field.getKey(), field.getValue().asText());
  }
  return properties;
}
 
Example 6
Source File: JsonServiceLoader.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
private static void validateMeta(URL moduleYamlFile, JsonNode root) {
  JsonNode typeNode = root.at(MODULE_META_TYPE);
  if (typeNode.isMissingNode()) {
    throw new IllegalStateException("Unable to find a module type in " + moduleYamlFile);
  }
  if (!typeNode.asText().equalsIgnoreCase(ModuleType.REMOTE.name())) {
    throw new IllegalStateException(
        "Unknown module type "
            + typeNode.asText()
            + ", currently supported: "
            + ModuleType.REMOTE);
  }
}
 
Example 7
Source File: KinesisIngressSpecJsonParser.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
static Optional<KinesisIngressStartupPosition> optionalStartupPosition(JsonNode ingressSpecNode) {
  final JsonNode startupPositionSpecNode = ingressSpecNode.at(STARTUP_POSITION_POINTER);
  if (startupPositionSpecNode.isMissingNode()) {
    return Optional.empty();
  }

  final String type = Selectors.textAt(startupPositionSpecNode, StartupPosition.TYPE_POINTER);
  switch (type) {
    case StartupPosition.EARLIEST_TYPE:
      return Optional.of(KinesisIngressStartupPosition.fromEarliest());
    case StartupPosition.LATEST_TYPE:
      return Optional.of(KinesisIngressStartupPosition.fromLatest());
    case StartupPosition.DATE_TYPE:
      return Optional.of(
          KinesisIngressStartupPosition.fromDate(startupDate(startupPositionSpecNode)));
    default:
      final List<String> validValues =
          Arrays.asList(
              StartupPosition.EARLIEST_TYPE,
              StartupPosition.LATEST_TYPE,
              StartupPosition.DATE_TYPE);
      throw new IllegalArgumentException(
          "Invalid startup position type: "
              + type
              + "; valid values are ["
              + String.join(", ", validValues)
              + "]");
  }
}
 
Example 8
Source File: AwsAuthSpecJsonParser.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
static Optional<AwsCredentials> optionalAwsCredentials(JsonNode specNode) {
  final JsonNode awsCredentialsSpecNode = specNode.at(AWS_CREDENTIALS_POINTER);
  if (awsCredentialsSpecNode.isMissingNode()) {
    return Optional.empty();
  }

  final String type = Selectors.textAt(awsCredentialsSpecNode, Credentials.TYPE_POINTER);
  switch (type) {
    case Credentials.DEFAULT_TYPE:
      return Optional.of(AwsCredentials.fromDefaultProviderChain());
    case Credentials.BASIC_TYPE:
      return Optional.of(
          AwsCredentials.basic(
              Selectors.textAt(awsCredentialsSpecNode, Credentials.ACCESS_KEY_ID_POINTER),
              Selectors.textAt(awsCredentialsSpecNode, Credentials.SECRET_ACCESS_KEY_POINTER)));
    case Credentials.PROFILE_TYPE:
      final Optional<String> path =
          Selectors.optionalTextAt(awsCredentialsSpecNode, Credentials.PROFILE_PATH_POINTER);
      if (path.isPresent()) {
        return Optional.of(
            AwsCredentials.profile(
                Selectors.textAt(awsCredentialsSpecNode, Credentials.PROFILE_NAME_POINTER),
                path.get()));
      } else {
        return Optional.of(
            AwsCredentials.profile(
                Selectors.textAt(awsCredentialsSpecNode, Credentials.PROFILE_NAME_POINTER)));
      }
    default:
      final List<String> validValues =
          Arrays.asList(
              Credentials.DEFAULT_TYPE, Credentials.BASIC_TYPE, Credentials.PROFILE_TYPE);
      throw new IllegalArgumentException(
          "Invalid AWS credential type: "
              + type
              + "; valid values are ["
              + String.join(", ", validValues)
              + "]");
  }
}
 
Example 9
Source File: Selectors.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
private static JsonNode dereference(JsonNode node, JsonPointer pointer) {
  node = node.at(pointer);
  if (node.isMissingNode()) {
    throw new MissingKeyException(pointer);
  }
  return node;
}
 
Example 10
Source File: Selectors.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
public static Map<String, Long> longPropertiesAt(JsonNode node, JsonPointer pointer) {
  node = node.at(pointer);
  if (node.isMissingNode()) {
    return Collections.emptyMap();
  }
  if (!node.isArray()) {
    throw new WrongTypeException(pointer, "not a key-value list");
  }
  Map<String, Long> longProperties = new LinkedHashMap<>();
  for (JsonNode listElement : node) {
    Iterator<Map.Entry<String, JsonNode>> fields = listElement.fields();
    if (!fields.hasNext()) {
      throw new WrongTypeException(pointer, "not a key-value list");
    }
    Map.Entry<String, JsonNode> field = fields.next();
    if (!field.getValue().isLong() && !field.getValue().isInt()) {
      throw new WrongTypeException(
          pointer,
          "value for key-value pair at "
              + field.getKey()
              + " is not a long: "
              + field.getValue());
    }
    longProperties.put(field.getKey(), field.getValue().asLong());
  }
  return longProperties;
}
 
Example 11
Source File: Selectors.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
public static List<String> textListAt(JsonNode node, JsonPointer pointer) {
  node = node.at(pointer);
  if (node.isMissingNode()) {
    return Collections.emptyList();
  }
  if (!node.isArray()) {
    throw new WrongTypeException(pointer, "not a list");
  }
  return StreamSupport.stream(node.spliterator(), false)
      .filter(JsonNode::isTextual)
      .map(JsonNode::asText)
      .collect(Collectors.toList());
}
 
Example 12
Source File: Selectors.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
public static Iterable<? extends JsonNode> listAt(JsonNode node, JsonPointer pointer) {
  node = node.at(pointer);
  if (node.isMissingNode()) {
    return Collections.emptyList();
  }
  if (!node.isArray()) {
    throw new WrongTypeException(pointer, "not a list");
  }
  return node;
}
 
Example 13
Source File: Selectors.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
public static OptionalInt optionalIntegerAt(JsonNode node, JsonPointer pointer) {
  node = node.at(pointer);
  if (node.isMissingNode()) {
    return OptionalInt.empty();
  }
  if (!node.isInt()) {
    throw new WrongTypeException(pointer, "not an integer");
  }
  return OptionalInt.of(node.asInt());
}
 
Example 14
Source File: JsonServiceLoader.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
private static JsonNode requireValidModuleSpecNode(URL moduleYamlFile, JsonNode root) {
  final JsonNode moduleSpecNode = root.at(MODULE_SPEC);

  if (moduleSpecNode.isMissingNode()) {
    throw new IllegalStateException("A module without a spec at " + moduleYamlFile);
  }

  return moduleSpecNode;
}
 
Example 15
Source File: Selectors.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
private static JsonNode dereference(JsonNode node, JsonPointer pointer) {
  node = node.at(pointer);
  if (node.isMissingNode()) {
    throw new MissingKeyException(pointer);
  }
  return node;
}
 
Example 16
Source File: Selectors.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
public static List<String> textListAt(JsonNode node, JsonPointer pointer) {
  node = node.at(pointer);
  if (node.isMissingNode()) {
    return Collections.emptyList();
  }
  if (!node.isArray()) {
    throw new WrongTypeException(pointer, "not a list");
  }
  return StreamSupport.stream(node.spliterator(), false)
      .filter(JsonNode::isTextual)
      .map(JsonNode::asText)
      .collect(Collectors.toList());
}
 
Example 17
Source File: Selectors.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
public static Iterable<? extends JsonNode> listAt(JsonNode node, JsonPointer pointer) {
  node = node.at(pointer);
  if (node.isMissingNode()) {
    return Collections.emptyList();
  }
  if (!node.isArray()) {
    throw new WrongTypeException(pointer, "not a list");
  }
  return node;
}
 
Example 18
Source File: JsonModuleTest.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
private static StatefulFunctionModule fromPath(String path) {
  URL moduleUrl = JsonModuleTest.class.getClassLoader().getResource(path);
  ObjectMapper mapper = JsonServiceLoader.mapper();
  final JsonNode json;
  try {
    json = mapper.readTree(moduleUrl);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
  JsonNode spec = json.at("/module/spec");
  return new JsonModule(spec, moduleUrl);
}
 
Example 19
Source File: JsonServiceLoader.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
private static void validateMeta(URL moduleYamlFile, JsonNode root) {
  JsonNode typeNode = root.at(MODULE_META_TYPE);
  if (typeNode.isMissingNode()) {
    throw new IllegalStateException("Unable to find a module type in " + moduleYamlFile);
  }
  if (!typeNode.asText().equalsIgnoreCase(ModuleType.REMOTE.name())) {
    throw new IllegalStateException(
        "Unknown module type "
            + typeNode.asText()
            + ", currently supported: "
            + ModuleType.REMOTE);
  }
}
 
Example 20
Source File: JsonServiceLoader.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static StatefulFunctionModule fromUrl(ObjectMapper mapper, URL moduleUrl) {
  try {
    JsonNode root = readAndValidateModuleTree(mapper, moduleUrl);
    JsonNode spec = root.at(MODULE_SPEC);
    return new JsonModule(spec, moduleUrl);
  } catch (Throwable t) {
    throw new RuntimeException("Failed loading a module at " + moduleUrl, t);
  }
}