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

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

How to instruct Hymanson to serialize a field inside an Object instead of the Object it self?

javaserializationHymanson

提问by Ranhiru Jude Cooray

I have an Itemclass. There's an itemTypefield 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 Itemusing Hymanson ObjectMapper, it serializes the object ItemTypeas 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 namefield 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 @JsonValueannotation.

查看@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 @JsonIdentityInfoand @JsonIdentityReferenceannotations:

由于 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 Itemto return ItemType.name, and mark ItemTypegetter with @JsonIgnore?

也许一个快速的解决方法是在Itemreturn上添加一个额外的 getter ItemType.name,并将ItemTypegetter标记为@JsonIgnore?