1

I have a custom JSON serializer with two fields, one string and one double. I have a requirement to have my output look like this:

[
   {  
      "name":"name",
      "value":3.0
   },
   {  
      "name":"name2",
      "value":3.5
   }
]

But right now it looks like this:

[
   {  
      "name":"name",
      "value":3
   },
   {  
      "name":"name2",
      "value":3.5
   }
]

My serialization code looks like this:

gen.writeStringField("name",movie.getName());
gen.writeNumberField("value",movie.getAverageRating()); // double type

I can make the field value to be String type and use something like:

gen.writeStringField("value",String.format("%.1f", movie.getAverageRating()));

but then the value of field "value" will have double qoutes and will by of type String and not number. So my question is: is it even possible to achieve the requirement which I mentioned at the begining?

a4dev92
  • 531
  • 6
  • 15
  • 4
    IMO, formatting does not make sense for a number. `3` or `3.0` is the same number. If formatting matters, then it should be a string. – Arnaud Denoyelle May 02 '19 at 12:20
  • 2
    Possible duplicate of [Serialize a Double to 2 decimal places using Jackson](https://stackoverflow.com/questions/11520781/serialize-a-double-to-2-decimal-places-using-jackson) – Ori Marko May 02 '19 at 12:21
  • @ArnaudDenoyelle I agree that it doesn't make sense and 3 and 3.0 is the same number in that case but this is my project requirement to have the response look like that and I wanted to make sure that this requirement is not possible to meet. – a4dev92 May 02 '19 at 12:25
  • It's obviously possible. At worst build the JSON text by yourself. But your JSON library probably allows to define your own serializer for numbers. It's only a guess because I never needed to do that for Jackson – kumesana May 02 '19 at 12:26
  • What are you getting here `movie.getAverageRating()` ? – Sudhir Ojha May 02 '19 at 12:29
  • @ArnaudDenoyelle No, it's not. Just like in Java, numbers in Json are also typed. In Java, `3` is an `int` and `3.0` is a `double`. In Json, how many digits are behind a decimal point is significant and it's valid to use to use this information, for example to determine the type of the number as an integer or floating-point number. – Erwin Bolwidt May 02 '19 at 12:30
  • @SudhirOjha double type with value of 3.0 and in JSON it is shown as 3 – a4dev92 May 02 '19 at 12:30
  • Floating point (`float` and `double` do not have a precision/number of decimals). BigDecimal has, doing `new BigDecimal("3.0")` will provide a scale of -1. Also double 3.1 will never be exactly 3.1000Ø, but rather 3.09999999987 or such. – Joop Eggen May 02 '19 at 12:31
  • 2
    @ErwinBolwidt I politely disagree. JSON types come from javascript : `3` and `3.0` are of type `number`. The [RFC7159](https://tools.ietf.org/html/rfc7159.html#page-5) states that JSON values can be of type `object, array, number, string, true, false, null`. Also, in JS, `3 === 3.0` returns `true` – Arnaud Denoyelle May 02 '19 at 12:36
  • 3
    Keep in mind that if it is a *requirement*, then it is not JSON. The recipient should fix their code. – tevemadar May 02 '19 at 12:48
  • @tevemadar Yes, my opinion is the same as yours. I just wanted to confirm that. – a4dev92 May 02 '19 at 12:52
  • @ArnaudDenoyelle What's relevant is where the standard is defined, not where it comes from: https://www.json.org/ The "number" production is defined as "int frac exp" and nowhere is it defined that a fraction of `.0` is equal to not having a fraction at all. – Erwin Bolwidt May 02 '19 at 13:44
  • @ErwinBolwidt JSON has *number* only, there is no distinction between integer and floating point. Therefore `3` is `3.0` and `3.0` is `3`. – tevemadar May 02 '19 at 14:31
  • @tevemadar I just pointed you to the json spec at http://json.org which proves you wrong. Please back up your claim with a proper reference. – Erwin Bolwidt May 02 '19 at 14:58
  • @ErwinBolwidt actually you pointed to an ad-hoc informational site coincidentally named as [json.org](http://json.org), to *someone else*, who previously referred to an actual IETF RFC about [JSON](https://tools.ietf.org/html/rfc7159.html). "number = [ minus ] int [ frac ] [ exp ]", that is the definition, find it on [page 7](https://tools.ietf.org/html/rfc7159.html#page-7). – tevemadar May 02 '19 at 15:05

0 Answers0