Search in sources :

Example 1 with BeanDeserializerModifier

use of com.fasterxml.jackson.databind.deser.BeanDeserializerModifier in project jackson-databind by FasterXML.

the class TestCustomEnumKeyDeserializer method testCustomEnumValueAndKeyViaModifier.

// [databind#1445]
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testCustomEnumValueAndKeyViaModifier() throws IOException {
    SimpleModule module = new SimpleModule();
    module.setDeserializerModifier(new BeanDeserializerModifier() {

        @Override
        public JsonDeserializer<Enum> modifyEnumDeserializer(DeserializationConfig config, final JavaType type, BeanDescription beanDesc, final JsonDeserializer<?> deserializer) {
            return new JsonDeserializer<Enum>() {

                @Override
                public Enum deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
                    Class<? extends Enum> rawClass = (Class<Enum<?>>) type.getRawClass();
                    final String str = p.getValueAsString().toLowerCase();
                    return KeyEnum.valueOf(rawClass, str);
                }
            };
        }

        @Override
        public KeyDeserializer modifyKeyDeserializer(DeserializationConfig config, final JavaType type, KeyDeserializer deserializer) {
            if (!type.isEnumType()) {
                return deserializer;
            }
            return new KeyDeserializer() {

                @Override
                public Object deserializeKey(String key, DeserializationContext ctxt) throws IOException {
                    Class<? extends Enum> rawClass = (Class<Enum<?>>) type.getRawClass();
                    return Enum.valueOf(rawClass, key.toLowerCase());
                }
            };
        }
    });
    ObjectMapper mapper = new ObjectMapper().registerModule(module);
    // First, enum value as is
    KeyEnum key = mapper.readValue(quote(KeyEnum.replacements.name().toUpperCase()), KeyEnum.class);
    assertSame(KeyEnum.replacements, key);
    // and then as key
    EnumMap<KeyEnum, String> map = mapper.readValue(aposToQuotes("{'REPlaceMENTS':'foobar'}"), new TypeReference<EnumMap<KeyEnum, String>>() {
    });
    assertEquals(1, map.size());
    assertSame(KeyEnum.replacements, map.keySet().iterator().next());
}
Also used : BeanDeserializerModifier(com.fasterxml.jackson.databind.deser.BeanDeserializerModifier) IOException(java.io.IOException)

Example 2 with BeanDeserializerModifier

use of com.fasterxml.jackson.databind.deser.BeanDeserializerModifier in project autorest-clientruntime-for-java by Azure.

the class FlatteningDeserializer method getModule.

/**
 * Gets a module wrapping this serializer as an adapter for the Jackson
 * ObjectMapper.
 *
 * @param mapper the object mapper for default deserializations
 * @return a simple module to be plugged onto Jackson ObjectMapper.
 */
public static SimpleModule getModule(final ObjectMapper mapper) {
    SimpleModule module = new SimpleModule();
    module.setDeserializerModifier(new BeanDeserializerModifier() {

        @Override
        public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
            if (beanDesc.getBeanClass().getAnnotation(JsonFlatten.class) != null) {
                return new FlatteningDeserializer(beanDesc.getBeanClass(), deserializer, mapper);
            }
            return deserializer;
        }
    });
    return module;
}
Also used : BeanDeserializerModifier(com.fasterxml.jackson.databind.deser.BeanDeserializerModifier) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) DeserializationConfig(com.fasterxml.jackson.databind.DeserializationConfig) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 3 with BeanDeserializerModifier

use of com.fasterxml.jackson.databind.deser.BeanDeserializerModifier in project flink by apache.

the class AWSUtil method setAwsClientConfigProperties.

/**
 * Set all prefixed properties on {@link ClientConfiguration}.
 *
 * @param config
 * @param configProps
 */
