[FlexJS] Issue with JSON.stringify and Bindable VO objects

classic Classic list List threaded Threaded
29 messages Options
12
Reply | Threaded
Open this post in threaded view
|

[FlexJS] Issue with JSON.stringify and Bindable VO objects

piotrz
Hi All,

It look like we have issue in FlexJS with quite common scenario where JSON.stringify cannot create json from simple VO object which has been marked by [Bindable] property.

Description:
I have following class [1], which after compilation has additional properties:



In the results JSON.stringify is failing to convert object to a proper JSON [2], on swf sight there is no problem with such object.

I think creating JSON from object which is marked as [Bindable] will be quite common cases and need to be resolved.

Two propositions.

Proposition 1:

1) Create our own JSON class with stringify function
2) Create on the compiler sight array called "excluded" which will contains all additional properties which has been created during compilation
3) In our JSON.stringify use JS version of stringify with second parameter in following manner [3].

Proposition 2:
1) Same as above
2) Use "accessors" property from FLEXJS_REFLECTION_INFO [4] to determine all properties from VO
3) Inside our "stringify" function create simple object based on information from "accessors" and pass that object to JSON.stringify({title: "my title", page: 5}).

[1] https://paste.apache.org/fuSA
[2] https://paste.apache.org/CyqY
[3] https://paste.apache.org/9GlW
[4] https://paste.apache.org/gpbR

I would be appreciated for feedback and other ideas.

Thanks,
Piotr
Reply | Threaded
Open this post in threaded view
|

Re: [FlexJS] Issue with JSON.stringify and Bindable VO objects

pan Li


Piotr

I am afraid the javascript can only act as a tool to implementation the
JSON class, it can't replace Flex JSON in whole, Since they are different.
If you use different thing as replacement it isn't hard to find cases
didn't cover
and the FlexJS's JSON in js has more issues than Bindable, I talked on it
only because it is the issue blocking me, also swf mode is broken as well.
Longer story:

the sdk document lists other difference: because the page is comparing Flex
SDK's JSON with broswer's javascript's JSON, there may be difference to our
case.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/JSON.html

A I had noticed JSON didn't work long before, but those days we didn't work
on SDK code, so I didn't dig deeper.
These were important issues which break my code:
A.1 JSON isn't supposed to output private properties, but current
implementation (using JS's as replacement) does
A.2 default date format of Flex SDK's JSON is different to FlexJS SDK's
A.3 when property of integer is not set, actionscript output it as 0 but
Javascript ignores it.
I think these is because JS's JSON doesn't have knowledge of ActionScript's
class system.

B These are issue which didn't affect my code but since you use this
approach as SDK implementation it is very critical:
B.1 If Actionscript class has other class as it property, it is supposed to
process it correctly, but in FlexJS's JSON (using JS's), it goes no where.
if the property's value in created inside the main domain, then FlexJS JS
mode doesn't process it but FlexJS swf mode does, otherwise FlexJS js mode
processes it but swf mode doesn't

C in today's testing mores unexpected behaviour is noticed:
C.1 the javascript JSON ignores any assignment on variables' declaration
(like  public var donothideme:uint = 88)

D There are some further issues if you use javascript code as the FlexJS
SDK implementation
D.1 it can't handle actionscripts's property name with get/set correctly,
an extra underscore is added.
D.2 the code will ignore an user declared property named _target, it is not
rare to have a property named "_target" in VO objects

these are domain classes which can reproduce some of issues listed above,
you need to run in in Flex, FlexJS swf mode and FlexJS js mode to compare:



package
{

        public class Book
        {
                public function Book()
                {
                }

                public var title: String;
                public var page : uint;
                public var date : Date;
            public var authors : Array;
            public var topic : Topic = new Topic();


            public var donothide: uint = 99;
            private var internalDate: Date = new Date();



        }
}

package
{

        public class Topic
        {
                public function Topic()
                {
                }

                public var name: String = "Politics";


        }
}




