jackson map序列化,不调用key的自定义序列化器

时间:2021-06-20 18:01:49

I need to have functional which allow me to serialize Map<CustomType1, CustomType2>. I create custom Serializer inherited from JsonSerializer. I also create simple module and register it in my mapper;

我需要有功能,允许我序列化Map 。我创建了继承自JsonSerializer的自定义Serializer。我也创建了简单的模块并在我的mapper中注册它; ,customtype2>

SimpleModule myModule = new SimpleModule("myModule");
myModule.addKeySerializer(CustomType1.class, new CustomType1Serializer());
myModule.addSerializer(CustomType1.class, new CustomType1Serializer());
mapperInstance.registerModule(myModule);

And when I just serializing an instance of CustomType1 it works perfectly, but when I creating map and trying to serialize it, than jackson skip my serializer and using StdKeySerializer. How to fix that???

当我只是序列化CustomType1的一个实例时它工作得很好,但是当我创建map并尝试序列化它时,jackson跳过我的序列化器并使用StdKeySerializer。怎么解决???

Thanks for your attention.

感谢您的关注。

2 个解决方案

#1


5  

This problem seems related to Jackson's handling of generic objects. One way to get around the issue is by using a super type token to strictly define the map type. Illustrated:

这个问题似乎与杰克逊对通用对象的处理有关。解决该问题的一种方法是使用超类型令牌来严格定义地图类型。插图:

final ObjectMapper mapper = new ObjectMapper();

final SimpleModule module = new SimpleModule("myModule",
        Version.unknownVersion());
module.addKeySerializer(CustomType1.class, new CustomType1Serializer());
mapper.registerModule(module);

final MapType type = mapper.getTypeFactory().constructMapType(
        Map.class, CustomType1.class, CustomType2.class);
final Map<CustomType1, CustomType2> map = new HashMap<CustomType1, CustomType2>(4);
final ObjectWriter writer = mapper.writerWithType(type);
final String json = writer.writeValueAsString(map);

#2


0  

addSerializer and addKeySerializer are just two types of available serializers that deal only with simple, non-POJO types. To have custom serialization for more complex types such as maps and collections, you need to .setSerializerModifier on your module, with a BeanSerializerModifier that overrides the modifyMapSerializer method and returns your custom serializer

addSerializer和addKeySerializer只是两种类型的可用序列化程序,只处理简单的非POJO类型。要为更复杂的类型(如地图和集合)进行自定义序列化,需要在模块上使用.setSerializerModifier,BeanSerializerModifier将覆盖modifyMapSerializer方法并返回自定义序列化程序

#1


5  

This problem seems related to Jackson's handling of generic objects. One way to get around the issue is by using a super type token to strictly define the map type. Illustrated:

这个问题似乎与杰克逊对通用对象的处理有关。解决该问题的一种方法是使用超类型令牌来严格定义地图类型。插图:

final ObjectMapper mapper = new ObjectMapper();

final SimpleModule module = new SimpleModule("myModule",
        Version.unknownVersion());
module.addKeySerializer(CustomType1.class, new CustomType1Serializer());
mapper.registerModule(module);

final MapType type = mapper.getTypeFactory().constructMapType(
        Map.class, CustomType1.class, CustomType2.class);
final Map<CustomType1, CustomType2> map = new HashMap<CustomType1, CustomType2>(4);
final ObjectWriter writer = mapper.writerWithType(type);
final String json = writer.writeValueAsString(map);

#2


0  

addSerializer and addKeySerializer are just two types of available serializers that deal only with simple, non-POJO types. To have custom serialization for more complex types such as maps and collections, you need to .setSerializerModifier on your module, with a BeanSerializerModifier that overrides the modifyMapSerializer method and returns your custom serializer

addSerializer和addKeySerializer只是两种类型的可用序列化程序,只处理简单的非POJO类型。要为更复杂的类型(如地图和集合)进行自定义序列化,需要在模块上使用.setSerializerModifier,BeanSerializerModifier将覆盖modifyMapSerializer方法并返回自定义序列化程序