Skip to content

Instantly share code, notes, and snippets.

@mannodermaus
Last active September 16, 2021 12:28
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mannodermaus/8427b93b27483763d9cb to your computer and use it in GitHub Desktop.
Save mannodermaus/8427b93b27483763d9cb to your computer and use it in GitHub Desktop.
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
Copy link
Author

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

@yongjhih
Copy link

@Zhuinden
Copy link

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