Serialize circular object networks using writeObject / readObject
Asked Answered
S

1

2

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:

Please have a look at this related question.

Shears answered 6/10, 2011 at 22:51 Comment(1)
Hm, work suddenly. Probably come back later....Shears
R
0

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.

Rockabilly answered 11/10, 2011 at 10:2 Comment(1)
Marked as answer, although the actual problem is no longer visible. One problem was, that I called writeExternal() at the top-level, where I should have called writeObject. Internally, inside the serialization code of objects, writeExternal() and readExternal() need to be implemented but need to rely on e.g. writeObject() or readObject(). Regarding class registration: Yes, I'm aware, that class annotations are required to map byte streams to object instances. Thx!Shears

© 2022 - 2024 — McMap. All rights reserved.