All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.tsc4j.jackson.JacksonBeanMapper Maven / Gradle / Ivy

There is a newer version: 0.5.14
Show newest version
/*
 * Copyright 2017 - 2021 tsc4j project
 *
 * 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 com.github.tsc4j.jackson;

import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tsc4j.core.AbstractBeanMapper;
import com.github.tsc4j.core.BeanMapper;
import com.github.tsc4j.core.ByClassRegistry;
import com.github.tsc4j.core.Tsc4j;
import com.typesafe.config.ConfigRenderOptions;
import com.typesafe.config.ConfigValue;
import lombok.NonNull;
import java.util.Arrays;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Jackson-databind based {@link BeanMapper} implementation.
 */
public final class JacksonBeanMapper extends AbstractBeanMapper {
    private static final ConfigRenderOptions RENDER_OPTS = Tsc4j.renderOptions(-1);
    private final ObjectMapper mapper = createObjectMapper();

    @Override
    public int getOrder() {
        return -10000;
    }

    @Override
    protected  T createBean(@NonNull Class clazz, @NonNull ConfigValue value, @NonNull String path) {
        try {
            java.util.Objects.requireNonNull(clazz, "clazz is marked non-null but is null");
            java.util.Objects.requireNonNull(value, "value is marked non-null but is null");
            java.util.Objects.requireNonNull(path, "path is marked non-null but is null");
            final java.lang.String json = value.render(RENDER_OPTS);
            if (log.isDebugEnabled()) {
                log.debug("createBean(class: {} path: {}): config value: {}", clazz.getName(), path, value);
                log.debug("  as json: {}", json);
            }
            final T result = mapper.readValue(json, clazz);
            log.debug("deserialized {} from: {}", result, json);
            return result;
        } catch (final java.lang.Throwable $ex) {
            throw lombok.Lombok.sneakyThrow($ex);
        }
    }

    private ObjectMapper createObjectMapper() {
        return configureMapper(Jackson.get().copy());
    }

    @SuppressWarnings("unchecked")
    private ObjectMapper configureMapper(ObjectMapper objectMapper) {
        final com.github.tsc4j.core.ByClassRegistry> valConverters = getValueConverters();
        final java.util.List deserializers = valConverters.entrySet().stream().map(e -> new FunctionDeserializer(e.getKey(), e.getValue())).map(e -> (JsonDeserializer) e).collect(Collectors.toList());
        final java.util.List serializers = Arrays.asList(new ConfigSerializer(), new ConfigValueSerializer());
        final com.github.tsc4j.jackson.JacksonTsc4jModule module = new JacksonTsc4jModule(deserializers, serializers);
        return objectMapper.registerModule(module);
    }

    @Override
    protected ByClassRegistry> defaultValueConverters() {
        return 
        // we don't know how to properly unquote strings :-/
        super.defaultValueConverters().remove(String.class);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy