Last active
August 29, 2015 14:16
-
-
Save patrickhammond/0cd9d52bd4c4750b2899 to your computer and use it in GitHub Desktop.
Jackson parser behavior when encountering integer, nulls, and empty strings.
This file contains 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
android { | |
... | |
packagingOptions { | |
exclude 'META-INF/LICENSE' | |
exclude 'META-INF/NOTICE' | |
} | |
} | |
dependencies { | |
... | |
compile 'com.google.code.gson:gson:2.3.1' | |
compile 'com.fasterxml.jackson.core:jackson-databind:2.5.1' | |
compile 'com.fasterxml.jackson.core:jackson-annotations:2.5.1' | |
testCompile 'junit:junit:4.12' | |
testCompile "org.mockito:mockito-core:1.9.5" | |
} |
This file contains 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
import com.google.gson.Gson; | |
import java.io.IOException; | |
public class GsonParser implements Parser { | |
@Override | |
public <T> T parse(String json, Class<T> clazz) throws IOException { | |
return new Gson().fromJson(json, clazz); | |
} | |
} |
This file contains 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
import com.fasterxml.jackson.databind.ObjectMapper; | |
import java.io.IOException; | |
public class JacksonParser implements Parser { | |
@Override | |
public <T> T parse(String json, Class<T> clazz) throws IOException { | |
ObjectMapper mapper = new ObjectMapper(); | |
return mapper.readValue(json, clazz); | |
} | |
} |
This file contains 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
import java.io.IOException; | |
public interface Parser { | |
<T> T parse(String json, Class<T> clazz) throws IOException; | |
} |
This file contains 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
import org.junit.Assert; | |
import org.junit.Ignore; | |
import org.junit.Test; | |
import java.io.IOException; | |
public class ParserTest { | |
@Test | |
public void testJacksonParseNormalJson() throws IOException { | |
Parser parser = new JacksonParser(); | |
testParseNormalJson(parser); | |
} | |
@Test | |
public void testJacksonParseNullJson() throws IOException { | |
Parser parser = new JacksonParser(); | |
testParseNullJson(parser); | |
} | |
@Test | |
public void testJacksonParseEmptyStringAsNullJson() throws IOException { | |
Parser parser = new JacksonParser(); | |
testParseEmptyStringAsNullJson(parser); | |
} | |
@Test | |
public void testGsonParseNormalJson() throws IOException { | |
Parser parser = new GsonParser(); | |
testParseNormalJson(parser); | |
} | |
@Test | |
public void testGsonParseNullJson() throws IOException { | |
Parser parser = new GsonParser(); | |
testParseNullJson(parser); | |
} | |
@Test | |
public void testGsonParseEmptyStringAsNullJson() throws IOException { | |
Parser parser = new GsonParser(); | |
testParseEmptyStringAsNullJson(parser); | |
} | |
private void testParseNormalJson(Parser parser) throws IOException { | |
Person person = parser.parse("{\"name\": \"Bob\", \"age\": 30, \"pets\": 1}", Person.class); | |
Assert.assertEquals("Bob", person.getName()); | |
Assert.assertNotNull(person.getAge()); | |
Assert.assertEquals(30, person.getAge().intValue()); | |
Assert.assertEquals(1, person.getPets()); | |
} | |
private void testParseNullJson(Parser parser) throws IOException { | |
Person person = parser.parse("{\"name\": \"Bob\", \"age\": null, \"pets\": null}", Person.class); | |
Assert.assertEquals("Bob", person.getName()); | |
Assert.assertNull(person.getAge()); | |
Assert.assertEquals(-1, person.getPets()); | |
} | |
private void testParseEmptyStringAsNullJson(Parser parser) throws IOException { | |
Person person = parser.parse("{\"name\": \"Bob\", \"age\": \"\", \"pets\": \"\"}", Person.class); | |
Assert.assertEquals("Bob", person.getName()); | |
Assert.assertNull(person.getAge()); | |
Assert.assertEquals(-1, person.getPets()); | |
} | |
} |
This file contains 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
import com.fasterxml.jackson.annotation.JsonProperty; | |
public class Person { | |
@JsonProperty("name") | |
String name; | |
@JsonProperty("age") | |
Integer age; | |
int pets; | |
public String getName() { | |
return name; | |
} | |
public Integer getAge() { | |
return age; | |
} | |
public int getPets() { | |
return pets; | |
} | |
@JsonProperty("pets") | |
void setPets(Object pets) { | |
if (pets == null) { | |
this.pets = -1; | |
} else if ("".equals(pets)) { | |
this.pets = -1; | |
} else { | |
this.pets = (Integer) pets; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment