Formatting doubles

4,400 views
Skip to first unread message

xtian

unread,
Jul 28, 2010, 12:20:49 PM7/28/10
to google-gson
When I serialize an array of doubles, I get something like

[16.0,16.0,14.666666666666666,16.22222222222222,15.777777777777779]

In an effort to save bandwidth I implemented a custom serializer that
removes ".0" from integers and keeps 2 decimal digits for numbers
greater than 1.

The result is now the following:

["16","16","14.67","16.22","15.78"]

Almost there, but all numbers have become strings.

I'm stuck here. How to achieve the desired goal?

My code:

NumberFormat doubleFormatter = NumberFormat.getInstance(Locale.US);
doubleFormatter.setMaximumFractionDigits(2);
Gson gson = new GsonBuilder()
.registerTypeAdapter(double.class, new JsonSerializer<Double>() {
public JsonElement serialize(Double value, Type theType,
JsonSerializationContext context) {
String result;
if (value.isInfinite() || value.isNaN() || value.doubleValue()<1)
{
// Small or special values are handled by toString()
result = value.toString();
} else {
// Values greater than 1 are rounded to save bandwidth
result = doubleFormatter.format(value);
}
return new JsonPrimitive(result);
}
}).create();

P.S. someone had a similar problem back in 2008, here:
http://groups.google.com/group/google-gson/browse_thread/thread/51381d16b0bef288/ad09951d165d6cb5?lnk=gst&q=number+formatting#ad09951d165d6cb5

Pawel Veselov

unread,
Jul 28, 2010, 7:11:42 PM7/28/10
to googl...@googlegroups.com

Since you are creating JsonPrimitive over a string, the output becomes string.
My suggestion would be to use BigDecimal everywhere and use the necessary precision/scale, or at convert to a big decimal in the custom serializer:
...
return new JsonPrimitive((new BigDecimal(value)).setScale(2, BigDecimal.ROUND_HALF_UP));
...

Thanks,
  Pawel.


--
You received this message because you are subscribed to the Google Groups "google-gson" group.
To post to this group, send email to googl...@googlegroups.com.
To unsubscribe from this group, send email to google-gson...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-gson?hl=en.




--
With best of best regards
Pawel S. Veselov

xtian

unread,
Jul 29, 2010, 5:33:32 AM7/29/10
to google-gson
Thank you, that's the trick!

My snippet is now the following (just for reference):

private Gson gson = new GsonBuilder()
.registerTypeAdapter(double.class, new JsonSerializer<Double>() {
public JsonElement serialize(Double value, Type theType,
JsonSerializationContext context) {
if (value.isNaN()) {
return new JsonPrimitive(0); // Convert NaN to zero
} else if (value.isInfinite() || value.doubleValue()<1) {
return new JsonPrimitive(value); // Leave small numbers and
infinite alone
} else {
// Keep 2 decimal digits only
return new JsonPrimitive((new BigDecimal(value)).setScale(2,
BigDecimal.ROUND_HALF_UP));
}
}
})
.create();
> >http://groups.google.com/group/google-gson/browse_thread/thread/51381...
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "google-gson" group.
> > To post to this group, send email to googl...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > google-gson...@googlegroups.com<google-gson%2Bunsubscribe@googlegr oups.com>
> > .
Reply all
Reply to author
Forward
0 new messages