POJO不能含有构造器。下面代码首先用Customer的实例产生json,再读入json转换成cusomer实例。以下是json结果:
{"customerId":555,"address":{"city":"Seattle","state":null}}
MyJsonHelper.java:
public class MyJsonHelper {
public void toObject() throws JsonParseException, JsonMappingException, IOException {
Customer customer = mapper.readValue(new File("test.json"), Customer.class);
System.out.println(customer.getAddress());
}
public void toJson() throws JsonGenerationException, JsonMappingException, IOException {
Customer customer = new Customer();
Address address = new Address();
address.setCity("Seattle");
customer.setCustomerId(555);
customer.setAddress(address);
//Object to JSON in file
mapper.writeValue(new File("test.json"), customer);
//Object to JSON in String
String jsonInString = mapper.writeValueAsString(customer);
System.out.println(jsonInString);
}
private ObjectMapper mapper = new ObjectMapper();
}
Customer.java:
public class Customer {
private int customerId = 4;
private Address address;
public void setCustomerId(int custid){
this.customerId = custid;
}
public int getCustomerId(){
return customerId;
}
public void setAddress(Address address){
this.address = address;
}
public Address getAddress(){
return address;
}
}
Address.java:
public class Address {
private String city;
private String state;
public void setCity(String city){
this.city = city;
}
public String getCity(){
return city;
}
public void setState(String state){
this.state = state;
}
public String getState(){
return state;
}
@Override public boolean equals(Object other) { return EqualsBuilder.reflectionEquals(this, other); }
@Override
public String toString()
{
return ToStringBuilder.reflectionToString(this);
}
}
//Exclude fields
EqualsBuilder(this, other, new String[]{"id", "name"})
Annotation:
@JsonInclude(JsonInclude.Include.NON_EMPTY)表示只有非空域才会进入json文件,如结果变为
{"customerId":555,"address":{"city":"Seattle"}}
之前提过不能用构造器,如果要用,就要加入@JsonCreator和@JsonProperty
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Address {
@JsonCreator
public Address(@JsonProperty("city") String city, @JsonProperty("state") String state){
this.state = state;
this.city = city;
}
public void setCity(String city){
this.city = city;
}
@JsonProperty
public String getCity(){
return city;
}
public void setState(String state){
this.state = state;
}
@JsonProperty
public String getState(){
return state;
}
private String city;
private String state;
}
fasterxml + AutoValue
@AutoValue@JsonDeserialize(builder = AutoValue_Account.Builder.class)
public abstract class Account {
@JsonProperty("name")
public abstract String getName();
@JsonProperty("accountNumber")
public abstract String getAccountNumber();
public static Builder builder(){
return new AutoValue_Account.Builder();
}
@AutoValue.Builder
public abstract static class Builder {
@JsonProperty("name")
public abstract Builder setName(String value);
@JsonProperty("accountNumber")
public abstract Builder setAccountNumber(String value);
public abstract Account build();
}
}
@JsonIgnore 不加入Json生成中
ref:
fasterxml例子
java小程序帮助初学者
ReplyDelete在java编程中创建ZIP文件