I am writing a custom serializer to convert double values into strings in JSON objects. My code so far:
public String toJson(Object obj) throws IOException {
You can register the JsonSerializer
for the primitive type double
.
module.addSerializer(double.class, new DoubleSerializer());
You can do it with custom serializer, but there is a simpler way to.
Enable JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS
:
mapper.getFactory().enable(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS);