json全局序列化(包含各种类型null自定义序列化器)


   
      
         
            
               application/json;charset=UTF-8
            
         
      
      
         
            
               application/json;charset=UTF-8
               
               
               
               
               
            
         
         
            
               
                  
                     
                  
               
               
            
         
      
   

 

 

 

 

public class JsonObjectMapper extends ObjectMapper {

   /**
    *
    */
   private static final long serialVersionUID = 8034942567620323925L;

   public JsonObjectMapper() {
      super();
      this.setSerializerFactory(this.getSerializerFactory().withSerializerModifier(new MyBeanSerializerModifier()));
   }

   public class MyBeanSerializerModifier extends BeanSerializerModifier {

      @Override
      public List changeProperties(SerializationConfig config, BeanDescription beanDesc, List beanProperties) {
         //循环所有的beanPropertyWriter
         for (Object beanProperty : beanProperties) {
            BeanPropertyWriter writer = (BeanPropertyWriter) beanProperty;
            Class clazz = writer.getType().getRawClass();
            //字符串
            if (String.class.equals(clazz)) {
               writer.assignNullSerializer(new JsonSerializer() {
                  @Override
                  public void serialize(Object value, JsonGenerator jg, SerializerProvider sp) throws IOException, JsonProcessingException {
                     jg.writeString("");
                  }
               });
            }
            //list
            else if(List.class.isAssignableFrom(clazz)){
               writer.assignNullSerializer(new JsonSerializer() {
                  @Override
                  public void serialize(Object value, JsonGenerator jg, SerializerProvider sp) throws IOException, JsonProcessingException {
                     jg.writeObject(Collections.EMPTY_LIST);
                  }
               });
            }
            //map
            else if (Map.class.isAssignableFrom(clazz)) {
               writer.assignNullSerializer(new JsonSerializer() {
                  @Override
                  public void serialize(Object value, JsonGenerator jg, SerializerProvider sp) throws IOException, JsonProcessingException {
                     jg.writeObject(Collections.EMPTY_MAP);
                  }
               });
            }
            //String[]
            else if (String[].class.isAssignableFrom(clazz)) {
               writer.assignNullSerializer(new JsonSerializer() {
                  @Override
                  public void serialize(Object value, JsonGenerator jg, SerializerProvider sp) throws IOException, JsonProcessingException {
                     jg.writeObject(new String[0]);
                  }
               });
            }
            //其它
            else {
               writer.assignNullSerializer(new JsonSerializer() {
                  @Override
                  public void serialize(Object value, JsonGenerator jg, SerializerProvider sp) throws IOException, JsonProcessingException {
                     jg.writeString("");
                  }
               });
            }
         }
         return beanProperties;
      }
   }

} 
  

                            
                        
                    
                    
                    

你可能感兴趣的:(json全局序列化(包含各种类型null自定义序列化器))