- 注解:@JsonSerialize(using = DictSerializer.class)
package com.zyjk.common.core.json;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.zyjk.common.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zyjk.common.core.annotation.DictType;
import com.zyjk.common.core.utils.DictUtils;
import java.io.IOException;
import java.util.Objects;
public class DictSerializer extends JsonSerializer<Object> implements ContextualSerializer {
private final Logger log = LoggerFactory.getLogger(DictSerializer.class);
private static final String LABEL_SUFFIX = "Str";
private DictType dictType;
public DictSerializer() {
super();
}
public DictSerializer(DictType dictType) {
this.dictType = dictType;
}
@Override
public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty property) throws JsonMappingException {
DictType dictAnno = property.getAnnotation(DictType.class);
return new DictSerializer(dictAnno);
}
@Override
public void serialize(Object value, JsonGenerator generator, SerializerProvider serializerProvider) throws IOException {
serializerProvider.defaultSerializeValue(value, generator);
String fieldName = generator.getOutputContext().getCurrentName();
generator.writeStringField(fieldName.concat(LABEL_SUFFIX), value != null ? this.getDictLabel(dictType, value) : null);
}
private String getDictLabel(DictType dictType, Object value) {
try {
String propertyValue = Objects.toString(value);
if(StringUtils.isNotEmpty(dictType.readConverterExp())){
return convertByExp(propertyValue,dictType.readConverterExp(),null);
}
String[] values = propertyValue.split(",");
String label = null;
for(String var : values){
if (StringUtils.isNotEmpty(var)){
if (label == null) {
label = DictUtils.getDictLabel(dictType.value(), var);
} else {
label += "," + DictUtils.getDictLabel(dictType.value(), var);
}
} else {
if (label == null) {
label = "";
}
}
}
return label;
} catch (Exception e) {
log.error("字典转换:获取字典描述异常,使用默认值:{},key:{}, dict:{}, 异常:{}", dictType.defaultValue(), value, dictType.value(), e.getMessage());
return dictType.defaultValue();
}
}
private String convertByExp(String propertyValue, String converterExp, String separator) {
StringBuilder propertyString = new StringBuilder();
String[] convertSource = converterExp.split(",");
for (String item : convertSource) {
String[] itemArray = item.split("=");
if (StringUtils.isNotEmpty(separator) && StringUtils.containsAny(separator, propertyValue)) {
for (String value : propertyValue.split(separator)) {
if (itemArray[0].equals(value)) {
propertyString.append(itemArray[1]).append(separator);
break;
}
}
} else {
if (itemArray[0].equals(propertyValue)) {
return itemArray[1];
}
}
}
return StringUtils.stripEnd(propertyString.toString(), separator);
}
}
package com.zyjk.common.core.annotation;
import java.lang.annotation.*;
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DictType {
String value() default "";
String defaultValue() default "";
String readConverterExp() default "";
}