io.micronaut.jackson.databind.convert.JacksonConverterRegistrar Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micronaut-jackson-databind Show documentation
Show all versions of micronaut-jackson-databind Show documentation
Core components supporting the Micronaut Framework
/*
* 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
*
* 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 io.micronaut.jackson.databind.convert;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ContainerNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.type.TypeFactory;
import io.micronaut.context.BeanProvider;
import io.micronaut.context.annotation.Prototype;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.convert.ArgumentConversionContext;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.convert.MutableConversionService;
import io.micronaut.core.convert.TypeConverter;
import io.micronaut.core.convert.TypeConverterRegistrar;
import io.micronaut.core.convert.value.ConvertibleValues;
import io.micronaut.core.naming.NameUtils;
import io.micronaut.core.type.Argument;
import io.micronaut.core.util.StringUtils;
import io.micronaut.jackson.JacksonConfiguration;
import jakarta.inject.Inject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Optional;
/**
* Converter registrar for Jackson.
*
* @author graemerocher
* @since 2.0
*/
@Prototype
@Internal
public class JacksonConverterRegistrar implements TypeConverterRegistrar {
private final BeanProvider objectMapper;
private final ConversionService conversionService;
/**
* Default constructor.
*
* @param objectMapper The object mapper provider
* @param conversionService The conversion service
*/
@Inject
protected JacksonConverterRegistrar(
BeanProvider objectMapper,
ConversionService conversionService) {
this.objectMapper = objectMapper;
this.conversionService = conversionService;
}
@Override
public void register(MutableConversionService conversionService) {
conversionService.addConverter(
ArrayNode.class,
Object[].class,
arrayNodeToObjectConverter()
);
conversionService.addConverter(
ArrayNode.class,
Iterable.class,
arrayNodeToIterableConverter()
);
conversionService.addConverter(
JsonNode.class,
Object.class,
jsonNodeToObjectConverter()
);
conversionService.addConverter(
ObjectNode.class,
ConvertibleValues.class,
objectNodeToConvertibleValuesConverter()
);
conversionService.addConverter(
Object.class,
JsonNode.class,
objectToJsonNodeConverter()
);
conversionService.addConverter(
CharSequence.class,
PropertyNamingStrategy.class,
(charSequence, targetType, context) -> {
Optional propertyNamingStrategy = resolvePropertyNamingStrategy(charSequence);
if (propertyNamingStrategy.isEmpty()) {
context.reject(charSequence, new IllegalArgumentException("Unable to convert '%s' to a com.fasterxml.jackson.databind.PropertyNamingStrategy".formatted(charSequence)));
}
return propertyNamingStrategy;
}
);
}
/**
* @return A converter that converts an object to a json node
*/
protected TypeConverter
© 2015 - 2024 Weber Informatics LLC | Privacy Policy