Skip to content

Serializing a double property as string

Which @JsonXyz annotation do I have to use when I have a bean to be jsonified but serializing a double proeprty as string.

An example: I have a bean:

public MyBean {
    private double myDouble;
    //getter and setter

And I wan tto have a JSON like:


instead of:


So, the value 100.0 shall be in quotes.


If you are using Jackson you can use @JsonSerialize and ToStringSerializer :

public MyBean {
    @JsonSerialize(using = ToStringSerializer.class)
    private double myDouble;
    //getter and setter

The code to test it (Jackson version 2.9.8) :

MyBean myBean = new MyBean(20.3);

ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(myBean);


The output is :