java 基于 Jackson 字段的序列化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10240372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:10:45  来源:igfitidea点击:

Hymanson field based serialization

javajsonHymanson

提问by Hyman_carver

I think Hymanson does method based serialization, is there any way I could make it field based?

我认为 Hymanson 是基于方法的序列化,有什么办法可以让它基于字段?

Ex:

前任:

class Bean {
    Integer i;
    String s;

    public Integer getI() { return this.i; }
    public void setI(Integer i) { this.i = i; }
    public bool isSetI() { return this.i != null; }

    // same for s as well
}

The output JSON has "i" and "setI". Is there anyway I could override this to get only "i"? Also it would be great if there was a way to do this without adding any annotations to the class (they are auto generated).

输出 JSON 具有“i”和“setI”。无论如何我可以覆盖它以只获得“i”?如果有一种方法可以在不向类添加任何注释的情况下执行此操作(它们是自动生成的),那也会很棒。

采纳答案by Nilzor

To accomplish this without annotations you can configure the ObjectMapperbefore serializing/deserializing in the following manner:

要在没有注释的情况下完成此操作,您可以ObjectMapper按以下方式配置序列化/反序列化之前:

ObjectMapper om = new ObjectMapper();
om.setVisibilityChecker(om.getSerializationConfig().getDefaultVisibilityChecker().
            withGetterVisibility(JsonAutoDetect.Visibility.NONE).
            withSetterVisibility(JsonAutoDetect.Visibility.NONE));

回答by JohnC

Check out the @JsonAutoDetect annotation. Example:

查看@JsonAutoDetect 注释。例子:

@JsonAutoDetect(fieldVisibility=Visibility.ANY, getterVisibility=Visibility.NONE, isGetterVisibility=Visibility.NONE, setterVisibility=Visibility.NONE)
public class Bean {
    Integer i;
    String s;

    Integer getI() { return this.i; }
    void setI(Integer i) { this.i = i; }
    bool isSetI() return { this.i == null; }

    // same for s as well
}

回答by StaxMan

Hymanson can use fields as well, yes; but by default only public fields are discovered. But you can use @JsonPropertyto mark non-public fields.

Hyman逊也可以使用字段,是的;但默认情况下只发现公共字段。但是你可以@JsonProperty用来标记非公共字段。

One thing to note is that if there is both a method (setX, getX) and field (x), method will have precedence. This is usually not a problem, but if it is, you need to explicitly then disable method(s) that is not to be used, by adding @JsonIgnoreenxt to them.

需要注意的一点是,如果同时存在方法(setX、getX)和字段(x),则方法优先。这通常不是问题,但如果是,则需要通过向@JsonIgnore它们添加enxt 来显式禁用不使用的方法。

回答by Helder Pereira

The accepted answer didn't produce the expected result for me. What works for me without adding annotations is configuring the ObjectMapperin the following way:

接受的答案没有为我产生预期的结果。在不添加注释的情况下对我有用的是ObjectMapper按以下方式配置:

ObjectMapper om = new ObjectMapper()
        .setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE)
        .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

This is the way that seems to make more sense to me, which uses the fields instead of getters\setters like standard Java serialization would do.

这种方式对我来说似乎更有意义,它使用字段而不是像标准 Java 序列化那样使用 getter\setter。