Browse Source

Jackson2ObjectMapperBuilder/FactoryBean accepts deserializers by handled type

Issue: SPR-14337
pull/1073/merge
Juergen Hoeller 10 years ago
parent
commit
c412eabb00
  1. 24
      spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java
  2. 15
      spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBean.java

24
spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -284,8 +284,7 @@ public class Jackson2ObjectMapperBuilder { @@ -284,8 +284,7 @@ public class Jackson2ObjectMapperBuilder {
/**
* Configure custom serializers. Each serializer is registered for the type
* returned by {@link JsonSerializer#handledType()}, which must not be
* {@code null}.
* returned by {@link JsonSerializer#handledType()}, which must not be {@code null}.
* @see #serializersByType(Map)
*/
public Jackson2ObjectMapperBuilder serializers(JsonSerializer<?>... serializers) {
@ -324,6 +323,25 @@ public class Jackson2ObjectMapperBuilder { @@ -324,6 +323,25 @@ public class Jackson2ObjectMapperBuilder {
return this;
}
/**
* Configure custom deserializers. Each deserializer is registered for the type
* returned by {@link JsonDeserializer#handledType()}, which must not be {@code null}.
* @since 4.3
* @see #deserializersByType(Map)
*/
public Jackson2ObjectMapperBuilder deserializers(JsonDeserializer<?>... deserializers) {
if (deserializers != null) {
for (JsonDeserializer<?> deserializer : deserializers) {
Class<?> handledType = deserializer.handledType();
if (handledType == null || handledType == Object.class) {
throw new IllegalArgumentException("Unknown handled type in " + deserializer.getClass().getName());
}
this.deserializers.put(deserializer.handledType(), deserializer);
}
}
return this;
}
/**
* Configure a custom deserializer for the given type.
* @since 4.1.2

15
spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBean.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -256,8 +256,7 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper @@ -256,8 +256,7 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
/**
* Configure custom serializers. Each serializer is registered for the type
* returned by {@link JsonSerializer#handledType()}, which must not be
* {@code null}.
* returned by {@link JsonSerializer#handledType()}, which must not be {@code null}.
* @see #setSerializersByType(Map)
*/
public void setSerializers(JsonSerializer<?>... serializers) {
@ -272,6 +271,16 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper @@ -272,6 +271,16 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
this.builder.serializersByType(serializers);
}
/**
* Configure custom deserializers. Each deserializer is registered for the type
* returned by {@link JsonDeserializer#handledType()}, which must not be {@code null}.
* @since 4.3
* @see #setDeserializersByType(Map)
*/
public void setDeserializers(JsonDeserializer<?>... deserializers) {
this.builder.deserializers(deserializers);
}
/**
* Configure custom deserializers for the given types.
*/

Loading…
Cancel
Save