【7.6.0】【rule】【format】增加一个根据字典类型编码+字典编码进行转义中文的注解

pull/57/head
fengshuonan 2023-06-27 21:13:50 +08:00
parent ebccde979e
commit 3e0f5960a2
4 changed files with 158 additions and 0 deletions

View File

@ -0,0 +1,56 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.rule.annotation;
import cn.stylefeng.roses.kernel.rule.enums.FormatTypeEnum;
import java.lang.annotation.*;
/**
* json
* <p>
*
*
* @author fengshuonan
* @since 2023/6/27 21:01
*/
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface DictCodeFieldFormat {
/**
* {@link FormatTypeEnum}
* <p>
*
*/
FormatTypeEnum formatType() default FormatTypeEnum.ADD_FIELD;
/**
*
*/
String dictTypeCode();
}

View File

@ -24,6 +24,14 @@
<version>${roses.version}</version>
</dependency>
<!--字典模块的api-->
<!--新增加了一个字典编码的转化-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>dict-api</artifactId>
<version>${roses.version}</version>
</dependency>
<!--jackson相关基础依赖-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>

View File

@ -0,0 +1,79 @@
package cn.stylefeng.roses.kernel.wrapper.field.dict;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.dict.api.context.DictContext;
import cn.stylefeng.roses.kernel.rule.enums.FormatTypeEnum;
import cn.stylefeng.roses.kernel.rule.util.MixFieldTypeUtil;
import cn.stylefeng.roses.kernel.wrapper.field.util.CommonFormatUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
/**
* @DictCodeFieldFormat
*
* @author fengshuonan
* @since 2023/6/27 21:05
*/
@Slf4j
public class DictCodeFormatSerializer extends JsonSerializer<Object> {
/**
* wrapper
*/
private final FormatTypeEnum formatTypeEnum;
/**
*
*/
private final String dictTypeCode;
public DictCodeFormatSerializer(FormatTypeEnum formatTypeEnum, String dictTypeCode) {
this.formatTypeEnum = formatTypeEnum;
this.dictTypeCode = dictTypeCode;
}
@Override
public void serialize(Object originValue, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
try {
this.action(originValue, jsonGenerator, serializerProvider);
} catch (Exception e) {
log.error("执行json的字段序列化出错", e);
// 报错后继续写入原始值否则会响应的json不是规范的json
jsonGenerator.writeObject(originValue);
}
}
/**
*
*
* @author fengshuonan
* @since 2022/9/7 11:11
*/
private void action(Object originValue, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws
InstantiationException, IllegalAccessException, IOException {
// 参数值为空,直接跳过格式化
if (ObjectUtil.isEmpty(originValue)) {
jsonGenerator.writeObject(originValue);
return;
}
// 判断当前字段类型是否是String类型如果不是String类型则直接跳过因为字典编码是String类型的
boolean stringFlag = MixFieldTypeUtil.whetherAssignClass(originValue, String.class);
if (!stringFlag) {
jsonGenerator.writeObject(originValue);
return;
}
// 执行转化,根据字典类型编码,以及字典的编码,获取指定字典的中文名称
String dictName = DictContext.me().getDictName(this.dictTypeCode, originValue.toString());
// 将转化的值根据策略进行写入到渲染的json中
CommonFormatUtil.writeField(formatTypeEnum, originValue, dictName, jsonGenerator);
}
}

View File

@ -1,10 +1,12 @@
package cn.stylefeng.roses.kernel.wrapper.field.jackson;
import cn.stylefeng.roses.kernel.rule.annotation.DictCodeFieldFormat;
import cn.stylefeng.roses.kernel.rule.annotation.EnumFieldFormat;
import cn.stylefeng.roses.kernel.rule.annotation.SimpleFieldFormat;
import cn.stylefeng.roses.kernel.rule.base.ReadableEnum;
import cn.stylefeng.roses.kernel.rule.base.SimpleFieldFormatProcess;
import cn.stylefeng.roses.kernel.rule.enums.FormatTypeEnum;
import cn.stylefeng.roses.kernel.wrapper.field.dict.DictCodeFormatSerializer;
import cn.stylefeng.roses.kernel.wrapper.field.enums.EnumFieldFormatDeserializer;
import cn.stylefeng.roses.kernel.wrapper.field.enums.EnumFieldFormatSerializer;
import cn.stylefeng.roses.kernel.wrapper.field.simple.SimpleFieldFormatSerializer;
@ -53,6 +55,19 @@ public class CustomJacksonIntrospector extends JacksonAnnotationIntrospector {
return new EnumFieldFormatSerializer(formatTypeEnum, process);
}
// 字典编码的格式化转化过程
DictCodeFieldFormat dictCodeFieldFormat = annotated.getAnnotation(DictCodeFieldFormat.class);
if (dictCodeFieldFormat != null && dictCodeFieldFormat.dictTypeCode() != null) {
// 获取格式化处理的方式
FormatTypeEnum formatTypeEnum = dictCodeFieldFormat.formatType();
// 获取指定的字典类型的编码
String dictTypeCode = dictCodeFieldFormat.dictTypeCode();
// 创建对应的序列化模式
return new DictCodeFormatSerializer(formatTypeEnum, dictTypeCode);
}
return super.findSerializer(annotated);
}