io.micronaut.json.convert.JsonConverterRegistrar Maven / Gradle / Ivy
/*
* Copyright 2017-2021 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.json.convert;
import io.micronaut.context.BeanProvider;
import io.micronaut.core.annotation.Experimental;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.bind.ArgumentBinder;
import io.micronaut.core.bind.BeanPropertyBinder;
import io.micronaut.core.convert.ArgumentConversionContext;
import io.micronaut.core.convert.ConversionContext;
import io.micronaut.core.convert.ConversionService;
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.json.JsonMapper;
import io.micronaut.json.tree.JsonArray;
import io.micronaut.json.tree.JsonNode;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* Converter registrar for json.
*
* @author Jonas Konrad
* @since 3.1
*/
@Experimental
@Singleton
public final class JsonConverterRegistrar implements TypeConverterRegistrar {
private final BeanProvider objectCodec;
private final ConversionService> conversionService;
private final BeanProvider beanPropertyBinder;
@Inject
public JsonConverterRegistrar(
BeanProvider objectCodec,
ConversionService> conversionService,
BeanProvider beanPropertyBinder
) {
this.objectCodec = objectCodec;
this.conversionService = conversionService;
this.beanPropertyBinder = beanPropertyBinder;
}
@Override
public void register(ConversionService> conversionService) {
conversionService.addConverter(
JsonArray.class,
Object[].class,
arrayNodeToObjectConverter()
);
conversionService.addConverter(
JsonNode.class,
ConvertibleValues.class,
objectNodeToConvertibleValuesConverter()
);
conversionService.addConverter(
JsonArray.class,
Iterable.class,
arrayNodeToIterableConverter()
);
conversionService.addConverter(
JsonNode.class,
Object.class,
jsonNodeToObjectConverter()
);
conversionService.addConverter(
Map.class,
Object.class,
mapToObjectConverter()
);
conversionService.addConverter(
Object.class,
JsonNode.class,
objectToJsonNodeConverter()
);
}
/**
* @return A converter that converts object nodes to convertible values
*/
@Internal
public TypeConverter objectNodeToConvertibleValuesConverter() {
return (object, targetType, context) -> {
if (object.isObject()) {
return Optional.of(new JsonNodeConvertibleValues<>(object, conversionService));
} else {
// ConvertibleValues only works for objects
return Optional.empty();
}
};
}
/**
* @return Converts array nodes to iterables.
*/
public TypeConverter arrayNodeToIterableConverter() {
return (node, targetType, context) -> {
Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy