使用杰克逊序列化,我如何序列化双精度值null并且在0.0时不返回 [英] use jackson serialize, How can I serialize double value null and not return when 0.0

查看:176
本文介绍了使用杰克逊序列化,我如何序列化双精度值null并且在0.0时不返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试实现自定义的 Jackson 序列化程序,并且我还想在等于 0.0 writeNull(),而不返回。

这是我的序列化程序代码

I try to implement custom Jackson serializer, and also I want to handle double value when equals 0.0, writeNull(), and not return.
Here is my serializer code

public class DoubleGTZeroSerializer extends JsonSerializer<Double> {

  private DecimalFormat df = new DecimalFormat("##.##");

  @Override
  public Class<Double> handledType() {
    return Double.class;
  }

  @Override
  public void serialize(Double value, JsonGenerator gen, SerializerProvider serializers)
      throws IOException, JsonProcessingException {
    if (value != null && value.doubleValue() > 0) {
      gen.writeString(df.format(value));
    } else {
      gen.writeNull();
    }
  }
}

以下是pojo

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Bill implements Serializable {

  private static final long serialVersionUID = -5034123031564773631L;

  @JsonSerialize(using = DoubleToStringSerializer.class)
  private Double orderFee;

  @JsonSerialize(using = DoubleToStringAfterSymbolSerializer.class)
  private Double transFee;

  @JsonSerialize(using = DoubleToStringBeforeSymbolSerializer.class)
  private Double otherFee;

  @JsonSerialize(using = DoubleGTZeroSerializer.class)
  private Double gtZeroFee;
 ...
}

而我的请求是
< a href = http:// localhost:5509 / test?orderFee = 10.1& transFee = 100.00& otherFee =& gtZeroFee = 0 rel = nofollow noreferrer> http:// localhost:5509 / test?orderFee = 10.1& transFee = 100.00& otherFee =& gtZeroFee = 0
api结果

and my request is http://localhost:5509/test?orderFee=10.1&transFee=100.00&otherFee=&gtZeroFee=0 the api result

{
    "status": {
        "desc": "操作成功",
        "code": 0
    },
    "data": [
        {
            "orderFee": "10.1",
            "transFee": "100元",
            "gtZeroFee": null
        }
    ],
    "success": true
}

我不想把 gtZeroFee 为空,但注释 @JsonInclude(JsonInclude.Include.NON_NULL)无效,请帮助我,谢谢

I don't want to out put the gtZeroFee with null, but the annotation @JsonInclude(JsonInclude.Include.NON_NULL) not works, please help me, thanks all.

推荐答案

您可以使用 JsonInclude.Include.NON_NULL 代替 JsonInclude.Include.NON_EMPTY 和隐含ement isEmpty 方法:

Instead JsonInclude.Include.NON_NULL you can use JsonInclude.Include.NON_EMPTY and implement isEmpty method:

class DoubleGTZeroSerializer extends JsonSerializer<Double> {

    private DecimalFormat df = new DecimalFormat("##.##");

    @Override
    public Class<Double> handledType() {
        return Double.class;
    }

    @Override
    public void serialize(Double value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeString(df.format(value));
    }

    @Override
    public boolean isEmpty(SerializerProvider provider, Double value) {
        return value <= 0;
    }
}

并更改 Bill 注释:

@JsonInclude(JsonInclude.Include.NON_EMPTY)
class Bill implements Serializable {

这篇关于使用杰克逊序列化,我如何序列化双精度值null并且在0.0时不返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