Merge pull request #16544 from maly7

* pr/16544:
  Polish "Support JsonComponent key serializers/deserialzers"
  Support JsonComponent key serializers/deserialzers

Closes gh-16544
This commit is contained in:
Phillip Webb 2019-06-02 20:47:15 -07:00
commit f9f6544d06
8 changed files with 432 additions and 42 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 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.
@ -24,16 +24,16 @@ import java.lang.annotation.Target;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.KeyDeserializer;
import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Component;
/**
* {@link Component} that provides {@link JsonSerializer} and/or {@link JsonDeserializer}
* implementations to be registered with Jackson when {@link JsonComponentModule} is in
* use. Can be used to annotate {@link JsonSerializer} or {@link JsonDeserializer}
* implementations directly or a class that contains them as inner-classes. For example:
* <pre class="code">
* {@link Component} that provides {@link JsonSerializer}, {@link JsonDeserializer} or
* {@link KeyDeserializer} implementations to be registered with Jackson when
* {@link JsonComponentModule} is in use. Can be used to annotate implementations directly
* or a class that contains them as inner-classes. For example: <pre class="code">
* &#064;JsonComponent
* public class CustomerJsonComponent {
*
@ -56,6 +56,7 @@ import org.springframework.stereotype.Component;
* @see JsonComponentModule
* @since 1.4.0
* @author Phillip Webb
* @author Paul Aly
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ -71,4 +72,44 @@ public @interface JsonComponent {
@AliasFor(annotation = Component.class)
String value() default "";
/**
* The types that are handled by the provided serializer/deserializer. This attribute
* is mandatory for a {@link KeyDeserializer}, as the type cannot be inferred. For a
* {@link JsonSerializer} or {@link JsonDeserializer} it can be used to limit handling
* to a subclasses of type inferred from the generic.
* @return the types that should be handled by the component
* @since 2.2.0
*/
Class<?>[] type() default {};
/**
* The scope under which the serializer/deserializer should be registered with the
* module.
* @return the component's handle type
* @since 2.2.0
*/
Scope scope() default Scope.VALUES;
/**
* The various scopes under which a serializer/deserialzier can be registered.
* @since 2.2.0
*/
enum Scope {
/**
* A serializer/deserializer for regular value content.
* @see JsonSerializer
* @see JsonDeserializer
*/
VALUES,
/**
* A serializer/deserializer for keys.
* @see JsonSerializer
* @see KeyDeserializer
*/
KEYS
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@ -18,26 +18,36 @@ package org.springframework.boot.jackson;
import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.function.BiConsumer;
import javax.annotation.PostConstruct;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.KeyDeserializer;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.HierarchicalBeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.boot.jackson.JsonComponent.Scope;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
* Spring Bean and Jackson {@link Module} to register {@link JsonComponent} annotated
* beans.
*
* @author Phillip Webb
* @author Paul Aly
* @since 1.4.0
* @see JsonComponent
*/
@ -72,38 +82,74 @@ public class JsonComponentModule extends SimpleModule implements BeanFactoryAwar
}
private void addJsonBean(Object bean) {
MergedAnnotation<JsonComponent> annotation = MergedAnnotations
.from(bean.getClass(), SearchStrategy.EXHAUSTIVE)
.get(JsonComponent.class);
Class<?>[] types = annotation.getClassArray("type");
Scope scope = annotation.getEnum("scope", JsonComponent.Scope.class);
addJsonBean(bean, types, scope);
}
private void addJsonBean(Object bean, Class<?>[] types, Scope scope) {
if (bean instanceof JsonSerializer) {
addSerializerWithDeducedType((JsonSerializer<?>) bean);
addJsonSerializerBean((JsonSerializer<?>) bean, scope, types);
}
if (bean instanceof JsonDeserializer) {
addDeserializerWithDeducedType((JsonDeserializer<?>) bean);
else if (bean instanceof JsonDeserializer) {
addJsonDeserializerBean((JsonDeserializer<?>) bean, types);
}
else if (bean instanceof KeyDeserializer) {
addKeyDeserializerBean((KeyDeserializer) bean, types);
}
for (Class<?> innerClass : bean.getClass().getDeclaredClasses()) {
if (!Modifier.isAbstract(innerClass.getModifiers())
&& (JsonSerializer.class.isAssignableFrom(innerClass)
|| JsonDeserializer.class.isAssignableFrom(innerClass))) {
try {
addJsonBean(innerClass.newInstance());
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
if (isSuitableInnerClass(innerClass)) {
Object innerInstance = BeanUtils.instantiateClass(innerClass);
addJsonBean(innerInstance, types, scope);
}
}
}
@SuppressWarnings({ "unchecked" })
private <T> void addSerializerWithDeducedType(JsonSerializer<T> serializer) {
ResolvableType type = ResolvableType.forClass(JsonSerializer.class,
serializer.getClass());
addSerializer((Class<T>) type.resolveGeneric(), serializer);
private boolean isSuitableInnerClass(Class<?> innerClass) {
return !Modifier.isAbstract(innerClass.getModifiers())
&& (JsonSerializer.class.isAssignableFrom(innerClass)
|| JsonDeserializer.class.isAssignableFrom(innerClass)
|| KeyDeserializer.class.isAssignableFrom(innerClass));
}
@SuppressWarnings({ "unchecked" })
private <T> void addDeserializerWithDeducedType(JsonDeserializer<T> deserializer) {
ResolvableType type = ResolvableType.forClass(JsonDeserializer.class,
deserializer.getClass());
addDeserializer((Class<T>) type.resolveGeneric(), deserializer);
@SuppressWarnings("unchecked")
private <T> void addJsonSerializerBean(JsonSerializer<T> serializer,
JsonComponent.Scope scope, Class<?>[] types) {
Class<T> baseType = (Class<T>) ResolvableType
.forClass(JsonSerializer.class, serializer.getClass()).resolveGeneric();
addBeanToModule(serializer, baseType, types,
(scope == Scope.VALUES) ? this::addSerializer : this::addKeySerializer);
}
@SuppressWarnings("unchecked")
private <T> void addJsonDeserializerBean(JsonDeserializer<T> deserializer,
Class<?>[] types) {
Class<T> baseType = (Class<T>) ResolvableType
.forClass(JsonDeserializer.class, deserializer.getClass())
.resolveGeneric();
addBeanToModule(deserializer, baseType, types, this::addDeserializer);
}
private void addKeyDeserializerBean(KeyDeserializer deserializer, Class<?>[] types) {
Assert.notEmpty(types, "Type must be specified for KeyDeserializer");
addBeanToModule(deserializer, Object.class, types, this::addKeyDeserializer);
}
@SuppressWarnings("unchecked")
private <E, T> void addBeanToModule(E element, Class<T> baseType, Class<?>[] types,
BiConsumer<Class<T>, E> consumer) {
if (ObjectUtils.isEmpty(types)) {
consumer.accept(baseType, element);
return;
}
for (Class<?> type : types) {
Assert.isAssignable(baseType, type);
consumer.accept((Class<T>) type, element);
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@ -16,6 +16,12 @@
package org.springframework.boot.jackson;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.After;
@ -24,12 +30,14 @@ import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Tests for {@link JsonComponentModule}.
*
* @author Phillip Webb
* @author Vladimir Tsanev
* @author Paul Aly
*/
public class JsonComponentModuleTests {
@ -73,6 +81,38 @@ public class JsonComponentModuleTests {
context.close();
}
@Test
public void moduleShouldRegisterKeySerializers() throws Exception {
load(OnlyKeySerializer.class);
JsonComponentModule module = this.context.getBean(JsonComponentModule.class);
assertKeySerialize(module);
}
@Test
public void moduleShouldRegisterKeyDeserializers() throws Exception {
load(OnlyKeyDeserializer.class);
JsonComponentModule module = this.context.getBean(JsonComponentModule.class);
assertKeyDeserialize(module);
}
@Test
public void moduleShouldRegisterInnerClassesForKeyHandlers() throws Exception {
load(NameAndAgeJsonKeyComponent.class);
JsonComponentModule module = this.context.getBean(JsonComponentModule.class);
assertKeySerialize(module);
assertKeyDeserialize(module);
}
@Test
public void moduleShouldRegisterOnlyForSpecifiedClasses() throws Exception {
load(NameAndCareerJsonComponent.class);
JsonComponentModule module = this.context.getBean(JsonComponentModule.class);
assertSerialize(module, new NameAndCareer("spring", "developer"),
"{\"name\":\"spring\"}");
assertSerialize(module);
assertDeserializeForSpecifiedClasses(module);
}
private void load(Class<?>... configs) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(configs);
@ -81,11 +121,17 @@ public class JsonComponentModuleTests {
this.context = context;
}
private void assertSerialize(Module module) throws Exception {
private void assertSerialize(Module module, Name value, String expectedJson)
throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module);
String json = mapper.writeValueAsString(new NameAndAge("spring", 100));
assertThat(json).isEqualToIgnoringWhitespace("{\"name\":\"spring\",\"age\":100}");
String json = mapper.writeValueAsString(value);
assertThat(json).isEqualToIgnoringWhitespace(expectedJson);
}
private void assertSerialize(Module module) throws Exception {
assertSerialize(module, new NameAndAge("spring", 100),
"{\"name\":\"spring\",\"age\":100}");
}
private void assertDeserialize(Module module) throws Exception {
@ -97,6 +143,37 @@ public class JsonComponentModuleTests {
assertThat(nameAndAge.getAge()).isEqualTo(100);
}
private void assertDeserializeForSpecifiedClasses(JsonComponentModule module)
throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module);
assertThatExceptionOfType(JsonMappingException.class).isThrownBy(() -> mapper
.readValue("{\"name\":\"spring\",\"age\":100}", NameAndAge.class));
NameAndCareer nameAndCareer = mapper.readValue(
"{\"name\":\"spring\",\"career\":\"developer\"}", NameAndCareer.class);
assertThat(nameAndCareer.getName()).isEqualTo("spring");
assertThat(nameAndCareer.getCareer()).isEqualTo("developer");
}
private void assertKeySerialize(Module module) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module);
Map<NameAndAge, Boolean> map = new HashMap<>();
map.put(new NameAndAge("spring", 100), true);
String json = mapper.writeValueAsString(map);
assertThat(json).isEqualToIgnoringWhitespace("{\"spring is 100\": true}");
}
private void assertKeyDeserialize(Module module) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module);
TypeReference<Map<NameAndAge, Boolean>> typeRef = new TypeReference<Map<NameAndAge, Boolean>>() {
};
Map<NameAndAge, Boolean> map = mapper.readValue("{\"spring is 100\": true}",
typeRef);
assertThat(map).containsEntry(new NameAndAge("spring", 100), true);
}
@JsonComponent
static class OnlySerializer extends NameAndAgeJsonComponent.Serializer {
@ -121,4 +198,14 @@ public class JsonComponentModuleTests {
}
@JsonComponent(scope = JsonComponent.Scope.KEYS)
static class OnlyKeySerializer extends NameAndAgeJsonKeyComponent.Serializer {
}
@JsonComponent(scope = JsonComponent.Scope.KEYS, type = NameAndAge.class)
static class OnlyKeyDeserializer extends NameAndAgeJsonKeyComponent.Deserializer {
}
}

View File

@ -0,0 +1,36 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.jackson;
/**
* Sample object used for tests.
*
* @author Paul Aly
*/
public class Name {
protected final String name;
public Name(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 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.
@ -16,28 +16,56 @@
package org.springframework.boot.jackson;
import org.springframework.util.ObjectUtils;
/**
* Sample object used for tests.
*
* @author Phillip Webb
* @author Paul Aly
*/
public final class NameAndAge {
private final String name;
public final class NameAndAge extends Name {
private final int age;
public NameAndAge(String name, int age) {
this.name = name;
super(name);
this.age = age;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public String asKey() {
return this.name + " is " + this.age;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (obj instanceof NameAndAge) {
NameAndAge other = (NameAndAge) obj;
boolean rtn = true;
rtn = rtn && ObjectUtils.nullSafeEquals(this.name, other.name);
rtn = rtn && ObjectUtils.nullSafeEquals(this.age, other.age);
return rtn;
}
return super.equals(obj);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ObjectUtils.nullSafeHashCode(this.name);
result = prime * result + ObjectUtils.nullSafeHashCode(this.age);
return result;
}
}

View File

@ -0,0 +1,56 @@
/*
* Copyright 2012-2019 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.jackson;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.KeyDeserializer;
import com.fasterxml.jackson.databind.SerializerProvider;
/**
* Sample {@link JsonComponent} used for tests.
*
* @author Paul Aly
*/
@JsonComponent(type = NameAndAge.class, scope = JsonComponent.Scope.KEYS)
public class NameAndAgeJsonKeyComponent {
public static class Serializer extends JsonSerializer<NameAndAge> {
@Override
public void serialize(NameAndAge value, JsonGenerator jgen,
SerializerProvider serializers) throws IOException {
jgen.writeFieldName(value.asKey());
}
}
public static class Deserializer extends KeyDeserializer {
@Override
public NameAndAge deserializeKey(String key, DeserializationContext ctxt)
throws IOException {
String[] keys = key.split("is");
return new NameAndAge(keys[0].trim(), Integer.valueOf(keys[1].trim()));
}
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.jackson;
/**
* Sample object used for tests.
*
* @author Paul Aly
*/
public class NameAndCareer extends Name {
private final String career;
public NameAndCareer(String name, String career) {
super(name);
this.career = career;
}
public String getCareer() {
return this.career;
}
}

View File

@ -0,0 +1,59 @@
/*
* Copyright 2012-2019 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.jackson;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializerProvider;
/**
* Sample {@link JsonComponent} used for tests.
*
* @author Paul Aly
*/
@JsonComponent(type = NameAndCareer.class)
public class NameAndCareerJsonComponent {
public static class Serializer extends JsonObjectSerializer<Name> {
@Override
protected void serializeObject(Name value, JsonGenerator jgen,
SerializerProvider provider) throws IOException {
jgen.writeStringField("name", value.getName());
}
}
public static class Deserializer extends JsonObjectDeserializer<Name> {
@Override
protected Name deserializeObject(JsonParser jsonParser,
DeserializationContext context, ObjectCodec codec, JsonNode tree)
throws IOException {
String name = nullSafeValue(tree.get("name"), String.class);
String career = nullSafeValue(tree.get("career"), String.class);
return new NameAndCareer(name, career);
}
}
}