All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.macrocloud.kernel.toolkit.jackson.BaseBeanSerializerModifier Maven / Gradle / Ivy

There is a newer version: 1.1.0-RELEASE
Show newest version
package org.macrocloud.kernel.toolkit.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.macrocloud.kernel.toolkit.utils.StringPool;
import org.macrocloud.kernel.toolkit.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 时的处理
 * 

* 主要是为了避免 app 端出现null导致闪退 *

* 规则: * number -1 * string "" * date "" * boolean false * array [] * Object {}. * * @author macro */ public class BaseBeanSerializerModifier extends BeanSerializerModifier { /** *

Title: changeProperties

*

Description:

. * * @param config the config * @param beanDesc the bean desc * @param beanProperties the bean properties * @return the list * @see com.fasterxml.jackson.databind.ser.BeanSerializerModifier#changeProperties(com.fasterxml.jackson.databind.SerializationConfig, com.fasterxml.jackson.databind.BeanDescription, java.util.List) */ @Override public List changeProperties( SerializationConfig config, BeanDescription beanDesc, List 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); } /** * The Interface NullJsonSerializers. */ public interface NullJsonSerializers { /** The string json serializer. */ JsonSerializer STRING_JSON_SERIALIZER = new JsonSerializer() { @Override public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeString(StringPool.EMPTY); } }; /** The number json serializer. */ JsonSerializer NUMBER_JSON_SERIALIZER = new JsonSerializer() { @Override public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeNumber(StringUtil.INDEX_NOT_FOUND); } }; /** The boolean json serializer. */ JsonSerializer BOOLEAN_JSON_SERIALIZER = new JsonSerializer() { @Override public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeObject(Boolean.FALSE); } }; /** The array json serializer. */ JsonSerializer ARRAY_JSON_SERIALIZER = new JsonSerializer() { @Override public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartArray(); gen.writeEndArray(); } }; /** The object json serializer. */ JsonSerializer OBJECT_JSON_SERIALIZER = new JsonSerializer() { @Override public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); gen.writeEndObject(); } }; } }