简体   繁体   English

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

[英]Jackson - serialize/deserialize property as JSON value

Using Jackson 2, I'm looking for a generic way 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. 使用Jackson 2,我正在寻找一种通用的方法将对象序列化为单个值(然后将它们序列化,以后只填充该单个字段),而不必重复创建JsonSerializer / JsonDeserializer来处理每个案例。 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. @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 everything but the one I want. 对于显示的第二个JSON,@ JsonIgnoreProperties也可能非常接近,但这意味着我必须选择退出除了我想要的一切之外的所有东西

When I deserialize back I would just want the Customer to be initialzed with the "id" field only. 当我反序列化时,我只希望客户仅使用“id”字段进行初始化。

Any magic way to do this that I'm missing without getting into the soupy internals of Jackson? 任何神奇的方式来做到这一点,我错过了没有进入杰克逊的汤内部? 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 Jackson Mix-in then just look at that annotation in the custom Serializer/Deserializer. 据我所知,JsonDeserializer / JsonSerializer没有关于父级的上下文信息,因此似乎没有一种简单的方法来创建@JsonValueFromProperty(“id”)类型Jackson Mix-in然后只看一下该注释自定义Serializer / Deserializer。

Any ideas would be greatly appreciated. 任何想法将不胜感激。 Thanks! 谢谢!

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"}

Another possibility could be use of @JsonValue , used on id field. 另一种可能是使用@JsonValue ,用于id字段。

Jackson 2.1 will add a feature to force serialization of even the force reference as id (by new property, "firstAsId" for @JsonIdentityInfo ). Jackson 2.1将添加一个强制序列甚至将强制引用序列化为id的功能(通过新属性,对@JsonIdentityInfo “firstAsId”)。 That may not work well with deserialization for all cases but perhaps would work for you. 对于所有情况,这可能不适用于反序列化,但可能对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM