com.github.jasminb.jsonapi.retrofit.JSONAPIRequestBodyConverter 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.MediaType;
import okhttp3.RequestBody;
import retrofit2.Converter;
/**
* JSON API request body converter implementation.
*
* @author jbegic
*/
public class JSONAPIRequestBodyConverter implements Converter {
private final ResourceConverter converter;
/**
* Creates new JSONAPIRequestBodyConverter.
* @param converter {@link ResourceConverter} converter instance
*/
public JSONAPIRequestBodyConverter(ResourceConverter converter) {
this.converter = converter;
}
@Override
public RequestBody convert(T t) throws IOException {
try {
MediaType mediaType = MediaType.parse("application/vnd.api+json");
JSONAPIDocument> document;
boolean isCollection;
if (t instanceof JSONAPIDocument) {
document = (JSONAPIDocument>) t;
isCollection = Iterable.class.isAssignableFrom(document.get().getClass());
} else {
document = new JSONAPIDocument<>(t);
isCollection = Iterable.class.isAssignableFrom(t.getClass());
}
if (isCollection) {
return RequestBody.create(mediaType,
converter.writeDocumentCollection((JSONAPIDocument extends Iterable>>) document));
} else {
return RequestBody.create(mediaType, converter.writeDocument(document));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy