Skip to content

Commit bf633fc

Browse files
committed
Polish "Support JsonComponent key serializers/deserialzers"
See gh-16544
1 parent 361efc7 commit bf633fc

File tree

6 files changed

+97
-94
lines changed

6 files changed

+97
-94
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jackson/JsonComponent.java

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,11 +30,10 @@
3030
import org.springframework.stereotype.Component;
3131

3232
/**
33-
* {@link Component} that provides {@link JsonSerializer} and/or {@link JsonDeserializer}
34-
* implementations to be registered with Jackson when {@link JsonComponentModule} is in
35-
* use. Can be used to annotate {@link JsonSerializer}, {@link JsonDeserializer}, or
36-
* {@link KeyDeserializer} implementations directly or a class that contains them as
37-
* inner-classes. For example: <pre class="code">
33+
* {@link Component} that provides {@link JsonSerializer}, {@link JsonDeserializer} or
34+
* {@link KeyDeserializer} implementations to be registered with Jackson when
35+
* {@link JsonComponentModule} is in use. Can be used to annotate implementations directly
36+
* or a class that contains them as inner-classes. For example: <pre class="code">
3837
* &#064;JsonComponent
3938
* public class CustomerJsonComponent {
4039
*
@@ -57,6 +56,7 @@
5756
* @see JsonComponentModule
5857
* @since 1.4.0
5958
* @author Phillip Webb
59+
* @author Paul Aly
6060
*/
6161
@Target(ElementType.TYPE)
6262
@Retention(RetentionPolicy.RUNTIME)
@@ -73,33 +73,40 @@
7373
String value() default "";
7474

7575
/**
76-
* Indicates whether the component should be registered as a type serializer and/or
77-
* deserializer or a key serializer and/or deserializer.
78-
* @return the component's handle type
76+
* The types that are handled by the provided serializer/deserializer. This attribute
77+
* is mandatory for a {@link KeyDeserializer}, as the type cannot be inferred. For a
78+
* {@link JsonSerializer} or {@link JsonDeserializer} it can be used to limit handling
79+
* to a subclasses of type inferred from the generic.
80+
* @return the types that should be handled by the component
81+
* @since 2.2.0
7982
*/
80-
Handle handle() default Handle.TYPES;
83+
Class<?>[] type() default {};
8184

8285
/**
83-
* Specify the classes handled by the serialization and/or deserialization of the
84-
* component. Necessary to be specified for a {@link KeyDeserializer}, as the type
85-
* cannot be inferred. On other types can be used to only handle a subset of
86-
* subclasses.
87-
* @return the classes that should be handled by the component
86+
* The scope under which the serializer/deserializer should be registered with the
87+
* module.
88+
* @return the component's handle type
89+
* @since 2.2.0
8890
*/
89-
Class<?>[] handleClasses() default {};
91+
Scope scope() default Scope.VALUES;
9092

