Why not provide a new serialiser for Double
? (You will likely have to rewrite your object to use Double
instead of double
).
Then in the serialiser you can convert to a BigDecimal
, and play with the scale etc.
e.g.
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Double.class, new JsonSerializer<Double>() {
@Override
public JsonElement serialize(final Double src, final Type typeOfSrc, final JsonSerializationContext context) {
BigDecimal value = BigDecimal.valueOf(src);
return new JsonPrimitive(value);
}
});
gson = gsonBuilder.create();
The above will render (say) 9.166666E-6
as 0.000009166666
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…