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);
}
}
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);
}
}
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);
}
}
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");
}
Aggregations