Code to create testing instance:

                        var book : Book = new Book();
                        book.title = "Trump";
                        book.date = new Date();
                        book.authors = new Array();
                        book.authors.push("Kitten");
                        book.authors.push("Puppy");





My Apache Flex community contribution is working on the open source
Moonshine-IDE.com for FlexJS.
Reply | Threaded
Open this post in threaded view
|

Re: [FlexJS] Issue with JSON.stringify and Bindable VO objects

Alex Harui
Hi Pan,

What do you mean by "Flex JSON"?

AFAICT, the JSON class in Flash/AIR and the JSON class in JavaScript
intend to have equivalent functionality.  Both seem to support a toJSON
method which might be the right answer.

Piotr, have you tested out toJSON methods?  If you manually add those to
your example and they work, we can look into have the compiler auto
generate those methods.

HTH,
-Alex

On 4/18/17, 6:07 PM, "Pan Li" <[hidden email]> wrote:

>
>
>Piotr
>
>I am afraid the javascript can only act as a tool to implementation the
>JSON class, it can't replace Flex JSON in whole, Since they are different.
>If you use different thing as replacement it isn't hard to find cases
>didn't cover
>and the FlexJS's JSON in js has more issues than Bindable, I talked on it
>only because it is the issue blocking me, also swf mode is broken as well.
>Longer story:
>
>the sdk document lists other difference: because the page is comparing
>Flex
>SDK's JSON with broswer's javascript's JSON, there may be difference to
>our
>case.
>https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fhelp.adobe
>.com%2Fen_US%2FFlashPlatform%2Freference%2Factionscript%2F3%2FJSON.html&da
>ta=02%7C01%7C%7Ca2c9d03adf034738262e08d486c08587%7Cfa7b1b5a7b34438794aed2c
>178decee1%7C0%7C0%7C636281608813640368&sdata=5jiKvvY%2BJQlw%2FWVbyw4RrMkre
>ULC8Ghln0vGhS2KwHU%3D&reserved=0
>
>A I had noticed JSON didn't work long before, but those days we didn't
>work
>on SDK code, so I didn't dig deeper.
>These were important issues which break my code:
>A.1 JSON isn't supposed to output private properties, but current
>implementation (using JS's as replacement) does
>A.2 default date format of Flex SDK's JSON is different to FlexJS SDK's
>A.3 when property of integer is not set, actionscript output it as 0 but
>Javascript ignores it.
>I think these is because JS's JSON doesn't have knowledge of
>ActionScript's
>class system.
>
>B These are issue which didn't affect my code but since you use this
>approach as SDK implementation it is very critical:
>B.1 If Actionscript class has other class as it property, it is supposed
>to
>process it correctly, but in FlexJS's JSON (using JS's), it goes no where.
>if the property's value in created inside the main domain, then FlexJS JS
>mode doesn't process it but FlexJS swf mode does, otherwise FlexJS js mode
>processes it but swf mode doesn't
>
>C in today's testing mores unexpected behaviour is noticed:
>C.1 the javascript JSON ignores any assignment on variables' declaration
>(like  public var donothideme:uint = 88)
>
>D There are some further issues if you use javascript code as the FlexJS
>SDK implementation
>D.1 it can't handle actionscripts's property name with get/set correctly,
>an extra underscore is added.
>D.2 the code will ignore an user declared property named _target, it is
>not
>rare to have a property named "_target" in VO objects
>
>these are domain classes which can reproduce some of issues listed above,
>you need to run in in Flex, FlexJS swf mode and FlexJS js mode to compare:
>
>
>
>package
>{
>
> public class Book
> {
> public function Book()
> {
> }
>
> public var title: String;
> public var page : uint;
> public var date : Date;
>    public var authors : Array;
>    public var topic : Topic = new Topic();
>
>
>    public var donothide: uint = 99;
>    private var internalDate: Date = new Date();
>
>
>
> }
>}
>
>package
>{
>
> public class Topic
> {
> public function Topic()
> {
> }
>
> public var name: String = "Politics";
>
>
> }
>}
>
>
>
>
>Code to create testing instance:
>
> var book : Book = new Book();
> book.title = "Trump";
> book.date = new Date();
> book.authors = new Array();
> book.authors.push("Kitten");
> book.authors.push("Puppy");
>
>
>
>
>
>My Apache Flex community contribution is working on the open source
>Moonshine-IDE.com for FlexJS.

