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

com.github.jasminb.jsonapi.retrofit.JSONAPIRequestBodyConverter 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.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>) document));
			} else {
				return RequestBody.create(mediaType, converter.writeDocument(document));
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy