json 为什么杰克逊也在序列化临时成员?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21745593/
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
Why Hymanson is serializing transient member also?
提问by Naman
I am serializing a POJO into JSON using Hymanson 2.1.4 but I want to ignore a particular field from getting serialized. I used transient but still it is serializing that element.
我正在使用 Hymanson 2.1.4 将 POJO 序列化为 JSON,但我想忽略特定字段的序列化。我使用了瞬态,但它仍在序列化该元素。
public class TestElement {
int x;
private transient String y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public String getY() {
return y;
}
public void setY(String y) {
this.y = y;
}
}
I am serializing as following:
我正在序列化如下:
public static void main(String[] args) throws JsonProcessingException {
TestElement testElement = new TestElement();
testElement.setX(10);
testElement.setY("adasd");
ObjectMapper om = new ObjectMapper();
String serialized = om.writeValueAsString(testElement);
System.err.println(serialized);
}
Please don't suggest @JsonIgnoreas I don't want to tie my model to Hymanson specific annotations. Can it be done using transient only? Is there any API on objectmapper for visibility settings?
请不要建议,@JsonIgnore因为我不想将我的模型与Hyman逊特定的注释联系起来。可以只使用瞬态完成吗?objectmapper 上是否有用于可见性设置的 API?
回答by Eugen
The reason Hymanson serializes the transientmember is because the getters are used to determine what to serialize, not the member itself - and since yhas a public getter, that gets serialized.
If you want to change that default and have Hymanson use fields - simply do:
Hymanson 序列化transient成员的原因是因为 getter 用于确定要序列化的内容,而不是成员本身 - 并且由于y有一个公共 getter,它会被序列化。如果您想更改该默认值并让 Hymanson 使用字段 - 只需执行以下操作:
om.setVisibilityChecker(
om.getSerializationConfig().
getDefaultVisibilityChecker().
withFieldVisibility(JsonAutoDetect.Visibility.ANY).
withGetterVisibility(JsonAutoDetect.Visibility.NONE));
Another way to ignore a property on serialization is to do it directly on the class:
忽略序列化属性的另一种方法是直接在类上执行:
@JsonIgnoreProperties(value = { "y" })
public class TestElement {
...
And another way is directly on the field:
而另一种方式是直接在场上:
public class TestElement {
@JsonIgnore
private String y;
...
Hope this helps.
希望这可以帮助。
回答by manuel
A new way to stop Hymanson from serializing and deserializing is to call mapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true).
阻止 Hymanson 序列化和反序列化的一种新方法是调用mapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true).
回答by idelvall
I can't make comments so complete the previous response here, changing the (now) deprecated method setVisibilityCheckerand adding a missing clause for booleans:
我无法在这里完成之前的回复,更改(现在)已弃用的方法setVisibilityChecker并为布尔值添加一个缺失的子句:
mapper.setVisibility(
mapper.getSerializationConfig().
getDefaultVisibilityChecker().
withFieldVisibility(JsonAutoDetect.Visibility.ANY).
withGetterVisibility(JsonAutoDetect.Visibility.NONE).
withIsGetterVisibility(JsonAutoDetect.Visibility.NONE)
);
回答by Carlos Cuesta
You can configure it with springs properties
您可以使用 springs 属性对其进行配置
spring.Hymanson.mapper.propagate-transient-marker=true