public static void setAwsClientConfigProperties(ClientConfiguration config, Properties configProps) {
    Map<String, Object> awsConfigProperties = new HashMap<>();
    for (Map.Entry<Object, Object> entry : configProps.entrySet()) {
        String key = (String) entry.getKey();
        if (key.startsWith(AWS_CLIENT_CONFIG_PREFIX)) {
            awsConfigProperties.put(key.substring(AWS_CLIENT_CONFIG_PREFIX.length()), entry.getValue());
        }
    }
    // Jackson does not like the following properties
    String[] ignorableProperties = { "secureRandom" };
    BeanDeserializerModifier modifier = new BeanDeserializerModifierForIgnorables(ClientConfiguration.class, ignorableProperties);
    DeserializerFactory factory = BeanDeserializerFactory.instance.withDeserializerModifier(modifier);
    ObjectMapper mapper = new ObjectMapper(null, null, new DefaultDeserializationContext.Impl(factory));
    JsonNode propTree = mapper.convertValue(awsConfigProperties, JsonNode.class);
    try {
        mapper.readerForUpdating(config).readValue(propTree);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : BeanDeserializerModifier(com.fasterxml.jackson.databind.deser.BeanDeserializerModifier) DefaultDeserializationContext(com.fasterxml.jackson.databind.deser.DefaultDeserializationContext) HashMap(java.util.HashMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) BeanDeserializerFactory(com.fasterxml.jackson.databind.deser.BeanDeserializerFactory) DeserializerFactory(com.fasterxml.jackson.databind.deser.DeserializerFactory) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with BeanDeserializerModifier

use of com.fasterxml.jackson.databind.deser.BeanDeserializerModifier in project nakadi by zalando.

the class JsonConfig method enumModule.

private SimpleModule enumModule() {
    // see http://stackoverflow.com/questions/24157817/jackson-databind-enum-case-insensitive
    final SimpleModule enumModule = new SimpleModule();
    enumModule.setDeserializerModifier(new BeanDeserializerModifier() {

        @Override
        public JsonDeserializer<Enum> modifyEnumDeserializer(final DeserializationConfig config, final JavaType type, final BeanDescription beanDesc, final JsonDeserializer<?> deserializer) {
            return new LowerCaseEnumJsonDeserializer(type);
        }
    });
    enumModule.addSerializer(Enum.class, new LowerCaseEnumJsonSerializer());
    return enumModule;
}
Also used : BeanDeserializerModifier(com.fasterxml.jackson.databind.deser.BeanDeserializerModifier) JavaType(com.fasterxml.jackson.databind.JavaType) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) DeserializationConfig(com.fasterxml.jackson.databind.DeserializationConfig) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 5 with BeanDeserializerModifier

use of com.fasterxml.jackson.databind.deser.BeanDeserializerModifier in project web3sdk by FISCO-BCOS.

the class ObjectMapperFactory method configureObjectMapper.

private static ObjectMapper configureObjectMapper(ObjectMapper objectMapper, boolean shouldIncludeRawResponses) {
    if (shouldIncludeRawResponses) {
        SimpleModule module = new SimpleModule();
        module.setDeserializerModifier(new BeanDeserializerModifier() {

            @Override
            public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
                if (Response.class.isAssignableFrom(beanDesc.getBeanClass())) {
                    return new RawResponseDeserializer(deserializer);
                }
                return deserializer;
            }
        });
        objectMapper.registerModule(module);
    }
    objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return objectMapper;
}
Also used : Response(org.bcos.web3j.protocol.core.Response) BeanDeserializerModifier(com.fasterxml.jackson.databind.deser.BeanDeserializerModifier) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) DeserializationConfig(com.fasterxml.jackson.databind.DeserializationConfig) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) RawResponseDeserializer(org.bcos.web3j.protocol.deserializer.RawResponseDeserializer) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Aggregations

BeanDeserializerModifier (com.fasterxml.jackson.databind.deser.BeanDeserializerModifier)5 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)3 DeserializationConfig (com.fasterxml.jackson.databind.DeserializationConfig)3 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)3 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)3 IOException (java.io.IOException)2 JavaType (com.fasterxml.jackson.databind.JavaType)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 BeanDeserializerFactory (com.fasterxml.jackson.databind.deser.BeanDeserializerFactory)1 DefaultDeserializationContext (com.fasterxml.jackson.databind.deser.DefaultDeserializationContext)1 DeserializerFactory (com.fasterxml.jackson.databind.deser.DeserializerFactory)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Response (org.bcos.web3j.protocol.core.Response)1 RawResponseDeserializer (org.bcos.web3j.protocol.deserializer.RawResponseDeserializer)1