|
@@ -0,0 +1,64 @@
|
|
|
+/*
|
|
|
+ * Copyright (c) 2025 GaoKunW
|
|
|
+ *
|
|
|
+ */
|
|
|
+
|
|
|
+package org.eco.vip.mapper.handler;
|
|
|
+
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+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 org.eco.vip.mapper.annotation.FieldMapper;
|
|
|
+import org.eco.vip.mapper.service.IFieldMapperInterface;
|
|
|
+import org.eco.vip.orm.utils.ReflectUtils;
|
|
|
+import org.eco.vip.orm.utils.StrUtils;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @description FieldMapperHandler
|
|
|
+ *
|
|
|
+ * @author GaoKunW
|
|
|
+ * @date 2025/7/4 13:24
|
|
|
+ */
|
|
|
+public class FieldMapperHandler extends JsonSerializer<Object> implements ContextualSerializer {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 全局实现类映射器
|
|
|
+ */
|
|
|
+ public static final Map<String, IFieldMapperInterface<?>> FIELD_MAPPER = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ private FieldMapper fieldMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
|
|
|
+ IFieldMapperInterface<?> trans = FIELD_MAPPER.get(fieldMapper.type());
|
|
|
+ if (ObjectUtil.isNotNull(trans)) {
|
|
|
+ // 如果映射字段不为空 则取映射字段的值
|
|
|
+ if (StrUtils.isNotBlank(fieldMapper.mapper())) {
|
|
|
+ o = ReflectUtils.invokeGetter(jsonGenerator.getCurrentValue(), fieldMapper.mapper());
|
|
|
+ }
|
|
|
+ // 如果为 null 直接写出
|
|
|
+ if (ObjectUtil.isNull(o)) {
|
|
|
+ jsonGenerator.writeNull();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Object result = trans.fieldMapper(o, fieldMapper.other());
|
|
|
+ jsonGenerator.writeObject(result);
|
|
|
+ } else {
|
|
|
+ jsonGenerator.writeObject(o);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|