是否有任何 Jackson json 策略(使用注释或其他方式),将在字段反序列化之前和之后执行一些逻辑?

发布于 2024-10-28 12:24:21 字数 960 浏览 8 评论 0原文

在 POJO 字段上完成每次反序列化后,我需要执行一些代码。 有什么办法可以通过一些杰克逊注释(或其他)策略来做到这一点?

  1. 一种方法是为每个字段类型创建一个自定义反序列化器,该反序列化器将实现 PostLogicDeserializerInterface 或扩展某些字段类型PostLogicDeserializerAbstract。但这会产生大量难以维护的混乱代码(而不是仅仅使用@JsonProperty)。所以我认为这不是一个好主意。

  2. 我发现您可以在类级别使用@JsonDeserialize,但仅适用于值类。从文档中:

注释值类时,配置用于值类的实例,但可以被更具体的注释(附加到方法或字段的注释)覆盖。

所以我认为这也行不通。

  1. 在 POJOs setter 方法中使用一些自定义逻辑是一种不好的做法!另一方面,我认为杰克逊无论如何都使用反射来设置字段......这也不是一个好的策略。

我的目标是确定反序列化器设置的字段的百分比。我需要一个计数器,该计数器会在每次调用的反序列化(填充字段)时增加。一旦整个类(POJO)反序列化结束,我需要使用反射执行一些逻辑。

我现在实现的方式是,

  • 一旦我通过 Jackson 映射器反序列化了 POJO,我就会使用反射
  • 检查每个字段是否已设置,fi 如果为 null 或 -1原始数字(以前的初始值)。 (这种方法的一个缺点是,如果设置了布尔值,则无法检查它)
  • 使用反射进行其他类型的检查(我们称之为逻辑 X)
  • 执行取决于设置字段和逻辑 X 的百分比的逻辑。

我会更喜欢一些 Jackson 策略,因为我不需要通过反射检查 POJO。它宁愿就地完成(在 POJO 反序列化时)。

干杯,
暴君

I need to execute some code after each deserialization is done on a POJO's fields. Is there any way I can do this through some jackson annotation (or other) strategy?

  1. One way to go with this is create a custom deserializer for each field type that will implement PostLogicDeserializerInterface or extend some PostLogicDeserializerAbstract. But this will make loads of cluttering code that will be hard to maintain (instead of just using @JsonProperty). So I think this is not a good idea.

  2. I saw that you can use @JsonDeserialize on class level but only for value classes. From the documentation:

When annotating value classes, configuration is used for instances of the value class but can be overridden by more specific annotations (ones that attach to methods or fields).

So I think this won't work, either.

  1. Using some custom logic in the POJOs setter methods is a bad practice! And on the other side, I think jackson uses reflection to set the fields anyway... Not a good strategy, neither.

My goal is to determine the percentage of fields that were set by the deserializer. I would need to have a counter that will increase on each invoked deserialization (populated field). And once the whole class(POJO) deserialization is over, I would need to execute some logic using reflection.

The way I have that implemented now, is

  • once I have the POJO deserialized by the jackson mapper, I go through each field using reflection
  • check if it has been set, f.i. if it is null or -1 for primitive numbers (previously initial values). (one conn of this approach is that you can't check a boolean if it was set)
  • use reflection for some other kind of checking (lets call it logic X)
  • execute logic that depends on percentage of set fields and logic X.

I would prefer some jackson strategy, since I wouldn't need to check the POJO with reflection. It would rather be done in place (at the time the POJO gets deserialized).

Cheers,
Despot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别把无礼当个性 2024-11-04 12:24:21

此时没有任何特定功能可以进行后处理或预处理;这已经接近数据绑定应该做的事情的界限了。
如果我必须对特定字段执行此操作,我可能只会将其添加到 setter 中,因为这很简单并且有效;但在所有相关设置器中需要相同的逻辑。

@JsonDeserialize 还可以用于单个属性(字段、设置器),因此您可以创建自定义反序列化器:并且由于您想要后处理,您可以只找到“真正的”反序列化器(最好通过制作 JsonDeserializer 实现 ContextualDeserializer 或 ResolvableDeserializer - 这在这里可能并不重要,但对于一般情况,在这里完成是为了避免循环依赖问题),委托给它并修改值。这假设您更关心的是值而不是字段。

最后,还有修改 BeanDeserializer 实例的方法(通过注册 BeanDeserializerModifier)——您可以对相关组件进行子类化(我认为 SettableBeanProperty...)以挂钩其他组件处理,甚至替换要使用的解串器,保留对原始“默认”解串器的引用。

但最后,你的情况听起来可能最好由其他东西来处理:例如 Bean Validation API (jsr-303) 似乎很适合后处理逻辑。由于它在某种程度上与数据绑定正交,因此它可能是一个更好的替代方案,因为它独立于数据绑定(杰克逊),可重用,所有这些好东西。

There isn't any specific feature to do post- or pre-processing at this point; and this sort of gets close to boundaries of what data binding should do.
If I had to do this for specific fields, I would probably just add it in setter, since that is simple thing to do and works; but requires same logic in all relevant setters.

@JsonDeserialize can also be used for individual properties (field, setter), so you could create a custom deserializer: and since you want post-processing, you could just locate "real" deserializer (ideally by making JsonDeserializer implement either ContextualDeserializer or ResolvableDeserializer -- this may not matter here, but for general case it's done here to avoid problems with cyclic dependencies), delegate to it, and modify value. This assumes it is value you care about more than field.

Finally, there are also ways to modify BeanDeserializer instances (by registering BeanDeserializerModifier) -- you could sub-class relevant components (SettableBeanProperty I think...) to hook in additional handling, or even replace deserializer to use, keeping a reference to the original "default" deserializer.

But in the end, your case sounds like something that may be best handled by something else: for example Bean Validation API (jsr-303) seems like a potentially good match for post-processing logic. Since it is somewhat orthogonal to data binding, it could be a superior alternative since it would be independent of data binding (jackson), reusable, all the good stuff.

生生漫 2024-11-04 12:24:21

另一种方法是使用 @JsonCreator 标记将接收数据的构造函数。在构造函数内,您可以完全控制如何将它们分配给字段。

Another way is to use @JsonCreator to mark a constructor which will receive the data. Inside the constructor, you have full control over how to assign them to fields.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文