io.micronaut.jackson.convert.JacksonConverterRegistrar 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
*
* 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.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.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.core.bind.ArgumentBinder;
import io.micronaut.core.bind.BeanPropertyBinder;
import io.micronaut.core.convert.*;
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 javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import java.util.*;
import java.io.IOException;
/**
* Converter registrar for Jackson.
*
* @author graemerocher
* @since 2.0
*/
@Singleton
public class JacksonConverterRegistrar implements TypeConverterRegistrar {
private final BeanProvider objectMapper;
private final ConversionService> conversionService;
private final BeanProvider beanPropertyBinder;
/**
* Default constructor.
* @param objectMapper The object mapper provider
* @param beanPropertyBinder The bean property binder provider
* @param conversionService The conversion service
*/
@Deprecated
protected JacksonConverterRegistrar(
Provider objectMapper,
Provider beanPropertyBinder,
ConversionService> conversionService) {
this.objectMapper = objectMapper::get;
this.conversionService = conversionService;
this.beanPropertyBinder = beanPropertyBinder::get;
}
/**
* Default constructor.
* @param objectMapper The object mapper provider
* @param beanPropertyBinder The bean property binder provider
* @param conversionService The conversion service
*/
@Inject
protected JacksonConverterRegistrar(
BeanProvider objectMapper,
BeanProvider beanPropertyBinder,
ConversionService> conversionService) {
this.objectMapper = objectMapper;
this.conversionService = conversionService;
this.beanPropertyBinder = beanPropertyBinder;
}
@Override
public void register(ConversionService> 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(
Map.class,
Object.class,
mapToObjectConverter()
);
conversionService.addConverter(
CharSequence.class,
PropertyNamingStrategy.class,
(charSequence, targetType, context) -> {
PropertyNamingStrategy propertyNamingStrategy = null;
if (charSequence != null) {
String stringValue = NameUtils.environmentName(charSequence.toString());
if (StringUtils.isNotEmpty(stringValue)) {
switch (stringValue) {
case "SNAKE_CASE":
propertyNamingStrategy = PropertyNamingStrategy.SNAKE_CASE;
break;
case "UPPER_CAMEL_CASE":
propertyNamingStrategy = PropertyNamingStrategy.UPPER_CAMEL_CASE;
break;
case "LOWER_CASE":
propertyNamingStrategy = PropertyNamingStrategy.LOWER_CASE;
break;
case "KEBAB_CASE":
propertyNamingStrategy = PropertyNamingStrategy.KEBAB_CASE;
break;
case "LOWER_CAMEL_CASE":
propertyNamingStrategy = PropertyNamingStrategy.LOWER_CAMEL_CASE;
break;
default:
break;
}
}
}
if (propertyNamingStrategy == null) {
context.reject(charSequence, new IllegalArgumentException(String.format("Unable to convert '%s' to a com.fasterxml.jackson.databind.PropertyNamingStrategy", charSequence)));
}
return Optional.ofNullable(propertyNamingStrategy);
}
);
}
/**
* @return The map to object converter
*/
protected TypeConverter
© 2015 - 2025 Weber Informatics LLC | Privacy Policy