Skip to content

Commit

Permalink
Update Jackson converter to latest. Add default constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Feb 1, 2014
1 parent 18e1dd8 commit bcfc6ff
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 69 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

<!-- Converter Dependencies -->
<protobuf.version>2.5.0</protobuf.version>
<jackson.version>2.2.2</jackson.version>
<jackson.version>2.3.1</jackson.version>
<wire.version>1.2.0</wire.version>
<simplexml.version>2.7.1</simplexml.version>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package retrofit.converter;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;

import retrofit.mime.TypedByteArray;
import retrofit.mime.TypedInput;
import retrofit.mime.TypedOutput;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
* A {@link Converter} which uses Jackson for reading and writing entities.
*
Expand All @@ -24,30 +22,34 @@ public class JacksonConverter implements Converter {

private final ObjectMapper objectMapper;

public JacksonConverter() {
this(new ObjectMapper());
}

public JacksonConverter(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

@Override public Object fromBody(TypedInput body, final Type type) throws ConversionException {
@Override public Object fromBody(TypedInput body, Type type) throws ConversionException {
try {
final JavaType javaType = objectMapper.getTypeFactory().constructType(type);
JavaType javaType = objectMapper.getTypeFactory().constructType(type);
return objectMapper.readValue(body.in(), javaType);
} catch (final JsonParseException e) {
} catch (JsonParseException e) {
throw new ConversionException(e);
} catch (final JsonMappingException e) {
} catch (JsonMappingException e) {
throw new ConversionException(e);
} catch (final IOException e) {
} catch (IOException e) {
throw new ConversionException(e);
}
}

@Override public TypedOutput toBody(Object object) {
try {
final String json = objectMapper.writeValueAsString(object);
String json = objectMapper.writeValueAsString(object);
return new TypedByteArray(MIME_TYPE, json.getBytes("UTF-8"));
} catch (final JsonProcessingException e) {
} catch (JsonProcessingException e) {
throw new AssertionError(e);
} catch (final UnsupportedEncodingException e) {
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,47 @@
package retrofit.converter;

import static org.fest.assertions.api.Assertions.assertThat;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.ByteArrayOutputStream;

import org.junit.Test;

import retrofit.mime.TypedByteArray;
import retrofit.mime.TypedInput;
import retrofit.mime.TypedOutput;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import static org.fest.assertions.api.Assertions.assertThat;

public class JacksonConverterTest {
private static final String MIME_TYPE = "application/json; charset=UTF-8";
private static final MyObject OBJECT = new MyObject("hello world", 10);
private final String JSON = "{\"message\":\"hello world\",\"count\":10}";

private final MyObject obj = new MyObject("hello world", 10);
private final String objAsJson = String.format("{\"message\":\"%s\",\"count\":%d}", obj.getMessage(), obj.getCount());
private final JacksonConverter converter = new JacksonConverter(new ObjectMapper());
private final JacksonConverter converter = new JacksonConverter();

@Test public void serialize() throws Exception {
final TypedOutput typedOutput = converter.toBody(obj);
TypedOutput typedOutput = converter.toBody(OBJECT);
assertThat(typedOutput.mimeType()).isEqualTo(MIME_TYPE);
assertThat(asString(typedOutput)).isEqualTo(objAsJson);
assertThat(asString(typedOutput)).isEqualTo(JSON);
}

@Test public void deserialize() throws Exception {
final TypedInput input = new TypedByteArray(MIME_TYPE, objAsJson.getBytes());
final MyObject result = (MyObject) converter.fromBody(input, MyObject.class);
assertThat(result).isEqualTo(obj);
TypedInput input = new TypedByteArray(MIME_TYPE, JSON.getBytes());
MyObject result = (MyObject) converter.fromBody(input, MyObject.class);
assertThat(result).isEqualTo(OBJECT);
}

@Test(expected = ConversionException.class) public void deserializeWrongValue() throws Exception {
final TypedInput input = new TypedByteArray(MIME_TYPE, "{\"foo\":\"bar\"}".getBytes());
@Test(expected = ConversionException.class)
public void deserializeWrongValue() throws Exception {
TypedInput input = new TypedByteArray(MIME_TYPE, "{\"foo\":\"bar\"}".getBytes());
converter.fromBody(input, MyObject.class);
}

@Test(expected = ConversionException.class) public void deserializeWrongClass() throws Exception {
final TypedInput input = new TypedByteArray(MIME_TYPE, objAsJson.getBytes());
@Test(expected = ConversionException.class)
public void deserializeWrongClass() throws Exception {
TypedInput input = new TypedByteArray(MIME_TYPE, JSON.getBytes());
converter.fromBody(input, String.class);
}

private String asString(TypedOutput typedOutput) throws Exception {
final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
typedOutput.writeTo(bytes);
return new String(bytes.toByteArray());
}
Expand All @@ -57,46 +55,21 @@ public MyObject(@JsonProperty("message") String message, @JsonProperty("count")
this.count = count;
}

public String getMessage() {
return message;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

public int getCount() {
return count;
MyObject myObject = (MyObject) o;
return count == myObject.count
&& !(message != null ? !message.equals(myObject.message) : myObject.message != null);
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + count;
result = prime * result + ((message == null) ? 0 : message.hashCode());
int result = message != null ? message.hashCode() : 0;
result = 31 * result + count;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
MyObject other = (MyObject) obj;
if (count != other.count) {
return false;
}
if (message == null) {
if (other.message != null) {
return false;
}
} else if (!message.equals(other.message)) {
return false;
}
return true;
}
}
}

0 comments on commit bcfc6ff

Please sign in to comment.