Jackson - 将属性序列化/反序列化为JSON值 [英] Jackson - serialize/deserialize property as JSON value

查看:227
本文介绍了Jackson - 将属性序列化/反序列化为JSON值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Jackson 2,我正在寻找一种泛型方式将对象序列化为单个值(然后将它们序列化,以后只填充该单个字段),而不必重复创建JsonSerializer / JsonDeserializer处理每个案例。 @JsonIdentityInfo注释非常接近但是稍微错过了标记,因为据我所知,它将始终在第一次出现时序列化完整的子对象。

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. 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.

以下是我想要做的一个例子。给出类:

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):

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

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

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).

对于显示的第二个JSON,@ JsonIgnoreProperties也可能非常接近,但这意味着我必须选择退出一切,而不是我想要的那个。

@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.

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

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

任何神奇的方式来做到这一点我没有进入杰克逊的内心深处?据我所知,JsonDeserializer / JsonSerializer没有关于父级的上下文信息,因此似乎没有一种简单的方法来创建@JsonValueFromProperty(id)类型Jackson Mix-in然后只看一下该注释定制的Serializer / Deserializer。

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.

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

Any ideas would be greatly appreciated. Thanks!

推荐答案

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

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.

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

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

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

返回的JSON将是:

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

这篇关于Jackson - 将属性序列化/反序列化为JSON值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