在内部,我使用映射来快速查找值,例如private Map<String, CustomObject> lookupTable
,其中的CustomObject
某些id
字段被用作lookupTable
. 序列化地图的键是愚蠢的,所以我想注释内部变量以仅序列化值。这显然行不通:
@JsonSerialize(as = List.class)
private final Map<String, CustomObject> lookupTable;
为什么自定义序列化程序不起作用
我创建了一个自定义序列化程序,例如:
public static class MapValuesJsonSerializer extends JsonSerializer<Map<?, ?>> {
@Override
public void serialize(
Map<?, ?> value,
JsonGenerator generator,
SerializerProvider provider)
throws IOException, JsonProcessingException {
generator.writeObject(value.values());
}
}
然后,我相应地注释了该字段:
@JsonSerialize(using = MapValuesJsonSerializer.class)
public Map<String, CustomObject> lookupTable;
这适用于一些 Java 测试代码,但在使用 MongoDB 的驱动程序和 MongoJack 时会中断。我得到了一些无用的异常,我认为这是因为它实际上并没有序列化返回的对象,而是只是试图将该原始对象传递给 BSON 序列化程序:
Caused by: java.lang.IllegalArgumentException: can't serialize class CustomObject
at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:270)
at org.bson.BasicBSONEncoder.putIterable(BasicBSONEncoder.java:295)
at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:234)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:174)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:120)
at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:27)
at com.mongodb.OutMessage.putObject(OutMessage.java:289)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:239)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:204)
at com.mongodb.DBCollection.insert(DBCollection.java:148)
at com.mongodb.DBCollection.insert(DBCollection.java:91)
at org.mongojack.JacksonDBCollection.insert(JacksonDBCollection.java:255)
// My code (not the serializer)...