自学内容网 自学内容网

Json Filter and Serializer

package mydto;

import com.fasterxml.jackson.annotation.JsonFilter;

import com.fasterxml.jackson.annotation.JsonIgnore;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties("password")

@JsonFilter("myFilter")

public class MyDto {

private int intValue;

private String name;

private String email;

private String city;

private String province;

private String country;

private String password;

@JsonIgnore

private String secondPassword;

private String descr;

private DtoInfo info;

private Long length;

private Long weight;

public MyDto() {

super();

}

public int getIntValue() {

return intValue;

}

 .......

}

package mydto;

import java.util.Date;

public class DtoInfo {

private String author;

private Date createDt;

private String access;

private String desc;

.....

}

package mydto;

import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.databind.SerializerProvider;

import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;

import com.fasterxml.jackson.databind.ser.PropertyWriter;

import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;

public class DtoFilter extends SimpleBeanPropertyFilter {

@Override

public void serializeAsField(Object pojo, JsonGenerator jgen, SerializerProvider provider,

PropertyWriter writer) throws Exception {

if (include(writer)) {

if (!writer.getName().equals("intValue")) {

writer.serializeAsField(pojo, jgen, provider);

return;

}

int intValue = ((MyDto) pojo).getIntValue();

if (intValue >= 0) {

writer.serializeAsField(pojo, jgen, provider);

}

} else if (!jgen.canOmitFields()) { // since 2.3

writer.serializeAsOmittedField(pojo, jgen, provider);

}

}

@Override

protected boolean include(BeanPropertyWriter writer) {

return true;

}

@Override

protected boolean include(PropertyWriter writer) {

return true;

}

}

package mydto;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.JsonSerializer;

import com.fasterxml.jackson.databind.SerializerProvider;

public class DtoSerializer extends JsonSerializer<MyDto> {

private JsonSerializer<MyDto> defaultSerializer;

public DtoSerializer(JsonSerializer<MyDto> serializer) {

defaultSerializer = serializer;

}

@Override

public void serialize(MyDto dto, JsonGenerator jgen, SerializerProvider provider)

throws IOException, JsonProcessingException {

if("cn".equals(dto.getCountry())) {

dto.setCountry("China");

}

defaultSerializer.serialize(dto, jgen, provider);

}

@Override

public boolean isEmpty(SerializerProvider provider, MyDto value) {

return (value == null );

}

}

package mydto;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;

import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.SerializerProvider;

import com.fasterxml.jackson.databind.ser.std.StdSerializer;

public class DtoInfoSerializer extends StdSerializer<DtoInfo> {

private static final long serialVersionUID = 1L;

public DtoInfoSerializer(Class<DtoInfo> dt) {

super(dt);

}

@Override

public void serialize(DtoInfo info, JsonGenerator jgen, SerializerProvider serializers) throws IOException, JsonProcessingException {

if (info != null) {

String pattern = "yyyy-MM-dd HH:mm:ss";

SimpleDateFormat sdf = new SimpleDateFormat(pattern);

if (info.getCreateDt() == null) {

info.setCreateDt(new Date());

}

String formattedDate = sdf.format(info.getCreateDt());

jgen.writeStartObject();

jgen.writeStringField("authorName", info.getAuthor());

jgen.writeStringField("grantedAccess", info.getAccess());

jgen.writeStringField("createDate",formattedDate) ;

jgen.writeEndObject();

}

}

}

package mydto;

import java.util.Date;

import com.fasterxml.jackson.annotation.JsonInclude.Include;

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.BeanDescription;

import com.fasterxml.jackson.databind.JsonSerializer;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.databind.SerializationConfig;

import com.fasterxml.jackson.databind.module.SimpleModule;

import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;

import com.fasterxml.jackson.databind.ser.FilterProvider;

import com.fasterxml.jackson.databind.ser.PropertyFilter;

import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;

public class MyDtoMain {

public static void main(String[] args) throws JsonProcessingException {

test();

}

public static void test() throws JsonProcessingException {

PropertyFilter theFilter = new DtoFilter();

FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", theFilter);

MyDto dto = new MyDto();

dto.setIntValue(-1);

dto.setName("BlueSky");

dto.setEmail("bluesky@net.sky");

dto.setPassword("123456");

dto.setPassword("13579x");

dto.setCity("Dalian");

dto.setProvince("LiaoNing");

dto.setCountry("cn");

DtoInfo info=new DtoInfo();

info.setAccess("WRX");

info.setAuthor("admin");

info.setCreateDt(new Date());

dto.setInfo(info);

ObjectMapper mapper = new ObjectMapper();

mapper.setSerializationInclusion(Include.NON_EMPTY);

mapper.registerModule(new SimpleModule().addSerializer(DtoInfo.class, new DtoInfoSerializer(DtoInfo.class)));

mapper.registerModule(new SimpleModule() {

@Override

public void setupModule(SetupContext context) {

super.setupModule(context);

context.addBeanSerializerModifier(new BeanSerializerModifier() {

@Override

public JsonSerializer<?> modifySerializer(

SerializationConfig config, BeanDescription desc, JsonSerializer<?> serializer) {

if (MyDto.class.isAssignableFrom(desc.getBeanClass())) {

return new DtoSerializer((JsonSerializer<MyDto>) serializer);

}

return serializer;

}

});

}

}); 

String dtoAsString = mapper.writer(filters).writeValueAsString(dto);

System.out.println(dtoAsString);

}

}


原文地址:https://blog.csdn.net/lianghyan/article/details/144059223

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!