Skip to content

Instantly share code, notes, and snippets.

@svendroid
Last active March 8, 2017 21:30
Show Gist options
  • Save svendroid/fa31cc559cc86c728b99 to your computer and use it in GitHub Desktop.
Save svendroid/fa31cc559cc86c728b99 to your computer and use it in GitHub Desktop.
Retrofit Jackson1ConverterFactory
compile('org.codehaus.jackson:jackson-mapper-asl:1.9.12')
package retrofit;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.ResponseBody;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectReader;
import org.codehaus.jackson.map.ObjectWriter;
import org.codehaus.jackson.type.JavaType;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
/**
* A {@link Converter.Factory converter} which uses Jackson 1.x for reading and writing entities.
* A Jackson 1 converter is necessary because Jackson 1.9.12 is used by the application server and
* therefore the shared data modeles depend on it.
* <p/>
* Adapted from {@linkplain retrofit.Converter.JacksonConverterFactory} in retrofit-converters
*/
public class Jackson1ConverterFactory extends Converter.Factory {
public static Jackson1ConverterFactory create() {
return create(new ObjectMapper());
}
/**
* Create an instance using {@code mapper} for conversion.
*/
public static Jackson1ConverterFactory create(ObjectMapper mapper) {
return new Jackson1ConverterFactory(mapper);
}
private final ObjectMapper mapper;
private Jackson1ConverterFactory(ObjectMapper mapper) {
if (mapper == null) throw new NullPointerException("mapper == null");
this.mapper = mapper;
}
@Override
public Converter<ResponseBody, ?> fromResponseBody(Type type, Annotation[] annotations) {
JavaType javaType = mapper.getTypeFactory().constructType(type);
ObjectReader reader = mapper.reader(javaType);
return new Jackson1ResponseBodyConverter<>(reader);
}
@Override
public Converter<?, RequestBody> toRequestBody(Type type, Annotation[] annotations) {
JavaType javaType = mapper.getTypeFactory().constructType(type);
ObjectWriter writer = mapper.writerWithType(javaType);
return new Jackson1RequestBodyConverter<>(writer);
}
}
package retrofit;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.RequestBody;
import org.codehaus.jackson.map.ObjectWriter;
import java.io.IOException;
public class Jackson1RequestBodyConverter<T> implements Converter<T, RequestBody> {
private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
private final ObjectWriter adapter;
Jackson1RequestBodyConverter(ObjectWriter adapter) {
this.adapter = adapter;
}
@Override
public RequestBody convert(T value) throws IOException {
byte[] bytes = adapter.writeValueAsBytes(value);
return RequestBody.create(MEDIA_TYPE, bytes);
}
}
package retrofit;
import com.squareup.okhttp.ResponseBody;
import org.codehaus.jackson.map.ObjectReader;
import java.io.IOException;
import java.io.Reader;
public class Jackson1ResponseBodyConverter<T> implements Converter<ResponseBody, T> {
private ObjectReader adapter;
public Jackson1ResponseBodyConverter(ObjectReader reader) {
this.adapter = reader;
}
@Override
public T convert(ResponseBody value) throws IOException {
Reader reader = value.charStream();
try {
return adapter.readValue(reader);
} finally {
Utils.closeQuietly(reader);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment