com.github.jasminb.jsonapi.retrofit.JSONAPIDocumentResponseBodyConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsonapi-converter Show documentation
Show all versions of jsonapi-converter Show documentation
JSONAPI-Converter is a library that provides means for integrating with services using JSON API specification.
package com.github.jasminb.jsonapi.retrofit;
import com.github.jasminb.jsonapi.JSONAPIDocument;
import com.github.jasminb.jsonapi.ResourceConverter;
import java.io.IOException;
import okhttp3.ResponseBody;
import retrofit2.Converter;
/**
* JSON API response body converter.
*
* @author jbegic
*/
public class JSONAPIDocumentResponseBodyConverter implements Converter> {
private final Class> clazz;
private final Boolean isCollection;
private final ResourceConverter parser;
/**
* Creates new JSONAPIDocumentResponseBodyConverter.
* @param parser {@link ResourceConverter} parser instance
* @param clazz {@link Class} class to be handled
* @param isCollection {@link Boolean} flag that denotes if processed resource is a single object or collection
*/
public JSONAPIDocumentResponseBodyConverter(ResourceConverter parser, Class> clazz, boolean isCollection) {
this.clazz = clazz;
this.isCollection = isCollection;
this.parser = parser;
}
@Override
public JSONAPIDocument convert(ResponseBody responseBody) throws IOException {
if (isCollection) {
return (JSONAPIDocument) parser.readDocumentCollection(responseBody.byteStream(), clazz);
} else {
return (JSONAPIDocument) parser.readDocument(responseBody.byteStream(), clazz);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy