序列化 Flex 对象以保存/恢复应用程序状态

发布于 2024-07-14 08:20:53 字数 758 浏览 7 评论 0 原文

是否可以序列化 Flex 中的对象层次结构,将二进制数据发送到 URL 以在服务器上存储/检索,并反序列化数据以恢复对象的原始状态?

我知道可以将对象转换为 XML 格式(尚未尝试过),但我希望避免手动解析 XML 并重建对象。 如果有功能可以将对象序列化/反序列化为简单的二进制格式,那就太好了(我过去在 Java 中做了类似的事情,尽管不像我希望的那么容易)。 Perl 中的“eval”函数与我正在寻找的函数类似,当然没有保存代码。

在伪代码中,这就是我想要做的:

private var contentToSave:HBox = new HBox();

private function saveState(event:Event):void {
    var toSave:HBox = this.contentToSave;
    var data:? = /* serialize 'toSave' ActionScript classes to binary data*/;
    sendDataToServer(data, filename);
}
private function restoreState(filename):void {
    var data:? = getDataFromServer(filename);
    var savedData:HBox = /* deserialize binary 'data' to ActionScript classes */;
    this.contentToSave = savedData;
}

Is it possible to serialize a hierarchy of objects in Flex, send the binary data to a URL for storage/retrieval on/from a server, and deserialize the data to restore the objects' original state?

I know it's possible to convert the objects into an XML format (haven't tried it yet), but I'm hoping to avoid parsing XML and rebuilding the objects manually. It would be nice to have functionality which can serialize/deserialize objects to a simple binary format (I did something similar in the past in Java, though not quite as easily as I would have liked). The 'eval' function in Perl is similar to what I'm looking for, sans saving code, of course.

In pseudo-code, here's what I would like to do:

private var contentToSave:HBox = new HBox();

private function saveState(event:Event):void {
    var toSave:HBox = this.contentToSave;
    var data:? = /* serialize 'toSave' ActionScript classes to binary data*/;
    sendDataToServer(data, filename);
}
private function restoreState(filename):void {
    var data:? = getDataFromServer(filename);
    var savedData:HBox = /* deserialize binary 'data' to ActionScript classes */;
    this.contentToSave = savedData;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

翻身的咸鱼 2024-07-21 08:20:53

看一下 ByteArray.writeObject( )。 它将传递的对象以 AMF 格式保存到字节数组中。 我没有太多使用这个函数,我不太清楚它可以序列化什么样的对象,但绝对不是全部。

Take a look at ByteArray.writeObject(). which saves the passed object in AMF format into the byte array. I have not used this function too much, I don't exactly know what kind of objects it can serialize, but definitely not all.

一江春梦 2024-07-21 08:20:53

尝试 serialization 包rel="nofollow noreferrer">ascorelib

[...]但我希望避免手动解析 XML 并重建对象

因为处理 XML 就像任何其他本机类型一样。 放心。 XML 是处理从服务器上取出和放回的数据的首选方式。 当然,ascorelib 为您提供了一个 JSON 类——所以您可能也想看看它。

Perl 中的“eval”函数与我正在寻找的函数类似,当然没有保存代码。

IIRC,eval 是 ECMAScript 规范的一部分(您可以在 Javascript 中找到它)。 但在 AS3.0 中则不然。 它在某些以前的版本中在一定程度上存在,但不再受支持。

Try the JSON based serialization package in ascorelib.

[...]but I'm hoping to avoid parsing XML and rebuilding the objects manually

AS handles XML just like any other native type. Rest assured. XML is the preferred way of dealing with data you will be pulling off and putting back on a server. Of course, the ascorelib gives you a JSON class -- so you may want to look at that as well.

The 'eval' function in Perl is similar to what I'm looking for, sans saving code, of course.

IIRC, eval is part of the ECMAScript specification (and you will find it in Javascript). But not in AS3.0. It was there to a certain extent in some previous version(s?) but is no longer supported.

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