Last active
September 16, 2021 12:28
-
-
Save mannodermaus/8427b93b27483763d9cb to your computer and use it in GitHub Desktop.
LoganSquare Retrofit Converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
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
For users of Retrofit 2, check this out:
https://github.com/aurae/retrofit-logansquare