Search in sources :

Example 1 with UnrecognizedPropertyException

use of com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException in project moco by dreamhead.

the class CollectionReader method read.

public <T> ImmutableList<T> read(final InputStream is, final Class<T> elementClass) {
    try {
        CollectionType type = factory.constructCollectionType(List.class, elementClass);
        List<T> sessionSettings = mapper.readValue(is, type);
        return copyOf(sessionSettings);
    } catch (UnrecognizedPropertyException e) {
        logger.info("Unrecognized field: {}", e.getMessage());
        throw new RuntimeException(format("Unrecognized field [ %s ], please check!", e.getPropertyName()));
    } catch (JsonMappingException e) {
        logger.info("{} {}", e.getMessage(), e.getPathReference());
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        closeQuietly(is);
    }
}
Also used : CollectionType(com.fasterxml.jackson.databind.type.CollectionType) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) UnrecognizedPropertyException(com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException) IOException(java.io.IOException)

Example 2 with UnrecognizedPropertyException

use of com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException 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 3 with UnrecognizedPropertyException

use of com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException in project OpenAM by OpenRock.

the class JsonPolicyParser method parsePrivilege.

private Privilege parsePrivilege(String providedName, JsonValue jsonValue) throws EntitlementException {
    try {
        // Note: this is a bit ugly as we re-serialise the JsonValue back into a JSON String to then parse it
        // again using Jackson. Unfortunately, that appears to be the easiest way as JsonValue does not support
        // data binding.
        JsonPolicy policy = MAPPER.readValue(jsonValue.toString(), JsonPolicy.class);
        Privilege privilege = policy.asPrivilege();
        if (isBlank(privilege.getName())) {
            privilege.setName(providedName);
        }
        if (isBlank(privilege.getName())) {
            throw new EntitlementException(EntitlementException.MISSING_PRIVILEGE_NAME);
        }
        // Validate the condition if present
        if (privilege.getCondition() != null) {
            privilege.getCondition().validate();
        }
        return privilege;
    } catch (UnrecognizedPropertyException ex) {
        throw new EntitlementException(EntitlementException.INVALID_VALUE, new Object[] { ex.getUnrecognizedPropertyName() });
    } catch (JsonMappingException ex) {
        throw new EntitlementException(EntitlementException.INVALID_JSON, ex, ex.getMessage());
    } catch (IOException e) {
        throw new EntitlementException(EntitlementException.UNABLE_TO_CREATE_POLICY, e);
    }
}
Also used : JsonPolicy(org.forgerock.openam.entitlement.rest.model.json.JsonPolicy) EntitlementException(com.sun.identity.entitlement.EntitlementException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) UnrecognizedPropertyException(com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException) IOException(java.io.IOException) Privilege(com.sun.identity.entitlement.Privilege)

Example 4 with UnrecognizedPropertyException

use of com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException in project jackson-databind by FasterXML.

the class DeserExceptionTypeTest method testHandlingOfUnrecognized.

public void testHandlingOfUnrecognized() throws Exception {
    UnrecognizedPropertyException exc = null;
    try {
        MAPPER.readValue("{\"bar\":3}", Bean.class);
    } catch (UnrecognizedPropertyException e) {
        exc = e;
    }
    if (exc == null) {
        fail("Should have failed binding");
    }
    assertEquals("bar", exc.getPropertyName());
    assertEquals(Bean.class, exc.getReferringClass());
    // also: should get list of known properties
    verifyException(exc, "propX");
}
Also used : UnrecognizedPropertyException(com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException)

Aggregations

UnrecognizedPropertyException (com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException)4 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)3 IOException (java.io.IOException)2 InvalidFormatException (com.fasterxml.jackson.databind.exc.InvalidFormatException)1 TreeTraversingParser (com.fasterxml.jackson.databind.node.TreeTraversingParser)1 CollectionType (com.fasterxml.jackson.databind.type.CollectionType)1 EntitlementException (com.sun.identity.entitlement.EntitlementException)1 Privilege (com.sun.identity.entitlement.Privilege)1 List (java.util.List)1 Map (java.util.Map)1 JsonPolicy (org.forgerock.openam.entitlement.rest.model.json.JsonPolicy)1