use of com.fasterxml.jackson.core.JacksonException in project app-Tiger by gematik.
the class TigerConfigurationLoader method instantiateConfigurationBean.
@SneakyThrows
public <T> Optional<T> instantiateConfigurationBean(Class<T> configurationBeanClass, String... baseKeys) {
initialize();
TreeNode targetTree = convertToTree();
final TigerConfigurationKey configurationKey = new TigerConfigurationKey(baseKeys);
for (TigerConfigurationKeyString key : configurationKey) {
if (targetTree.get(key.getValue()) == null) {
return Optional.empty();
}
targetTree = targetTree.get(key.getValue());
}
try {
return Optional.of(objectMapper.treeToValue(targetTree, configurationBeanClass));
} catch (JacksonException e) {
log.debug("Error while converting the following tree: {}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(targetTree));
throw new TigerConfigurationException("Error while reading configuration for class " + configurationBeanClass.getName() + " with base-keys " + baseKeys, e);
}
}
use of com.fasterxml.jackson.core.JacksonException in project app-Tiger by gematik.
the class TigerConfigurationLoader method instantiateConfigurationBean.
@SneakyThrows
public <T> T instantiateConfigurationBean(TypeReference<T> configurationBeanType, String... baseKeys) {
initialize();
TreeNode targetTree = convertToTree();
final TigerConfigurationKey configurationKey = new TigerConfigurationKey(baseKeys);
for (TigerConfigurationKeyString key : configurationKey) {
if (targetTree.get(key.getValue()) == null) {
return objectMapper.readValue("[]", configurationBeanType);
}
targetTree = targetTree.get(key.getValue());
}
try {
return objectMapper.treeAsTokens(targetTree).readValueAs(configurationBeanType);
} catch (JacksonException e) {
log.debug("Error while converting the following tree: {}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(targetTree));
throw new TigerConfigurationException("Error while reading configuration for class " + configurationBeanType.getType().getTypeName() + " with base-keys " + baseKeys, e);
}
}
use of com.fasterxml.jackson.core.JacksonException in project jackson-dataformats-binary by FasterXML.
the class RecordWithComplexTest method testRecordWithMissingRequiredEnumFields.
@Test
public void testRecordWithMissingRequiredEnumFields() {
RecursiveDummyRecord original = new RecursiveDummyRecord("Hello", 12353, new DummyRecord("World", 234));
original.requiredEnum = null;
//
try {
roundTrip(RecursiveDummyRecord.class, original);
fail("Should throw an exception");
} catch (JacksonException e) {
// sometimes we get this
assertThat(e).isInstanceOf(DatabindException.class);
} catch (AvroTypeException e) {
// sometimes (not wrapped)
;
}
}
use of com.fasterxml.jackson.core.JacksonException in project appng by appNG.
the class RestConfig method getDateModule.
// @formatter:on
protected <T extends Temporal> SimpleModule getDateModule(Class<T> temporal, Function<String, T> parseFunction, DateTimeFormatter formatter) {
SimpleModule module = new SimpleModule();
module.addDeserializer(temporal, new JsonDeserializer<T>() {
@Override
public T deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException, JacksonException {
if (StringUtils.isNotBlank(parser.getText())) {
return parseFunction.apply(parser.getText());
}
return null;
}
});
module.addSerializer(temporal, new JsonSerializer<T>() {
@Override
public void serialize(T value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException {
if (value != null) {
jsonGenerator.writeString(formatter.format(value));
}
}
});
LOGGER.debug("Added Module handling {}.", temporal.getName());
return module;
}
use of com.fasterxml.jackson.core.JacksonException in project appng by appNG.
the class RestConfig method defaultObjectMapper.
/**
* Creates an {@link ObjectMapper} that can (de)serialize {@link OffsetDateTime} using the
* {@link DateTimeFormatter#ISO_DATE_TIME} pattern.
*
* @return the {@link ObjectMapper}
*/
@Bean
public ObjectMapper defaultObjectMapper() {
SimpleModule dateModule = new SimpleModule();
dateModule.addSerializer(OffsetDateTime.class, new JsonSerializer<OffsetDateTime>() {
@Override
public void serialize(OffsetDateTime value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException {
if (value != null) {
jsonGenerator.writeString(DateTimeFormatter.ISO_DATE_TIME.format(value));
}
}
});
dateModule.addDeserializer(OffsetDateTime.class, new JsonDeserializer<OffsetDateTime>() {
@Override
public OffsetDateTime deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException, JacksonException {
return OffsetDateTime.parse(parser.getText(), DateTimeFormatter.ISO_DATE_TIME);
}
});
// @formatter:off
return new ObjectMapper().setDefaultPropertyInclusion(Include.NON_ABSENT).enable(SerializationFeature.INDENT_OUTPUT).disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).registerModule(dateModule);
// @formatter:on
}
Aggregations