Skip to content

Instantly share code, notes, and snippets.

@brentworden
Last active December 21, 2015 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentworden/6386024 to your computer and use it in GitHub Desktop.
Save brentworden/6386024 to your computer and use it in GitHub Desktop.
Jackson deserialization example for inner classes. The example is in the form of a JUnit test.
package jacksontest;
import java.io.IOException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert;
import org.junit.Test;
public class JacksonTest {
public static class Foo {
public static class Qux {
private String foobar;
public String getFoobar() {
return foobar;
}
public void setFoobar(String foobar) {
this.foobar = foobar;
}
}
private String bar;
// additional property
private Qux qux;
public String getBar() {
return bar;
}
public Qux getQux() {
return qux;
}
public void setBar(String bar) {
this.bar = bar;
}
public void setQux(Qux value) {
qux = value;
}
}
@Test
public void test() throws JsonParseException, JsonMappingException, IOException {
String in = "{ \"bar\": \"123\", \"qux\" : {\"foobar\": \"234\"}}";
ObjectMapper mapper = new ObjectMapper();
Foo obj = mapper.readValue(in, Foo.class);
Assert.assertNotNull(obj);
Assert.assertEquals("123", obj.getBar());
Assert.assertEquals("234", obj.getQux().getFoobar());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment