com.fasterxml.jackson.databind.node.TreeTraversingParser Java Examples

The following examples show how to use com.fasterxml.jackson.databind.node.TreeTraversingParser. 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: JsonNodeConfigurationFactory.java    From bootique with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T config(TypeRef<? extends T> type, String prefix) {

    JsonNode child = findChild(prefix);

    JavaType jacksonType = typeFactory.constructType(type.getType());

    try {
        return mapper.readValue(new TreeTraversingParser(child, mapper), jacksonType);
    }
    // TODO: implement better exception handling. See ConfigurationFactory
    // in Dropwizard for inspiration
    catch (IOException e) {
        throw new RuntimeException("Error creating config", e);
    }
}
 
Example #2
Source File: FilterRegistry.java    From heroic with Apache License 2.0 6 votes vote down vote up
private Filter deserializeObject(final JsonParser p, final DeserializationContext c)
    throws IOException {
    final ObjectNode object = (ObjectNode) p.readValueAs(JsonNode.class);

    final JsonNode typeNode = object.remove("type");

    if (typeNode == null) {
        throw c.mappingException("Expected 'type' field");
    }

    if (!typeNode.isTextual()) {
        throw c.mappingException("Expected 'type' to be string");
    }

    final String type = typeNode.asText();

    final Class<? extends Filter> cls = typeNameMapping.get(type);

    if (cls == null) {
        throw c.mappingException("No such type: " + type);
    }

    // use tree traversing parser to operate on the node (without 'type') again.
    final TreeTraversingParser parser = new TreeTraversingParser(object, p.getCodec());
    return parser.readValueAs(cls);
}
 
Example #3
Source File: YamlConfigurationParser.java    From springboot-plugin-framework-parent with Apache License 2.0 5 votes vote down vote up
@Override
protected Object parse(Resource resource, Class<?> pluginConfigClass)
        throws Exception{
    InputStream input = new FileInputStream(resource.getFile());
    YAMLParser yamlParser = yamlFactory.createParser(input);
    final JsonNode node = objectMapper.readTree(yamlParser);
    if(node == null){
        return pluginConfigClass.newInstance();
    }
    TreeTraversingParser treeTraversingParser = new TreeTraversingParser(node);
    return objectMapper.readValue(treeTraversingParser, pluginConfigClass);
}
 
Example #4
Source File: ObjectReader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public JsonParser treeAsTokens(TreeNode n) {
    // 05-Dec-2017, tatu: Important! Must clear "valueToUpdate" since we do not
    //    want update to be applied here, as a side effect
    ObjectReader codec = withValueToUpdate(null);
    return new TreeTraversingParser((JsonNode) n, codec);
}
 
Example #5
Source File: JsonNodeConfigurationFactory.java    From bootique with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T config(Class<T> type, String prefix) {

    JsonNode child = findChild(prefix);

    try {
        return mapper.readValue(new TreeTraversingParser(child, mapper), type);
    }
    // TODO: implement better exception handling. See ConfigurationFactory
    // in Dropwizard for inspiration
    catch (IOException e) {
        throw new RuntimeException("Error creating config", e);
    }
}
 
Example #6
Source File: DefaultJacksonServiceIT.java    From bootique with Apache License 2.0 5 votes vote down vote up
protected <T> T readValue(Class<T> type, ObjectMapper mapper, String json) throws IOException {
    JsonParser parser = new JsonFactory().createParser(json);
    JsonNode node = mapper.readTree(parser);
    assertNotNull(node);

    return mapper.readValue(new TreeTraversingParser(node), type);
}
 
Example #7
Source File: DataContainer.java    From kork with Apache License 2.0 5 votes vote down vote up
@NotNull
public <T> T getOfType(@NotNull String key, Class<T> type) {
  JsonParser parser =
      new TreeTraversingParser(
          objectMapper.valueToTree(data).at(normalizeKey(key)), objectMapper);
  try {
    return objectMapper.readValue(parser, type);
  } catch (IOException e) {
    throw new KorkTestException(format("Unable to map '%s' to %s", key, type), e);
  }
}
 
