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

mn.foreman.api.JsonUtils Maven / Gradle / Ivy

There is a newer version: 1.78.0
Show newest version
package mn.foreman.api;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Optional;

/** Utilities for parsing json. */
public class JsonUtils {

    /** The logger for this class. */
    private static final Logger LOG =
            LoggerFactory.getLogger(JsonUtils.class);

    /**
     * Converts the provided json to the desired type.
     *
     * @param json          The json.
     * @param objectMapper  The mapper.
     * @param typeReference The type reference.
     * @param            The response type.
     *
     * @return The response object.
     */
    public static  Optional fromJson(
            final String json,
            final ObjectMapper objectMapper,
            final TypeReference typeReference) {
        try {
            return Optional.of(
                    objectMapper.readValue(
                            json,
                            typeReference));
        } catch (final IOException e) {
            LOG.warn("Exception occurred while parsing response", e);
        }
        return Optional.empty();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy