这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
static class Test {
@JsonProperty(value="json_field0", index = 1)
private String field0;
@JsonProperty(value="json_field1", index = 0)
public String getField1() {
return "111";
}
public Test(String field0) {
this.field0 = field0;
}
}
static class Test {
@JsonProperty(value = "json_field0")
private String field0;
private String field1;
@JsonProperty(value = "json_field1")
public void setField1(String field1) {
this.field1 = field1;
}
@Override
public String toString() {
return "Test{" +
"field0='" + field0 + '\'' +
", field1='" + field1 + '\'' +
'}';
}
}
static class Test {
private String field0;
@JsonIgnore
private String field1;
private String field2;
public String getField0() { return field0; }
public void setField0(String field0) { this.field0 = field0; }
public String getField1() { return field1; }
public void setField1(String field1) { this.field1 = field1; }
public void setField2(String field2) { this.field2 = field2; }
@JsonIgnore
public String getField2() { return field2; }
}
static class Test {
private String field0;
@JacksonInject(value = "defaultField1")
private String field1;
@JacksonInject
private String field2;
InjectableValues.Std injectableValues = new InjectableValues.Std();
// 指定key为"defaultField1"对应的注入参数
injectableValues.addValue("defaultField1","field1 default value");
// 指定String类型对应的注入参数
injectableValues.addValue(String.class,"String type default value");
ObjectMapper mapper = new ObjectMapper(); // 把注入参数的配置设置给mapper
mapper.setInjectableValues(injectableValues);
static class Date2LongSerialize extends JsonSerializer<Date> {
@Override
public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeNumber(value.getTime());
}
}
static class Test {
@JsonDeserialize(using = Long2DateDeserialize.class)
private Date field0;
@Override
public String toString() { return "Test{" + "field0='" + field0 + '\'' + '}'; }
}
static class Long2DateDeserialize extends JsonDeserializer<Date> {
@Override
public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
if(null!=p && null!=ctxt && p.getLongValue()>0L ) {
return new Date(p.getLongValue());
}
return null;
}
}
最后要介绍的是JsonRawValue,使用该注解的字段或者方法,都会被序列化,但是序列化结果是原始值,例如字符串是不带双引号的: