use of com.fasterxml.jackson.databind.node.TreeTraversingParser in project dropwizard by dropwizard.
the class BaseConfigurationFactory method build.
protected T build(JsonNode node, String path) throws IOException, ConfigurationException {
for (Map.Entry<Object, Object> pref : System.getProperties().entrySet()) {
final String prefName = (String) pref.getKey();
if (prefName.startsWith(propertyPrefix)) {
final String configName = prefName.substring(propertyPrefix.length());
addOverride(node, configName, System.getProperty(prefName));
}
}
try {
final T config = mapper.readValue(new TreeTraversingParser(node), klass);
validate(path, config);
return config;
} catch (UnrecognizedPropertyException e) {
final List<String> properties = e.getKnownPropertyIds().stream().map(Object::toString).collect(Collectors.toList());
throw ConfigurationParsingException.builder("Unrecognized field").setFieldPath(e.getPath()).setLocation(e.getLocation()).addSuggestions(properties).setSuggestionBase(e.getPropertyName()).setCause(e).build(path);
} catch (InvalidFormatException e) {
final String sourceType = e.getValue().getClass().getSimpleName();
final String targetType = e.getTargetType().getSimpleName();
throw ConfigurationParsingException.builder("Incorrect type of value").setDetail("is of type: " + sourceType + ", expected: " + targetType).setLocation(e.getLocation()).setFieldPath(e.getPath()).setCause(e).build(path);
} catch (JsonMappingException e) {
throw ConfigurationParsingException.builder("Failed to parse configuration").setDetail(e.getMessage()).setFieldPath(e.getPath()).setLocation(e.getLocation()).setCause(e).build(path);
}
}
use of com.fasterxml.jackson.databind.node.TreeTraversingParser in project ratpack by ratpack.
the class DefaultConfigData method getAsConfigObject.
@Override
public <O> ConfigObject<O> getAsConfigObject(String pointer, TypeToken<O> type) {
JsonNode node = pointer != null ? rootNode.at(pointer) : rootNode;
if (node.isMissingNode()) {
node = emptyNode;
}
try {
JavaType javaType = objectMapper.getTypeFactory().constructType(type.getType());
O value = objectMapper.readValue(new TreeTraversingParser(node, objectMapper), javaType);
return new DefaultConfigObject<>(pointer, type, value);
} catch (IOException ex) {
throw Exceptions.uncheck(ex);
}
}
use of com.fasterxml.jackson.databind.node.TreeTraversingParser in project jackson-databind by FasterXML.
the class ArrayNodeTest method testParser.
public void testParser() throws Exception {
ArrayNode n = new ArrayNode(JsonNodeFactory.instance);
n.add(123);
TreeTraversingParser p = new TreeTraversingParser(n, null);
p.setCodec(null);
assertNull(p.getCodec());
assertNotNull(p.getParsingContext());
assertNotNull(p.getTokenLocation());
assertNotNull(p.getCurrentLocation());
assertNull(p.getEmbeddedObject());
assertNull(p.currentNode());
//assertNull(p.getNumberType());
assertToken(JsonToken.START_ARRAY, p.nextToken());
p.skipChildren();
assertToken(JsonToken.END_ARRAY, p.getCurrentToken());
p.close();
p = new TreeTraversingParser(n, null);
p.nextToken();
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(JsonParser.NumberType.INT, p.getNumberType());
p.close();
}
Aggregations