Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LoganSquare Retrofit Converter
package retrofit.converter;
import com.bluelinelabs.logansquare.LoganSquare;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import retrofit.converter.ConversionException;
import retrofit.converter.Converter;
import retrofit.mime.TypedInput;
import retrofit.mime.TypedOutput;
import retrofit.mime.TypedString;
/**
* A {@link Converter} which uses parsing and serialization provided by bluelinelabs' LoganSquare library.
* @author aurae
*/
public class LoganSquareConverter implements Converter {
private static final String TAG = "LoganSquareConverter";
@Override public Object fromBody(TypedInput body, Type type) throws ConversionException {
InputStream inputStream = null;
try {
// Check if the type contains a parametrized list
inputStream = body.in();
if(ParameterizedType.class.isAssignableFrom(type.getClass())) {
// Grab the actual type parameter from the parametrized list and delegate to LoganSquare
ParameterizedType parameterized = (ParameterizedType) type;
return LoganSquare.parseList(inputStream, (Class) parameterized.getActualTypeArguments()[0]);
} else {
// Single elements get parsed immediately
return LoganSquare.parse(inputStream, (Class) type);
}
} catch(Exception e) {
throw new ConversionException(e);
} finally {
try {
if(inputStream != null) {
inputStream.close();
}
} catch(Exception e) {
Log.e(TAG, "Could not close input stream.", e);
}
}
}
@SuppressWarnings("unchecked") @Override public TypedOutput toBody(Object object) {
try {
// Check if the type contains a parametrized list
if (List.class.isAssignableFrom(object.getClass())) {
// Convert the input to a list first, access the first element and serialize the list
List<Object> list = (List<Object>) object;
if (list.isEmpty()) {
return new TypedString("[]");
} else {
Object firstElement = list.get(0);
return new TypedString(LoganSquare.serialize(list, (Class<Object>) firstElement.getClass()));
}
} else {
// Serialize single elements immediately
return new TypedString(LoganSquare.serialize(object));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
@mannodermaus

This comment has been minimized.

Copy link
Owner Author

@mannodermaus mannodermaus commented May 26, 2015

For users of Retrofit 2, check this out:
https://github.com/aurae/retrofit-logansquare

@yongjhih

This comment has been minimized.

@Zhuinden

This comment has been minimized.

Copy link

@Zhuinden Zhuinden commented Mar 5, 2016

Replace fromBody implementation to close body.in() to prevent leaks in OkHttp

private static final String TAG = "LoganSquareConverter";

@Override
public Object fromBody(TypedInput body, Type type)
        throws ConversionException {
    InputStream inputStream = null;
    try {
        // Check if the type contains a parametrized list
        inputStream = body.in();
        if(ParameterizedType.class.isAssignableFrom(type.getClass())) {
            // Grab the actual type parameter from the parametrized list and delegate to LoganSquare
            ParameterizedType parameterized = (ParameterizedType) type;
            return LoganSquare.parseList(inputStream, (Class) parameterized.getActualTypeArguments()[0]);
        } else {
            // Single elements get parsed immediately
            return LoganSquare.parse(inputStream, (Class) type);
        }
    } catch(Exception e) {
        throw new ConversionException(e);
    } finally {
        try {
            if(inputStream != null) {
                inputStream.close();
            }
        } catch(Exception e) {
            Log.e(TAG, "Could not close input stream.", e);
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment