parent
730215a286
commit
5edd04bd78
12 changed files with 198 additions and 132 deletions
@ -0,0 +1,31 @@ |
|||||||
|
package org.springblade.desk.util.json.serializer; |
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerator; |
||||||
|
import com.fasterxml.jackson.databind.SerializerProvider; |
||||||
|
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
/** |
||||||
|
* 自定义Long序列化器:0L值 或 null 序列化为空字符串,其他值正常序列化 |
||||||
|
*/ |
||||||
|
public class IdSerializer extends StdScalarSerializer<Long> { |
||||||
|
|
||||||
|
/** |
||||||
|
* 提供无参构造器,指定处理的类型为Integer |
||||||
|
*/ |
||||||
|
public IdSerializer() { |
||||||
|
super(Long.class); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void serialize(Long value, JsonGenerator gen, SerializerProvider provider) |
||||||
|
throws IOException { |
||||||
|
// 核心逻辑:判断值是否为null或0,是则写空字符串,否则写原数值
|
||||||
|
if (value == null || value == 0L) { |
||||||
|
gen.writeString(""); // 0或null时返回空字符串
|
||||||
|
} else { |
||||||
|
gen.writeString("" + value); // 返回字符串类型
|
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue