如何使对象网络序列化为文件,而不是共享对象?

发布于 2024-12-09 03:25:44 字数 1273 浏览 0 评论 0原文

目前,我将模型对象序列化为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂牵梦绕锁你心扉 2024-12-16 03:25:44

我曾经必须对我构建的外部化框架执行单元测试,这就是我所做的:

byteArray.writeObject(myObject);
byteArray.position = 0;
readValue = byteArray.readObject();

另外,我认为您不必担心字节顺序,我认为默认值无论如何都是大端字节序。

因此,对于您的情况,我认为您需要类似:

fileStream.writeObject(myObject)

而不是:

myObject.writeExternal(_prefsStream);

运行时应该自动在模型上调用 writeExternal

I once had to perform unit tests on an externalization framework I built and this is how I did it:

byteArray.writeObject(myObject);
byteArray.position = 0;
readValue = byteArray.readObject();

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:

fileStream.writeObject(myObject)

as opposed to:

myObject.writeExternal(_prefsStream);

The runtime should call writeExternal on your model automagically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文