读写注解
@JsonProperty
public class Person implements Serializable {
private static final long serialVersionUID = -4653164706668338935L;
private int id;
private String username;
private String password;
@JsonProperty("location")
private Address address;
private String[] hobby;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
}
@JsonIgnore和@JsonIgnoreProperties
@JsonIgnoreProperties({"username","password"})
public class Person implements Serializable {
private static final long serialVersionUID = -4653164706668338935L;
private int id;
private String username;
private String password;
private Address address;
@JsonIgnore
private String[] hobby;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
}
@JsonIgnoreType
@JsonIgnoreType
public class Address implements Serializable {
private static final long serialVersionUID = 1310389268587385235L;
private String city;
private String street;
public Address() {
super();
}
public Address(String city, String street) {
super();
this.city = city;
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
}
@JsonAutoDetect
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY,
getterVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY,
setterVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY,
creatorVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY)
public class Person implements Serializable {
private static final long serialVersionUID = -4653164706668338935L;
private int id = 1002;
public String username = "zhangxiaoxiao";
private String password = "123456";
private Address address = new Address("ShenZhen", "HuaWei");
private String[] hobby = new String[]{"eat","drink","play"};
private Date birthdy = new Date();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
protected String getPassword() {
return password;
}
protected void setPassword(String password) {
this.password = password;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
protected Date getBirthdy() {
return birthdy;
}
protected void setBirthdy(Date birthdy) {
this.birthdy = birthdy;
}
}
读注解
@JsonSetter
public class Person implements Serializable {
private static final long serialVersionUID = -4653164706668338935L;
private int id;
private String username;
private String password;
private Address address;
private String[] hobby;
private Date birthdy;
public int getId() {
return id;
}
@JsonSetter("person_id")
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public Date getBirthdy() {
return birthdy;
}
public void setBirthdy(Date birthdy) {
this.birthdy = birthdy;
}
@Override
public String toString() {
return "Person [id=" + id + ", username=" + username + ", password="
+ password + ", address=" + address + ", hobby="
+ Arrays.toString(hobby) + ", birthdy=" + birthdy + "]";
}
}
@Test
public void testJsonSetter() throws IOException{
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
InputStream objectJsonIn = this.getClass().getClassLoader()
.getResourceAsStream("serialize.json");
Person person = mapper.readValue(objectJsonIn, Person.class);
System.out.println(person);
}
@JsonAnySetter
public class Person implements Serializable {
private static final long serialVersionUID = -4653164706668338935L;
private int id;
private String username;
private String password;
private Address address;
private String[] hobby;
private Date birthdy;
private Map<String, Object> properties = new HashMap<>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public Date getBirthdy() {
return birthdy;
}
public void setBirthdy(Date birthdy) {
this.birthdy = birthdy;
}
public Object get(String fieldName){
return this.properties.get(fieldName);
}
@JsonAnySetter
public void set(String fieldName, Object value){
this.properties.put(fieldName, value);
}
@Override
public String toString() {
return "Person [id=" + id + ", username=" + username + ", password="
+ password + ", address=" + address + ", hobby="
+ Arrays.toString(hobby) + ", birthdy=" + birthdy
+ ", properties=" + properties + "]";
}
}
@JsonCreator
public class Person implements Serializable {
private static final long serialVersionUID = -4653164706668338935L;
private int id;
private String username;
private String password;
private Address address;
private String[] hobby;
private Date birthdy;
@JsonCreator
public Person(@JsonProperty("id") int id,
@JsonProperty("username") String username,
@JsonProperty("password") String password,
@JsonProperty("address") Address address,
@JsonProperty("hobby") String[] hobby,
@JsonProperty("birthdy") Date birthdy) {
super();
this.id = id;
this.username = username;
this.password = password;
this.address = address;
this.hobby = hobby;
this.birthdy = birthdy;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public Date getBirthdy() {
return birthdy;
}
public void setBirthdy(Date birthdy) {
this.birthdy = birthdy;
}
@Override
public String toString() {
return "Person [id=" + id + ", username=" + username + ", password="
+ password + ", address=" + address + ", hobby="
+ Arrays.toString(hobby) + ", birthdy=" + birthdy + "]";
}
}
@JacksonInject
public class Person implements Serializable {
private static final long serialVersionUID = -4653164706668338935L;
private int id;
private String username;
private String password;
private Address address;
private String[] hobby;
private Date birthdy;
@JacksonInject
private String source;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public Date getBirthdy() {
return birthdy;
}
public void setBirthdy(Date birthdy) {
this.birthdy = birthdy;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
@Override
public String toString() {
return "Person [id=" + id + ", username=" + username + ", password="
+ password + ", address=" + address + ", hobby="
+ Arrays.toString(hobby) + ", birthdy=" + birthdy + ", source="
+ source + "]";
}
}
@Test
public void testJacksonInject() throws IOException{
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
InputStream objectJsonIn = this.getClass().getClassLoader()
.getResourceAsStream("serialize.json");
InjectableValues inject = new InjectableValues.Std()
.addValue(String.class, "com.ifangyou");
Person person = mapper.reader(inject)
.forType(Person.class).readValue(objectJsonIn);
System.out.println(person);
}
@JsonDeserialize
例一:
public class Person implements Serializable {
private static final long serialVersionUID = -4653164706668338935L;
private int id;
private String username;
private String password;
@JsonDeserialize(as=Location.class)
private Address address;
private String[] hobby;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "Person [id=" + id + ", username=" + username + ", password="
+ password + ", address=" + address + ", hobby="
+ Arrays.toString(hobby) + "]";
}
}
/**
* 继承Address类,并加入了新的字段
* @author Alex Jiang
*/
public class Location extends Address implements Serializable {
private static final long serialVersionUID = 1L;
private String province;
public Location() {
super();
}
public Location(String city, String street, String province) {
super(city, street);
this.province = province;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
@Override
public String toString() {
return "Location [province=" + province + ", getProvince()="
+ getProvince() + ", getCity()=" + getCity() + ", getStreet()="
+ getStreet() + "]";
}
public class Address implements Serializable {
private static final long serialVersionUID = 1310389268587385235L;
private String city;
private String street;
public Address() {
super();
}
public Address(String city, String street) {
super();
this.city = city;
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
@Override
public String toString() {
return "Address [city=" + city + ", street=" + street + "]";
}
}
@Test
public void testJsonSerialize() throws IOException{
Person person = new Person();
person.setId(1);
person.setUsername("zhangSan");
person.setPassword("1001");
person.setAddress(new Location("heze", "renminglu", "shangdong"));
person.setHobby(new String[]{"object1", "object2", "object3"});
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
String basePath = this.getClass().getClassLoader()
.getResource("").getPath().substring(1);
mapper.writeValue(new FileOutputStream(
new File(basePath + "serialize.json")), person);
}
@Test
public void testJsonDeserialize_1() throws IOException{
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
InputStream objectJsonIn = this.getClass().getClassLoader()
.getResourceAsStream("serialize.json");
Person person = mapper.readValue(objectJsonIn, Person.class);
System.out.println(person);
}
例二
public class Person implements Serializable {
private static final long serialVersionUID = -4653164706668338935L;
private int id;
private String username;
private String password;
private Address address;
private String[] hobby;
private Date birthdy;
@JsonDeserialize(using = OptimizedBooleanDeserializer.class)
private boolean deleted;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public Date getBirthdy() {
return birthdy;
}
public void setBirthdy(Date birthdy) {
this.birthdy = birthdy;
}
public boolean isDeleted() {
return deleted;
}
public void setDeleted(boolean deleted) {
this.deleted = deleted;
}
@Override
public String toString() {
return "Person [id=" + id + ", username=" + username + ", password="
+ password + ", address=" + address + ", hobby="
+ Arrays.toString(hobby) + ", birthdy=" + birthdy
+ ", deleted=" + deleted + "]";
}
}
public class OptimizedBooleanDeserializer extends JsonDeserializer<Boolean> {
@Override
public Boolean deserialize(JsonParser jsonParser,
DeserializationContext deserializationContext) throws IOException,
JsonProcessingException {
String text = jsonParser.getText();
if ("0".equals(text))
return false;
return true;
}
}
写注解
@JsonInclude
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Person implements Serializable {
private static final long serialVersionUID = -4653164706668338935L;
private int id;
private String username;
private String password;
private Address address;
private String[] hobby;
private Date birthdy;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public Date getBirthdy() {
return birthdy;
}
public void setBirthdy(Date birthdy) {
this.birthdy = birthdy;
}
}
@Test
public void testJsonInclude() throws IOException{
Person person = new Person();
person.setId(1);
person.setUsername(null);
person.setPassword(null);
person.setAddress(new Address("shanghai", "nanjinglu"));
person.setHobby(new String[]{"object1", "object2", "object3"});
person.setBirthdy(new Date());
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.writeValue(System.out, person);
}
@JsonGetter
public class Person implements Serializable {
private static final long serialVersionUID = -4653164706668338935L;
private int id;
private String username;
private String password;
private Address address;
private String[] hobby;
private Date birthdy;
private String jsonGetter;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public Date getBirthdy() {
return birthdy;
}
public void setBirthdy(Date birthdy) {
this.birthdy = birthdy;
}
@JsonGetter("jsonGetterKey")
public String jsonGetter() {
return jsonGetter;
}
@JsonSetter("jsonGetterKey")
public void jsonGetter(String jsonGetter) {
this.jsonGetter = jsonGetter;
}
}
@Test
public void testJsonGetter() throws IOException{
Person person = new Person();
person.setId(1);
person.setUsername(null);
person.setPassword(null);
person.setAddress(new Address("shanghai", "nanjinglu"));
person.setHobby(new String[]{"object1", "object2", "object3"});
person.setBirthdy(new Date());
person.jsonGetter("jsonGetterValue");
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.writeValue(System.out, person);
}
@JsonAnyGetter
public class Person implements Serializable {
private static final long serialVersionUID = -4653164706668338935L;
private int id;
private String username;
private String password;
private Address address;
private String[] hobby;
private Date birthdy;
private Map<String, Object> properties = new HashMap<>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public Date getBirthdy() {
return birthdy;
}
public void setBirthdy(Date birthdy) {
this.birthdy = birthdy;
}
@JsonAnyGetter
public Map<String, Object> properties() {
return properties;
}
@JsonAnySetter
public void properties(Map<String, Object> properties) {
this.properties = properties;
}
}
@Test
public void testJsonAnyGetter() throws IOException{
Person person = new Person();
person.setId(1);
person.setUsername("zhangsan");
person.setPassword("jiubugaosuni");
person.setAddress(new Address("shanghai", "nanjinglu"));
person.setHobby(new String[]{"object1", "object2", "object3"});
person.setBirthdy(new Date());
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("propKey1", "propValue1");
properties.put("propKey2", "propValue2");
person.properties(properties);
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.writeValue(System.out, person);
}
@JsonPropertyOrder
@JsonPropertyOrder({"birthdy","hobby","address","password","username","id"})
public class Person implements Serializable {
private static final long serialVersionUID = -4653164706668338935L;
private int id;
private String username;
private String password;
private Address address;
private String[] hobby;
private Date birthdy;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public Date getBirthdy() {
return birthdy;
}
public void setBirthdy(Date birthdy) {
this.birthdy = birthdy;
}
}
@JsonRawValue
public class Person implements Serializable {
private static final long serialVersionUID = -4653164706668338935L;
private int id;
private String username;
private String password;
private Address address;
private String[] hobby;
private Date birthdy;
@JsonRawValue
private String specialCharacter;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public Date getBirthdy() {
return birthdy;
}
public void setBirthdy(Date birthdy) {
this.birthdy = birthdy;
}
public String getSpecialCharacter() {
return specialCharacter;
}
public void setSpecialCharacter(String specialCharacter) {
this.specialCharacter = specialCharacter;
}
}
@Test
public void testJsonRawValue() throws IOException{
Person person = new Person();
person.setId(1);
person.setUsername("zhangsan");
person.setPassword("jiubugaosuni");
person.setAddress(new Address("shanghai", "nanjinglu"));
person.setHobby(new String[]{"object1", "object2", "object3"});
person.setBirthdy(new Date());
person.setSpecialCharacter("$#&%$#");
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.writeValue(System.out, person);
}
@JsonValue
public class Person implements Serializable {
private static final long serialVersionUID = -4653164706668338935L;
private int id;
private String username;
private String password;
private Address address;
private String[] hobby;
private Date birthdy;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public Date getBirthdy() {
return birthdy;
}
public void setBirthdy(Date birthdy) {
this.birthdy = birthdy;
}
@JsonValue
public String toJson(){
return this.username+","+this.password+","+Arrays.toString(this.hobby);
}
}
@JsonSerialize
public class Person implements Serializable {
private static final long serialVersionUID = -4653164706668338935L;
private int id;
private String username;
private String password;
private Address address;
private String[] hobby;
private Date birthdy;
@JsonSerialize(using = OptimizedBooleanSerializer.class)
private boolean deleted;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public Date getBirthdy() {
return birthdy;
}
public void setBirthdy(Date birthdy) {
this.birthdy = birthdy;
}
public boolean isDeleted() {
return deleted;
}
public void setDeleted(boolean deleted) {
this.deleted = deleted;
}
}
public class OptimizedBooleanSerializer extends JsonSerializer<Boolean> {
@Override
public void serialize(Boolean value, JsonGenerator gen,
SerializerProvider serializers) throws IOException,
JsonProcessingException {
if(value){
gen.writeNumber(1);
}else{
gen.writeNumber(0);
}
}
}
@Test
public void testJsonSerialize() throws IOException{
Person person = new Person();
person.setId(1);
person.setUsername("zhangsan");
person.setPassword("jiubugaosuni");
person.setAddress(new Address("shanghai", "nanjinglu"));
person.setHobby(new String[]{"object1", "object2", "object3"});
person.setBirthdy(new Date());
person.setDeleted(true);
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.writeValue(System.out, person);
}
public class Person implements Serializable {
private static final long serialVersionUID = -4653164706668338935L;
private int id;
private String username;
private String password;
@JsonUnwrapped
private Address address;
private String[] hobby;
@JsonFormat(pattern="yyyy-MM-dd")
private Date birthdy;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public Date getBirthdy() {
return birthdy;
}
public void setBirthdy(Date birthdy) {
this.birthdy = birthdy;
}
@Override
public String toString() {
return "Person [id=" + id + ", username=" + username + ", password="
+ password + ", address=" + address + ", hobby="
+ Arrays.toString(hobby) + ", birthdy=" + birthdy + "]";
}
}
@Test
public void testJsonFormatJsonUnwrapped() throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
Person person = new Person();
person.setId(1);
person.setUsername("zhangSan");
person.setPassword("1001");
person.setAddress(new Address("shanghai", "nanjinglu"));
person.setHobby(new String[]{"object1", "object2", "object3"});
person.setBirthdy(new Date());
mapper.writeValue(System.out, person);
}
@JsonTypeInfo
@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")
public class Person implements Serializable {
private static final long serialVersionUID = -5634559189866427421L;
private int id;
private String name;
private int age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Employee extends Person {
private static final long serialVersionUID = 6751608503844118238L;
private Double salary;
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
}
public class Customer extends Person {
private static final long serialVersionUID = 4801233166931706124L;
private String contact;
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
}
@Test
public void testJsonTypeInfo() throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
Employee employee = new Employee();
employee.setId(10012);
employee.setName("Alex");
employee.setAge(29);
employee.setSalary(9999d);
Customer customer = new Customer();
customer.setId(20012);
customer.setName("Eric");
customer.setAge(32);
customer.setContact("13612345678");
mapper.writeValue(System.out, employee);
mapper.writeValue(System.out, customer);
}