为什么杰克逊在 Json 中添加 "empty": false,

Why does jackson add an "empty": false, into the Json

我用 Jackson

注释了一个 Java class
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonPropertyOrder({
  "estimated_shipping_weight",
  "useestimatedweight",
  "matched",
  "available",
  "primary_vendor",
  "stock_status_code",
  "primary_vendor",
  "listprice",
  "webprice",
  "hits",
  "IsIndividual",
  "clearance",
  "display_uom",
  "display_uom_factor",
  "weight",
  "rank",
  "box_qty",
  "ddsFileDateSource",
  "dateLastUpdated",
  "ddiFileDateSource",
  "cost"
})

public class AdditionalFields {

  public static final Logger LOG = LoggerFactory.getLogger(AdditionalFields.class);
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @JsonProperty("estimated_shipping_weight")
  private Double estimatedShippingWeight;
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @JsonProperty("useestimatedweight")
  private String useestimatedweight;
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @JsonProperty("matched")
  private String matched;
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @JsonProperty("IsIndividual")
  private String IsIndividual;
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @JsonProperty("stock_status_code")
  private String stockStatusCode;
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @JsonProperty("available")
  private Integer available;
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @JsonProperty("sch_available")
  private String schAvailable;
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @JsonProperty("primary_vendor")
  private Integer primaryVendor;
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @JsonProperty("listprice")
  private Double listprice;
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @JsonProperty("webprice")
  private Double webprice;
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @JsonProperty("hits")
  private Integer hits;
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @JsonProperty("clearance")
  private String clearance;
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @JsonProperty("display_uom_factor")
  private Integer displayUomFactor;
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @JsonProperty("display_uom")
  private String displayUom;
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @JsonProperty("weight")
  private Double weight;
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @JsonProperty("box_qty")
  private Integer boxQty;
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @JsonProperty("rank")
  private Integer rank;
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @JsonProperty("product_line_description")
  private String productLineDescription;
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @JsonProperty("ddiFileDateSource")
  private String ddiFileDateSource;
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @JsonProperty("ddsFileDateSource")
  private String ddsFileDateSource;
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @JsonProperty("dateLastUpdated")
  private String dateLastUpdated;
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  @JsonProperty("cost")
  private String cost;

然而,我在 json 输出中得到了一个额外的字段 "empty": false,这是不需要的。我想知道为什么该字段会出现在输出中以及如何删除它。

   "additional_fields": {
        "matched": "f",
        "available": 0,
        "primary_vendor": 864,
        "listprice": 1364.29,
        "webprice": 1604.414,
        "display_uom": "EA",
        "display_uom_factor": 1,
        "ddiFileDateSource": "DDI_2021_10_06_14_36_37.zip",
        "empty": false,
        "product_line_description": "ABB"
    }

序列化

ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
String json = mapper.writeValueAsString(additionalFields);

However I'm getting an additional field "empty": false, in the json output which is unwanted. I'm wondering why that field shows up in the output and how to remove it.

正如一位用户在他的评论中所建议的那样,这种行为是由于用于序列化的 Jackson 库检查您的 class 并且当它找到以前缀 is,它会自动在 json 文件中添加一个字段,其名称(在本例中为 empty)出现在前缀 is 和相应的值之后.为避免这种情况,您可以使用 JsonIgnore.

注释您的方法