javajavascriptjsonjackson

How can I force Jackson to write numbers as strings when serializing my objects


I have an id that is pretty large on one of my Java objects. When Jackson converts it to JSON, it writes it 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.

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.


Solution

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

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