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());
}
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;
}
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);
}
}
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;
}
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;
}
Aggregations