java 嵌套对象的杰克逊序列化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33151974/
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
Hymanson serialization of nested objects
提问by Anar Amrastanov
I have problem with Hymanson serialization of object by its interface.
我在通过接口对对象进行 Hymanson 序列化时遇到问题。
I have class
我有课
class Point implements PointView {
private String id;
private String name;
public Point() {
}
public Point(String id, String name) {
this.id = id;
this.name = name;
}
@Override
public String getId() {
return id;
}
public String getName() {
return name;
}
}
which implements
其中实现
interface PointView {
String getId();
}
and have class
并上课
class Map implements MapView {
private String id;
private String name;
private Point point;
public Map() {
}
public Map(String id, String name, Point point) {
this.id = id;
this.name = name;
this.point = point;
}
@Override
public String getId() {
return id;
}
public String getName() {
return name;
}
@JsonSerialize(as = PointView.class)
public Point getPoint() {
return point;
}
}
which implements
其中实现
interface MapView {
String getId();
Point getPoint();
}
And have class
并上课
class Container {
private Map map;
public Container() {
}
public Container(Map map) {
this.map = map;
}
@JsonSerialize(as = MapView.class)
public Map getMap() {
return map;
}
}
I want serialize Container with Hymanson and get result
我想用 Hymanson 序列化 Container 并得到结果
{"map":{"id":"mapId","point":{"id":"pointId"}}}
But in fact I get result
但实际上我得到了结果
{"map":{"id":"mapId","point":{"id":"pointId","name":"pointName"}}}
that have property "name" in nested object "point" although I specified serializition type of Point in Map (@JsonSerialize(as = PointView.class)
). Interface PointView dont have method getName, but in result exists field "name" of Point.
尽管我在 Map ( @JsonSerialize(as = PointView.class)
) 中指定了 Point 的序列化类型,但在嵌套对象“point”中具有属性“name” 。接口 PointView 没有方法 getName,但在结果中存在 Point 的字段“名称”。
If I remove annotation (@JsonSerialize(as = MapView.class)
) from method getMap in class Container I get result
如果我@JsonSerialize(as = MapView.class)
从类 Container 中的方法 getMap 中删除注释(),我会得到结果
{"map":{"id":"mapId","name":"mapName","point":{"id":"pointId"}}}
Now point dont have property "name", but map have.
现在点没有属性“名称”,但地图有。
How can I get result
我怎样才能得到结果
{"map":{"id":"mapId","point":{"id":"pointId"}}}
?
?
采纳答案by Anar Amrastanov
To get the desired result also the same method in interface must be annotated by @JsonSerialize
要获得所需的结果,接口中的相同方法也必须由 @JsonSerialize
interface MapView {
String getId();
@JsonSerialize(as = PointView.class)
Point getPoint();
}
回答by Bohemian
You can annotate the method like this:
您可以像这样注释方法:
@JsonIgnore
public String getName() {
return name;
}
Or if you want specific serialization in this use case, but normal serialization in others, you can use a @JsonView
(see doc).
或者,如果您想要在此用例中进行特定序列化,而在其他用例中进行正常序列化,则可以使用 a @JsonView
(请参阅doc)。
The reason it's serializing out the name
is that the instancehas the accessor getName()
, even though interface does not.
它序列化的原因name
是实例具有访问器getName()
,即使接口没有。
回答by StaxMan
Yes, you can use
是的,你可以使用
@JsonSerialize(as=MyInterface.class)
public class ConcreteClass implements MyInterface { .... }
either on implementation class (as above), or on property that has value.
在实现类(如上)或具有值的属性上。