Search in sources :

Example 6 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) Link(org.springframework.hateoas.Link) java.util(java.util) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) JsonParser(com.fasterxml.jackson.core.JsonParser) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Links(org.springframework.hateoas.Links) MediaType(org.springframework.http.MediaType) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) IOException(java.io.IOException) Configuration(org.springframework.context.annotation.Configuration) Api(net.ripe.rpki.rtr.api.Api) 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) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonObjectSerializer(org.springframework.boot.jackson.JsonObjectSerializer) ContentNegotiationConfigurer(org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) Bean(org.springframework.context.annotation.Bean) WebMvcConfigurer(org.springframework.web.servlet.config.annotation.WebMvcConfigurer) JsonObjectSerializer(org.springframework.boot.jackson.JsonObjectSerializer) 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) Link(org.springframework.hateoas.Link) JsonParser(com.fasterxml.jackson.core.JsonParser) Bean(org.springframework.context.annotation.Bean)

Example 7 with DeserializationContext

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

the class DeserializerStringCacheTest method testUppercaseConversionWithLowercasePreset.

@Test
public void testUppercaseConversionWithLowercasePreset() throws IOException {
    DeserializationContext deserializationContext = mock(DeserializationContext.class);
    DeserializerStringCache deserializerStringCache = DeserializerStringCache.from(deserializationContext);
    String lowerCaseValue = deserializerStringCache.apply("value", CacheScope.APPLICATION_SCOPE);
    assertThat(lowerCaseValue, is("value"));
    JsonParser jsonParser = mock(JsonParser.class);
    when(jsonParser.getTextCharacters()).thenReturn(new char[] { 'v', 'a', 'l', 'u', 'e' });
    when(jsonParser.getTextLength()).thenReturn(5);
    String upperCaseValue = deserializerStringCache.apply(jsonParser, CacheScope.APPLICATION_SCOPE, () -> "VALUE");
    assertThat(upperCaseValue, is("VALUE"));
}
Also used : DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 8 with DeserializationContext

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

the class DeserializerStringCacheTest method testUppercaseConversionWithLongString.

@Test
public void testUppercaseConversionWithLongString() throws IOException {
    DeserializationContext deserializationContext = mock(DeserializationContext.class);
    DeserializerStringCache deserializerStringCache = DeserializerStringCache.from(deserializationContext);
    char[] lowercaseValue = new char[1024];
    Arrays.fill(lowercaseValue, 'a');
    JsonParser jsonParser = mock(JsonParser.class);
    when(jsonParser.getText()).thenReturn(new String(lowercaseValue));
    when(jsonParser.getTextCharacters()).thenReturn(lowercaseValue);
    when(jsonParser.getTextOffset()).thenReturn(0);
    when(jsonParser.getTextLength()).thenReturn(lowercaseValue.length);
    String upperCaseValue = deserializerStringCache.apply(jsonParser, CacheScope.APPLICATION_SCOPE, () -> {
        try {
            return jsonParser.getText().toUpperCase();
        } catch (IOException ioe) {
            // not likely from mock above
            throw new IllegalStateException("mock threw unexpected exception", ioe);
        }
    });
    char[] expectedValueChars = new char[1024];
    Arrays.fill(expectedValueChars, 'A');
    String expectedValue = new String(expectedValueChars);
    assertThat(upperCaseValue, is(expectedValue));
}
Also used : DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 9 with DeserializationContext

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

the class SerializedThrowableDeserializer method deserialize.

@Override
public SerializedThrowable deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException {
    final JsonNode root = p.readValueAsTree();
    final byte[] serializedException = root.get(FIELD_NAME_SERIALIZED_THROWABLE).binaryValue();
    try {
        return InstantiationUtil.deserializeObject(serializedException, ClassLoader.getSystemClassLoader());
    } catch (ClassNotFoundException e) {
        throw new IOException("Failed to deserialize " + SerializedThrowable.class.getCanonicalName(), e);
    }
}
Also used : JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) SerializedThrowable(org.apache.flink.util.SerializedThrowable)

Example 10 with DeserializationContext

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

the class JobResultDeserializer method parseAccumulatorResults.

@SuppressWarnings("unchecked")
private Map<String, SerializedValue<OptionalFailure<Object>>> parseAccumulatorResults(final JsonParser p, final DeserializationContext ctxt) throws IOException {
    final Map<String, SerializedValue<OptionalFailure<Object>>> accumulatorResults = new HashMap<>();
    while (true) {
        final JsonToken jsonToken = p.nextToken();
        assertNotEndOfInput(p, jsonToken);
        if (jsonToken == JsonToken.END_OBJECT) {
            break;
        }
        final String accumulatorName = p.getValueAsString();
        p.nextValue();
        accumulatorResults.put(accumulatorName, (SerializedValue<OptionalFailure<Object>>) serializedValueDeserializer.deserialize(p, ctxt));
    }
    return accumulatorResults;
}
Also used : HashMap(java.util.HashMap) OptionalFailure(org.apache.flink.util.OptionalFailure) JsonToken(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken) SerializedValue(org.apache.flink.util.SerializedValue)

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