SoFunction
Updated on 2025-03-06

@JsonSerialize Use of serialization annotations

@JsonSerialize serialization annotation

JSON is a common format for data exchange

The @JsonSerialize annotation is part of the Jackson library to define how to customize Java objects when serializing them into JSON format.

Jackson is a popular Java library for handling the serialization and deserialization of JSON data. You can customize the serialization class on a field, method, or class level. There is the using property that allows you to specify a custom serializer class to decide how to convert Java objects to JSON data.

Customize the data that needs to be converted into JSON, which can be used on the class to customize the JSON processing of this class, or it can be used on the field to customize the processing of the field, such as the date format, how many digits are retained, and whether it is correct to be 1 or 0

Create a class first

package ;
 
import .Customer1DoubleSerialize;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
 
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class TestVo {
 
    //Usage method, directly load the fields that need to retain decimals    @JsonSerialize(using = )
    private Double num;
 
    @ApiModelProperty("id")
    private String Id;
 
    @ApiModelProperty("name")
    private String name;
 
    @ApiModelProperty("Data Type")
    @ExcelProperty(converter = )
    private String dataType;
 
 
}

Keep one decimal point

package ;
 
import ;
import ;
import ;
 
import ;
import ;
import ;
 
public class Customer1DoubleSerialize extends JsonSerializer {
    private DecimalFormat df = new DecimalFormat("0.0");
    @Override
    public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        (RoundingMode.HALF_DOWN);
        if(() != null && !"-".equals(o)) {
            Double dd=(());
 
            ((dd));
 
        } else{
            (());
 
        }
    }
}

The decimal point retains 2 digits (it only takes three digits to define three decimals)

package ;
 
import ;
import ;
import ;
 
import ;
import ;
import ;
 
public class CustomerDoubleSerialize extends JsonSerializer {
    private DecimalFormat df = new DecimalFormat("0.00");
    @Override
    public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        (RoundingMode.HALF_DOWN);
        if(() != null && !"-".equals(o)) {
            Double dd=(());
 
            ((dd));
 
        } else{
            (());
 
        }
    }
}

When excel exports - splicing units

package ;
 
import ;
import ;
import ;
import ;
import ;
 
public class UnitConverter implements Converter<String> {
    @Override
    public Class<?> supportJavaTypeKey() {
        return ();
    }
 
    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return ();
    }
 
    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String dateValue = "";
        if (("2D")) {
            dateValue = "km";
        } else if (("3D")) {
            dateValue = "km2";
        }
        return new WriteCellData<>(dateValue);
    }
}

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.