gpt4 book ai didi

java - Jackson - 通用 TreeSet 未序列化

转载 作者:行者123 更新时间:2023-12-02 07:45:18 25 4
gpt4 key购买 nike

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

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类型 TreeSet<TimeSlot<T>> 的成员没有序列化。日志中没有出现任何异常。我刚刚得到不包含 TreeSet<TimeSlot<T>> 的 json成员(member)。

调试 jackson 代码并没有引导我到任何地方。 我想知道需要做什么才能获得TreeSet<TimeSlot<T>>连载了吗?

编辑

我的BaseSlidingWindow类定义如下:

public class BaseSlidingWindow<T extends ICountable<T>> 
implements ISlidingWindow<T>{

boolean dirty = false;

private DateTime createdOn;
private DateTime updatedOn;
private DateTime windowLifeStart;
private DateTime windowLifeEnd;

private final SlidingWindowConfig slidingWindowConfig;

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());

}
});

private final Class firstSeenDataType;

// constructor, accessors and IFS implementations
// ...
}

最佳答案

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

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

关于java - Jackson - 通用 TreeSet 未序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10967347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com