Search in sources :

Example 1 with TreeTraversingParser

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);
    }
}
Also used : TreeTraversingParser(com.fasterxml.jackson.databind.node.TreeTraversingParser) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) UnrecognizedPropertyException(com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException) List(java.util.List) Map(java.util.Map) InvalidFormatException(com.fasterxml.jackson.databind.exc.InvalidFormatException)

Example 2 with TreeTraversingParser

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);
    }
}
Also used : TreeTraversingParser(com.fasterxml.jackson.databind.node.TreeTraversingParser) JavaType(com.fasterxml.jackson.databind.JavaType) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 3 with TreeTraversingParser

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();
}
Also used : TreeTraversingParser(com.fasterxml.jackson.databind.node.TreeTraversingParser) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Aggregations

TreeTraversingParser (com.fasterxml.jackson.databind.node.TreeTraversingParser)3 JavaType (com.fasterxml.jackson.databind.JavaType)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 InvalidFormatException (com.fasterxml.jackson.databind.exc.InvalidFormatException)1 UnrecognizedPropertyException (com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 IOException (java.io.IOException)1 List (java.util.List)1 Map (java.util.Map)1