jackson自定义序列化过程

1.自定义序列化过程
<1>方式1 参见http://www.cnblogs.com/lic309/p/5048631.htm 建议此方法

public class MyBeanSerializerModifier extends BeanSerializerModifier {
private JsonSerializer<Object> customeJsonSerializer = new NullSerializer();
private static final Set<String> IGNORED_FIELDS = new HashSet<String>(Arrays.asList("pwd", "id", "userName"));

@Override
public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc,
        List<BeanPropertyWriter> beanProperties) {
    // 循环所有的beanPropertyWriter
    for (int i = 0; i < beanProperties.size(); i++) {
        BeanPropertyWriter writer = beanProperties.get(i);
        final String propName = writer.getName();
        if (IGNORED_FIELDS.contains(propName)) {
             writer.assignSerializer(this.defaultCustomeJsonSerializer());
        }
    }
    return beanProperties;
}
private JsonSerializer<Object> defaultCustomeJsonSerializer() {
    return customeJsonSerializer;
}

}

public class CustomeSerializer extends JsonSerializer<Object>{
@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers)
        throws IOException, JsonProcessingException {
    gen.writeString("********");  
}

}

 //调用
 StringWriter stringWriter = new StringWriter();
    try {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializerFactory(mapper.getSerializerFactory().withSerializerModifier(new MyBeanSerializerModifier())); 
        mapper.writeValue(stringWriter, paramObj);
    } catch (JsonGenerationException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return stringWriter.toString();

<2>方式2

public class EntityCustomSerializationFactory extends BeanSerializerFactory {
private static final long serialVersionUID = 1L;
public EntityCustomSerializationFactory() {
    this(null);
}
protected EntityCustomSerializationFactory(SerializerFactoryConfig config) {
    super(config);
}
// ignored fields
private static final Set<String> IGNORED_FIELDS = new HashSet<String>(Arrays.asList("pwd", "id", "obj"));
@Override
protected void processViews(SerializationConfig config, BeanSerializerBuilder builder) {
    super.processViews(config, builder);
    // ignore fields only for concrete class
    // note, that you can avoid or change this check
    // if (builder.getBeanDescription().getBeanClass().equals(Entity.class))
    // {
    // get original writer
    List<BeanPropertyWriter> originalWriters = builder.getProperties();
    // create actual writers
    List<BeanPropertyWriter> writers = new ArrayList<BeanPropertyWriter>();
    for (BeanPropertyWriter writer : originalWriters) {
        final String propName = writer.getName();
        // if it isn't ignored field, add to actual writers list
        if (IGNORED_FIELDS.contains(propName)) {
            writer.assignSerializer(new JsonSerializer<Object>() {
                @Override
                public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers)
                        throws IOException, JsonProcessingException {
                    gen.writeString("****");
                }
            });
            writers.add(writer);
        } else {
            writers.add(writer);
        }
    }
    builder.setProperties(writers);
}

}

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializerFactory(new EntityCustomSerializationFactory());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tof21

支持原创

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值