Example #8
Source File: YamlConfigurationLoader.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@Override
public IndexerConfiguration loadConfiguration() throws IOException {
	FileReader reader = new FileReader(configFile);
       final JsonNode node = mapper.readTree(yamlFactory.createParser(reader));
       final IndexerConfiguration config = mapper.readValue(new TreeTraversingParser(node), IndexerConfiguration.class);
	
	// Close the file reader
	reader.close();
	
	return config;
}
 
Example #9
Source File: YamlConfigurationLoader.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@Override
public IndexerConfiguration loadConfiguration() throws IOException {
	FileReader reader = new FileReader(configFile);
       final JsonNode node = mapper.readTree(yamlFactory.createParser(reader));
       final IndexerConfiguration config = mapper.readValue(new TreeTraversingParser(node), IndexerConfiguration.class);
	
	// Close the file reader
	reader.close();
	
	return config;
}
 
Example #10
Source File: BaseJsonProcessor.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public void setSource(JsonNode node) {
  this.parser = new TreeTraversingParser(node);
}
 
Example #11
Source File: JSONOptions.java    From Bats with Apache License 2.0 4 votes vote down vote up
public JsonParser asParser(){
  Preconditions.checkArgument(this.root != null, "Attempted to grab JSONOptions as Parser when no root node was stored.  You can only convert non-opaque JSONOptions values to parsers.");
  return new TreeTraversingParser(root);
}
 
Example #12
Source File: JSONOptions.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public JsonParser asParser(){
  Preconditions.checkArgument(this.root != null, "Attempted to grab JSONOptions as Parser when no root node was stored.  You can only convert non-opaque JSONOptions values to parsers.");
  return new TreeTraversingParser(root);
}
 
Example #13
Source File: BaseJsonProcessor.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public void setSource(JsonNode node) {
  this.parser = new TreeTraversingParser(node);
}
 
Example #14
Source File: Spotify100.java    From heroic with Apache License 2.0 4 votes vote down vote up
private AsyncFuture<Void> handleVersion1(final JsonNode tree)
    throws ConsumerSchemaValidationException {
    final JsonMetric metric;

    try {
        metric = new TreeTraversingParser(tree, mapper).readValueAs(JsonMetric.class);
    } catch (IOException e) {
        throw new ConsumerSchemaValidationException("Invalid metric", e);
    }

    if (metric.getValue() == null) {
        throw new ConsumerSchemaValidationException(
            "Metric must have a value but this metric has a null value: " + metric);
    }

    if (metric.getTime() == null) {
        throw new ConsumerSchemaValidationException("time: field must be defined: " + tree);
    }

    if (metric.getTime() <= 0) {
        throw new ConsumerSchemaValidationException(
            "time: field must be a positive number: " + tree);
    }

    if (metric.getKey() == null) {
        throw new ConsumerSchemaValidationException("key: field must be defined: " + tree);
    }

    final Map<String, String> tags = new HashMap<>(metric.getAttributes());

    if (metric.getHost() != null) {
        tags.put(HOST_TAG, metric.getHost());
    }

    final Map<String, String> resource = new HashMap<>(metric.getResource());

    final Series series = Series.of(metric.getKey(), tags, resource);
    final Point p = new Point(metric.getTime(), metric.getValue());
    final List<Point> points = ImmutableList.of(p);

    reporter.reportMessageDrift(clock.currentTimeMillis() - p.getTimestamp());
    reporter.reportMetricsIn(1);
    AsyncFuture<Ingestion> ingestionFuture =
        ingestion.write(new Request(series, MetricCollection.points(points)));

    // Return Void future, to not leak unnecessary information from the backend but just
    // allow monitoring of when the consumption is done.
    return ingestionFuture.directTransform(future -> null);
}
 
Example #15
Source File: ConfigurationFactory.java    From monasca-common with Apache License 2.0 4 votes vote down vote up
private T build(JsonNode node, String filename) throws IOException, ConfigurationException {
  T config = mapper.readValue(new TreeTraversingParser(node), configType);
  validate(filename, config);
  return config;
}