Reply | Threaded
Open this post in threaded view
|

Re: [FlexJS] Issue with JSON.stringify and Bindable VO objects

piotrz
Hi Alex,

I just tried toJSON method on object in JS and it failed with error that those method do not exist. Did you mean something like this ?

 var book:Book = new Book();
book.title = "Trump";

var toJSON:Object = book["toJSON"]();
var jsonString:String = JSON.stringify(toJSON);

If such function do not exist we can still do something with this ?

Thanks,
Piotr

Reply | Threaded
Open this post in threaded view
|

Re: [FlexJS] Issue with JSON.stringify and Bindable VO objects

Alex Harui


On 4/18/17, 10:34 PM, "piotrz" <[hidden email]> wrote:

>Hi Alex,
>
>I just tried toJSON method on object in JS and it failed with error that
>those method do not exist. Did you mean something like this ?
>
> var book:Book = new Book();
>book.title = "Trump";
>
>var toJSON:Object = book["toJSON"]();
>var jsonString:String = JSON.stringify(toJSON);
>
>If such function do not exist we can still do something with this ?

Yes, we can probably make this work.  Does this work for SWF as well?

Thanks,
-Alex

Reply | Threaded
Open this post in threaded view
|

Re: [FlexJS] Issue with JSON.stringify and Bindable VO objects

piotrz
Alex,

Just tested on SWF sight and got this one:


In JS stack trace is:


If we can still to do something with this I will raise a jir - let me know.

Piotr

Reply | Threaded
Open this post in threaded view
|

Re: [FlexJS] Issue with JSON.stringify and Bindable VO objects

Harbs
That seems to make sense to me.

toJSON is something which needs to be defined. If it’s not defined, it’s not going to work...

Am I missing something?

> On Apr 19, 2017, at 1:57 AM, piotrz <[hidden email]> wrote:
>
> Alex,
>
> Just tested on SWF sight and got this one:
> <http://apache-flex-development.2333347.n4.nabble.com/file/n61213/to_json_swf.png>
>
> In JS stack trace is:
> <http://apache-flex-development.2333347.n4.nabble.com/file/n61213/to_json_js.png>
>
> If we can still to do something with this I will raise a jir - let me know.
>
> Piotr
>
>
>
>
>
> -----
> Apache Flex PMC
> [hidden email]
> --
> View this message in context: http://apache-flex-development.2333347.n4.nabble.com/FlexJS-Issue-with-JSON-stringify-and-Bindable-VO-objects-tp61195p61213.html
> Sent from the Apache Flex Development mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: [FlexJS] Issue with JSON.stringify and Bindable VO objects

Alex Harui

On 4/18/17, 11:20 PM, "Harbs" <[hidden email]> wrote:

>That seems to make sense to me.
>
>toJSON is something which needs to be defined. If it’s not defined, it’s
>not going to work...

That's my understanding as well.  The ValueObject class will need to
define a toJSON method.  I don't think there is a default.

-Alex

Reply | Threaded
Open this post in threaded view
|

Re: [FlexJS] Issue with JSON.stringify and Bindable VO objects

piotrz
Alex, Harbs,

I understand, but I thought that you will handle those method on the compiler sight - Am I miss something ?

Piotr
Reply | Threaded
Open this post in threaded view
|

Re: [FlexJS] Issue with JSON.stringify and Bindable VO objects

Alex Harui
Hi Piotr,

I am asking you to manually prototype and test that using toJSON will work
before I spend time trying to get the compiler to generate such code.  I
believe it shouldn't take you that long to manually test it.  It will take
quite a while to change the compiler.

Thanks
-Alex



On 4/18/17, 11:23 PM, "piotrz" <[hidden email]> wrote:

>Alex, Harbs,
>
>I understand, but I thought that you will handle those method on the
>compiler sight - Am I miss something ?
>
>Piotr
>
>
>
>-----
>Apache Flex PMC
>[hidden email]
>--
>View this message in context:
>https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fapache-fle
>x-development.2333347.n4.nabble.com%2FFlexJS-Issue-with-JSON-stringify-and
>-Bindable-VO-objects-tp61195p61216.html&data=02%7C01%7C%7Cbdc6026905e6430e
>257708d486ee13e7%7Cfa7b1b5a7b34438794aed2c178decee1%7C0%7C0%7C636281804490
>300757&sdata=6Oz7wQCVVFgJDUk%2F4FlAMTqXN0B%2FdysQo2qNV37YFlk%3D&reserved=0
>Sent from the Apache Flex Development mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: [FlexJS] Issue with JSON.stringify and Bindable VO objects

Greg Dove
In reply to this post by piotrz
I'd be happy to look at compiler side support for this at some point during
the next month, it is something I have hand-coded many times and I think
to/from JSON style support would be helpful for VO, especially bindables.
It would need to add an interface I think so you could cast the VO in
serialize/deserialize parts of service calls etc.

e.g. creating VO from json:

var myVO:VOClass = new VOClass()
(myVO as IJSONable).fromJSON(jsonData)

I know not everyone likes metadata, but this seems like a VO-specific
metadata on the class, that the compiler would pick up on. It could even
ignore [Transient] members if there are any.

This keeps it away from being dependent on the reflection data and makes it
specific to the classes that need it




On Wed, Apr 19, 2017 at 6:23 PM, piotrz <[hidden email]> wrote:

> Alex, Harbs,
>
> I understand, but I thought that you will handle those method on the
> compiler sight - Am I miss something ?
>
> Piotr
>
>
>
> -----
> Apache Flex PMC
> [hidden email]
> --
> View this message in context: http://apache-flex-
> development.2333347.n4.nabble.com/FlexJS-Issue-with-JSON-
> stringify-and-Bindable-VO-objects-tp61195p61216.html
> Sent from the Apache Flex Development mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|

Re: [FlexJS] Issue with JSON.stringify and Bindable VO objects

Greg Dove
maybe fromJson is not the best example :)


On Wed, Apr 19, 2017 at 6:42 PM, Greg Dove <[hidden email]> wrote:

> I'd be happy to look at compiler side support for this at some point
> during the next month, it is something I have hand-coded many times and I
> think to/from JSON style support would be helpful for VO, especially
> bindables. It would need to add an interface I think so you could cast the
> VO in serialize/deserialize parts of service calls etc.
>
> e.g. creating VO from json:
>
> var myVO:VOClass = new VOClass()
> (myVO as IJSONable).fromJSON(jsonData)
>
> I know not everyone likes metadata, but this seems like a VO-specific
> metadata on the class, that the compiler would pick up on. It could even
> ignore [Transient] members if there are any.
>
> This keeps it away from being dependent on the reflection data and makes
> it specific to the classes that need it
>
>
>
>
> On Wed, Apr 19, 2017 at 6:23 PM, piotrz <[hidden email]> wrote:
>
>> Alex, Harbs,
>>
>> I understand, but I thought that you will handle those method on the
>> compiler sight - Am I miss something ?
>>
>> Piotr
>>
>>
>>
>> -----
>> Apache Flex PMC
>> [hidden email]
>> --
>> View this message in context: http://apache-flex-development
>> .2333347.n4.nabble.com/FlexJS-Issue-with-JSON-stringify-and-
>> Bindable-VO-objects-tp61195p61216.html
>> Sent from the Apache Flex Development mailing list archive at Nabble.com.
>>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: [FlexJS] Issue with JSON.stringify and Bindable VO objects

Alex Harui


On 4/18/17, 11:45 PM, "Greg Dove" <[hidden email]> wrote:

>maybe fromJson is not the best example :)

I'm not sure I understand what you want to do.  Are you trying to support
types in the JSON reviver?

-Alex

