import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import java.io.IOException;
public class Application {
public static void main(String[] args) throws IOException {
SimpleModule module = new SimpleModule();
module.addSerializer(Long.class, ToStringSerializer.instance);
module.addSerializer(Long.TYPE, ToStringSerializer.instance);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(module);
Long l1 = 1L;
long l2 = 1L;
Long[] ls1 = new Long[]{1L, 2L};
long[] ls2 = new long[]{1L, 2L};
System.out.println(objectMapper.writeValueAsString(l1));
System.out.println(objectMapper.writeValueAsString(l2));
System.out.println(objectMapper.writeValueAsString(ls1));
System.out.println(objectMapper.writeValueAsString(ls2));
}
}
对于此代码片段,输出为:
"1"
"1"
["1","2"]
[1,2]
你能解释一下为什么ls2被序列化为[1,2]吗?我认为应该是[“1”,2]。