是否可以序列化 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;
}
发布评论
评论(2)
看一下 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.
尝试 serialization 包rel="nofollow noreferrer">ascorelib。
因为处理 XML 就像任何其他本机类型一样。 放心。 XML 是处理从服务器上取出和放回的数据的首选方式。 当然,
ascorelib
为您提供了一个 JSON 类——所以您可能也想看看它。IIRC,
eval
是 ECMAScript 规范的一部分(您可以在 Javascript 中找到它)。 但在 AS3.0 中则不然。 它在某些以前的版本中在一定程度上存在,但不再受支持。Try the JSON based
serialization
package in ascorelib.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.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.