json、xstream转换器及配置全局jackson

fastJson 转换器

// 反序列化
public class SexFastJsonSerialize implements ObjectDeserializer{
    
    }

// 序列化
public class SexFastJsonSerialize implements ObjectSerializer {
    
    

    @Override
    public void write(JSONSerializer jsonSerializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
    
    
        // 强制把值转换为String
        String formCode = (String) object;
        // 序列化为自定义的name属性,输出就行
        jsonSerializer.out.writeString(HosGenderType.getHosGenderTypeByFormCode(formCode).getCode());
    }
}

Jackson 序列化转换器


// 反序列化
public class SexJacksonSerialize implements JsonDeserializer{
    
    }

// 序列化
public class SexJacksonSerialize extends JsonSerializer<String> {
    
    


    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException {
    
    
        gen.writeString(HosGenderType.getHosGenderTypeByFormCode(value).getCode());
    }
}

json转换器使用实例

    @JSONField(serializeUsing = SexFastJsonSerialize.class)
    @JsonSerialize(using = SexJacksonSerialize.class)

xStream转换器

/***
 *  xml 字段转换器
 * @author qb
 * @since 2023/6/2 8:34
 * @version 1.0
 */
public class SexConverter implements Converter {
    
    

    @Override
    public boolean canConvert(Class type) {
    
    
        return String.class.isAssignableFrom(type);
    }

    /**
     * 值转换器
     * @param o                     /
     * @param writer                /
     * @param marshallingContext    /
     */
    @Override
    public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext marshallingContext) {
    
    
        String cData = (String) o;
        writer.setValue(HosGenderType.getHosGenderTypeByToCode(cData).getCode());
    }

    /**
     * Convert textual data back into an object.
     *
     * @param reader  The stream to read the text from.
     * @param context
     * @return The resulting object.
     */
    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    
    
        return reader.getValue();
    }
}

xStream 使用

@XStreamConverter(value = SexConverter.class)

全局jackson配置

遇到了一个bug,在使用jackson的别名注解时,swagger生成的参数不是别名而是实体参数,还是重复的,所以便使用了全局jackson统一使用fastjson的名称序列化


@Configuration
@AutoConfigureAfter(SwaggerConfiguration.class)
public class JacksonConfiguration {
    
    

    @Bean
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
    
    
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        objectMapper.configOverride(String.class).setSetterInfo(JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY));
        objectMapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
    
    
            @Override
            public boolean isAnnotationBundle(Annotation ann) {
    
    
                if (ann.annotationType() == JSONField.class) {
    
    
                    return true;
                }
                return super.isAnnotationBundle(ann);
            }
            @Override
            public PropertyName findNameForSerialization(Annotated a) {
    
    
                PropertyName nameForSerialization = super.findNameForSerialization(a);
                if (nameForSerialization == null || nameForSerialization == PropertyName.USE_DEFAULT) {
    
    
                    JSONField jsonField = _findAnnotation(a, JSONField.class);
                    if (jsonField != null) {
    
    
                        return PropertyName.construct(jsonField.name());
                    }

                }
                return nameForSerialization;
            }

            @Override
            public PropertyName findNameForDeserialization(Annotated a) {
    
    
                PropertyName nameForDeserialization = super.findNameForDeserialization(a);
                if (nameForDeserialization == null || nameForDeserialization == PropertyName.USE_DEFAULT) {
    
    
                    JSONField jsonField = _findAnnotation(a, JSONField.class);
                    if (jsonField != null) {
    
    
                        return PropertyName.construct(jsonField.name());
                    }
                }
                return nameForDeserialization;
            }
        });
        return objectMapper;
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_43861630/article/details/131009286