>
>
>On Wed, Apr 19, 2017 at 6:42 PM, Greg Dove <[hidden email]> wrote:
>
>> I'd be happy to look at compiler side support for this at some point
>> during the next month, it is something I have hand-coded many times and
>>I
>> think to/from JSON style support would be helpful for VO, especially
>> bindables. It would need to add an interface I think so you could cast
>>the
>> VO in serialize/deserialize parts of service calls etc.
>>
>> e.g. creating VO from json:
>>
>> var myVO:VOClass = new VOClass()
>> (myVO as IJSONable).fromJSON(jsonData)
>>
>> I know not everyone likes metadata, but this seems like a VO-specific
>> metadata on the class, that the compiler would pick up on. It could even
>> ignore [Transient] members if there are any.
>>
>> This keeps it away from being dependent on the reflection data and makes
>> it specific to the classes that need it
>>
>>
>>
>>
>> On Wed, Apr 19, 2017 at 6:23 PM, piotrz <[hidden email]>
>>wrote:
>>
>>> Alex, Harbs,
>>>
>>> I understand, but I thought that you will handle those method on the
>>> compiler sight - Am I miss something ?
>>>
>>> Piotr
>>>
>>>
>>>
>>> -----
>>> Apache Flex PMC
>>> [hidden email]
>>> --
>>> View this message in context: http://apache-flex-development
>>> .2333347.n4.nabble.com/FlexJS-Issue-with-JSON-stringify-and-
>>> Bindable-VO-objects-tp61195p61216.html
>>> Sent from the Apache Flex Development mailing list archive at
>>>Nabble.com.
>>>
>>
>>

Reply | Threaded
Open this post in threaded view
|

Re: [FlexJS] Issue with JSON.stringify and Bindable VO objects

piotrz
In reply to this post by Greg Dove
Greg,

The deal is that I don't want to any interface. In pure Flex we don't need interface and stringify is working. It would be awesome if you could take a look into that.

Alex,

So I don't understand what do you want from me. Do you want me to manually create toJSON inside object and implement logic which return pure Object ?

Piotr
Reply | Threaded
Open this post in threaded view
|

Re: [FlexJS] Issue with JSON.stringify and Bindable VO objects

Alex Harui


On 4/18/17, 11:40 PM, "piotrz" <[hidden email]> wrote:

>Greg,
>
>The deal is that I don't want to any interface. In pure Flex we don't need
>interface and stringify is working. It would be awesome if you could take
>a
>look into that.
>
>Alex,
>
>So I don't understand what do you want from me. Do you want me to manually
>create toJSON inside object and implement logic which return pure Object ?

Yes.  Imagine you are the compiler.  What code can you add to your value
objects that will make it work?  Once we know what code is needed to make
it work, we can try to get the compiler to generate that code.

I'm stopping work for tonight so hopefully you can make progress.

Thanks,
-Alex

Reply | Threaded
Open this post in threaded view
|

Re: [FlexJS] Issue with JSON.stringify and Bindable VO objects

piotrz
Ohh! I got you :) I will prepare something and post here.

Thank you!
Reply | Threaded
Open this post in threaded view
|

Re: [FlexJS] Issue with JSON.stringify and Bindable VO objects

Greg Dove
In reply to this post by Alex Harui
Yes I was thinking more in terms of the times I did this type of thing to
support nested typed objects from raw json, more than simple VOs. Not so
much in terms of using reviver, but going through from json -> generic
object -> typed object with most of the support for the last part being in
the abstract base class, and extra field mappings to complex field types
defined in subclasses for specific fields. It was maintainable at the
individual class level, but still quite complicated. Poor man's amf.

Probably not relevant here for simple VOs.




On Wed, Apr 19, 2017 at 6:47 PM, Alex Harui <[hidden email]> wrote:

