Search in sources :

Example 46 with JsonMappingException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonMappingException in project spring-framework by spring-projects.

the class Jackson2ObjectMapperBuilderTests method completeSetup.

@Test
void completeSetup() throws JsonMappingException {
    NopAnnotationIntrospector introspector = NopAnnotationIntrospector.instance;
    Map<Class<?>, JsonDeserializer<?>> deserializerMap = new HashMap<>();
    JsonDeserializer<Date> deserializer = new DateDeserializers.DateDeserializer();
    deserializerMap.put(Date.class, deserializer);
    JsonSerializer<Class<?>> serializer1 = new ClassSerializer();
    JsonSerializer<Number> serializer2 = new NumberSerializer(Integer.class);
    Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json().modules(// Disable well-known modules detection
    new ArrayList<>()).serializers(serializer1).serializersByType(Collections.singletonMap(Boolean.class, serializer2)).deserializersByType(deserializerMap).annotationIntrospector(introspector).annotationIntrospector(current -> AnnotationIntrospector.pair(current, introspector)).featuresToEnable(SerializationFeature.FAIL_ON_EMPTY_BEANS, DeserializationFeature.UNWRAP_ROOT_VALUE, JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS).featuresToDisable(MapperFeature.AUTO_DETECT_GETTERS, MapperFeature.AUTO_DETECT_FIELDS, JsonParser.Feature.AUTO_CLOSE_SOURCE, JsonGenerator.Feature.QUOTE_FIELD_NAMES).serializationInclusion(JsonInclude.Include.NON_NULL);
    ObjectMapper mapper = new ObjectMapper();
    builder.configure(mapper);
    assertThat(getSerializerFactoryConfig(mapper).hasSerializers()).isTrue();
    assertThat(getDeserializerFactoryConfig(mapper).hasDeserializers()).isTrue();
    Serializers serializers = getSerializerFactoryConfig(mapper).serializers().iterator().next();
    assertThat(serializers.findSerializer(null, SimpleType.construct(Class.class), null)).isSameAs(serializer1);
    assertThat(serializers.findSerializer(null, SimpleType.construct(Boolean.class), null)).isSameAs(serializer2);
    assertThat(serializers.findSerializer(null, SimpleType.construct(Number.class), null)).isNull();
    Deserializers deserializers = getDeserializerFactoryConfig(mapper).deserializers().iterator().next();
    assertThat(deserializers.findBeanDeserializer(SimpleType.construct(Date.class), null, null)).isSameAs(deserializer);
    AnnotationIntrospectorPair pair1 = (AnnotationIntrospectorPair) mapper.getSerializationConfig().getAnnotationIntrospector();
    AnnotationIntrospectorPair pair2 = (AnnotationIntrospectorPair) mapper.getDeserializationConfig().getAnnotationIntrospector();
    assertThat(pair1.allIntrospectors()).containsExactly(introspector, introspector);
    assertThat(pair2.allIntrospectors()).containsExactly(introspector, introspector);
    assertThat(mapper.getSerializationConfig().isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS)).isTrue();
    assertThat(mapper.getDeserializationConfig().isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE)).isTrue();
    assertThat(mapper.getFactory().isEnabled(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER)).isTrue();
    assertThat(mapper.getFactory().isEnabled(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS)).isTrue();
    assertThat(mapper.getSerializationConfig().isEnabled(MapperFeature.AUTO_DETECT_GETTERS)).isFalse();
    assertThat(mapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)).isFalse();
    assertThat(mapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)).isFalse();
    assertThat(mapper.getDeserializationConfig().isEnabled(MapperFeature.AUTO_DETECT_FIELDS)).isFalse();
    assertThat(mapper.getFactory().isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE)).isFalse();
    assertThat(mapper.getFactory().isEnabled(JsonGenerator.Feature.QUOTE_FIELD_NAMES)).isFalse();
    assertThat(mapper.getSerializationConfig().getSerializationInclusion()).isSameAs(JsonInclude.Include.NON_NULL);
}
Also used : Module(com.fasterxml.jackson.databind.Module) Arrays(java.util.Arrays) JacksonAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector) AnnotationIntrospector(com.fasterxml.jackson.databind.AnnotationIntrospector) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SimpleBeanPropertyFilter(com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) Locale(java.util.Locale) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) Map(java.util.Map) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) DeserializerFactoryConfig(com.fasterxml.jackson.databind.cfg.DeserializerFactoryConfig) XmlFactory(com.fasterxml.jackson.dataformat.xml.XmlFactory) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) Path(java.nio.file.Path) IntRange(kotlin.ranges.IntRange) NopAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector) TimeZone(java.util.TimeZone) Visibility(com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility) Version(com.fasterxml.jackson.core.Version) SerializerFactoryConfig(com.fasterxml.jackson.databind.cfg.SerializerFactoryConfig) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) Test(org.junit.jupiter.api.Test) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) BasicDeserializerFactory(com.fasterxml.jackson.databind.deser.BasicDeserializerFactory) BasicSerializerFactory(com.fasterxml.jackson.databind.ser.BasicSerializerFactory) Optional(java.util.Optional) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) Serializers(com.fasterxml.jackson.databind.ser.Serializers) SimpleType(com.fasterxml.jackson.databind.type.SimpleType) CBORFactory(com.fasterxml.jackson.dataformat.cbor.CBORFactory) AnnotationIntrospectorPair(com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair) ArrayList(java.util.ArrayList) MapperFeature(com.fasterxml.jackson.databind.MapperFeature) Deserializers(com.fasterxml.jackson.databind.deser.Deserializers) ClassSerializer(com.fasterxml.jackson.databind.ser.std.ClassSerializer) DateDeserializers(com.fasterxml.jackson.databind.deser.std.DateDeserializers) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) StreamSupport(java.util.stream.StreamSupport) PropertyAccessor(com.fasterxml.jackson.annotation.PropertyAccessor) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) PropertyNamingStrategy(com.fasterxml.jackson.databind.PropertyNamingStrategy) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) JsonParser(com.fasterxml.jackson.core.JsonParser) FatalBeanException(org.springframework.beans.FatalBeanException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleSerializers(com.fasterxml.jackson.databind.module.SimpleSerializers) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SmileFactory(com.fasterxml.jackson.dataformat.smile.SmileFactory) JsonFilter(com.fasterxml.jackson.annotation.JsonFilter) Paths(java.nio.file.Paths) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) Collections(java.util.Collections) NumberSerializer(com.fasterxml.jackson.databind.ser.std.NumberSerializer) StringUtils(org.springframework.util.StringUtils) AnnotationIntrospectorPair(com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair) NopAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector) HashMap(java.util.HashMap) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) Date(java.util.Date) ClassSerializer(com.fasterxml.jackson.databind.ser.std.ClassSerializer) NumberSerializer(com.fasterxml.jackson.databind.ser.std.NumberSerializer) Deserializers(com.fasterxml.jackson.databind.deser.Deserializers) DateDeserializers(com.fasterxml.jackson.databind.deser.std.DateDeserializers) Serializers(com.fasterxml.jackson.databind.ser.Serializers) SimpleSerializers(com.fasterxml.jackson.databind.module.SimpleSerializers) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 47 with JsonMappingException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonMappingException in project dropwizard by dropwizard.

