¶Ô±ÈÐÂÎļþ |
| | |
| | | package org.springblade.core.tool.jackson; |
| | | |
| | | import com.fasterxml.jackson.core.JsonGenerator; |
| | | import com.fasterxml.jackson.databind.*; |
| | | import com.fasterxml.jackson.databind.ser.BeanPropertyWriter; |
| | | import com.fasterxml.jackson.databind.ser.BeanSerializerModifier; |
| | | import org.springblade.core.tool.utils.StringPool; |
| | | import org.springblade.core.tool.utils.StringUtil; |
| | | |
| | | import java.io.IOException; |
| | | import java.time.OffsetDateTime; |
| | | import java.time.temporal.TemporalAccessor; |
| | | import java.util.Collection; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * jackson é»è®¤å¼ä¸º null æ¶çå¤ç |
| | | * <p> |
| | | * ä¸»è¦æ¯ä¸ºäºé¿å
app 端åºç°null导è´éªé |
| | | * <p> |
| | | * è§åï¼ |
| | | * number -1 |
| | | * string "" |
| | | * date "" |
| | | * boolean false |
| | | * array [] |
| | | * Object {} |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class BladeBeanSerializerModifier extends BeanSerializerModifier { |
| | | @Override |
| | | public List<BeanPropertyWriter> changeProperties( |
| | | SerializationConfig config, BeanDescription beanDesc, |
| | | List<BeanPropertyWriter> beanProperties) { |
| | | // å¾ªç¯ææçbeanPropertyWriter |
| | | beanProperties.forEach(writer -> { |
| | | // å¦æå·²ç»æ null åºååå¤ç妿³¨è§£ï¼@JsonSerialize(nullsUsing = xxx) è·³è¿ |
| | | if (writer.hasNullSerializer()) { |
| | | return; |
| | | } |
| | | JavaType type = writer.getType(); |
| | | Class<?> clazz = type.getRawClass(); |
| | | if (type.isTypeOrSubTypeOf(Number.class)) { |
| | | // writer.assignNullSerializer(NullJsonSerializers.NUMBER_JSON_SERIALIZER); |
| | | } else if (type.isTypeOrSubTypeOf(Boolean.class)) { |
| | | writer.assignNullSerializer(NullJsonSerializers.BOOLEAN_JSON_SERIALIZER); |
| | | } else if (type.isTypeOrSubTypeOf(Character.class)) { |
| | | writer.assignNullSerializer(NullJsonSerializers.STRING_JSON_SERIALIZER); |
| | | } else if (type.isTypeOrSubTypeOf(String.class)) { |
| | | writer.assignNullSerializer(NullJsonSerializers.STRING_JSON_SERIALIZER); |
| | | } else if (type.isArrayType() || clazz.isArray() || type.isTypeOrSubTypeOf(Collection.class)) { |
| | | writer.assignNullSerializer(NullJsonSerializers.ARRAY_JSON_SERIALIZER); |
| | | } else if (type.isTypeOrSubTypeOf(OffsetDateTime.class)) { |
| | | writer.assignNullSerializer(NullJsonSerializers.STRING_JSON_SERIALIZER); |
| | | } else if (type.isTypeOrSubTypeOf(Date.class) || type.isTypeOrSubTypeOf(TemporalAccessor.class)) { |
| | | writer.assignNullSerializer(NullJsonSerializers.STRING_JSON_SERIALIZER); |
| | | } else { |
| | | writer.assignNullSerializer(NullJsonSerializers.OBJECT_JSON_SERIALIZER); |
| | | } |
| | | }); |
| | | return super.changeProperties(config, beanDesc, beanProperties); |
| | | } |
| | | |
| | | public interface NullJsonSerializers { |
| | | |
| | | JsonSerializer<Object> STRING_JSON_SERIALIZER = new JsonSerializer<Object>() { |
| | | @Override |
| | | public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException { |
| | | gen.writeString(StringPool.EMPTY); |
| | | } |
| | | }; |
| | | |
| | | JsonSerializer<Object> NUMBER_JSON_SERIALIZER = new JsonSerializer<Object>() { |
| | | @Override |
| | | public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException { |
| | | gen.writeNumber(StringUtil.INDEX_NOT_FOUND); |
| | | } |
| | | }; |
| | | |
| | | JsonSerializer<Object> BOOLEAN_JSON_SERIALIZER = new JsonSerializer<Object>() { |
| | | @Override |
| | | public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException { |
| | | gen.writeObject(Boolean.FALSE); |
| | | } |
| | | }; |
| | | |
| | | JsonSerializer<Object> ARRAY_JSON_SERIALIZER = new JsonSerializer<Object>() { |
| | | @Override |
| | | public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException { |
| | | gen.writeStartArray(); |
| | | gen.writeEndArray(); |
| | | } |
| | | }; |
| | | |
| | | JsonSerializer<Object> OBJECT_JSON_SERIALIZER = new JsonSerializer<Object>() { |
| | | @Override |
| | | public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException { |
| | | gen.writeStartObject(); |
| | | gen.writeEndObject(); |
| | | } |
| | | }; |
| | | |
| | | } |
| | | |
| | | } |