>
>
> On 4/18/17, 11:45 PM, "Greg Dove" <[hidden email]> wrote:
>
> >maybe fromJson is not the best example :)
>
> I'm not sure I understand what you want to do.  Are you trying to support
> types in the JSON reviver?
>
> -Alex
>
> >
> >
> >On Wed, Apr 19, 2017 at 6:42 PM, Greg Dove <[hidden email]> wrote:
> >
> >> I'd be happy to look at compiler side support for this at some point
> >> during the next month, it is something I have hand-coded many times and
> >>I
> >> think to/from JSON style support would be helpful for VO, especially
> >> bindables. It would need to add an interface I think so you could cast
> >>the
> >> VO in serialize/deserialize parts of service calls etc.
> >>
> >> e.g. creating VO from json:
> >>
> >> var myVO:VOClass = new VOClass()
> >> (myVO as IJSONable).fromJSON(jsonData)
> >>
> >> I know not everyone likes metadata, but this seems like a VO-specific
> >> metadata on the class, that the compiler would pick up on. It could even
> >> ignore [Transient] members if there are any.
> >>
> >> This keeps it away from being dependent on the reflection data and makes
> >> it specific to the classes that need it
> >>
> >>
> >>
> >>
> >> On Wed, Apr 19, 2017 at 6:23 PM, piotrz <[hidden email]>
> >>wrote:
> >>
> >>> Alex, Harbs,
> >>>
> >>> I understand, but I thought that you will handle those method on the
> >>> compiler sight - Am I miss something ?
> >>>
> >>> Piotr
> >>>
> >>>
> >>>
> >>> -----
> >>> Apache Flex PMC
> >>> [hidden email]
> >>> --
> >>> View this message in context: http://apache-flex-development
> >>> .2333347.n4.nabble.com/FlexJS-Issue-with-JSON-stringify-and-
> >>> Bindable-VO-objects-tp61195p61216.html
> >>> Sent from the Apache Flex Development mailing list archive at
> >>>Nabble.com.
> >>>
> >>
> >>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: [FlexJS] Issue with JSON.stringify and Bindable VO objects

piotrz
Greg,

In order to touch this subject on compiler sight, would it be helpful if I prepare what Alex asked me ?

Scenario and generated code in JS sight ?

Piotr
Reply | Threaded
Open this post in threaded view
|

Re: [FlexJS] Issue with JSON.stringify and Bindable VO objects

Greg Dove
Yes Piotr, although I think we are all guessing what you want, it will be
helpful to be certain.



On Wed, Apr 19, 2017 at 9:03 PM, piotrz <[hidden email]> wrote:

> Greg,
>
> In order to touch this subject on compiler sight, would it be helpful if I
> prepare what Alex asked me ?
>
> Scenario and generated code in JS sight ?
>
> Piotr
>
>
>
> -----
> Apache Flex PMC
> [hidden email]
> --
> View this message in context: http://apache-flex-
> development.2333347.n4.nabble.com/FlexJS-Issue-with-JSON-
> stringify-and-Bindable-VO-objects-tp61195p61232.html
> Sent from the Apache Flex Development mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|

Re: [FlexJS] Issue with JSON.stringify and Bindable VO objects

Alex Harui
In reply to this post by Greg Dove


On 4/19/17, 1:29 AM, "Greg Dove" <[hidden email]> wrote:

>Yes I was thinking more in terms of the times I did this type of thing to
>support nested typed objects from raw json, more than simple VOs. Not so
>much in terms of using reviver, but going through from json -> generic
>object -> typed object with most of the support for the last part being in
>the abstract base class, and extra field mappings to complex field types
>defined in subclasses for specific fields. It was maintainable at the
>individual class level, but still quite complicated. Poor man's amf.
>
>Probably not relevant here for simple VOs.

Did you look at the AMFX classes (not regular AMF)?  It might serve as a
starting point for a JSON version.

[1]
https://books.google.com/books?id=qtlUSkC4HRQC&pg=PT1458&lpg=PT1458&dq=AMFX
+specification+adobe&source=bl&ots=1Hx1n3jo0d&sig=puxfHwWuDSkP-jwbXVKGFmriy
64&hl=en&sa=X&ved=0ahUKEwipx8fAp7PTAhXpsVQKHSL1Cf4Q6AEIRjAH#v=onepage&q=AMF
X%20specification%20adobe&f=false

-Alex

12