Search in sources :

Example 6 with JacksonException

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);
    }
}
Also used : JacksonException(com.fasterxml.jackson.core.JacksonException) TreeNode(com.fasterxml.jackson.core.TreeNode) SneakyThrows(lombok.SneakyThrows)

Example 7 with JacksonException

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);
    }
}
Also used : JacksonException(com.fasterxml.jackson.core.JacksonException) TreeNode(com.fasterxml.jackson.core.TreeNode) SneakyThrows(lombok.SneakyThrows)

Example 8 with JacksonException

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)
        ;
    }
}
Also used : JacksonException(com.fasterxml.jackson.core.JacksonException) DummyRecord(com.fasterxml.jackson.dataformat.avro.interop.DummyRecord) DatabindException(com.fasterxml.jackson.databind.DatabindException) AvroTypeException(org.apache.avro.AvroTypeException) Test(org.junit.Test)

Example 9 with JacksonException

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;
}
Also used : JacksonException(com.fasterxml.jackson.core.JacksonException) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 10 with JacksonException

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
}
Also used : JacksonException(com.fasterxml.jackson.core.JacksonException) OffsetDateTime(java.time.OffsetDateTime) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) IOException(java.io.IOException) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser) Bean(org.springframework.context.annotation.Bean)

Aggregations

JacksonException (com.fasterxml.jackson.core.JacksonException)17 IOException (java.io.IOException)9 JsonToken (com.fasterxml.jackson.core.JsonToken)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ArrayList (java.util.ArrayList)4 Response (okhttp3.Response)4 ResponseBody (okhttp3.ResponseBody)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 InvalidMailboxIdException (org.briarproject.bramble.api.mailbox.InvalidMailboxIdException)3 EDataCorruption (com.accenture.trac.common.exception.EDataCorruption)2 EUnexpected (com.accenture.trac.common.exception.EUnexpected)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)2 JsonParser (com.fasterxml.jackson.core.JsonParser)2 TreeNode (com.fasterxml.jackson.core.TreeNode)2 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)2 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)2 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 CsvFactory (com.fasterxml.jackson.dataformat.csv.CsvFactory)2