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

查看:36
本文介绍了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;
}

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

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

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

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

当我反序列化时,我只希望 Customer 仅使用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 然后看看那个注释自定义序列化器/反序列化器.

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 将是:

The returned JSON would then be:

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

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

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