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

com.github.jasminb.jsonapi.ErrorUtils Maven / Gradle / Ivy

Go to download

JSONAPI-Converter is a library that provides means for integrating with services using JSON API specification.

There is a newer version: 0.14
Show newest version
package com.github.jasminb.jsonapi;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.jasminb.jsonapi.models.errors.Errors;

import java.io.IOException;
import java.io.InputStream;

import okhttp3.ResponseBody;

/**
 * Utility class providing methods needed for parsing JSON API Spec errors.
 *
 * @author jbegic
 */
public class ErrorUtils {

    private ErrorUtils() {
        // Private constructor
    }

    /**
     * Parses provided ResponseBody and returns it as T.
     *
     * @param mapper        Jackson Object mapper instance
     * @param errorResponse error response body
     * @return T collection
     * @throws IOException
     */
    public static  T parseErrorResponse(ObjectMapper mapper, ResponseBody errorResponse, Class cls) throws IOException {
        return mapper.readValue(errorResponse.bytes(), cls);
    }

    /**
     * Parses provided JsonNode and returns it as T.
     *
     * @param mapper        Jackson Object mapper instance
     * @param errorResponse error response body
     * @return T collection
     * @throws JsonProcessingException thrown in case JsonNode cannot be parsed
     */
    public static  T parseError(ObjectMapper mapper, JsonNode errorResponse, Class cls) throws JsonProcessingException {
        return mapper.treeToValue(errorResponse, cls);
    }

    public static  T parseError(ObjectMapper mapper, InputStream errorResponse, Class cls) throws IOException {
        return mapper.readValue(errorResponse, cls);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy