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

discord4j.common.JacksonResourceProvider Maven / Gradle / Ivy

/*
 * This file is part of Discord4J.
 *
 * Discord4J is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Discord4J is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Discord4J. If not, see .
 */

package discord4j.common;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import discord4j.common.jackson.PossibleModule;
import discord4j.common.jackson.UnknownPropertyHandler;

import java.util.function.Function;

/**
 * Provides a centralized Jackson 2.9 {@link com.fasterxml.jackson.databind.ObjectMapper} allowing customization and
 * reuse across the application.
 */
public class JacksonResourceProvider {

    private static final Function initializer = mapper -> mapper
            .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
            .registerModules(new PossibleModule(), new Jdk8Module());

    private final ObjectMapper objectMapper;

    /**
     * Create a default {@link com.fasterxml.jackson.databind.ObjectMapper} that allows any field visibility,
     * registers modules to handle Discord4J specific mappings and ignores unknown properties.
     */
    public JacksonResourceProvider() {
        this(mapper -> mapper.addHandler(new UnknownPropertyHandler(true)));
    }

    /**
     * Create a custom {@link com.fasterxml.jackson.databind.ObjectMapper}, based on the defaults given by
     * {@link #JacksonResourceProvider()}
     *
     * @param mapper a Function to customize the ObjectMapper to be created
     */
    public JacksonResourceProvider(Function mapper) {
        this.objectMapper = initializer.andThen(mapper).apply(new ObjectMapper());
    }

    /**
     * Create with a pre-configured {@link com.fasterxml.jackson.databind.ObjectMapper}. Using this will replace the
     * recommended default and can lead to unexpected behavior and errors.
     *
     * @param objectMapper a pre-configured ObjectMapper to use
     */
    public JacksonResourceProvider(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    /**
     * Get the {@link com.fasterxml.jackson.databind.ObjectMapper} configured by this provider.
     *
     * @return a Jackson ObjectMapper used to map POJOs to and from JSON format
     */
    public ObjectMapper getObjectMapper() {
        return objectMapper;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy