下面列出了怎么用com.fasterxml.jackson.databind.node.TreeTraversingParser的API类实例代码及写法,或者点击链接到github查看源代码。
@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);
}
}
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);
}
@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);
}
@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);
}
@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);
}
}
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);
}
@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);
}
}
@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;
}
@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;
}
@Override
public void setSource(JsonNode node) {
this.parser = new TreeTraversingParser(node);
}
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);
}
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);
}
@Override
public void setSource(JsonNode node) {
this.parser = new TreeTraversingParser(node);
}
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);
}
private T build(JsonNode node, String filename) throws IOException, ConfigurationException {
T config = mapper.readValue(new TreeTraversingParser(node), configType);
validate(filename, config);
return config;
}