从源码跟了一下,现在的代码,对于null值处理时,针对Number、String、Boolean、Collection这四种类型是不支持JSONField.serializeUsing 的(这里是做特例处理造成的,不知道是否有什么考量)
有两段相关代码
1.JavaBeanSerializer中,对于属性值为null处理时,会特殊处理几个特定类型,对于这里相关的Number.class类型处理时,会优先考虑定义的特性(SerializerFeature),如果null不写则直接跳过处理,所以也就不会再进注解自定义的处理器,相关代码为
JavaBeanSerializer 350行 } else if (Number.class.isAssignableFrom(fieldClass)) { int defaultMask = SerializerFeature.WriteNullNumberAsZero.mask; final int mask = defaultMask | SerializerFeature.WriteMapNullValue.mask; if ((!writeAsArray) && (serialzeFeatures & mask) == 0 && (out.features & mask) == 0) { continue; }
2.如果这里我们开启WriteMapNullValue,执行了FieldSerializer(实现JSONField处理的地方)了,则会因为下述代码直写null而不触发JSONField.serializeUsing 的处理逻辑
//FieldSerializer 233行 if (Number.class.isAssignableFrom(runtimeFieldClass)) { out.writeNull(features, SerializerFeature.WriteNullNumberAsZero.mask); return; } else if (String.class == runtimeFieldClass) { out.writeNull(features, SerializerFeature.WriteNullStringAsEmpty.mask); return; } else if (Boolean.class == runtimeFieldClass) { out.writeNull(features, SerializerFeature.WriteNullBooleanAsFalse.mask); return; } else if (Collection.class.isAssignableFrom(runtimeFieldClass) || runtimeFieldClass.isArray()) { out.writeNull(features, SerializerFeature.WriteNullListAsEmpty.mask); return; }