如何使对象网络序列化为文件,而不是共享对象?
目前,我将模型对象序列化为 SharedObject 实例:
try {
var mySo:SharedObject = SharedObject.getLocal("sig");
mySo.clear();
mySo.data.model = _model;
mySo.flush();
} catch ( e:Error ) {
Alert.show( 'Leider konnte kein Modell geladen werden.' );
}
同样,我使用 SharedObject 实例加载保存的模型。效果很好。
最终,我想将其序列化为文件 - 但失败了。操作方法如下:
var fp: File = File.applicationStorageDirectory;
fp = fp.resolvePath( PREFS_FILENAME );
var _prefsStream:FileStream;
_prefsStream = new FileStream();
_prefsStream.open( fp, FileMode.WRITE );
_prefsStream.endian = Endian.BIG_ENDIAN;
_model.writeExternal( _prefsStream );
_prefsStream.close();
补充读取操作突然中断并报告丢失字节。
事实上,我无法想象 FileStream / _model.writeExternal() 如何序列化,因为它需要以某种方式知道新的序列化操作即将开始。如果它不知道,就无法确定哪些对象实例需要序列化。
因此,我认为我的概念是完全错误的,或者我错过了如何初始化序列化操作。
请解释一下我缺少什么。
我很乐意从共享对象中读取原始 ByteArray 并将其写入文件。不幸的是,我没有找到从 SharedObject 检索某个属性的 ByteArray 的方法,在我的例子中是 mySo.data.model
。 我的问题与此问题松散相关: 为什么要删除( DictionaryInstance[ key ] );失败?
Currently, I do serialize my model object to the SharedObject instance:
try {
var mySo:SharedObject = SharedObject.getLocal("sig");
mySo.clear();
mySo.data.model = _model;
mySo.flush();
} catch ( e:Error ) {
Alert.show( 'Leider konnte kein Modell geladen werden.' );
}
Likewise, I load the saved model using the SharedObject instance. Works great.
Ultimately, I'd like to serialize it to a file - which fails. Here is how:
var fp: File = File.applicationStorageDirectory;
fp = fp.resolvePath( PREFS_FILENAME );
var _prefsStream:FileStream;
_prefsStream = new FileStream();
_prefsStream.open( fp, FileMode.WRITE );
_prefsStream.endian = Endian.BIG_ENDIAN;
_model.writeExternal( _prefsStream );
_prefsStream.close();
The complementing read operation suddenly breaks and reports missing bytes.
In fact, I can't image how FileStream / _model.writeExternal() is able to serialize, since it needs to somehow know, that a new serialization operation is about to start. If it doesn't know, it won't be able to determine, which object instances are left to serialize.
Thus, I image that my concept is completely wrong or I missed how to initialize the serialization operation.
Please explains, what I'm missing.
I'd be happy to read the raw ByteArray from the shared object and write it to a file. Unfortunately, I didn't find a method to retrieve from a SharedObject a ByteArray of a certain property, in my case mySo.data.model
.
My question is loosely related to this one: Why does delete( DictionaryInstance[ key ] ); fail?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我曾经必须对我构建的外部化框架执行单元测试,这就是我所做的:
另外,我认为您不必担心字节顺序,我认为默认值无论如何都是大端字节序。
因此,对于您的情况,我认为您需要类似:
而不是:
运行时应该自动在模型上调用
writeExternal
。I once had to perform unit tests on an externalization framework I built and this is how I did it:
Also, I don't think you should have to worry about byte order, I think the default is big endian anyways.
So, for your case, I think you need something like:
as opposed to:
The runtime should call
writeExternal
on your model automagically.