io.micronaut.jackson.modules.BeanIntrospectionModule Maven / Gradle / Ivy
/*
* Copyright 2017-2020 original 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
*
* http://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 io.micronaut.jackson.modules;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.SerializableString;
import com.fasterxml.jackson.core.io.SerializedString;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.*;
import com.fasterxml.jackson.databind.deser.impl.MethodProperty;
import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.introspect.TypeResolutionContext;
import com.fasterxml.jackson.databind.introspect.VirtualAnnotatedMember;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerBuilder;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap;
import com.fasterxml.jackson.databind.type.TypeFactory;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.annotation.AnnotationMetadata;
import io.micronaut.core.annotation.Experimental;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.beans.BeanIntrospection;
import io.micronaut.core.beans.BeanIntrospector;
import io.micronaut.core.beans.BeanProperty;
import io.micronaut.core.type.Argument;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.core.util.StringUtils;
import io.micronaut.http.hateoas.Resource;
import io.micronaut.jackson.JacksonConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Singleton;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.*;
/**
* A Jackson module that adds reflection-free bean serialization and deserialization for Micronaut.
*
* @author graemerocher
* @since 1.1
*/
@Internal
@Experimental
@Singleton
@Requires(property = JacksonConfiguration.PROPERTY_USE_BEAN_INTROSPECTION, value = StringUtils.TRUE, defaultValue = StringUtils.TRUE)
public class BeanIntrospectionModule extends SimpleModule {
private static final Logger LOG = LoggerFactory.getLogger(BeanIntrospectionModule.class);
/**
* Default constructor.
*/
public BeanIntrospectionModule() {
setDeserializerModifier(new BeanIntrospectionDeserializerModifier());
setSerializerModifier(new BeanIntrospectionSerializerModifier());
}
private JavaType newType(Argument> argument, TypeFactory typeFactory) {
return JacksonConfiguration.constructType(argument, typeFactory);
}
private PropertyMetadata newPropertyMetadata(Argument> argument, AnnotationMetadata annotationMetadata) {
final Boolean required = argument.isNonNull() ||
annotationMetadata.booleanValue(JsonProperty.class, "required").orElse(false);
int index = annotationMetadata.intValue(JsonProperty.class, "index").orElse(-1);
return PropertyMetadata.construct(
required,
annotationMetadata.stringValue(JsonPropertyDescription.class).orElse(null),
index > -1 ? index : null,
annotationMetadata.stringValue(JsonProperty.class, "defaultValue").orElse(null)
);
}
/**
* Modifies bean serialization.
*/
private class BeanIntrospectionSerializerModifier extends BeanSerializerModifier {
@Override
public BeanSerializerBuilder updateBuilder(SerializationConfig config, BeanDescription beanDesc, BeanSerializerBuilder builder) {
final Class> beanClass = beanDesc.getBeanClass();
final boolean isResource = Resource.class.isAssignableFrom(beanDesc.getBeanClass());
final BeanIntrospection