提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.jackson.core.databind; |
H |
2 |
|
|
3 |
import com.fasterxml.jackson.core.JsonGenerator; |
|
4 |
import com.fasterxml.jackson.databind.SerializerProvider; |
|
5 |
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl; |
|
6 |
|
|
7 |
import java.io.IOException; |
|
8 |
|
|
9 |
/** |
|
10 |
* Long 序列化规则 |
|
11 |
* |
|
12 |
* 会将超长 long 值转换为 string,解决前端 JavaScript 最大安全整数是 2^53-1 的问题 |
|
13 |
* |
|
14 |
* @author 星语 |
|
15 |
*/ |
|
16 |
@JacksonStdImpl |
|
17 |
public class NumberSerializer extends com.fasterxml.jackson.databind.ser.std.NumberSerializer { |
|
18 |
|
|
19 |
private static final long MAX_SAFE_INTEGER = 9007199254740991L; |
|
20 |
private static final long MIN_SAFE_INTEGER = -9007199254740991L; |
|
21 |
|
|
22 |
public static final NumberSerializer INSTANCE = new NumberSerializer(Number.class); |
|
23 |
|
|
24 |
public NumberSerializer(Class<? extends Number> rawType) { |
|
25 |
super(rawType); |
|
26 |
} |
|
27 |
|
|
28 |
@Override |
|
29 |
public void serialize(Number value, JsonGenerator gen, SerializerProvider serializers) throws IOException { |
|
30 |
// 超出范围 序列化位字符串 |
|
31 |
if (value.longValue() > MIN_SAFE_INTEGER && value.longValue() < MAX_SAFE_INTEGER) { |
|
32 |
super.serialize(value, gen, serializers); |
|
33 |
} else { |
|
34 |
gen.writeString(value.toString()); |
|
35 |
} |
|
36 |
} |
|
37 |
} |