JSON.decode() / specific class

32 views
Skip to first unread message

Henrique Vilela

unread,
Feb 7, 2010, 6:08:25 PM2/7/10
to ActionScript 3 Core Library
Is it possible to define a specific class when using JSON.decode()?

Paul Feakins

unread,
Feb 10, 2010, 3:05:23 PM2/10/10
to as3co...@googlegroups.com
I haven't seen any replies to this... perhaps more information?

Do you mean you want a JS object to because an AS object of a specific class? Perhaps you can cast it i.e.

import YourClass;

var yourInstance:YourClass = YourClass( JSON.decode( stuff ) );

Is that what you mean?

saverio

unread,
Feb 28, 2010, 6:25:40 PM2/28/10
to ActionScript 3 Core Library
I've developed an extension of the JSONDecoder class, which
deserializes a JSON string into a specific class. There are a couple
of issues open, but until now it manages to:
- deserialize dates from strings (in UTC form "YYYY-MM-DD hh:mm:ss")
(and serialize them into UTC date strings)
- deserialize "typed" arrays, provided
. every object in the array is of a specific class (not a subclass)
. the array is annotated with the
[ArrayElementType("fully.qualified.ClassName")] metadata (also the
project must be compiled preserving the ArrayElementType metadata for
runtime)

The real issue in doing it automatically is that there is no means in
a class to resolve a getDefinitionByName call if the named class is
not imported, so you have to provide your own "getDefinitionByName",
AKA a resolver object, where you register your classes by name and the
pass to the decoder for it to resolve class definitions.

In the end, encoding is almost the same (a part of Date -> String
conversion) but decoding accounts to a method like
JSON.encode2(string, targetClass, resolver) (the result should be of
the class targetClass)

If anyone is interested, I could post the classes, or merge them in
some developer branch.

Best regards,

Saverio

shankar

unread,
Mar 11, 2010, 3:17:47 AM3/11/10
to ActionScript 3 Core Library

shankar

unread,
Mar 11, 2010, 3:01:30 AM3/11/10
to ActionScript 3 Core Library
Paul,

YourClass constructor will have to basically take values from the
Object (returned by JSON.decode(stuff)) and set it to its properties
right ?

Thanks,
Shankar K

shankar

unread,
Mar 11, 2010, 2:58:47 AM3/11/10
to ActionScript 3 Core Library
Saverio,

Appreciate if you can pass on your code that can decode JSON to a
custom class.. Btw, i have used Vectors in my AS class, will that be a
problem ?

Thanks,
Shankar

saverio

unread,
Mar 11, 2010, 2:46:50 PM3/11/10
to ActionScript 3 Core Library
I'd rather contribute to an official branch, so more people could give
feedback and contribute back.

The whole point is to not have a special constructor (actually classes
should have an empty constructor, like in AMF3 case).

I am not sure about the vector matter.

Answering to a previous question, the point is not having the classes
doing the parsing, but only (as much as possible) giving type
information. That's why it's necessary to declare the
[ArrayElementType] for array properties (and to keep that metadata for
runtime).

On the other side, it's not possible to use Vector as an actual
generic (in the Java sense), like

var c:Class = getClass();
var v:Vector.<c>;

this raises an error, because vector type must be a compile-time
constant.

If you have any idea on solving this problem, I'll correct it and post
the corrected version.

OTOH, I found a problem wrt the [Transient] properties, sometimes
they're being serialized... I have to look for the origin of this
problem before posting the code.

Best regards,

Saverio

Reply all
Reply to author
Forward
0 new messages