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.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.Provider;
import javax.inject.Singleton;
import java.util.*;
import java.io.IOException;
@Singleton
public class JacksonConverterRegistrar implements TypeConverterRegistrar {
private final Provider<ObjectMapper> objectMapper;
private final ConversionService<?> conversionService;
private final Provider<BeanPropertyBinder> beanPropertyBinder;
protected JacksonConverterRegistrar(
Provider<ObjectMapper> objectMapper,
Provider<BeanPropertyBinder> 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);
}
);
}
protected TypeConverter<Map, Object> mapToObjectConverter() {
return (map, targetType, context) -> {
ArgumentConversionContext<Object> conversionContext;
if (context instanceof ArgumentConversionContext) {
conversionContext = (ArgumentConversionContext<Object>) context;
} else {
conversionContext = ConversionContext.of(targetType);
}
ArgumentBinder binder = this.beanPropertyBinder.get();
ArgumentBinder.BindingResult result = binder.bind(conversionContext, correctKeys(map));
return result.getValue();
};
}
private Map correctKeys(Map<?, ?> map) {
Map mapWithExtraProps = new LinkedHashMap(map.size());
for (Map.Entry entry : map.entrySet()) {
Object key = entry.getKey();
Object value = correctKeys(entry.getValue());
mapWithExtraProps.put(NameUtils.decapitalize(NameUtils.dehyphenate(key.toString())), value);
}
return mapWithExtraProps;
}
private List correctKeys(List list) {
List newList = new ArrayList(list.size());
for (Object o : list) {
newList.add(correctKeys(o));
}
return newList;
}
private Object correctKeys(Object o) {
if (o instanceof List) {
return correctKeys((List) o);
} else if (o instanceof Map) {
return correctKeys((Map) o);
}
return o;
}
protected TypeConverter<Object, JsonNode> objectToJsonNodeConverter() {
return (object, targetType, context) -> {
try {
return Optional.of(objectMapper.get().valueToTree(object));
} catch (IllegalArgumentException e) {
context.reject(e);
return Optional.empty();
}
};
}
protected TypeConverter<ObjectNode, ConvertibleValues> objectNodeToConvertibleValuesConverter() {
return (object, targetType, context) -> Optional.of(new ObjectNodeConvertibleValues<>(object, conversionService));
}
protected TypeConverter<JsonNode, Object> jsonNodeToObjectConverter() {
return (node, targetType, context) -> {
try {
if (CharSequence.class.isAssignableFrom(targetType) && node instanceof ObjectNode) {
return Optional.of(node.toString());
} else {
Argument<Object> argument = null;
if (node instanceof ContainerNode && context instanceof ArgumentConversionContext && targetType.getTypeParameters().length != 0) {
argument = ((ArgumentConversionContext<Object>) context).getArgument();
}
Object result;
if (argument != null) {
ObjectMapper om = this.objectMapper.get();
JsonParser jsonParser = om.treeAsTokens(node);
TypeFactory typeFactory = om.getTypeFactory();
JavaType javaType = JacksonConfiguration.constructType(argument, typeFactory);
result = om.readValue(jsonParser, javaType);
} else {
result = this.objectMapper.get().treeToValue(node, targetType);
}
return Optional.ofNullable(result);
}
} catch (IOException e) {
context.reject(e);
return Optional.empty();
}
};
}
protected TypeConverter<ArrayNode, Iterable> arrayNodeToIterableConverter() {
return (node, targetType, context) -> {
Map<String, Argument<?>> typeVariables = context.getTypeVariables();
Class elementType = typeVariables.isEmpty() ? Map.class : typeVariables.values().iterator().next().getType();
List results = new ArrayList();
node.elements().forEachRemaining(jsonNode -> {
Optional converted = conversionService.convert(jsonNode, elementType, context);
if (converted.isPresent()) {
results.add(converted.get());
}
});
return Optional.of(results);
};
}
protected TypeConverter<ArrayNode, Object[]> arrayNodeToObjectConverter() {
return (node, targetType, context) -> {
try {
Object[] result = objectMapper.get().treeToValue(node, targetType);
return Optional.of(result);
} catch (JsonProcessingException e) {
context.reject(e);
return Optional.empty();
}
};
}
}