java 如何指示杰克逊序列化对象内的字段而不是对象本身?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11031110/
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
How to instruct Hymanson to serialize a field inside an Object instead of the Object it self?
提问by Ranhiru Jude Cooray
I have an Item
class. There's an itemType
field inside of that class which is of type ItemType.
我有一Item
堂课。itemType
该类中有一个类型为 ItemType的字段。
roughly, something like this.
大致,类似这样的。
class Item
{
int id;
ItemType itemType;
}
class ItemType
{
String name;
int somethingElse;
}
When I am serializing an object of type Item
using Hymanson ObjectMapper
, it serializes the object ItemType
as a sub-object. Which is expected, but not what I want.
当我Item
使用 HymansonObjectMapper
序列化类型的对象时,它将对象序列化为ItemType
子对象。这是预期的,但不是我想要的。
{
"id": 4,
"itemType": {
"name": "Coupon",
"somethingElse": 1
}
}
What I would like to do is to show the itemType
's name
field instead when serialized.
我想做的是在序列化时显示itemType
'sname
字段。
Something like below.
像下面这样的东西。
{
"id": 4,
"itemType": "Coupon"
}
Is there anyway to instruct Hymanson to do so?
反正有没有指示Hyman逊这样做?
采纳答案by pingw33n
You need to create and use a custom serializer.
您需要创建和使用自定义序列化程序。
public class ItemTypeSerializer extends JsonSerializer<ItemType>
{
@Override
public void serialize(ItemType value, JsonGenerator jgen,
SerializerProvider provider)
throws IOException, JsonProcessingException
{
jgen.writeString(value.name);
}
}
@JsonSerialize(using = ItemTypeSerializer.class)
class ItemType
{
String name;
int somethingElse;
}
回答by StaxMan
Check out @JsonValue
annotation.
查看@JsonValue
注释。
EDIT: like this:
编辑:像这样:
class ItemType
{
@JsonValue
public String name;
public int somethingElse;
}
回答by Jacob van Lingen
As OP only wants to serialize one field, you could also use the @JsonIdentityInfo
and @JsonIdentityReference
annotations:
由于 OP 只想序列化一个字段,您还可以使用@JsonIdentityInfo
和@JsonIdentityReference
注释:
class Item {
int id;
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="name")
@JsonIdentityReference(alwaysAsId=true)
ItemType itemType;
}
For more info, see How to serialize only the ID of a child with Hymanson.
有关更多信息,请参阅如何使用 Hymanson 仅序列化孩子的 ID。
回答by u7565209
To return simple string, you can use default ToStringSerializer without define any extra classes. But you have to define toString() method return this value only.
要返回简单的字符串,您可以使用默认的 ToStringSerializer 而无需定义任何额外的类。但是你必须定义 toString() 方法只返回这个值。
@JsonSerialize(using = ToStringSerializer.class)
class ItemType
{
String name;
int somethingElse;
public String toString(){ return this.name;}
}
回答by maksimov
Perhaps a quick workaround is to add an extra getter on Item
to return ItemType.name
, and mark ItemType
getter with @JsonIgnore
?
也许一个快速的解决方法是在Item
return上添加一个额外的 getter ItemType.name
,并将ItemType
getter标记为@JsonIgnore
?