9193
/**
92-
* An enumeration of possible handling types for the component.
94+
* The various scopes under which a serializer/deserialzier can be registered.
95+
* @since 2.2.0
9396
*/
94-
enum Handle {
97+
enum Scope {
9598

9699
/**
97-
* Register the component as a Type serializer and/or deserializer.
100+
* A serializer/deserializer for regular value content.
101+
* @see JsonSerializer
102+
* @see JsonDeserializer
98103
*/
99-
TYPES,
104+
VALUES,
100105

101106
/**
102-
* Register the component as a Key serializer and/or deserializer.
107+
* A serializer/deserializer for keys.
108+
* @see JsonSerializer
109+
* @see KeyDeserializer
103110
*/
104111
KEYS
105112

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jackson/JsonComponentModule.java

Lines changed: 59 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.lang.reflect.Modifier;
2020
import java.util.Map;
21+
import java.util.function.BiConsumer;
2122

2223
import javax.annotation.PostConstruct;
2324

@@ -27,13 +28,19 @@
2728
import com.fasterxml.jackson.databind.Module;
2829
import com.fasterxml.jackson.databind.module.SimpleModule;
2930

31+
import org.springframework.beans.BeanUtils;
3032
import org.springframework.beans.BeansException;
3133
import org.springframework.beans.factory.BeanFactory;
3234
import org.springframework.beans.factory.BeanFactoryAware;
3335
import org.springframework.beans.factory.HierarchicalBeanFactory;
3436
import org.springframework.beans.factory.ListableBeanFactory;
37+
import org.springframework.boot.jackson.JsonComponent.Scope;
3538
import org.springframework.core.ResolvableType;
36-
import org.springframework.core.annotation.AnnotationUtils;
39+
import org.springframework.core.annotation.MergedAnnotation;
40+
import org.springframework.core.annotation.MergedAnnotations;
41+
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
42+
import org.springframework.util.Assert;
43+
import org.springframework.util.ObjectUtils;
3744

3845
/**
3946
* Spring Bean and Jackson {@link Module} to register {@link JsonComponent} annotated
@@ -70,88 +77,78 @@ private void addJsonBeans(ListableBeanFactory beanFactory) {
7077
Map<String, Object> beans = beanFactory
7178
.getBeansWithAnnotation(JsonComponent.class);
7279
for (Object bean : beans.values()) {
73-
JsonComponent annotation = AnnotationUtils.findAnnotation(bean.getClass(),
74-
JsonComponent.class);
75-
addJsonBean(bean, annotation);
80+
addJsonBean(bean);
7681
}
7782
}
7883

79-
private void addJsonBean(Object bean, JsonComponent annotation) {
84+
private void addJsonBean(Object bean) {
85+
MergedAnnotation<JsonComponent> annotation = MergedAnnotations
86+
.from(bean.getClass(), SearchStrategy.EXHAUSTIVE)
87+
.get(JsonComponent.class);
88+
Class<?>[] types = annotation.getClassArray("type");
89+
Scope scope = annotation.getEnum("scope", JsonComponent.Scope.class);
90+
addJsonBean(bean, types, scope);
91+
}
92+
93+
private void addJsonBean(Object bean, Class<?>[] types, Scope scope) {
8094
if (bean instanceof JsonSerializer) {
81-
addSerializerForTypes((JsonSerializer<?>) bean, annotation.handle(),
82-
annotation.handleClasses());
95+
addJsonSerializerBean((JsonSerializer<?>) bean, scope, types);
8396
}
84-
if (bean instanceof KeyDeserializer) {
85-
addKeyDeserializerForTypes((KeyDeserializer) bean,
86-
annotation.handleClasses());
97+
else if (bean instanceof JsonDeserializer) {
98+
addJsonDeserializerBean((JsonDeserializer<?>) bean, types);
8799
}
88-
if (bean instanceof JsonDeserializer) {
89-
addDeserializerForTypes((JsonDeserializer<?>) bean,
90-
annotation.handleClasses());
100+
else if (bean instanceof KeyDeserializer) {
101+
addKeyDeserializerBean((KeyDeserializer) bean, types);
91102
}
92103
for (Class<?> innerClass : bean.getClass().getDeclaredClasses()) {
93-
if (!Modifier.isAbstract(innerClass.getModifiers())
94-
&& (JsonSerializer.class.isAssignableFrom(innerClass)
95-
|| JsonDeserializer.class.isAssignableFrom(innerClass)
96-
|| KeyDeserializer.class.isAssignableFrom(innerClass))) {
97-
try {
98-
addJsonBean(innerClass.newInstance(), annotation);
99-
}
100-
catch (Exception ex) {
101-
throw new IllegalStateException(ex);
102-
}
104+
if (isSuitableInnerClass(innerClass)) {
105+
Object innerInstance = BeanUtils.instantiateClass(innerClass);
106+
addJsonBean(innerInstance, types, scope);
103107
}
104108
}
105109
}
106110

107-
@SuppressWarnings({ "unchecked" })
108-
private <T> void addSerializerForTypes(JsonSerializer<T> serializer,
109-
JsonComponent.Handle handle, Class<?>[] types) {
110-
for (Class<?> type : types) {
111-
addSerializerWithType(serializer, handle, (Class<T>) type);
112-
}
113-
114-
if (types.length == 0) {
115-
ResolvableType type = ResolvableType.forClass(JsonSerializer.class,
116-
serializer.getClass());
117-
addSerializerWithType(serializer, handle, (Class<T>) type.resolveGeneric());
118-
}
111+
private boolean isSuitableInnerClass(Class<?> innerClass) {
112+
return !Modifier.isAbstract(innerClass.getModifiers())
113+
&& (JsonSerializer.class.isAssignableFrom(innerClass)
114+
|| JsonDeserializer.class.isAssignableFrom(innerClass)
115+
|| KeyDeserializer.class.isAssignableFrom(innerClass));
119116
}
120117

121-
private <T> void addSerializerWithType(JsonSerializer<T> serializer,
122-
JsonComponent.Handle handle, Class<? extends T> type) {
123-
if (JsonComponent.Handle.KEYS.equals(handle)) {
124-
addKeySerializer(type, serializer);
125-
}
126-
else {
127-
addSerializer(type, serializer);
128-
}
118+
@SuppressWarnings("unchecked")
119+
private <T> void addJsonSerializerBean(JsonSerializer<T> serializer,
120+
JsonComponent.Scope scope, Class<?>[] types) {
121+
Class<T> baseType = (Class<T>) ResolvableType
122+
.forClass(JsonSerializer.class, serializer.getClass()).resolveGeneric();
123+
addBeanToModule(serializer, baseType, types,
124+
(scope == Scope.VALUES) ? this::addSerializer : this::addKeySerializer);
125+
129126
}
130127

131-
@SuppressWarnings({ "unchecked" })
132-
private <T> void addDeserializerForTypes(JsonDeserializer<T> deserializer,
128+
@SuppressWarnings("unchecked")
129+
private <T> void addJsonDeserializerBean(JsonDeserializer<T> deserializer,
133130
Class<?>[] types) {
134-
for (Class<?> type : types) {
135-
addDeserializer((Class<T>) type, deserializer);
136-
}
137-
138-
if (types.length == 0) {
139-
addDeserializerWithDeducedType(deserializer);
140-
}
131+
Class<T> baseType = (Class<T>) ResolvableType
132+
.forClass(JsonDeserializer.class, deserializer.getClass())
133+
.resolveGeneric();
134+
addBeanToModule(deserializer, baseType, types, this::addDeserializer);
141135
}
142136

143-
@SuppressWarnings({ "unchecked" })
144-
private <T> void addDeserializerWithDeducedType(JsonDeserializer<T> deserializer) {
145-
ResolvableType type = ResolvableType.forClass(JsonDeserializer.class,
146-
deserializer.getClass());
147-
addDeserializer((Class<T>) type.resolveGeneric(), deserializer);
148-
137+
private void addKeyDeserializerBean(KeyDeserializer deserializer, Class<?>[] types) {
138+
Assert.notEmpty(types, "Type must be specified for KeyDeserializer");
139+
addBeanToModule(deserializer, Object.class, types, this::addKeyDeserializer);
149140
}
150141

151-
private void addKeyDeserializerForTypes(KeyDeserializer deserializer,
152-
Class<?>[] types) {
142+
@SuppressWarnings("unchecked")
143+
private <E, T> void addBeanToModule(E element, Class<T> baseType, Class<?>[] types,
144+
BiConsumer<Class<T>, E> consumer) {
145+
if (ObjectUtils.isEmpty(types)) {
146+
consumer.accept(baseType, element);
147+
return;
148+
}
153149
for (Class<?> type : types) {
154-
addKeyDeserializer(type, deserializer);
150+
Assert.isAssignable(baseType, type);
151+
consumer.accept((Class<T>) type, element);
155152
}
156153
}
157154

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jackson/JsonComponentModuleTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -198,12 +198,12 @@ static class ConcreteSerializer extends AbstractSerializer {
198198

199199
}
200200

201-
@JsonComponent(handle = JsonComponent.Handle.KEYS)
201+
@JsonComponent(scope = JsonComponent.Scope.KEYS)
202202
static class OnlyKeySerializer extends NameAndAgeJsonKeyComponent.Serializer {
203203

204204
}
205205

206-
@JsonComponent(handle = JsonComponent.Handle.KEYS, handleClasses = NameAndAge.class)
206+
@JsonComponent(scope = JsonComponent.Scope.KEYS, type = NameAndAge.class)
207207
static class OnlyKeyDeserializer extends NameAndAgeJsonKeyComponent.Deserializer {
208208

209209
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jackson/NameAndAge.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
* Sample object used for tests.
2323
*
2424
* @author Phillip Webb
25+
* @author Paul Aly
2526
*/
2627
public final class NameAndAge extends Name {
2728

@@ -37,7 +38,7 @@ public int getAge() {
3738
}
3839

3940
public String asKey() {
40-
return name + " is " + age;
41+
return this.name + " is " + this.age;
4142
}
4243

4344
@Override
@@ -48,15 +49,13 @@ public boolean equals(Object obj) {
4849
if (obj == null) {
4950
return false;
5051
}
51-
5252
if (obj instanceof NameAndAge) {
5353
NameAndAge other = (NameAndAge) obj;
5454
boolean rtn = true;
5555
rtn = rtn && ObjectUtils.nullSafeEquals(this.name, other.name);
5656
rtn = rtn && ObjectUtils.nullSafeEquals(this.age, other.age);
5757
return rtn;
5858
}
59-
6059
return super.equals(obj);
6160
}
6261

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jackson/NameAndAgeJsonKeyComponent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,7 +29,7 @@
2929
*
3030
* @author Paul Aly
3131
*/
32-
@JsonComponent(handle = JsonComponent.Handle.KEYS, handleClasses = NameAndAge.class)
32+
@JsonComponent(type = NameAndAge.class, scope = JsonComponent.Scope.KEYS)
3333
public class NameAndAgeJsonKeyComponent {
3434

3535
public static class Serializer extends JsonSerializer<NameAndAge> {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jackson/NameAndCareerJsonComponent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,7 +30,7 @@
3030
*
3131
* @author Paul Aly
3232
*/
33-
@JsonComponent(handleClasses = NameAndCareer.class)
33+
@JsonComponent(type = NameAndCareer.class)
3434
public class NameAndCareerJsonComponent {
3535

3636
public static class Serializer extends JsonObjectSerializer<Name> {

0 commit comments

Comments
 (0)