使用flexjson的java序列化对象列表
我在玩flexjson
和Google Cloud Endpoints
。我需要序列化的模型是:
public class SampleModel {
Long id;
DateTime createdAt;
String message;
OtherModel other;
}
我刚刚创建了DateTimeObjectFactory
以找到一种创建DateTime
对象的方法(缺少arg构造函数)。现在我还有关于{
实际上,我想序列化List
的SampleModel
。这是我的代码:
List<SampleModel> sampleList = new ArrayList<SampleModel>();
// ...
// adding some items to sampleList
// ...
String s = new JSONSerializer().deepSerialize(sampleList);
我想暂时删除它以避免一些未序列化的字段,但现在就这样
当我想反序列化s
时,我会这样做:
sampleList = new JSONDeserializer<List<SampleModel>>()
.use("other", OtherModel.class)
.use(DateTime.class, new DateTimeObjectFactory())
.deserialize(s);
我认为在这种反序列化中一切都很好,因为我可以在日志中看到反序列化的对象。但事实上,当我想从新的sampleList
中获取项目时,我会得到一个错误:
java.lang.ClassCastException: java.util.HashMap cannot be cast to com.test.games.testapi.model.SampleModel
如果我理解得很好,如果我没有将正确的类指向反序列化器,那么每个不平凡的对象都将被反序列化为Map
。所以这个错误意味着脚本不知道SampleModel
?这是什么意思
共 (0) 个答案