Skip to content

Instantly share code, notes, and snippets.

@christophercurrie
Created July 13, 2013 18:58
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 christophercurrie/5991810 to your computer and use it in GitHub Desktop.
Save christophercurrie/5991810 to your computer and use it in GitHub Desktop.
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;
import java.io.IOException;
class JAddress {
private String address;
private String city;
private String state;
@JsonCreator
public JAddress(
@JsonProperty("address1") String address,
@JsonProperty("city") String city,
@JsonProperty("state") String state
){
this.address = address;
this.city = 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;
@JsonCreator
public JPerson(
@JsonProperty("name") String name,
@JsonUnwrapped JAddress address,
@JsonProperty("alias") String alias
) {
this.name = 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 {
@Test
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);
System.out.println(json);
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