Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.askfast.util;
 
A retrofit.converter.Converter which uses Jackson for reading and writing entities.

Author(s):
Kai Waldron (kaiwaldron@gmail.com)
public class JacksonConverter implements Converter {
    private static final String MIME_TYPE = "application/json; charset=UTF-8";
    private final ObjectMapper objectMapper;
    public JacksonConverter() {
        this(new ObjectMapper());
    }
    public JacksonConverter(ObjectMapper objectMapper) {
        this. = objectMapper;
    }
    @Override
    public Object fromBody(TypedInput bodyType typethrows ConversionException {
        try {
            JavaType javaType = .getTypeFactory().constructType(type);
            return .readValue(body.in(), javaType);
        }
        catch (JsonParseException e) {
            throw new ConversionException(e);
        }
        catch (JsonMappingException e) {
            throw new ConversionException(e);
        }
        catch (IOException e) {
            throw new ConversionException(e);
        }
    }
    @Override
    public TypedOutput toBody(Object object) {
        try {
            String json = .writeValueAsString(object);
            return new TypedByteArray(json.getBytes("UTF-8"));
        }
        catch (JsonProcessingException e) {
            throw new AssertionError(e);
        }
        catch (UnsupportedEncodingException e) {
            throw new AssertionError(e);
        }
  }
New to GrepCode? Check out our FAQ X