Search in sources :

Example 26 with DeserializationContext

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext in project rpki-validator-3 by RIPE-NCC.

the class ApiConfig method customizeLinksRendering.

@Bean
public Jackson2ObjectMapperBuilderCustomizer customizeLinksRendering() {
    return (jacksonObjectMapperBuilder) -> {
        jacksonObjectMapperBuilder.serializerByType(Links.class, new JsonObjectSerializer<Links>() {

            @Override
            protected void serializeObject(Links value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
                for (Link link : value) {
                    jgen.writeStringField(link.getRel(), link.getHref());
                }
            }
        });
        jacksonObjectMapperBuilder.deserializerByType(Links.class, new JsonObjectDeserializer<Links>() {

            @Override
            protected Links deserializeObject(JsonParser jsonParser, DeserializationContext context, ObjectCodec codec, JsonNode tree) throws IOException {
                Iterator<Map.Entry<String, JsonNode>> iterator = tree.fields();
                List<Link> links = new ArrayList<>();
                while (iterator.hasNext()) {
                    Map.Entry<String, JsonNode> field = iterator.next();
                    links.add(new Link(field.getValue().asText(), field.getKey()));
                }
                return new Links(links);
            }
        });
    };
}
Also used : Jackson2ObjectMapperBuilderCustomizer(org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer) Arrays(java.util.Arrays) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Links(org.springframework.hateoas.Links) Autowired(org.springframework.beans.factory.annotation.Autowired) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) ApiLogger(net.ripe.rpki.validator3.api.ApiLogger) ArrayList(java.util.ArrayList) Api(net.ripe.rpki.validator3.api.Api) InterceptorRegistry(org.springframework.web.servlet.config.annotation.InterceptorRegistry) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) Link(org.springframework.hateoas.Link) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) JsonParser(com.fasterxml.jackson.core.JsonParser) Iterator(java.util.Iterator) MediaType(org.springframework.http.MediaType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) JsonObjectDeserializer(org.springframework.boot.jackson.JsonObjectDeserializer) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) JsonObjectSerializer(org.springframework.boot.jackson.JsonObjectSerializer) ContentNegotiationConfigurer(org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer) Bean(org.springframework.context.annotation.Bean) WebMvcConfigurer(org.springframework.web.servlet.config.annotation.WebMvcConfigurer) JsonObjectSerializer(org.springframework.boot.jackson.JsonObjectSerializer) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) JsonObjectDeserializer(org.springframework.boot.jackson.JsonObjectDeserializer) Links(org.springframework.hateoas.Links) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) Map(java.util.Map) Link(org.springframework.hateoas.Link) JsonParser(com.fasterxml.jackson.core.JsonParser) Bean(org.springframework.context.annotation.Bean)

Example 27 with DeserializationContext

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext in project incubator-servicecomb-java-chassis by apache.

the class TestAbstractDeserializer method testDeserialize.

@SuppressWarnings("unchecked")
@Test
public void testDeserialize() {
    boolean status = false;
    try {
        DeserializationContext ctxt = Mockito.mock(DeserializationContext.class);
        @SuppressWarnings("rawtypes") JsonDeserializer JsonDeserializer = Mockito.mock(JsonDeserializer.class);
        Object object = null;
        Mockito.when(jsonParser.nextFieldName()).thenReturn("abc", (String) null);
        Mockito.when(readerHelpData.getDeser()).thenReturn(JsonDeserializer);
        Mockito.when(JsonDeserializer.deserialize(jsonParser, ctxt)).thenReturn(object);
        Object deserializeObject = abstractDeserializer.deserialize(jsonParser, ctxt);
        Assert.assertNotNull(deserializeObject);
    } catch (Exception e) {
        status = true;
    }
    Assert.assertFalse(status);
}
Also used : DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) Test(org.junit.Test)

Example 28 with DeserializationContext

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext in project jocean-http by isdom.

the class MessageUtil method unserializeAsXml.

public static <T> T unserializeAsXml(final InputStream is, final Class<T> type) {
    final XmlMapper mapper = new XmlMapper();
    mapper.addHandler(new DeserializationProblemHandler() {

        @Override
        public boolean handleUnknownProperty(final DeserializationContext ctxt, final JsonParser p, final JsonDeserializer<?> deserializer, final Object beanOrClass, final String propertyName) throws IOException {
            LOG.warn("UnknownProperty [{}], just skip", propertyName);
            p.skipChildren();
            return true;
        }
    });
    try {
        return mapper.readValue(is, type);
    } catch (Exception e) {
        LOG.warn("exception when parse as xml, detail: {}", ExceptionUtils.exception2detail(e));
        return null;
    }
}
Also used : DeserializationProblemHandler(com.fasterxml.jackson.databind.deser.DeserializationProblemHandler) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) HttpObject(io.netty.handler.codec.http.HttpObject) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 29 with DeserializationContext

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext in project snow-owl by b2ihealthcare.

the class ExpansionParameterDeserializer method deserialize.

@Override
public Parameter<?> deserialize(JsonParser parser, DeserializationContext arg1) throws IOException, JsonProcessingException {
    TreeNode node = parser.readValueAsTree();
    ObjectCodec objectCodec = parser.getCodec();
    Iterator<String> fieldNames = node.fieldNames();
    FhirDataType[] parameterTypes = FhirDataType.values();
    FhirDataType parameterType = null;
    while (fieldNames.hasNext()) {
        String fieldName = (String) fieldNames.next();
        if (fieldName.startsWith(VALUE_PREFIX)) {
            parameterType = Sets.newHashSet(parameterTypes).stream().filter(t -> t.getSerializedName().equalsIgnoreCase(fieldName)).findFirst().orElseThrow(() -> new IllegalArgumentException("Unknown expansion parameter type '" + fieldName + "'."));
            break;
        }
    }
    if (parameterType == null) {
        throw new IllegalArgumentException("Invalid parameter type with null value.");
    }
    switch(parameterType) {
        case URI:
            return objectCodec.treeToValue(node, UriParameter.class);
        case CODE:
            return objectCodec.treeToValue(node, CodeParameter.class);
        case DATETIME:
            return objectCodec.treeToValue(node, DateTimeParameter.class);
        case STRING:
            return objectCodec.treeToValue(node, StringParameter.class);
        case BOOLEAN:
            return objectCodec.treeToValue(node, BooleanParameter.class);
        case DECIMAL:
            return objectCodec.treeToValue(node, DecimalParameter.class);
        case INTEGER:
            return objectCodec.treeToValue(node, IntegerParameter.class);
        default:
            throw new IllegalArgumentException("Unsupported property type '" + parameterType + "'.");
    }
}
Also used : StdDeserializer(com.fasterxml.jackson.databind.deser.std.StdDeserializer) TreeNode(com.fasterxml.jackson.core.TreeNode) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) Iterator(java.util.Iterator) JsonParser(com.fasterxml.jackson.core.JsonParser) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) FhirDataType(com.b2international.snowowl.fhir.core.model.dt.FhirDataType) Sets(com.google.common.collect.Sets) TreeNode(com.fasterxml.jackson.core.TreeNode) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) FhirDataType(com.b2international.snowowl.fhir.core.model.dt.FhirDataType)

Example 30 with DeserializationContext

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext in project presto by prestodb.

the class TestLocalProperties method testJsonSerialization.

@Test
public void testJsonSerialization() throws Exception {
    ObjectMapper mapper = new JsonObjectMapperProvider().get().registerModule(new SimpleModule().addDeserializer(ColumnHandle.class, new JsonDeserializer<ColumnHandle>() {

        @Override
        public ColumnHandle deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
            return new JsonObjectMapperProvider().get().readValue(jsonParser, TestingColumnHandle.class);
        }
    }));
    TestingColumnHandle columnHandle = new TestingColumnHandle("a");
    LocalProperty<ColumnHandle> property1 = new ConstantProperty<>(columnHandle);
    assertEquals(property1, mapper.readValue(mapper.writeValueAsString(property1), new TypeReference<LocalProperty<ColumnHandle>>() {
    }));
    LocalProperty<ColumnHandle> property2 = new SortingProperty<>(columnHandle, SortOrder.ASC_NULLS_FIRST);
    assertEquals(property2, mapper.readValue(mapper.writeValueAsString(property2), new TypeReference<LocalProperty<ColumnHandle>>() {
    }));
    LocalProperty<ColumnHandle> property3 = new GroupingProperty<>(ImmutableList.of(columnHandle));
    assertEquals(property3, mapper.readValue(mapper.writeValueAsString(property3), new TypeReference<LocalProperty<ColumnHandle>>() {
    }));
}
Also used : TestingColumnHandle(com.facebook.presto.testing.TestingMetadata.TestingColumnHandle) ColumnHandle(com.facebook.presto.spi.ColumnHandle) ConstantProperty(com.facebook.presto.spi.ConstantProperty) GroupingProperty(com.facebook.presto.spi.GroupingProperty) SortingProperty(com.facebook.presto.spi.SortingProperty) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) TestingColumnHandle(com.facebook.presto.testing.TestingMetadata.TestingColumnHandle) JsonObjectMapperProvider(com.facebook.airlift.json.JsonObjectMapperProvider) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.testng.annotations.Test)

Aggregations

DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)28 JsonParser (com.fasterxml.jackson.core.JsonParser)21 IOException (java.io.IOException)17 JsonNode (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode)12 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)11 Test (org.junit.Test)10 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)8 ObjectCodec (com.fasterxml.jackson.core.ObjectCodec)7 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)7 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)7 BeanProperty (com.fasterxml.jackson.databind.BeanProperty)6 StdDeserializer (com.fasterxml.jackson.databind.deser.std.StdDeserializer)6 Map (java.util.Map)6 InjectableValues (com.fasterxml.jackson.databind.InjectableValues)5 List (java.util.List)5 ObjectNode (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode)5 TypeReference (com.fasterxml.jackson.core.type.TypeReference)4 Iterator (java.util.Iterator)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)3