Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONEncoder now supports correct Vector json serialization #176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

JSONEncoder now supports correct Vector json serialization #176

wants to merge 1 commit into from

Conversation

ghost
Copy link

@ghost ghost commented Sep 13, 2011

I add support for serialazing Objects that contains Vector properties

@WORMSS
Copy link

WORMSS commented Jan 27, 2012

I am afraid to say you have not accounted for all types of vectors.
If you do some tests you will finds that not all vectors extend Vector.<*>.
Yes, I know, I know.. Its evil.

Use
else if ( value is Array || value is Vector.<*> || value is Vector.<Number> || value is Vector.<uint> || value is Vector.<int> )

@brianreavis
Copy link

@WORMSS That's not the best approach, considering a vector can contain any type of object. Chaining together a giant if statement to handle vectors of ints, uints, Numbers, etc is going to get out of control really fast... and you'll always be missing user-defined types. A better way would be to use describeType() in flash.utils:

if (describeType(value)[email protected]().indexOf('__AS3__.vec::Vector.') == 0) {
   // ...   
}

@IQAndreas
Copy link

@brianreavis Actually, Wormss is correct.

Number, uint, and int Vectors are registered as completely different classes and optimized in some fashion to be as fast as possible. Every other Vector class extends Vector.<*>.

The method proposed by Wormss would also be much faster than using describeType.

@WORMSS
Copy link

WORMSS commented Feb 13, 2012

@IQAndreas Thank you. I think the end of your message got cut off. (Atleast on my screen)

@brianreavis Not a giant IF, only 4 (5 including Array). There are only 4 types of Vectors, Number, uint, int and *(wildcard). Numbers are treated differently for performance considerations.

@IQAndreas
Copy link

@WORMSS I hit the submit button to early, but went back and edited the message. You were simply too quick to the keyboard. ;)

I noticed another problem with the proposed changes, you can't convert a Vector to an Array like this:
vector as Array
You simply get a null value (at least in my tests)

I modified the JSONEncoder class further to completely support Vectors now. I haven't actually tested it, but it should work in theory:
https://github.com/IQAndreas/as3corelib/commit/27b43ea2b51f8bf57e2337292c7837d48f98835a
Does anyone want to test it in a project and see if it works properly?

Also, what is the standard in case a "non-vector" is passed to the function vectorToString()? Should it return "" or "[]", or perhaps throw an error? It doesn't actually matter at the moment since the function is private, but still, it doesn't hurt to be proper. :-/

@WORMSS
Copy link

WORMSS commented Feb 13, 2012

I removed the limit of arrayToString having to accept a Array and had it accept *.
Since behide the scenes Array and Vector pretty much work in identical ways from a flash proxy point of view, I just used the same function for both.

Another solution would be to just make a vectorToArray, which is super easy to do, and then send the array via arrayToString.

No duplication of code for both types of list that way.

@brianreavis
Copy link

@IQAndreas @WORMSS There are more than four types of vectors. Try it:

var test:Vector.<FileReference>;
trace((test is Vector.<*>) ? "true" : "false");
trace((test instanceof Vector.<*>) ? "true" : "false");
// both yield "false"

@IQAndreas
Copy link

@brianreavis That's because the test variable is null.

You need to set it to a non-null value for the code to work properly:

var test:Vector.<FileReference> = null;

// all yield "false"
trace(test is Vector.<*>);
trace(test instanceof Vector.<*>);
trace(test is Vector.<FileReference>);

test = new Vector.<FileReference>();

// all yield "true"
trace(test is Vector.<*>);
trace(test instanceof Vector.<*>);
trace(test is Vector.<FileReference>);

@brianreavis
Copy link

Snap, you're right! Der, my bad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants