Serialize circular object networks using writeObject / readObject

怎甘沉沦 提交于 2020-01-04 07:35:10

问题


I'm about to implement

public function writeExternal( output:IDataOutput ):void {...}
public function readExternal( input:IDataInput ):void {...}

to make a set of object serializable.

Although I'm pretty sure, that I implemented all correctly, readExternal(..) at a certain point complains about too few data left to read:

RangeError: Error #2006: The supplied index is out of bounds.
    at flash.filesystem::FileStream/readObject()

I wonder, if I have a circular object network like

A = { left -> B, right -> B }
B = { father -> A }

and I call

writeObject( a )

will Flex serialize the hole object network and each object once and only one?

I did this:

  • Declared type annotations like this: [RemoteClass(alias="model.MyClass")]
  • Implemented parameter-free constructors
  • Declared all classes using implements IExternalizable

SharedObject's send() method is guaranteed to send each object once and only once.

Additional infos:

  • Reading and writing a ByteArray
  • Using Remote Object Components
  • IExternalizable interface

Please have a look at this related question.


回答1:


The meta [RemoteClass] is an instruction for and only for classes used in projects that Flex compiler identifies as using Flex framework. It is basically equivalent to calling registerClassAlias(MyClass, "model.MyClass"); For a number of reasons that extend the capacity of this topic, I'd suggest that you use registerClassAlias instead.

Regarding AMF built-in writer, the behavior is as follows: if the writer receives all objects it needs to write at once (as in a tree, you give it the root node), then it will write fully the object only once, the next time the reference to that object appears, it will use the reference. However, if you continually feed it objects, which may have already been referenced (such as through successive calls to writeExternal) it will treat every object as if it was new. At least theoretically, it should work the other way too. That is if the objects were serialized by reference, once they deserialized, the reference should be used. It would help to see more of your code, particularly the implementation of writeExternal and readExternal to give a better answer.



来源:https://stackoverflow.com/questions/7681374/serialize-circular-object-networks-using-writeobject-readobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!