the class JsonHealthResponseProviderTest method shouldThrowExceptionWhenJsonProcessorExceptionOccurs.

@Test
void shouldThrowExceptionWhenJsonProcessorExceptionOccurs() throws IOException {
    // given
    final ObjectMapper mapperMock = mock(ObjectMapper.class);
    this.jsonHealthResponseProvider = new JsonHealthResponseProvider(healthStatusChecker, healthStateAggregator, mapperMock);
    final HealthStateView view = new HealthStateView("foo", true, HealthCheckType.READY, true);
    final Map<String, Collection<String>> queryParams = Collections.singletonMap(JsonHealthResponseProvider.NAME_QUERY_PARAM, Collections.singleton(view.getName()));
    final JsonMappingException exception = JsonMappingException.fromUnexpectedIOE(new IOException("uh oh"));
    // when
    when(healthStateAggregator.healthStateView(view.getName())).thenReturn(Optional.of(view));
    when(mapperMock.writeValueAsString(any())).thenThrow(exception);
    // then
    assertThatThrownBy(() -> jsonHealthResponseProvider.healthResponse(queryParams)).isInstanceOf(RuntimeException.class).hasCauseReference(exception);
    verifyNoInteractions(healthStatusChecker);
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) HealthStateView(io.dropwizard.health.HealthStateView) Collection(java.util.Collection) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 48 with JsonMappingException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonMappingException in project symja_android_library by axkr.

the class APIExample method main.

