mn.foreman.api.JsonUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-api Show documentation
Show all versions of java-api Show documentation
A library for interacting with the Foreman API.
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();
}
}