1

We are using Spring MappingJackson2HttpMessageConverter to convert our data to json resources.

We have noticed that if the record id is a large number such as eg:1234567891234567891 the converted resource id (record id is the resource id) is automatically rounded up to 1234567891234568000.

It seems the Jackson can't handle the larger number or is it some limitation in javascript/json number representations?

Following is our current MappingJackson2HttpMessageConverter configurations

Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();

    DateFormat defaultResourceDateOutputFormat = new SimpleDateFormat(
            DEFAULT_INTERNAL_DATE_FORMAT);
    builder.dateFormat(defaultResourceDateOutputFormat);
    builder.timeZone(TimeZone.getDefault());
    builder.serializationInclusion(Include.NON_NULL);
    builder.featuresToDisable(
            DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    builder.defaultViewInclusion(Boolean.TRUE);

I have referred Large numbers erroneously rounded in Javascript but not sure it's caused by the same reason.

Appreciate any feedback. Thanks in advance

Community
  • 1
  • 1
virtualpathum
  • 753
  • 2
  • 11
  • 23
  • 1
    Perhaps http://stackoverflow.com/questions/16052314/how-can-i-force-jackson-to-write-numbers-as-strings-when-serializing-my-objects? – chrylis -cautiouslyoptimistic- Oct 24 '16 at 07:00
  • Thanks chrylis but that means we need to convert the number to a String right? I don't think we can do it in our situation. – virtualpathum Oct 24 '16 at 07:06
  • Why not? An ID is supposed to be essentially opaque, not something with intrinsic semantic value (e.g., you're not going to be performing math operations on it). – chrylis -cautiouslyoptimistic- Oct 24 '16 at 07:07
  • Also to do that we need add @JsonSerialize(using = ToStringSerializer.class) to each and every entity right? Im looking for an easy way like some configuration in Jackson to do that instead of touching all the entities. By the way thanks again – virtualpathum Oct 24 '16 at 07:16
  • Pretty much. There just isn't any other way; in JSON, you just have double-valued "numbers". – chrylis -cautiouslyoptimistic- Oct 24 '16 at 08:31
  • @chrylis as you said there is no any other way to go and therefore we decided to use @JsonSerialize(using = ToStringSerializer.class) and seems it does the job. I just wanted to know why does the default jackson serializer failed to prevent this and also as I understand ToStringSerializer.class is using toString() before write as json. How does it prevent the rounding up? – virtualpathum Nov 15 '16 at 07:29

0 Answers0