public static void main(String[] args) {
    ObjectMapper mapper = new ObjectMapper();
    try {
        JsonNode node = mapper.readTree(new URL(BASE_URL + "/v1/api?i=D(sin(x)%2Cx)&f=plaintext&appid=DEMO"));
        System.out.println(node.toPrettyString());
    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) URL(java.net.URL)

Example 49 with JsonMappingException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonMappingException in project hbase by apache.

the class ScannerResource method update.

Response update(final ScannerModel model, final boolean replace, final UriInfo uriInfo) {
    servlet.getMetrics().incrementRequests(1);
    if (servlet.isReadOnly()) {
        return Response.status(Response.Status.FORBIDDEN).type(MIMETYPE_TEXT).entity("Forbidden" + CRLF).build();
    }
    byte[] endRow = model.hasEndRow() ? model.getEndRow() : null;
    RowSpec spec = null;
    if (model.getLabels() != null) {
        spec = new RowSpec(model.getStartRow(), endRow, model.getColumns(), model.getStartTime(), model.getEndTime(), model.getMaxVersions(), model.getLabels());
    } else {
        spec = new RowSpec(model.getStartRow(), endRow, model.getColumns(), model.getStartTime(), model.getEndTime(), model.getMaxVersions());
    }
    try {
        Filter filter = ScannerResultGenerator.buildFilterFromModel(model);
        String tableName = tableResource.getName();
        ScannerResultGenerator gen = new ScannerResultGenerator(tableName, spec, filter, model.getCaching(), model.getCacheBlocks(), model.getLimit());
        String id = gen.getID();
        ScannerInstanceResource instance = new ScannerInstanceResource(tableName, id, gen, model.getBatch());
        scanners.put(id, instance);
        if (LOG.isTraceEnabled()) {
            LOG.trace("new scanner: " + id);
        }
        UriBuilder builder = uriInfo.getAbsolutePathBuilder();
        URI uri = builder.path(id).build();
        servlet.getMetrics().incrementSucessfulPutRequests(1);
        return Response.created(uri).build();
    } catch (Exception e) {
        LOG.error("Exception occurred while processing " + uriInfo.getAbsolutePath() + " : ", e);
        servlet.getMetrics().incrementFailedPutRequests(1);
        if (e instanceof TableNotFoundException) {
            return Response.status(Response.Status.NOT_FOUND).type(MIMETYPE_TEXT).entity("Not found" + CRLF).build();
        } else if (e instanceof RuntimeException || e instanceof JsonMappingException | e instanceof JsonParseException) {
            return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request" + CRLF).build();
        }
        return Response.status(Response.Status.SERVICE_UNAVAILABLE).type(MIMETYPE_TEXT).entity("Unavailable" + CRLF).build();
    }
}
Also used : JsonParseException(com.fasterxml.jackson.core.JsonParseException) URI(java.net.URI) TableNotFoundException(org.apache.hadoop.hbase.TableNotFoundException) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) TableNotFoundException(org.apache.hadoop.hbase.TableNotFoundException) Filter(org.apache.hadoop.hbase.filter.Filter) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) UriBuilder(org.apache.hbase.thirdparty.javax.ws.rs.core.UriBuilder)

Example 50 with JsonMappingException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonMappingException in project druid by druid-io.

the class QueryGranularityTest method testSerializePeriod.

@Test
public void testSerializePeriod() throws Exception {
    final ObjectMapper mapper = new DefaultObjectMapper();
    String json = "{ \"type\": \"period\", \"period\": \"P1D\" }";
    Granularity gran = mapper.readValue(json, Granularity.class);
    Assert.assertEquals(new PeriodGranularity(new Period("P1D"), null, null), gran);
    // Nonstandard period
    json = "{ \"type\": \"period\", \"period\": \"P2D\" }";
    gran = mapper.readValue(json, Granularity.class);
    Assert.assertEquals(new PeriodGranularity(new Period("P2D"), null, null), gran);
    // Set timeZone, origin
    json = "{ \"type\": \"period\", \"period\": \"P1D\"," + "\"timeZone\": \"America/Los_Angeles\", \"origin\": \"1970-01-01T00:00:00Z\"}";
    gran = mapper.readValue(json, Granularity.class);
    Assert.assertEquals(new PeriodGranularity(new Period("P1D"), DateTimes.EPOCH, DateTimes.inferTzFromString("America/Los_Angeles")), gran);
    PeriodGranularity expected = new PeriodGranularity(new Period("P1D"), DateTimes.of("2012-01-01"), DateTimes.inferTzFromString("America/Los_Angeles"));
    String jsonOut = mapper.writeValueAsString(expected);
    Assert.assertEquals(expected, mapper.readValue(jsonOut, Granularity.class));
    String illegalJson = "{ \"type\": \"period\", \"period\": \"P0D\" }";
    try {
        mapper.readValue(illegalJson, Granularity.class);
        Assert.fail();
    } catch (JsonMappingException e) {
    }
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) PeriodGranularity(org.apache.druid.java.util.common.granularity.PeriodGranularity) Period(org.joda.time.Period) DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) Granularity(org.apache.druid.java.util.common.granularity.Granularity) PeriodGranularity(org.apache.druid.java.util.common.granularity.PeriodGranularity) DurationGranularity(org.apache.druid.java.util.common.granularity.DurationGranularity) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) Test(org.junit.Test)

Aggregations

JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)185 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)93 IOException (java.io.IOException)80 JsonParseException (com.fasterxml.jackson.core.JsonParseException)57 Test (org.junit.Test)45 ATTest (org.jboss.eap.additional.testsuite.annotations.ATTest)33 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)24 ArrayList (java.util.ArrayList)16 Map (java.util.Map)16 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 File (java.io.File)15 HashMap (java.util.HashMap)15 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)13 InputStream (java.io.InputStream)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)10 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 List (java.util.List)8 Writer (org.alfresco.rest.framework.jacksonextensions.JacksonHelper.Writer)7 Test (org.junit.jupiter.api.Test)6