在序列化对象时,如何强制Jackson将数字写为字符串 [英] How can I force Jackson to write numbers as strings when serializing my objects

查看:392
本文介绍了在序列化对象时,如何强制Jackson将数字写为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个java对象上的id非常大。当它将jackson转换为JSON时,它会将其作为数字发送(例如{id:1000110040000000001})但是一旦它成为javascript对象,id就会变为1000110040000000000.我读到了这个问题在其他地方

I have an id that is pretty large on one of my java objects. When it jackson converts it to JSON it sends it down as a number (e.g. {"id":1000110040000000001}) but as soon as it becomes a javascript object the id gets changed to 1000110040000000000. I read about this issue here

当id更小。我的第一个想法是强迫杰克逊将所有数字转换成字符串,但我也对其他选项持开放态度。如果可能的话,我宁愿不将Jackson注释添加到我的java对象中。

It works fine when the id is smaller. My first thought is to just force Jackson to convert all the numbers to strings but I am open to other options as well. If possible I would prefer not to add Jackson annotations to my java objects.

推荐答案

Jackson-databind(至少2.1.3)提供特殊的ToStringSerializer。这样做对我来说。

Jackson-databind (at least 2.1.3) provides special ToStringSerializer. That did it for me.

@Id @JsonSerialize(using = ToStringSerializer.class)
private Long id;

这篇关于在序列化对象时,如何强制Jackson将数字写为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