本文介绍了如何在Hashmap中使用扩展的Jackson MapSerializer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试扩展com.fasterxml.jackson.databind.ser.std.MapSerializer包含我自己定义的一些要在serialize()方法中使用的函数.

I'm trying to extend the com.fasterxml.jackson.databind.ser.std.MapSerializer to include some of my own defined functions to be used in serialize() method.

public class MyMapSerializer extends MapSerializer{
    //have constructors
    //override method
    @Override
     public void serialize(Map<?,?> value, JsonGenerator jgen, SerializerProvider provider){}
}

然后我将注释添加到地图上,如下所示:

Then I add the annotation to my map as folllowing:

@JsonSerialize(using = org.entities.generator.MyMapSerializer.class)
private Map<ObjectA,ObjectB> myObject;

但是当我在MapSerializer中添加一个断点时,它甚至都没有进入断点.而且我尝试扩展JsonSerializer>,它就包含在内.有谁知道如何使用扩展的MapSerializer?感谢您的帮助

But when I add a breakpoint in the MapSerializer, it does not even go into it.And I tried to extend JsonSerializer>, it goes into it.Anyone knows how to use the extended MapSerializer?Thanks for the help

推荐答案

我不建议尝试对Jackson的默认序列化器进行子类化:这很脆弱.

I would not recommend trying to sub-class Jackson's default serializers: this is fragile.

但是,如果您确实想这样做,请看一下MapSerializer:我的猜测是,它的createContextual()方法最终构造了一个配置不同的实例.您可能需要覆盖处理此类实例的创建的其他方法之一;您只需要那些来构造instanceof MyMapSerializer而不是默认的MapSerializer.

But if you do want to do it, have a look at MapSerializer: my guess is that its createContextual() method ends up constructing a differently configured instance. You may need to override one of other methods which handled creationg of such instances; you just need those to construct instanceof MyMapSerializer instead of default MapSerializer.

这篇关于如何在Hashmap中使用扩展的Jackson MapSerializer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 12:39