1

在内部,我使用映射来快速查找值,例如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)...
4

1 回答 1

2

当然,只要我一问,我就会想出一个解决办法。

使用上面的序列化器,使用provider来序列化值而不是generator直接。这是代码:

/**
 * <p>
 * Serializes the values of some map into a list.
 * <p>
 *
 * @author John Jenkins
 */
public class MapValuesJsonSerializer extends JsonSerializer<Map<?, ?>> {
    /*
     * (non-Javadoc)
     * @see com.fasterxml.jackson.databind.JsonSerializer#serialize(java.lang.Object, com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.databind.SerializerProvider)
     */
    @Override
    public void serialize(
        final Map<?, ?> value,
        final JsonGenerator generator,
        final SerializerProvider provider)
        throws IOException, JsonProcessingException {

        provider.defaultSerializeValue(value.values(), generator);
    }
}
于 2013-11-01T17:34:36.630 回答