Last active
December 21, 2015 23:59
-
-
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.
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
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