java Jackson - 将属性序列化/反序列化为 JSON 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12096216/
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 07:34:43  来源:igfitidea点击:

Hymanson - serialize/deserialize property as JSON value

javajsonHymanson

提问by jonc

Using Hymanson 2, I'm looking for a genericway to be serialize objects as a single value (then serialize them back later populating only that single field) without having to repetitively create a JsonSerializer / JsonDeserializer to handle each case. The @JsonIdentityInfo annotation comes pretty close but misses the mark a little bit since, as far as I can tell, it will always serialize the full child object on the first occurrence.

使用 Hymanson 2,我正在寻找一种通用方法将对象序列化为单个值(然后将它们序列化回仅填充该单个字段),而不必重复创建 JsonSerializer / JsonDeserializer 来处理每种情况。@JsonIdentityInfo 注释非常接近,但有点错过了标记,因为据我所知,它总是在第一次出现时序列化完整的子对象。

Here is an example of what I want to do. Given the classes:

这是我想要做的一个例子。鉴于课程:

class Customer {

    Long id = 99;
    String name = "Joe"; 
    // Lots more properties
}

class Order {
    String orderNum = "11111"
    @WhateverAnnotationsINeedHereToDoWhatIWant
    Customer customer;
}

I would like Order to serialize as either (either would be perfectly acceptable to me):

我希望 Order 序列化为(我完全可以接受):

{"orderNum":"11111", "customer":"99"}
{"orderNum":"11111", "customer":{"id":99}}

What @JsonIdentityInfo does makes it more difficult to deal with on the client-side (we can assume that the client knows how to map the customer ID back into the full customer information).

@JsonIdentityInfo 所做的事情使得在客户端处理变得更加困难(我们可以假设客户端知道如何将客户 ID 映射回完整的客户信息)。

@JsonIgnoreProperties could also come pretty close for the second JSON shown but would mean I would have to opt-out of everythingbut the one I want.

对于显示的第二个 JSON,@JsonIgnoreProperties 也可能非常接近,但这意味着我必须选择退出除我想要的之外的所有内容

When I deserialize back I would just want the Customer to be initialzed with the "id" field only.

当我反序列化时,我只希望 Customer 仅使用“id”字段进行初始化。

Any magic way to do this that I'm missing without getting into the soupy internals of Hymanson? As far as I can tell, JsonDeserializer/JsonSerializer has no contextual information on the parent so there doesn't seem to be an easy way to create a @JsonValueFromProperty("id") type Hymanson Mix-in then just look at that annotation in the custom Serializer/Deserializer.

有什么神奇的方法可以做到这一点,而我却没有深入了解Hyman逊的内部结构?据我所知,JsonDeserializer/JsonSerializer 没有关于父级的上下文信息,所以似乎没有一种简单的方法来创建 @JsonValueFromProperty("id") 类型的 Hymanson Mix-in 然后看看那个注释自定义序列化器/反序列化器。

Any ideas would be greatly appreciated. Thanks!

任何想法将不胜感激。谢谢!

采纳答案by Integrating Stuff

I once needed fine-grained control over the JSON returned per different type of request, and I am afraid I ended up using custom Serializers and Deserializers.

我曾经需要对每个不同类型的请求返回的 JSON 进行细粒度控制,恐怕我最终使用了自定义序列化程序和反序列化程序。

A simple alternative would be adding @JsonIgnore to the Customer field of Order and add the following getter to Order:

一个简单的替代方法是将 @JsonIgnore 添加到 Order 的 Customer 字段并将以下 getter 添加到 Order:

@JsonProperty("customer")
public Long getCustomerId(){
  if (customer != null){
    return customer.getId();
  }
  else {
    return null;
  }
}

The returned JSON would then be:

返回的 JSON 将是:

{"orderNum":"11111", "customer":"99"}

回答by StaxMan

Another possibility could be use of @JsonValue, used on id field.

另一种可能性是使用@JsonValue, 用于 id 字段。

Hymanson 2.1 will add a feature to force serialization of even the force reference as id (by new property, "firstAsId" for @JsonIdentityInfo). That may not work well with deserialization for all cases but perhaps would work for you.

Hymanson 2.1 将添加一项功能,以强制将强制引用序列化为 id(通过新属性“firstAsId”为@JsonIdentityInfo)。这可能不适用于所有情况下的反序列化,但可能对您有用。