Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example demonstrating complexities of JsonUnwrapped
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
class JAddress {
private String address;
private String city;
private String state;
public JAddress(
@JsonProperty("address1") String address,
@JsonProperty("city") String city,
@JsonProperty("state") String state
this.address = address; = city;
this.state = state;
public String getAddress1() { return address; }
public String getCity() { return city; }
public String getState() { return state; }
class JPerson {
private String name;
private JAddress address;
private String alias;
public JPerson(
@JsonProperty("name") String name,
@JsonUnwrapped JAddress address,
@JsonProperty("alias") String alias
) { = name;
this.address = address;
this.alias = alias;
public String getName() {
return name;
@JsonUnwrapped public JAddress getAddress() {
return address;
public String getAlias() {
return alias;
public class JsonUnwrappedTest {
public void testReadWriteJson() throws IOException {
JPerson person = new JPerson("MyName", new JAddress("main street", "springfield", null), null);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(person);
JPerson obj = mapper.readValue(json, JPerson.class);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment