杰克逊 - 通用TreeSet未序列化

时间:2012-06-10 08:34:40

标签: java generics serialization jackson

我正在尝试序列化具有以下字段的对象:

private TreeSet<TimeSlot<T>> counterTimeSlotSet = 
    new TreeSet<TimeSlot<T>>(
            new Comparator<TimeSlot<T>>(){
                @Override
                public int compare(TimeSlot<T> cb1, TimeSlot<T> cb2) {
                    return cb1.getPeriod().compareTo(cb2.getPeriod());
                }
            });

序列化代码如下:

    BaseSlidingWindow<BasicVelocityCounter> window1 = 
        new BaseSlidingWindow<BasicVelocityCounter>(
                BasicVelocityCounter.class, slidingWindowConfig);
    ...

    // jackson serializer test
    Version version = new Version(1, 0, 0, "SNAPSHOT");
    SimpleModule module = new SimpleModule("ZORRO", version);
    module = module.addSerializer(new DateTimeSerializer());
    // and so on...
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(module);

    mapper.writeValue(new File("C:\\tmp\\window1.json"), window1);

问题是类型window1的{​​{1}}成员未被序列化。 日志中不会出现任何异常。 我只是得到了不包含TreeSet<TimeSlot<T>>成员的json。

调试jackson代码并没有把我带到任何地方。 我想知道为了让TreeSet<TimeSlot<T>>序列化需要做些什么?

修改

我的TreeSet<TimeSlot<T>>类定义如下所示:

BaseSlidingWindow

1 个答案:

答案 0 :(得分:2)

我在BaseSlidingWindow中看不到任何公共字段或getter。因此,它没有可序列化的可观察属性。 如果这是问题,最简单的方法是在@JsonProperty之前添加counterTimeSlotSet以及要序列化的其他属性。

替代方法包括添加“getter”方法(如getCounterTimeSlotSet)或更改默认可见性设置以包含非公共字段。

相关问题